Introducing Dragonfly Cloud! Learn More

Question: What happens when a Redis database is full?

Answer

When a Redis instance reaches its maximum memory limit, the system's response will depend on its configuration for maxmemory-policy. If the maximum limit (maxmemory) configured for a Redis instance is achieved, the following scenarios might occur:

  1. noeviction (default): Redis will reply with an error to write commands (like SET, LPUSH, etc.), but read-only commands will continue to work.
# Python redis-py example import redis r = redis.Redis(host='localhost', port=6379, db=0) try: r.set('key', 'value') # This line will throw an error if Redis is full except redis.exceptions.ResponseError as err: print(f"Redis Error {err}")
  1. allkeys-lru: Least Recently Used (LRU) keys are removed first to make room for the new data.

  2. volatile-lru: LRU keys with an expiry set are removed first.

  3. allkeys-random: Random keys are removed until there is enough space.

  4. volatile-random: Random keys with an expiry set are removed first.

  5. volatile-ttl: Keys with an expiration set and with the shortest time-to-live (TTL) are evicted first.

You can set the maxmemory-policy using the Redis CONFIG SET command or in the Redis configuration file:

redis-cli CONFIG SET maxmemory-policy allkeys-lru

Remember that these policies only kick into effect when the maxmemory limit is reached. It's crucial to monitor your Redis instances to prevent unnecessary data eviction or application errors due to lack of available memory.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.