How to set Redis max memory?
Solution 1:
Yes - to set the memory limit just uncomment the maxmemory
line in the .conf file. The default is 0, which means unlimited (until the operating system runs out of RAM and kills the process - I recommend to always set maxmemory to a sane value).
Updated: as @Eric Uldall mentioned in the comments, a CONFIG SET maxmemory <sane value>
, followed by a CONFIG REWRITE
should also do the trick. This will modify your redis.conf to preserve changes in case of restart
Solution 2:
The documentation in the comments call out bytes but I've used extensions such as mb & gb without any issues.
$ grep ^maxmemory /etc/redis-server.conf
maxmemory 8gb
maxmemory-policy allkeys-lru
And to confirm:
$ redis-cli
...
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "8589934592"
Solution 3:
No need of changing any thing in the .conf file just follow the following steps
Step 1: Once check whether redis-server is working or not
$ redis-cli
127.0.0.1:6379> ping
PONG
if the reply is PONG
then you server is working absolutely fine.
Step 2: To get the current max memory run the following commands-
$ redis-cli
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "0"
Initially it is set to 0 by default.
Step 3: After running the above step run the following commands to set the maxmemory
127.0.0.1:6379> config set maxmemory 128M
OK
To check whether the maxmemory is set to 128M run the step 2
again.
Step 4: After changing the maxmemory restart the redis-server
$ sudo systemctl restart redis
OR
$ sudo systemctl restart redis-server