On today’s Tech Thursday, we’re going to take a look at Redis configurations. We’ll focus on managing memory limits and ensuring robust data persistence.
Max Memory Limit
Setting a maximum memory limit is like drawing a boundary for Redis. It’s telling Redis, “You have 2GB of space, make the most of it!” This ensures Redis doesn’t overstep and interfere with other applications.
When Redis hits the memory limit, we need a system in place to decide which data to discard. It’s like having a cleanup crew that removes the least recently used data first. This ensures that the most relevant data stays in memory.
To configure these settings, navigate to your /etc/redis/redis.conf file and modify the following settings:
maxmemory 2gb
maxmemory-policy allkeys-lru
Persistence
RDB (Redis Database) and AOF (Append Only File) are Redis’s way of saying, “Keep calm, your data is safe!” These are two methods that Redis uses to ensure your data isn’t lost in case of a server restart.
RDB periodically creates snapshots of your data and stores them on disk. AOF, on the other hand, maintains a detailed logbook of all write operations. Again in your /etc/redis/redis.conf file, configure these as follows:
appendonly yes save 900 1 save 300 10 save 60 1000
Testing
With these settings in place, we can test data persistence between restarts. Use the following command to restart Redis: /etc/init.d/redis-server restart
Next, connect to Redis via the command line using redis-cli and add some data using the incr command.
127.0.0.1:6379> incr x (integer) 1
127.0.0.1:6379> incr x
(integer) 2
Perform /etc/init.d/redis-server restart once again and open another redis-cli session to verify if the value was retained.
127.0.0.1:6379> get x
"2"
So there you have it! With these configurations, your Redis will remember everything! (well, almost everything).
Happy coding!
Daniel Ducro
CTO Pionect
7 December, 2023