Redis có 8 eviction policy; allkeys-lru là lựa chọn tốt nhất cho pure cache, volatile-lru khi mix cache và persistent keys, noeviction khi data không được phép mất.
Khi Redis đạt maxmemory, eviction policy quyết định key nào bị xóa để nhường chỗ. Có 8 policy chính:
noeviction(default): trả lỗi khi full — dùng cho DB không chấp nhận mất dataallkeys-lru: evict key ít dùng gần đây nhất trong toàn bộ keyspace — tốt nhất cho cache thuần túyallkeys-lfu(Redis 4.0+): evict key ít được access nhất về tần suất — tốt hơn LRU khi access pattern có hot/cold keys rõ ràngallkeys-random: evict random — hiếm dùngvolatile-lru: evict key có TTL, ưu tiên LRU — dùng khi mix cache + persistent keysvolatile-lfu: evict key có TTL, ưu tiên LFUvolatile-random: evict random trong keys có TTLvolatile-ttl: evict key có TTL ngắn nhất — dùng khi muốn keys expire sớm bị evict trước
Thực tế: cache-only Redis → allkeys-lru; Redis vừa cache vừa persistent → volatile-lru (chỉ evict keys có TTL, không đụng đến persistent keys). Monitor evicted_keys metric để biết eviction đang xảy ra.
Redis has 8 eviction policies; allkeys-lru is the best choice for a pure cache, volatile-lru when mixing cached and persistent keys, and noeviction when data must not be lost.
When Redis reaches maxmemory, the eviction policy determines which key is deleted to make room. There are 8 main policies:
noeviction(default): returns an error when full — use for databases that cannot tolerate data lossallkeys-lru: evicts the least recently used key across the entire keyspace — best for pure cachesallkeys-lfu(Redis 4.0+): evicts the least frequently accessed key — better than LRU when access patterns have clear hot/cold keysallkeys-random: evicts a random key — rarely usedvolatile-lru: evicts keys with a TTL, using LRU priority — use when mixing cached and persistent keysvolatile-lfu: evicts TTL keys using LFU priorityvolatile-random: evicts a random key with a TTLvolatile-ttl: evicts the key with the shortest remaining TTL — use when you want soon-to-expire keys evicted first
In practice: cache-only Redis → allkeys-lru; Redis serving both cache and persistent data → volatile-lru (only evicts TTL keys, leaves persistent keys untouched). Monitor the evicted_keys metric to know when eviction is occurring.