v11
v11

Reputation: 2234

How to set Redis max memory?

I find the configure in this, it just said the command to use the specify configure:

./redis-server <path>/redis.conf

But,I have no idea about how to write the configure. So I have find the default configure in this. But, I still don't understand how to set max memory. Does it just add this line in configure?

maxmemory 2mb

By the way, I want to know how much the default memory is. and I want to set the memory to 2GB, how to do it?

Then, I have added this line to the redis configure to set maxmemory to 40GB:

maxmemory 41943040

And I use the command in redis-cli:

config get maxmemory

it show me:

127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "41943040"

But, my java program throw the exception like this when key number is about 200000:

Exception in thread "Thread-228" redis.clients.jedis.exceptions.JedisDataException: OOM command not allowed when used memory > 'maxmemory'.
    at redis.clients.jedis.Protocol.processError(Protocol.java:117)
    at redis.clients.jedis.Protocol.process(Protocol.java:151)
    at redis.clients.jedis.Protocol.read(Protocol.java:205)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:297)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:196)
    at redis.clients.jedis.Jedis.hmset(Jedis.java:644)
    at cn.ict.dt2redis.analyser.AbstractAnalyser.pushOne(AbstractAnalyser.java:21)
    at cn.ict.dt2redis.analyser.BatchAbstractAnalyser.run(BatchAbstractAnalyser.java:16)
    at java.lang.Thread.run(Thread.java:722)

I have no idea about it, do I success in setting max memory to 40 GB? How to do it? please give me some code in detail.

Upvotes: 71

Views: 196988

Answers (7)

MKesper
MKesper

Reputation: 509

You can set the maxmemory option. Be aware though, that the default of maxmemory-policy noeviction will not even try to remove anything from redis on hitting that limit but just return errors on write operations. Even with other policies, this can still happen (also in that config file):

Note: with any of the above policies, when there are no suitable keys for eviction, Redis will return an error on write operations that require more memory.

In short, it's hard to set for existing redis instances and might blow up even on fresh instances when hitting the limit.

Upvotes: 0

Kshitij Sharma
Kshitij Sharma

Reputation: 191

No need of changing any thing in the .conf file just follow the following steps

Step 1: First check whether redis-server is working or not

$ redis-cli
127.0.0.1:6379> ping
PONG

if the reply is PONG then your 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 step 2 again.

Step 4: After changing the maxmemory restart the redis-server

$ sudo systemctl restart redis
OR
$ sudo systemctl restart redis-server

Upvotes: 19

Furqan Siddiqui
Furqan Siddiqui

Reputation: 421

Since this is an old question, users who are reading in 2019 and using Ubuntu 18.04, the configuration file is located in /etc/redis/redis.conf and if you have installed using (default/recommended method) apt install redis-server the default memory limit is set to "0" which practically means there is "no limit" which can be troublesome if user has limited/small amount of RAM/memory. To set your custom memory limit you may simply edit configuration file and type "maxmemory 1gb" as the very first line. Restart redis service for changes to take effect. To verify changes use redis-cli config get maxmemory

Ubuntu 18.04 users may read more here: How to install and configure REDIS on Ubuntu 18.04

Upvotes: 3

Itamar Haber
Itamar Haber

Reputation: 49942

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

Upvotes: 75

mwilson
mwilson

Reputation: 12900

If anyone is still having trouble setting the maxmemory config setting in a local environment, the actual steps are as follows:

  1. In a terminal, start your redis instance redis-server
  2. In a new terminal window run redis-cli
  3. In the new terminal window (step 2) run config set maxmemory 2mb
  4. Verify maxmemory by running config get maxmemory in same terminal window as steps 2/3

Somewhat documented here under the Changing Redis configuration while the server is running section

Upvotes: 1

slm
slm

Reputation: 16416

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"

Upvotes: 46

Max
Max

Reputation: 4620

maxmemory 41943040

option set in bytes, so you set 40MB

Upvotes: 8

Related Questions