Sobes.tech
Senior

What are eviction policies in Redis?

sobes.tech AI

Answer from AI

Redis has several eviction policies that determine how Redis frees memory when the memory limit (maxmemory) is reached. The main policies are:

  • noeviction — when the memory limit is reached, new entries are not added, and an error is returned.
  • allkeys-lru — the least recently used (LRU) keys are removed from all keys.
  • volatile-lru — the least recently used keys with an expiration time (TTL) are removed.
  • allkeys-random — random keys are removed from all keys.
  • volatile-random — random keys with TTL are removed.
  • volatile-ttl — keys with TTL that expire soonest are removed.

The choice of policy depends on the use case:

  • If data preservation is important, noeviction can be used.
  • allkeys-lru is often used for caching.
  • volatile-* policies are applicable if only temporary keys are important.

In Go, when working with Redis through a client (e.g., go-redis), policies are set on the Redis server side in the configuration, not in the client.

What are eviction policies in Redis? — Golang - sobes.tech