Skip to content
Memcached

만료, 축출 & TTL 전략

현명하게 TTL 선택과 압박 시 Memcached 축출 방식 이해.

#expiration#ttl#eviction#lru

Code

memcached
# exptime semantics
set k1 0 0      5      # 0 = no expiration (live until evicted)
hello
set k2 0 30     5      # expires in 30 seconds
world
set k3 0 86400  5      # 1 day
long
set k4 0 100000000 5   # > 30 days => treated as UNIX timestamp
abs

# After expiration, key is removed LAZILY (on next access or eviction),
# not actively swept. Active sweep runs every ~1s but isn't guaranteed.

# Touch — change TTL without rewriting value
touch session:abc 60   # extend to 60s

# Gat (get-and-touch) — read and extend in one round trip
gat 60 session:abc

# Eviction (LRU) — when memory is full and a new item arrives:
#   - least recently used item in the matching slab is evicted
#   - evictions counter increments
#   - ttl does NOT protect from eviction; ttl=0 is just "no timed expiry"

# Strategies:
#   hot data  -> short TTL (60s) + refresh on miss (read-through)
#   sessions  -> TTL = session timeout
#   computed  -> TTL = source-data freshness window
#   "permanent" -> TTL 0 + accept eventual eviction