In-memory database or a caching engine is used by developers to optimize processing speed and capabilities of their apps. There are several use cases such as storing large objects or simple scalar values. Since a caching engine stores these values in server’s RAM/memory, storing and retrieving are super fast compared to a standard database.
And, when it comes to choosing an in-memory database or caching engine, Redis is definitely the first choice for most developers. And, here is how you can install and configure REDIS server on a Ubuntu 18.04 server:
Installing REDIS server on Ubuntu 18 is a simple single command operation:
$ sudo apt-get install redis-server
You have now installed redis and it is accessible for your apps to connect using localhost or 127.0.0.1 as hostname while default REDIS port is 6379. You may also connect to REDIS using cli by simply typing redis-cli in your shell to begin an interactive session with redis server.
Configuring REDIS server
Now that redis server is installed, most important thing is to configure it’s maximum memory size. Default value is "0" which simply means "no limit" and it can be troublesome if you are using a small sized VPS.
You can check currently configured memory limit using this command:
$ redis-cli config get maxmemory
To configure a maximum memory limit, you should first determine the size of memory that available to your server and how much you an allocate/reserve for REDIS. This value entirely depends on your app and consumption rates. To check available RAM/memory, you can use htop OR free -m commands.
Editing REDIS server configuration file:
Open file for editing:
$ sudo pico /etc/redis/redis.conf
Now this is a one lengthy configuration file where you can edit several parameters. To set a custom memory limit of 1GB, simply put this as very first line: maxmemory 1gb
Now all you need to do is restart REDIS server for the changes to take effect.
$ sudo service redis restart
That’s pretty much it! Thank you for reading!