
Redis® is an open-source (BSD licensed), in-memory data structure store. You can use it as a Memcached alternative to store simple key-value pairs. Moreover, you can use it as a NoSQL database, or even a message broker with the Pub-Sub pattern.
This guide will show you how to install, configure, and fine-tune a recent Redis® version on Ubuntu 20.04 or Ubuntu 18.04.
The Redis® version comes from the official Ubuntu repo is usually far behind the latest version available. This guide will use the chris-lea/redis-server PPA, a long-time, up-to-date, and community-trusted PPA, to install a recent release.
Add the PPA to the system's Software Sources (press Enter when prompted):
Update package lists then install Redis®.
Make Redis® run at boot-time:
Open the Redis® configuration file in your favorite editor.
Update the IP address that Redis® will listen on:
If you installed Redis® on your application server, then you are safe with the default setting.
Don't change that line. Your application will use the loopback IP 127.0.0.1 to connect to Redis®.
Otherwise, enable and configure a private network between your Redis® server and your application server. Then add the private IP, for example, 192.0.2.1, to the end of the line above, separate by a space.
Your application will use the private IP 192.0.2.1 to connect to Redis®. Warning: Letting Redis® listen on a public IP is dangerous and will expose the instance to everyone on the internet.
Set the desired memory capacity for your application, for example, 128mb (mean 128 MB):
By default, when maxmemory is reached, Redis® will stop writing new data. If you want Redis® to continue to write new data by removing old data automatically, you have to tell Redis how to remove it. Set the recommended value allkeys-lru for the maxmemory-policy directive.
By default, Redis® will save its in-memory data on disk after a specified period and number of write operations against the DB. The default settings are:
That means the saving will occur:
With the default settings above, Redis® will load the saved data into memory every time it restarts. So your previous in-memory data will be restored. If you don't need this feature, you can disable it entirely by commenting out those lines.
If you decide to keep this feature, you should upgrade the server to a bigger plan to ensure that the memory reserved for Redis® is double the maxmemory declared above. Otherwise, in the worst-case scenario, when the maxmemory is reached, the saving process can cause your server to run out of memory.
Save and close the configuration file, then restart Redis® to apply the changes:
Open the Redis® log file:
You will see some information like this:
To fix the first warning above, enter the following commands:
To fix the second warning above, create a new text file:
Paste the following text into that file:
Save and close that file. Then make it runnable and owned by the root account.
Optional:
If this Redis® server is separate from your application server and you also installed UFW, you have to configure UFW to allow connections to the Redis® port, default is 6379:
If you install Redis® on Ubuntu 18.04, Redis® will trigger an additional warning about "The TCP backlog." To fix this warning, enter the following commands:
Reboot the system:
Open the Redis® log file to ensure the two warnings above have gone:
Switch to your application server (assuming it's also a Ubuntu 20.04 or Ubuntu 18.04 server). Make sure it can talk to the Redis® server by performing the following actions:
Install the redis-cli program provided by the redis-tools package from the official repo:
Make redis-cli connect to the Redis® server with the Redis® IP address configured above, for example, 192.0.2.1:
If the connecting is successful, you will see the Redis® command prompt like this:
Enter some Redis® commands to ensure it works:
If you see the following result, then your Redis® setup is perfect:
Installing a recent Redis® version on Ubuntu 20.04 or Ubuntu 18.04 is an easy task. But, properly configuring it is a bit tricky. To learn more about Redis®, see these resources:
0 Comments
Be the first to comment and share your perspective with the community.