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:
- 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}")
-
allkeys-lru: Least Recently Used (LRU) keys are removed first to make room for the new data.
-
volatile-lru: LRU keys with an expiry set are removed first.
-
allkeys-random: Random keys are removed until there is enough space.
-
volatile-random: Random keys with an expiry set are removed first.
-
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?
Other Common Redis Questions (and Answers)
- What is the default port used by Redis?
- How to start Redis server?
- Is Redis persistent?
- How fast is Redis?
- How to install Redis on Mac?
- How to check if Redis is running?
- How to restart Redis?
- Does Redis persist data?
- How to install Redis on Ubuntu?
- How to stop Redis server?
- How to see Redis version?
- Does Redis have tables?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost