How do I set the maximum memory size that Redis can use?

28,450

Solution 1

maxmemory is the correct configuration option to prevent Redis from using too much RAM.

If an insert causes maxmemory to be exceeded, the insert operation will sometimes fail.

Redis will do everything in its power to prevent the operation from failing, though. In the newer versions of Redis, you can configure the memory reclaiming policies in the configuration, as well by setting the maxmemory-policy option.

Also, if you have virtual memory options turned on, Redis will begin to store stale data to the disk.

More info:

Solution 2

You can do that using maxmemory option: maxmemory 314572800 means 300mb.

Solution 3

Since the last answer is from 2011. I am going to write some updated information for users reading in 2019 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

Share:
28,450

Related videos on Youtube

Cory
Author by

Cory

Updated on July 09, 2022

Comments

  • Cory
    Cory almost 2 years

    To be specific, I only have 1GB of free memory and would like to use only 300MB for Redis. How can I configure it so that it is only uses up to 300MB of memory?

    Out of curiosity, what happen when you try to insert a new data and Redis is already used all the memory allocated?

  • David A. Ventimiglia
    David A. Ventimiglia almost 11 years
    I could be wrong, but I found nothing in the Redis docs that spells out the UNITS for maxmemory, and this is very frustrating. Evidently, it's in bytes?
  • Henley
    Henley over 8 years
    It's maxmemory Xgb in the latest version of Redis.
  • Brad
    Brad over 8 years
    The units are called out at the top of the config file. Here's the config file from an old version: raw.githubusercontent.com/antirez/redis/2.4/redis.conf And for 3.0: raw.githubusercontent.com/antirez/redis/3.0/redis.conf