~/blog/redis-cache-field-guide/ko.mdx

Redis 캐시 현장 안내서: 만료, 축출, 그리고 한꺼번에 몰려오는 요청

캐시는 붙이기 쉽고 운영하기 어렵습니다. Redis 6.2 소스에서 만료와 축출이 실제로 어떻게 돌아가는지 읽고, 캐시 스탬피드와 근사 LRU를 직접 재 봤습니다. 끝에 면접 용어 정리와 퀴즈, 플래시카드가 있습니다.

캐시를 처음 붙이면 응답 시간 그래프가 바닥으로 내려갑니다. 그다음 몇 달은 그 캐시 때문에 생긴 문제를 고치면서 보냅니다. 만료가 몰려 DB가 넘어가고, 없는 키를 계속 묻는 요청이 캐시를 그냥 통과하고, 메모리가 차서 쓰기가 막힙니다.

이 글은 그런 문제를 하나씩 보고, Redis가 각각을 어떻게 다루는지 소스에서 확인합니다. 소스는 Redis 6.2.6이고, 측정은 같은 버전의 공식 Docker 이미지에서 Python 클라이언트(redis-py)로 했습니다. 환경은 Apple Silicon 노트북입니다. 숫자는 이 환경에서 나온 것이니 절댓값보다 모양을 보시면 됩니다.

앞부분은 캐시 패턴과 Redis의 만료, 축출 구조를 다루고, 뒷부분은 장애 유형별로 정리합니다. 마지막에 면접에서 자주 나오는 용어와 퀴즈가 있습니다. 백엔드 개발자와 Redis를 운영하는 분을 생각하고 썼지만, 프런트엔드 개발자가 읽어도 따라올 수 있게 썼습니다.

캐시를 어디에 끼우는가

캐시를 읽고 쓰는 순서에 따라 이름이 붙습니다.

Cache-aside(lazy loading)에서는 애플리케이션이 캐시를 먼저 보고, 없으면 DB에서 읽어 캐시에 넣습니다. 쓸 때는 DB 트랜잭션을 커밋한 뒤 캐시 키를 지웁니다. 캐시 없이도 읽을 경로를 만들 수 있지만, 아래 예제는 Redis 오류를 그대로 호출자에게 전달합니다. 장애 시 DB로 우회하려면 타임아웃과 DB 동시성 제한을 함께 설계해야 합니다.

cache-aside
def get_user(uid):
v = r.get(f"user:{uid}")
if v is not None:
return json.loads(v)
row = db.fetch_user(uid) # 캐시 미스
r.set(f"user:{uid}", json.dumps(row), ex=300)
return row
def update_user(uid, fields):
db.update_user(uid, fields)
r.delete(f"user:{uid}") # 덮어쓰지 않고 지운다

쓸 때 캐시를 새 값으로 덮어쓰지 않고 지우는 데는 이유가 있습니다. 두 요청이 거의 동시에 쓰면 DB에는 B가 마지막으로 남았는데 캐시에는 A가 마지막으로 남을 수 있습니다. 지우면 다음 읽기가 DB에서 다시 가져오니 이런 순서 꼬임이 TTL까지 남지 않습니다.

그래도 틈은 남습니다. 읽기가 DB에서 옛 값을 가져온 직후 쓰기가 캐시를 지우고, 그다음 읽기가 옛 값을 캐시에 넣는 순서입니다.

옛 값 A 조회 A 새 값 B 커밋 DEL user:42 SET user:42 A EX 300 읽기 요청 DB 쓰기 요청 Redis

읽기 요청이 A를 얻었습니다. DB 조회와 캐시 채우기는 별도 작업이므로, 그 사이 다른 요청이 들어올 수 있습니다.

쓰기 요청은 B를 커밋하고 캐시를 지웁니다. DB 갱신 후 삭제 순서를 지켰습니다.

늦게 끝난 읽기가 A를 다시 넣습니다. DB에는 B, 캐시에는 A가 남습니다.

TTL은 이 마지막 SET부터 옛 값이 남는 시간을 제한합니다. DB 커밋부터의 정합성 상한은 아닙니다. DB 읽기가 지연되거나 레플리카가 뒤처지면 옛 값이 늦게 들어갈 수 있고, 읽을 때마다 TTL을 늘리면 계속 살아남을 수도 있습니다.

같은 코드라도 요청이 끼어드는 위치에 따라 결과가 달라집니다. 아래에서 순서를 골라 한 단계씩 진행하면, 바뀐 값에 색이 들어옵니다.

DEL보다 SET이 늦으면

DBA
읽기 요청이 가진 값없음
Redis없음

시작

DB에는 A가 있고 캐시는 비어 있습니다.

위 예제의 실행 순서를 구성한 모델입니다. 실제 요청 시간이나 Redis 처리량을 재현하지 않습니다. 각 칸은 해당 단계가 끝난 뒤의 상태입니다.
전체 상태 보기 (JavaScript 없이도 읽을 수 있습니다)

늦은 SET: 옛 값이 돌아옵니다

  1. 시작

    DB: A · 읽기 요청이 가진 값: 없음 · Redis: 없음

    DB에는 A가 있고 캐시는 비어 있습니다.

  2. 읽기: DB에서 A를 받습니다

    DB: A · 읽기 요청이 가진 값: A · Redis: 없음

    읽기 요청은 아직 캐시를 채우지 않았습니다.

  3. 쓰기: B를 커밋합니다

    DB: B · 읽기 요청이 가진 값: A · Redis: 없음

    DB는 B가 되지만 읽기 요청이 이미 받은 A는 바뀌지 않습니다.

  4. 쓰기: DEL을 실행합니다

    DB: B · 읽기 요청이 가진 값: A · Redis: 없음

    빈 캐시를 지워도, 진행 중인 읽기 요청의 A까지 지우지는 않습니다.

  5. 읽기: A를 SET합니다

    DB: B · 읽기 요청이 가진 값: A · Redis: A · TTL 300s

    DB는 B, 캐시는 A입니다. TTL은 이 SET부터 시작합니다.

이 순서에서는 DEL이 마지막입니다

  1. 시작

    DB: A · 읽기 요청이 가진 값: 없음 · Redis: 없음

    같은 A와 빈 캐시에서 시작합니다.

  2. 읽기: DB에서 A를 받습니다

    DB: A · 읽기 요청이 가진 값: A · Redis: 없음

    이 요청도 DB에서 A를 읽습니다.

  3. 읽기: A를 SET합니다

    DB: A · 읽기 요청이 가진 값: A · Redis: A · TTL 300s

    이번에는 쓰기 요청이 들어오기 전에 캐시를 채웁니다.

  4. 쓰기: B를 커밋합니다

    DB: B · 읽기 요청이 가진 값: A · Redis: A · TTL 300s

    DEL 전까지는 캐시에 A가 남습니다.

  5. 쓰기: DEL을 실행합니다

    DB: B · 읽기 요청이 가진 값: A · Redis: 없음

    캐시는 비었습니다. 다음 미스는 DB를 다시 읽습니다. 이 순서의 결과이며 모든 동시 실행의 보장은 아닙니다.

가격이나 권한처럼 옛 값을 허용하기 어려운 읽기는 DB를 확인하거나, 캐시와 DB의 버전을 비교하는 별도 프로토콜이 필요합니다. 무효화 실패는 재시도할 수 있도록 남깁니다. DB 커밋과 무효화 이벤트 기록을 같은 트랜잭션에 넣는 outbox나 CDC도 방법이지만, 이벤트를 처리하기 전까지의 지연과 순서 역전은 여전히 다뤄야 합니다.

퀴즈잠깐 확인: TTL이 보장하는 범위

답을 고르면 해설이 나옵니다.

1문제 중 0개 맞힘

  1. B를 커밋하고 캐시를 지운 뒤, 늦은 읽기가 A를 TTL 60초로 넣었습니다. 어느 설명이 맞나요?

Read-through는 cache-aside와 같은 일을 캐시 계층(라이브러리나 프록시)이 대신 합니다. 애플리케이션은 캐시만 봅니다.

Write-through는 쓸 때 캐시와 DB를 둘 다 동기로 고칩니다. 쓴 값은 캐시에 들어가지만, 만료나 축출 뒤에는 다시 미스가 납니다. 두 저장소가 하나의 트랜잭션이 되는 것도 아닙니다. 한쪽만 성공했을 때의 복구 순서가 필요합니다.

Write-behind(write-back)는 캐시에 먼저 쓰고 DB 반영은 나중에 모아서 합니다. 쓰기가 빠르고 DB 부하가 줄지만, 캐시가 죽으면 반영 안 된 쓰기를 잃습니다. 조회수나 좋아요 수처럼 조금 잃어도 되는 값에 씁니다.

1. GET 미스 2. SELECT 3. SET ex=300 쓰기 1. UPDATE 및 커밋 쓰기 2. DEL 애플리케이션 Redis DB

만료: 지운다는 것이 언제인가

SET key value EX 60을 하면 Redis는 키 공간과 별도로 expires라는 해시 테이블에 키와 만료 시각(밀리초 유닉스 시간)을 적습니다. 60초가 지나는 순간 무언가가 키를 지워 주지는 않습니다. 지우는 길은 두 가지입니다.

게으른 만료

일반 키 조회 경로는 expireIfNeeded로 만료 여부를 확인합니다. DBSIZE처럼 키 공간의 크기를 보는 명령이 모든 키를 하나씩 만료시키는 것은 아닙니다.

src/db.c (Redis 6.2.6, 주석 생략)
int expireIfNeeded(redisDb *db, robj *key) {
if (!keyIsExpired(db,key)) return 0;
if (server.masterhost != NULL) return 1;
if (checkClientPauseTimeoutAndReturnIfPaused()) return 1;
/* Delete the key */
deleteExpiredKeyAndPropagate(db,key);
return 1;
}

만료 시각이 지나지 않았으면 아무 일도 없습니다. 대부분의 접근이 이 줄에서 끝납니다.

레플리카라면 “만료됐다”고 답만 하고 지우지는 않습니다. 레플리카의 키는 마스터가 보내는 DEL로만 지워집니다. 그래야 마스터와 레플리카의 데이터가 갈라지지 않습니다. 읽기에는 없는 키로 보이지만 메모리는 마스터가 지울 때까지 남습니다.

클라이언트가 일시 정지된 동안(CLIENT PAUSE, 장애 조치 중)에도 지우지 않습니다. 데이터셋을 그대로 두어야 하기 때문입니다.

마스터라면 키를 지우고, AOF와 레플리카에 DEL(또는 UNLINK)을 전파합니다.

이것만 있으면 아무도 읽지 않는 만료 키는 영원히 메모리에 남습니다.

능동 만료

그래서 서버가 주기적으로 expires 테이블을 표본 조사합니다. activeExpireCycle이 하는 일입니다. 상수는 expire.c 맨 위에 있습니다.

상수값뜻
KEYS_PER_LOOP20한 번에 표본으로 보는 키 수
FAST_DURATION1000µs이벤트 루프 사이에 도는 빠른 주기의 시간 한도
SLOW_TIME_PERC25serverCron에서 도는 느린 주기가 쓸 수 있는 CPU 비율
ACCEPTABLE_STALE10표본 중 만료 비율이 이보다 낮으면 그 DB는 그만 본다

active-expire-effort(기본 1, 최대 10)를 올리면 조사량과 시간 한도가 늘고 허용 비율은 내려갑니다. Redis 6.2.6의 activeExpireCycle은 커서로 해시 버킷을 훑습니다. 기본 조사 목표가 20개이며, 버킷 안의 충돌 체인을 끝까지 확인하므로 정확히 20개로 고정되지는 않습니다. 만료 비율이 10%를 넘으면 반복하되 시간 한도에 걸려도 멈춥니다. expires의 버킷 점유율이 1% 미만이면 테이블이 축소될 때까지 이번 조사를 건너뜁니다.

정말 그렇게 움직이는지 봤습니다. TTL 1초인 키 20만 개와 TTL 1시간인 키 20만 개를 넣고, 아무것도 읽지 않으면서 DBSIZE를 지켜봤습니다. hz는 기본값 10입니다.

0 0.5 1.0 1.5 2.0 5.1 12.2 200 250 300 350 400 경과 시간(초) 키 수(천) TTL 1초 키 20만 개를 넣은 뒤 DBSIZE (읽기 없음)

1초가 지나고 다음 1초 안에 19만 7천 개가 지워졌습니다. 그런데 2초 이후 곡선이 평평합니다. 남은 2천 개 남짓은 초당 100개 정도씩 천천히 빠졌습니다.

만료된 키가 2천 개, 살아 있는 키가 20만 개면 전체 만료 비율은 1% 근처입니다. 조사에서 낮은 비율을 만나면 추가 반복을 하지 않으므로 뒤쪽이 천천히 줄어드는 모양을 설명할 수 있습니다. 다만 표본과 시간 한도로 판단하므로 실제 만료 키 비율을 10% 아래로 보장하지는 않습니다. INFO stats의 expired_stale_perc도 누적 평활 추정치이며, 측정 중 최고 28%까지 갔다가 13% 근처로 내려왔습니다.

아직 지워지지 않은 만료 키는 DBSIZE에도 포함됩니다. 키 수가 줄었는데 프로세스 RSS가 잘 안 내려가면 allocator가 해제된 메모리를 OS에 돌려주지 않았거나 단편화가 남았을 수 있습니다. used_memory, used_memory_rss, lazyfree_pending_objects를 나눠 봐야 만료 지연과 해제 지연을 구분할 수 있습니다.

축출: 메모리가 찼을 때

maxmemory에 닿으면 Redis는 명령을 실행하기 전에 performEvictions로 공간을 만듭니다. 무엇을 지울지는 maxmemory-policy가 정합니다.

정책대상기준
noeviction없음SET 등 메모리를 늘릴 수 있는 명령에 OOM 에러
allkeys-lru / volatile-lru전체 / TTL 있는 키가장 오래 안 쓴 키
allkeys-lfu / volatile-lfu전체 / TTL 있는 키가장 덜 쓰는 키
allkeys-random / volatile-random전체 / TTL 있는 키무작위
volatile-ttlTTL 있는 키만료가 가장 가까운 키

기본 정책은 noeviction이며, 64비트 서버의 기본 maxmemory는 0(제한 없음)입니다. 한도를 설정해야 축출 정책이 의미가 있습니다. 한도 초과를 해소하지 못하면 server.c의 OOM 검사는 denyoom 플래그가 있는 SET 같은 명령을 거절합니다. 모든 쓰기가 막히는 것은 아닙니다. DEL처럼 공간을 돌려주는 명령은 실행할 수 있습니다. volatile-*도 TTL 있는 후보가 없으면 공간을 만들 수 없습니다.

LRU는 근사치입니다

진짜 LRU를 하려면 모든 키를 접근 순서로 잇는 연결 리스트가 필요합니다. 키마다 포인터 두 개, 16바이트가 더 들고, 읽을 때마다 리스트를 고쳐야 합니다.

Redis는 대신 키 객체 헤더의 24비트에 마지막 접근 시각을 초 단위로 적습니다. 지울 때는 키를 무작위로 maxmemory-samples개(기본 5) 뽑아 크기 16짜리 후보 풀에 넣고, 풀에서 가장 오래 쉰 것부터 지웁니다.

src/evict.c, evictionPoolPopulate (요약)
if (server.maxmemory_policy & MAXMEMORY_FLAG_LRU) {
idle = estimateObjectIdleTime(o);
} else if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
idle = 255-LFUDecrAndReturn(o);
} else if (server.maxmemory_policy == MAXMEMORY_VOLATILE_TTL) {
idle = ULLONG_MAX - (long)dictGetVal(de);
}

세 정책이 모두 “idle 점수가 큰 것부터 지운다” 하나로 통일되어 있습니다. LFU는 빈도를 뒤집고, TTL은 만료 시각을 뒤집어서 같은 풀에 넣습니다.

표본 수가 결과를 얼마나 바꾸는지 직접 쟀습니다. 2,000개씩 10묶음을 1.05초 간격으로 넣고, 메모리를 전체의 절반쯤으로 묶었습니다. 이상적인 LRU라면 살아남은 키가 전부 뒤쪽 절반(새 키)이어야 합니다.

정확한 LRU

samples 1

samples 3

samples 5기본값

samples 10

막대 하나가 키 묶음 하나이고, 왼쪽일수록 오래된 키입니다. 진하게 남은 막대가 축출되지 않고 살아남은 키입니다.
maxmemory-samples살아남은 키 중 새 절반 비율
153.6%
384.5%
5 (기본)92.0%
1096.6%
이상적 LRU100%

이 측정에서는 표본 1개의 결과가 무작위 축출에 가까웠습니다. 그래도 allkeys-random과 같은 알고리즘은 아닙니다. evictionPoolPopulate는 앞서 뽑은 후보를 풀에 남겨 다음 선택에도 사용합니다. 표본 수를 올리면 후보를 더 비교하지만 CPU 비용도 늘므로 적중률과 지연을 같이 재야 합니다. 막대는 측정 결과이며 알고리즘 실행 애니메이션은 아닙니다.

LFU는 8비트로 셉니다

LFU 모드에서는 같은 24비트를 둘로 나눕니다. 위 16비트는 마지막으로 값을 줄인 시각(분 단위), 아래 8비트는 접근 횟수입니다. 8비트면 255까지밖에 못 세니, 횟수를 그대로 더하지 않고 확률로 올립니다.

src/evict.c
uint8_t LFULogIncr(uint8_t counter) {
if (counter == 255) return 255;
double r = (double)rand()/RAND_MAX;
double baseval = counter - LFU_INIT_VAL;
if (baseval < 0) baseval = 0;
double p = 1.0/(baseval*server.lfu_log_factor+1);
if (r < p) counter++;
return counter;
}

새 키는 5에서 시작합니다(LFU_INIT_VAL). 0에서 시작하면 방금 들어온 키가 바로 축출 1순위가 되기 때문입니다. 값이 커질수록 다음 증가 확률이 떨어지니 카운터는 로그처럼 자랍니다. 실제로 키 하나를 N번 GET하고 OBJECT FREQ를 봤습니다.

lfu-log-factor0회1회10회100회1천1만10만100만
15691945145255255
10 (기본)566102145148255
100566791850162

이 실행에서는 기본 factor 10에서 100만 번 읽었을 때 255를 관찰했습니다. 확률로 증가하므로 100만 번이 포화까지 필요한 고정 횟수는 아닙니다. factor를 낮추면 더 빨리 증가하지만, 자주 읽히는 키끼리 포화된 뒤에는 구분하기 어렵습니다.

반대 방향도 있습니다. LFUDecrAndReturn은 마지막 접근 이후 흐른 분을 lfu-decay-time(기본 1분)으로 나눈 만큼 점수를 낮춥니다. 모든 키를 매분 고치는 타이머는 없습니다. 접근 시 값을 갱신하고, 축출 후보를 조사할 때도 감소한 점수로 비교합니다. 기본 설정에서 10분 쉰 키는 점수를 계산할 때 최대 10을 빼며, 0 아래로 내려가지는 않습니다.

LRU와 LFU 중 무엇을 고를지는 접근 패턴에 달렸습니다. 전체를 한 번 훑는 배치 작업이 돌면 LRU는 그 순간 캐시를 훑은 키로 갈아 끼웁니다. LFU에서는 한 번 읽힌 키가 5나 6에 머무니 원래 인기 키가 버팁니다.

캐시가 무너지는 네 가지 방식

면접에서 단골로 나오는 이름들입니다. 이름은 한국어 자료에서 흔히 쓰는 것을 따랐습니다.

1. 캐시 스탬피드 (Cache Stampede, Thundering Herd)

인기 키 하나가 만료되는 순간, 그 키를 기다리던 요청이 전부 미스를 보고 동시에 DB로 갑니다. DB 쿼리가 200ms라면 그 200ms 동안 들어온 요청은 모두 같은 쿼리를 날립니다. dog-piling이라고도 부릅니다.

직접 재 봤습니다. 백엔드는 200ms 동안 잠드는 함수이고, 위의 cache-aside 코드를 스레드 여러 개로 돌렸습니다. 스레드를 배리어로 묶어 같은 순간에 출발시켰습니다.

"10" "100" "500" "1000" 0 100 200 300 400 500 600 백엔드 호출 동시 요청 수별 백엔드 호출 횟수 (빈 캐시에서 출발)

100개까지는 요청 수만큼 DB를 쳤습니다. 500개와 1000개에서 호출이 요청 수보다 적은 것은 해결돼서가 아닙니다. Python 스레드가 다 출발하기 전에 첫 요청이 캐시를 채웠을 뿐입니다. 실제 서비스의 동시성은 이 스레드 실험과 다르므로 호출 수를 그대로 예측할 수 없습니다.

해결 1: 락. 미스가 난 요청 중 하나만 DB에 가고, 나머지는 잠깐 기다렸다 캐시를 다시 봅니다.

락 + 재확인
import math
import time
from uuid import uuid4
RELEASE = r.register_script("""
if redis.call('get', KEYS[1]) == ARGV[1] then
return redis.call('del', KEYS[1])
end
return 0
""")
PUBLISH = r.register_script("""
if redis.call('get', KEYS[1]) ~= ARGV[1] then return 0 end
redis.call('set', KEYS[2], ARGV[2], 'EX', ARGV[3])
return 1
""")
def get_with_lock(key, load, ttl=60, wait=1.0):
if type(ttl) is not int or ttl <= 0 or not math.isfinite(wait) or wait <= 0:
raise ValueError("ttl must be a positive integer; wait must be positive")
deadline = time.monotonic() + wait
lock_key = f"{key}:lock"
while time.monotonic() < deadline:
v = r.get(key)
if v is not None:
return v
token = uuid4().hex
if r.set(lock_key, token, nx=True, px=2000):
try:
v = r.get(key) # 다시 확인
if v is None:
remaining = deadline - time.monotonic()
if remaining <= 0:
raise TimeoutError("cache fill deadline")
v = load(timeout=remaining)
if time.monotonic() >= deadline:
raise TimeoutError("cache fill deadline")
if not PUBLISH(keys=[lock_key, key],
args=[token, v, ttl]):
raise TimeoutError("cache fill lease lost")
return v
finally:
RELEASE(keys=[lock_key], args=[token])
time.sleep(min(0.02, max(0, deadline - time.monotonic())))
raise TimeoutError("cache fill wait expired")

여기서 두 군데가 중요합니다.

  • 락 해제를 그냥 DEL로 하면, 내 락이 PX 2초를 넘겨 풀린 뒤 다른 요청이 잡은 락을 내가 지울 수 있습니다. 그래서 토큰을 넣고, 토큰이 내 것일 때만 지우는 것을 Lua로 한 번에 합니다.
  • 락을 잡은 직후 캐시를 한 번 더 봅니다. 앞 요청이 캐시를 채우고 락을 푼 직후에 락을 잡은 요청은 이 확인이 없으면 DB를 한 번 더 칩니다. 이 줄 없이 돌렸을 때 500개와 1000개에서 백엔드 호출이 1~2번 나왔고, 넣은 뒤에는 둘 다 정확히 1번이었습니다.

앞의 차트는 기본 cache-aside와 락 및 재확인을 비교한 측정입니다. 위 코드는 여기에 대기 마감과 소유권을 확인하는 캐시 저장을 추가했습니다. 무한 대기를 피하고, 락을 잃은 로더가 늦게 결과를 덮어쓰는 경로를 막습니다.

r은 유한한 소켓 연결 및 읽기 타임아웃을 설정한 redis-py 클라이언트입니다. load(timeout=...)도 DB 쿼리나 HTTP 요청에 실제 타임아웃을 전달해야 합니다. Python의 시간 검사만으로 멈추지 않는 함수를 취소할 수는 없습니다. 값은 Redis에 바로 저장할 수 있는 바이트열이나 문자열이며, Redis와 DB 오류는 호출자에게 전달합니다.

고친 예제는 npm run test:cache -- --docker로 Redis 6.2.6에 연결해 확인했습니다. 테스트는 글의 Python 코드 블록을 그대로 실행하고 Lua도 Redis에서 평가합니다. 동시 미스, 빈 값 적중, 대기 마감, 락 소유권 상실, DB 오류, 늦은 결과 저장 방지, OOM 동작을 검사하며 한국어와 영어 예제의 AST도 비교합니다. 이 검증은 글을 수정한 시점의 테스트이며 위의 원래 측정과는 구분합니다.

Cluster에서 Lua가 사용하는 키는 같은 슬롯이어야 합니다. 예를 들어 값 키를 {user:42}:value로 잡으면 락 키 {user:42}:value:lock도 같은 해시 태그를 사용합니다. 락의 TTL이 지나거나 락 키가 축출되거나 장애 조치가 일어나면 중복 로더가 생길 수 있습니다. 이 예제의 락은 캐시 채우기 중복을 줄이는 용도입니다. 결제나 재고 갱신을 정확히 한 번 실행하는 보장은 DB 트랜잭션과 멱등성 키에 맡겨야 합니다.

퀴즈잠깐 확인: 락을 잃은 로더

답을 고르면 해설이 나옵니다.

1문제 중 0개 맞힘

  1. 로더가 멈춘 동안 락 TTL이 지났습니다. 토큰을 넣었다면 여전히 독점하나요?

해결 2: 미리 갱신. 값과 함께 계산에 걸린 시간을 저장해 두고, 만료가 가까울수록 높은 확률로 먼저 갱신하게 하는 방법이 XFetch(확률적 조기 만료)입니다1. 갱신이 몰릴 가능성을 줄이지만 갱신 요청을 정확히 하나로 보장하지는 않습니다. 백그라운드 작업으로 미리 채우는 방법도 실패하거나 늦어질 수 있으므로, 만료 이후 경로가 필요합니다.

기다리는 대신 조금 오래된 값을 주기

뉴스 목록처럼 약간 늦어도 되는 값은 soft TTL과 hard TTL을 나눌 수 있습니다. soft TTL 전에는 그대로 응답합니다. soft TTL 뒤에는 옛 값을 응답하면서 로더 하나가 갱신합니다. hard TTL 뒤에는 옛 값을 더 이상 사용하지 않고, 제한된 대기나 실패 응답을 선택합니다. 이 방식이 stale-while-revalidate입니다.

soft TTL 전 soft와 hard 사이 hard TTL 뒤 요청 값의 나이 현재 값 응답 옛 값 응답 로더 하나로 갱신 제한된 대기 또는 실패

Redis의 EX는 hard TTL로 두고 soft 만료 시각은 값의 메타데이터에 넣습니다. 갱신 작업이 실패해도 hard TTL을 무조건 연장하지 않습니다. 권한 취소처럼 오래된 값이 위험한 읽기에는 이 정책을 적용하지 않습니다. 로컬 캐시에도 같은 신선도 예산을 적용해야 단계마다 오래된 값이 누적되지 않습니다.

2. 캐시 관통 (Cache Penetration)

DB에도 없는 키를 계속 물으면 캐시는 영원히 채워지지 않고 매번 DB로 갑니다. 존재하지 않는 상품 ID로 크롤러가 긁거나, 공격자가 무작위 ID를 보낼 때 생깁니다.

  • 없음도 캐시합니다(negative caching). DB에 없으면 빈 값 표시를 짧은 TTL로 넣습니다. 나중에 그 키가 생기면 쓰는 쪽에서 지워 주면 됩니다.
  • 블룸 필터를 앞에 둡니다. 삽입한 원소에는 거짓 음성이 없습니다. 하지만 DB에 새로 만든 ID를 필터에 반영하지 않았으면 정상 요청을 거절할 수 있습니다. 초기 구축과 갱신이 따라잡았을 때만 “확실히 없음”을 신뢰합니다. 필터가 준비되지 않았으면 DB 경로로 보내되 동시성은 제한합니다. 구조는 블룸 필터와 cuckoo filter 비교에서 다뤘습니다.
  • 입력 검증으로 말이 안 되는 ID(음수, 범위 밖)를 먼저 버리는 것이 가장 쌉니다.

3. 캐시 눈사태 (Cache Avalanche)

많은 키가 같은 순간에 만료되는 경우입니다. 배포 직후 캐시를 한꺼번에 채웠거나, 매일 자정에 일괄로 TTL 24시간을 걸었으면 다음 자정에 전부 같이 만료됩니다. 스탬피드가 키 하나라면 눈사태는 수천 개가 동시에 오는 것입니다. Redis 서버 자체가 죽어 캐시가 통째로 비는 것도 같은 이름으로 부릅니다.

  • TTL에 무작위 흔들기(jitter)를 섞습니다. ex=3600 + random.randint(0, 600)이라는 예시는 만료를 10분에 걸쳐 퍼뜨립니다. 최대 TTL 4,200초를 허용할 수 있는 데이터에만 적용합니다.
  • 서버가 죽는 경우에 대비해 레플리카와 Sentinel(또는 Cluster)로 장애 조치를 두고, DB 앞에 서킷 브레이커나 동시 요청 제한을 둡니다. 캐시가 비었을 때 DB가 받을 수 있는 만큼만 들여보내는 장치입니다.

4. 핫 키와 빅 키 (Hot Key, Big Key)

Redis는 명령을 한 스레드에서 차례로 실행합니다. 6.0의 I/O 스레드(io-threads)는 소켓 읽기와 쓰기만 나눠 맡고, 명령 실행은 여전히 메인 스레드 하나입니다. 그래서 키 하나에 요청이 몰리면(핫 키) 클러스터에서도 그 키가 있는 샤드 하나만 뜨거워집니다. 노드를 늘려도 나아지지 않습니다.

  • 애플리케이션 메모리에 아주 짧은 TTL로 한 번 더 캐시합니다(로컬 캐시, near cache). 6.0의 client-side caching(CLIENT TRACKING)을 쓰면 그 키가 바뀌었을 때 서버가 무효화 메시지를 보내 줍니다. 연결이 끊겨 무효화를 놓쳤다면 로컬 캐시를 비우거나 더 이상 신뢰하지 않는 복구 절차가 필요합니다. keyspace notification도 재생 가능한 이벤트 로그는 아니므로 연결 중단을 정합성 설계에 포함합니다.
  • 읽기 전용이면 hot:item:42:copy:0, hot:item:42:copy:1처럼 서로 다른 슬롯에 가는 키로 복제해 무작위로 읽습니다. 같은 {item:42} 해시 태그를 붙이면 전부 같은 슬롯에 남아 샤드 분산이 되지 않습니다. 복제본의 갱신과 삭제 비용도 늘어납니다.
  • 찾을 때는 redis-cli --hotkeys를 씁니다. LFU 카운터를 읽어 보는 것이라 LFU 정책일 때만 동작합니다.

빅 키는 원소가 수백만 개인 hash나 수백 MB짜리 문자열입니다. 문제는 지울 때 옵니다. 원소 수백만 개를 해제하는 동안 메인 스레드가 멈추고, 그동안 다른 모든 명령이 기다립니다.

  • DEL 대신 UNLINK를 씁니다. 키 공간에서 바로 떼어 낸 뒤 lazyfree.c가 해제 비용을 판단합니다. 비용이 64(LAZYFREE_THRESHOLD)를 넘고 참조 수가 1인 객체만 백그라운드로 넘깁니다. 큰 hash처럼 할당이 많은 구조에 도움이 됩니다. 문자열이나 한 덩어리로 인코딩된 구조는 비용을 1로 계산하므로, 바이트 수가 크다는 이유만으로 비동기 해제가 되지는 않습니다.
  • 만료와 축출로 지워질 때도 같은 문제가 있습니다. lazyfree-lazy-expire, lazyfree-lazy-eviction을 켜면 이것도 백그라운드로 갑니다. 6.2에서 기본값은 둘 다 no입니다.
  • 찾을 때는 redis-cli --bigkeys나 MEMORY USAGE key를 씁니다. 처음부터 쪼개 두는 것이 제일 낫습니다.

--bigkeys는 전체를 SCAN으로 훑으므로 운영 부하를 보면서 속도를 제한합니다. 운영 서버에서 KEYS *로 전체 키를 찾거나 큰 hash를 HGETALL로 통째로 읽는 진단은 피합니다. UNLINK는 해제 비용을 다루는 명령이지 큰 응답 직렬화와 전송 비용까지 없애 주지는 않습니다.

캐시라도 알아야 하는 영속성

“캐시니까 날아가도 된다”고 해도, 재시작하면 빈 캐시에서 출발하고 그게 곧 눈사태입니다. 그래서 캐시 서버에도 영속성 설정이 의미가 있습니다.

  • RDB는 fork()로 자식 프로세스를 만들어 스냅숏을 씁니다. 부모와 자식이 메모리 페이지를 공유하다가 부모가 고친 페이지만 복사됩니다(copy-on-write). 쓰기가 많은 서버면 스냅숏 동안 메모리가 최악의 경우 두 배 가까이 필요하고, 데이터가 클수록 fork() 자체도 오래 걸립니다.
  • AOF는 쓰기 명령을 로그로 남깁니다. Redis 6.2.6 설정 파일의 기본은 appendonly no입니다. AOF를 켰을 때 기본 fsync 정책이 everysec이며, 보통 약 1초의 유실 구간을 고려합니다. 디스크나 fsync 지연이 있으면 정확한 상한으로 쓰면 안 됩니다.
  • 복제는 비동기입니다. 클라이언트의 쓰기 성공 응답은 레플리카 수신이나 디스크 fsync 완료를 기다렸다는 뜻이 아닙니다. 장애 조치 때 승인된 쓰기를 잃을 수 있습니다. WAIT로 레플리카 확인을 기다려도 합의 기반 저장소나 디스크 영속성 보장으로 바뀌지는 않습니다.

캐시를 설계할 때 먼저 정하는 예산

상품 설명은 잠깐 늦어도 되지만 결제 금액은 그렇지 않습니다. 데이터마다 허용하는 오래됨, 응답 마감, 캐시가 없을 때 DB가 받을 수 있는 부하를 먼저 정합니다. TTL은 그 결정의 결과입니다.

TTL jitter를 넣을 때도 신선도 상한을 지킵니다. 허용 상한이 300초인 예시라면 300 + random.randint(0, 60)은 최대 360초가 되어 상한을 어깁니다. random.randint(240, 300)처럼 상한 안에서 흔드는 방법을 택합니다. 이 숫자는 측정값이 아니라 예산을 설명하기 위한 가정입니다.

캐시 키에는 결과를 바꾸는 입력을 넣습니다. 사용자 ID가 같아도 테넌트, 언어, 접근 권한이 다르면 같은 응답이 아닐 수 있습니다. 직렬화 형식이 바뀌면 user:v2:...처럼 네임스페이스도 바꿉니다. 다른 사용자의 응답을 돌려주는 버그는 짧은 TTL로 해결되지 않습니다.

Negative caching은 “없음”을 빈 문자열, 빈 목록, Redis 미스와 구별해 저장합니다. DB 조회 실패를 “없음”으로 바꾸면 장애가 정상 응답으로 남습니다. 새 데이터가 생겼을 때 negative entry를 지우는 경로도 확인합니다.

미스를 합치는 single-flight와 명령을 묶는 pipeline도 구별합니다. Pipeline은 여러 Redis 명령을 왕복마다 하나씩 기다리지 않고 보내는 방식입니다. 트랜잭션이 아니므로 GET 뒤 SET 사이에 다른 클라이언트가 끼어드는 정합성 문제를 해결하지 않습니다. 묶음 크기도 제한해야 응답 메모리와 대기가 커지지 않습니다.

퀴즈잠깐 확인: 왕복을 줄이면 원자적인가요?

답을 고르면 해설이 나옵니다.

1문제 중 0개 맞힘

  1. GET과 SET을 pipeline으로 묶었습니다. 다른 요청과의 순서 꼬임도 없어지나요?

단순한 부하 모형에서는

QDB≈Qread(1−h)Q_{\mathrm{DB}} \approx Q_{\mathrm{read}}(1-h)

입니다. 읽기 요청이 초당 10,000개라고 가정하면 적중률 99%에서는 DB 조회가 약 100개, 90%에서는 약 1,000개입니다. 식에 가정값을 대입한 결과이며, 미스마다 DB 조회를 한 번 한다고 가정했습니다. 재시도, refresh, 로컬 캐시, 쓰기는 제외한 모형입니다. 적중률이 조금 내려간 것으로 보여도 DB 부하는 훨씬 커질 수 있습니다.

평균 적중률 하나로 결정하지 않습니다. 싼 조회가 많이 적중하고 비싼 조회만 빠지면 적중률이 높아도 DB는 과부하입니다. 경로별 미스 수와 백엔드 소요 시간을 보고, 용량을 늘릴지 TTL을 바꿀지 판단합니다. 다시 읽히지 않는 결과까지 저장하는 캐시 오염도 확인합니다.

장애가 났을 때 보는 순서

Redis 6.2.6에서 확인할 명령
redis-cli INFO stats
redis-cli INFO memory
redis-cli INFO replication
redis-cli INFO persistence
redis-cli CONFIG GET maxmemory
redis-cli CONFIG GET maxmemory-policy
redis-cli SLOWLOG GET 20
redis-cli LATENCY LATEST

LATENCY LATEST는 latency-monitor-threshold로 수집을 켜 둔 이벤트만 보여 줍니다. 빈 결과가 지연이 없다는 증거는 아닙니다. SLOWLOG는 명령 실행 시간을 보여 주며 네트워크 왕복이나 클라이언트 대기 시간 전체를 재지 않습니다. 운영 ACL에서는 CONFIG GET 권한이 없을 수도 있습니다.

관측함께 볼 것판단
keyspace_misses가 증가요청 수, expired_keys, evicted_keys, 경로별 미스만료, 축출, 원래 없는 조회를 구분합니다
evicted_keys가 증가maxmemory, 메모리 사용량, 적중률축출은 캐시에서 정상 동작일 수도 있습니다. 적중률이 무너질 때 조사합니다
SET에서 OOM한도, 정책, TTL 후보, 실제 에러무작정 재시도하지 않고 공간과 정책을 확인합니다
RSS가 높게 유지used_memory, mem_fragmentation_ratio, lazyfree_pending_objects살아 있는 데이터, allocator, 해제 대기를 구분합니다
Redis는 빠른데 앱이 느림연결 풀 대기, 왕복, payload 크기, DB 미스Redis 명령 시간만 보지 않습니다
장애 조치 직후 미스 폭증복제 지연, cold cache, DB 동시 요청 수캐시 복구와 DB 보호를 함께 합니다

INFO stats의 hits와 misses는 누적값입니다. 같은 구간의 차이로 Δhits / (Δhits + Δmisses)를 계산하며, 분모가 0이면 적중률을 계산하지 않습니다. 이 값은 Redis 조회 통계여서 로컬 캐시 적중이나 애플리케이션 전체 요청 비율과 같지 않습니다.

maxmemory는 프로세스 RSS나 컨테이너 메모리의 절대 상한도 아닙니다. Redis 6.2.6의 freeMemoryGetNotCountedMemory는 AOF와 레플리카 출력 버퍼를 축출 계산에서 제외합니다. allocator의 여유 공간과 RDB의 copy-on-write도 따로 고려해야 합니다. 컨테이너 한도까지 데이터로 채우면 Redis가 축출할 기회보다 먼저 OS가 프로세스를 종료할 수 있습니다.

Redis 타임아웃 때 모든 요청을 DB로 보내면 캐시 장애가 DB 장애로 번질 수 있습니다. 연결 타임아웃과 읽기 타임아웃, 제한된 재시도, 백엔드 동시성 제한을 함께 둡니다. 이미 실패한 경로로 계속 보내지 않는 서킷 브레이커와, 허용된 오래된 값으로 응답하는 정책도 선택지입니다. 모든 기능을 무제한 우회시키는 정책은 피합니다.

복구 직후에도 전체 키를 한 번에 채우지 않습니다. 인기 데이터부터 속도를 제한해 예열합니다. TTL jitter는 만료 시각을 분산하고, single-flight는 같은 키의 로더를 합치며, 동시성 제한은 서로 다른 키의 미스까지 포함해 DB를 보호합니다. 각 장치가 막는 부하가 다릅니다.

면접 용어 정리

용어한 줄 설명
Cache-aside앱이 캐시를 먼저 보고, 미스면 DB에서 읽어 채운다. 쓰기는 DB 갱신 후 캐시 삭제
Write-through쓰기 때 캐시와 DB를 동기로 같이 갱신
Write-behind캐시에 먼저 쓰고 DB 반영은 비동기로 모아서
TTL캐시 저장부터의 수명. DB 커밋부터의 정합성 상한은 아닙니다
게으른 만료키에 접근할 때 만료 여부를 보고 지운다
능동 만료기본 목표 20개를 조사하며 만료 비율과 시간 한도로 반복을 판단합니다
근사 LRU기본 표본 수 5개로 후보 풀을 보충하고 오래 쉰 후보를 지웁니다
LFU 카운터8비트 로그 카운터, 초기값 5, 분 단위로 감소
noeviction기본 정책. 공간 부족 시 SET 등 denyoom 명령을 거절합니다
스탬피드인기 키 만료 순간 요청이 한꺼번에 DB로
관통존재하지 않는 키 조회가 캐시를 통과해 DB로
눈사태많은 키가 동시에 만료되거나 캐시 서버가 통째로 빔
핫 키키 하나에 요청이 몰려 샤드 하나가 과열
빅 키원소나 크기가 커서 삭제, 전송 때 메인 스레드를 막는 키
UNLINK키를 떼어 내고 해제 비용과 참조 수에 따라 비동기 해제를 선택합니다
Copy-on-writeRDB fork 중 부모가 고친 페이지만 복사

퀴즈

퀴즈

답을 고르면 해설이 나옵니다.

13문제 중 0개 맞힘

  1. Redis 6.2에서 maxmemory를 설정하고 기본 정책을 유지했습니다. 한도 초과를 해소할 수 없으면?

  2. 읽지 않는 만료 키를 능동 만료가 지웁니다. 소스가 보장하는 것은?

  3. maxmemory-samples=1을 allkeys-random과 같은 알고리즘이라고 해도 되나요?

  4. LFU 카운터가 0이 아니라 5에서 시작하는 이유는?

  5. 스탬피드 방지 락에서 락을 잡은 뒤 캐시를 한 번 더 보는 이유는?

  6. 모든 상품 캐시에 매일 자정 TTL 24시간을 걸었습니다. 가장 걱정되는 것은?

  7. 원소 500만 개짜리 hash를 지워야 합니다. 메인 스레드를 덜 막는 명령은?

  8. 레플리카에서 만료된 키를 GET하면?

  9. 권한 취소 직후 읽기에도 stale-while-revalidate를 적용하려고 합니다. 어떤 판단이 맞나요?

  10. 허용 TTL 상한이 300초인 예시에서 만료를 분산할 식은?

  11. DB에는 새 상품이 있는데 블룸 필터 갱신이 늦었습니다. 필터의 '없음'을 그대로 믿으면?

  12. 핫 키를 hot:{item:42}:0과 hot:{item:42}:1로 복제했습니다. Cluster의 부하가 분산되나요?

  13. Redis 장애 때 타임아웃 난 요청을 전부 DB로 우회시켰습니다. 먼저 필요한 보호 장치는?

플래시카드

카드를 누르면 뒤집힙니다. 면접 전날 한 번 넘겨 보기 좋게 만들었습니다.

암기 카드

카드를 누르면 뒤집힙니다.

15장 중 0장 확인

정리

  • 캐시를 쓸 때는 DB를 고치고 캐시를 지우고, TTL은 짧더라도 꼭 겁니다.
  • 만료는 접근 경로와 능동 조사에서 처리합니다. 시간 한도 때문에 일부 만료 키는 한동안 남습니다.
  • 메모리 한도와 축출 정책을 함께 정합니다. 기본 noeviction에서는 공간 부족 시 SET이 실패합니다.
  • LRU는 후보 풀을 사용하는 근사치이고 LFU는 확률 카운터입니다. 측정에서 나온 비율과 포화 횟수를 보장값으로 쓰지 않습니다.
  • 같은 키의 동시 미스, 없는 키 조회, 여러 키의 만료, 큰 객체 해제는 서로 다른 대책이 필요합니다. TTL과 락 하나로 모두 해결되지는 않습니다.
  • 신선도와 대기 시간을 정하고 DB를 보호합니다. 복습 뒤에는 실제 서비스의 장애 시 경로를 그려 보시면 됩니다.

각주

  1. Andrea Vattani, Flavio Chierichetti, Keegan Lowenstein, “Optimal Probabilistic Cache Stampede Prevention”, VLDB 2015. ↩

~/blog/redis-cache-field-guide/en.mdx

A Field Guide to Redis Caching: Expiry, Eviction, and the Herd at the Door

A cache is easy to add and hard to run. I read how expiry and eviction actually work in the Redis 6.2 source, then measured a cache stampede and approximate LRU. Interview glossary, quiz, and flash cards at the end.

The first time you put a cache in front of something, the latency graph drops to the floor. You then spend the next few months fixing problems the cache caused. Expiries line up and knock the database over, requests for keys that do not exist sail straight through, and memory fills until writes stop.

This post walks through those problems one at a time and checks in the source how Redis handles each. The source is Redis 6.2.6; measurements were taken against the official Docker image of the same version with a Python client (redis-py), on an Apple Silicon laptop. The numbers come from that setup, so look at their shape more than their absolute values.

The first half covers cache patterns and how Redis does expiry and eviction. The second half goes through failure modes. At the end there is a glossary of interview terms and a quiz. I wrote it with backend engineers and people who run Redis in mind, but a frontend developer should be able to follow along.

Where the cache goes

The patterns are named after the order in which you read and write.

With cache-aside (lazy loading), the application checks the cache first; on a miss it reads the database and fills the cache. On a write it commits the database transaction, then deletes the cache key. A path without the cache is possible, but the example below propagates Redis errors to its caller. Falling back to the database during an outage requires timeouts and a database concurrency limit.

cache-aside
def get_user(uid):
v = r.get(f"user:{uid}")
if v is not None:
return json.loads(v)
row = db.fetch_user(uid) # cache miss
r.set(f"user:{uid}", json.dumps(row), ex=300)
return row
def update_user(uid, fields):
db.update_user(uid, fields)
r.delete(f"user:{uid}") # delete, don't overwrite

Deleting instead of overwriting on write is deliberate. If two writes land almost together, the database can end with B as the last value while the cache ends with A. Deleting means the next read refetches from the database, so that ordering mix-up does not survive until the TTL.

A gap remains. A read fetches the old value from the database, a write then deletes the cache key, and the read puts the old value into the cache.

Read old value A A Commit new value B DEL user:42 SET user:42 A EX 300 Reader Database Writer Redis

The reader gets A. Reading the database and filling the cache are separate operations, so another request can run between them.

The writer commits B and deletes the cache entry. It followed the intended commit-then-delete order.

The delayed reader inserts A again. The database holds B while the cache holds A.

TTL bounds the lifetime of that last SET, not staleness measured from the database commit. A delayed read or a lagging database replica can insert the old value much later. Extending TTL on every read can keep it alive even longer.

The same code ends differently depending on where the other request intervenes. Choose an ordering below and advance one step at a time. Changed values are highlighted.

When SET arrives after DEL

DatabaseA
Value held by readerNone
RedisNone

Start

The database holds A and the cache is empty.

An authored model of the example's execution order, not a measurement of request timing or Redis throughput. Each cell shows the state after that step.
All snapshots (also available without JavaScript)

Late SET: the old value returns

  1. Start

    Database: A · Value held by reader: None · Redis: None

    The database holds A and the cache is empty.

  2. Reader: fetch A from the database

    Database: A · Value held by reader: A · Redis: None

    The reader has not filled the cache yet.

  3. Writer: commit B

    Database: B · Value held by reader: A · Redis: None

    The database changes to B. The A already held by the reader does not change.

  4. Writer: execute DEL

    Database: B · Value held by reader: A · Redis: None

    Deleting the empty cache cannot delete the A held by the in-flight reader.

  5. Reader: SET A

    Database: B · Value held by reader: A · Redis: A · TTL 300s

    The database holds B, the cache holds A. TTL starts at this SET.

In this ordering, DEL arrives last

  1. Start

    Database: A · Value held by reader: None · Redis: None

    Start with the same A and empty cache.

  2. Reader: fetch A from the database

    Database: A · Value held by reader: A · Redis: None

    This reader also gets A.

  3. Reader: SET A

    Database: A · Value held by reader: A · Redis: A · TTL 300s

    This time the reader fills the cache before the writer intervenes.

  4. Writer: commit B

    Database: B · Value held by reader: A · Redis: A · TTL 300s

    A remains cached until DEL.

  5. Writer: execute DEL

    Database: B · Value held by reader: A · Redis: None

    The cache is empty. The next miss reads the database again. This is one ordering, not a guarantee across all interleavings.

For prices or permissions where stale reads are unacceptable, read the database or design a protocol that checks versions against the authoritative data. Record failed invalidations so they can be retried. An outbox records the invalidation event in the same database transaction as the update; CDC is another option. Both still need to handle delivery delay and out-of-order events.

QuizCheckpoint: what TTL bounds

Choose an answer to see the explanation.

0 of 1 right

  1. After B is committed and the cache invalidated, a delayed reader inserts A with a 60-second TTL. Which statement is correct?

Read-through does the same thing as cache-aside, but the cache layer (a library or proxy) does it for you. The application only talks to the cache.

Write-through updates the cache and database synchronously on every write. Written values enter the cache, but expiration and eviction can still cause misses. Two stores do not become one transaction; partial success needs a recovery policy.

Write-behind (write-back) writes to the cache first and flushes to the database later in batches. Writes are fast and database load drops, but if the cache dies you lose the writes that were not flushed. It suits values where losing a little is fine, like view counts or likes.

1. GET miss 2. SELECT 3. SET ex=300 write 1. UPDATE and commit write 2. DEL Application Redis DB

Expiry: when does “delete” happen

SET key value EX 60 makes Redis record the key and its expiry time (Unix milliseconds) in a separate hash table called expires. Nothing wakes up at the 60-second mark to remove the key. There are two ways it gets deleted.

Lazy expiry

Ordinary key lookup paths check expiry with expireIfNeeded. A command such as DBSIZE, which reports the size of the keyspace, does not expire every key by visiting it.

src/db.c (Redis 6.2.6, comments removed)
int expireIfNeeded(redisDb *db, robj *key) {
if (!keyIsExpired(db,key)) return 0;
if (server.masterhost != NULL) return 1;
if (checkClientPauseTimeoutAndReturnIfPaused()) return 1;
/* Delete the key */
deleteExpiredKeyAndPropagate(db,key);
return 1;
}

If the expiry time has not passed, nothing happens. Most accesses stop on this line.

A replica only answers “expired” and does not delete. Keys on a replica are removed only by a DEL sent from the master, which keeps the two datasets from drifting apart. Reads see the key as missing, but its memory stays until the master deletes it.

While clients are paused (CLIENT PAUSE, during a failover) it does not delete either, because the dataset must stay unchanged.

On a master, it deletes the key and propagates a DEL (or UNLINK) to the AOF and the replicas.

With only this, an expired key that nobody reads stays in memory forever.

Active expiry

So the server samples the expires table periodically. That is activeExpireCycle. The constants sit at the top of expire.c.

ConstantValueMeaning
KEYS_PER_LOOP20Keys sampled per pass
FAST_DURATION1000µsTime limit of the fast cycle that runs between event loop iterations
SLOW_TIME_PERC25Share of CPU the slow cycle in serverCron may use
ACCEPTABLE_STALE10If the expired share of a sample is below this, stop on this DB

Raising active-expire-effort (default 1, max 10) increases work and time limits and lowers the acceptable share. Redis 6.2.6’s activeExpireCycle scans hash buckets with a cursor. Its default target is 20 keys, but it finishes a bucket’s collision chain, so that is not an exact count. It repeats when the expired share exceeds 10%, unless the time budget runs out. If the expires table has less than 1% bucket occupancy, it skips this pass until the table can shrink.

I checked that it really behaves like this. I inserted 200,000 keys with a 1-second TTL and 200,000 with a 1-hour TTL, read nothing, and watched DBSIZE. hz was the default 10.

0 0.5 1.0 1.5 2.0 5.1 12.2 200 250 300 350 400 Elapsed (s) Keys (thousands) DBSIZE after inserting 200K keys with a 1 s TTL (no reads)

Once the first second passed, 197,000 keys were gone within the next second. But after 2 seconds the curve goes flat. The remaining two thousand or so drained slowly, about 100 per second.

With 2,000 expired keys among 200,000 live ones, the overall expired share is around 1%. A pass finding a low share does not repeat, which can explain the slower tail. Sampling and time budgets do not guarantee that the actual expired share stays below 10%. The expired_stale_perc field in INFO stats is a smoothed estimate; during the run it peaked at 28% and settled near 13%.

Expired keys not yet deleted are included in DBSIZE. If the key count falls but process RSS stays high, the allocator may retain freed memory or leave fragmentation. Compare used_memory, used_memory_rss, and lazyfree_pending_objects to distinguish delayed expiry from delayed freeing.

Eviction: when memory is full

When maxmemory is reached, Redis calls performEvictions to make room before running a command. What gets removed is decided by maxmemory-policy.

PolicyCandidatesRule
noevictionnoneCommands that may grow memory, such as SET, get an OOM error
allkeys-lru / volatile-lruall / keys with TTLLeast recently used
allkeys-lfu / volatile-lfuall / keys with TTLLeast frequently used
allkeys-random / volatile-randomall / keys with TTLRandom
volatile-ttlkeys with TTLNearest expiry

The default policy is noeviction; the default maxmemory on a 64-bit server is 0, meaning unlimited. Set a limit for the policy to matter. If Redis cannot relieve an over-limit condition, the OOM check in server.c rejects commands marked denyoom, such as SET. It does not reject every write: commands such as DEL can return space. A volatile-* policy cannot make space if there are no keys with TTLs.

LRU is an approximation

True LRU needs a linked list ordering every key by access. That is two pointers, 16 bytes, per key, plus a list update on every read.

Instead, Redis writes the last access time, in seconds, into 24 bits of the key’s object header. To evict, it picks maxmemory-samples keys at random (default 5), puts them into a candidate pool of size 16, and evicts the one in the pool that has been idle longest.

src/evict.c, evictionPoolPopulate (excerpt)
if (server.maxmemory_policy & MAXMEMORY_FLAG_LRU) {
idle = estimateObjectIdleTime(o);
} else if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
idle = 255-LFUDecrAndReturn(o);
} else if (server.maxmemory_policy == MAXMEMORY_VOLATILE_TTL) {
idle = ULLONG_MAX - (long)dictGetVal(de);
}

All three policies reduce to one rule: evict the highest idle score first. LFU inverts the frequency and TTL inverts the expiry time so they fit in the same pool.

I measured how much the sample count matters. I inserted 10 batches of 2,000 keys, 1.05 seconds apart, with memory capped at about half the total. Under ideal LRU every surviving key would be in the newer half.

Exact LRU

samples 1

samples 3

samples 5default

samples 10

Each bar is one batch of keys; older keys are to the left. Solid bars are keys that survived eviction.
maxmemory-samplesSurvivors in the newer half
153.6%
384.5%
5 (default)92.0%
1096.6%
ideal LRU100%

In this run, one sample produced a result close to random eviction. It is still not the same algorithm as allkeys-random: evictionPoolPopulate retains earlier candidates for later choices. More samples compare more candidates at a CPU cost, so measure hit rate and latency together. These bars show measured results, not an animation of the eviction algorithm.

LFU counts in 8 bits

In LFU mode the same 24 bits are split in two. The high 16 bits hold the last decrement time in minutes, the low 8 bits the access count. Eight bits only reach 255, so instead of adding one per access, the counter goes up with a probability.

src/evict.c
uint8_t LFULogIncr(uint8_t counter) {
if (counter == 255) return 255;
double r = (double)rand()/RAND_MAX;
double baseval = counter - LFU_INIT_VAL;
if (baseval < 0) baseval = 0;
double p = 1.0/(baseval*server.lfu_log_factor+1);
if (r < p) counter++;
return counter;
}

A new key starts at 5 (LFU_INIT_VAL). Starting at 0 would make a freshly inserted key the first in line for eviction. The higher the value, the lower the chance of the next increment, so the counter grows roughly logarithmically. I ran GET on one key N times and read OBJECT FREQ.

lfu-log-factor01101001K10K100K1M
15691945145255255
10 (default)566102145148255
100566791850162

In this run, with the default factor of 10, I observed 255 after a million reads. Increments are probabilistic; a million is not a fixed saturation threshold. Lowering the factor makes the counter grow faster, but saturated popular keys become harder to distinguish.

LFUDecrAndReturn lowers the score by elapsed minutes since the last access divided by lfu-decay-time (default 1 minute). There is no timer updating every key each minute. Access updates the stored value; eviction sampling also compares decayed scores. With the defaults, a key idle for 10 minutes loses up to 10 when evaluated, never falling below zero.

Which to pick depends on the access pattern. When a batch job scans the whole dataset once, LRU swaps the cache over to the scanned keys. Under LFU a key read once stays at 5 or 6, so the keys that were popular before hold on.

Four ways a cache falls over

These are the names that come up in interviews over and over.

1. Cache stampede (thundering herd)

The moment a popular key expires, every request waiting on it sees a miss and goes to the database at once. If the query takes 200 ms, every request in those 200 ms runs the same query. Also called dog-piling.

I measured it. The backend was a function that sleeps for 200 ms, and I ran the cache-aside code above on many threads, held at a barrier so they all started at the same instant.

"10" "100" "500" "1000" 0 100 200 300 400 500 600 Backend calls Backend calls by number of concurrent requests (empty cache)

Up to 100 requests, every request hit the database. At 500 and 1000 there were fewer calls than requests, but not because anything fixed it. The first request filled the cache before all the Python threads got going. Production concurrency differs from this thread experiment, so it cannot predict exact call counts.

Fix 1: a lock. Only one of the requests that missed goes to the database; the rest wait briefly and check the cache again.

lock + re-check
import math
import time
from uuid import uuid4
RELEASE = r.register_script("""
if redis.call('get', KEYS[1]) == ARGV[1] then
return redis.call('del', KEYS[1])
end
return 0
""")
PUBLISH = r.register_script("""
if redis.call('get', KEYS[1]) ~= ARGV[1] then return 0 end
redis.call('set', KEYS[2], ARGV[2], 'EX', ARGV[3])
return 1
""")
def get_with_lock(key, load, ttl=60, wait=1.0):
if type(ttl) is not int or ttl <= 0 or not math.isfinite(wait) or wait <= 0:
raise ValueError("ttl must be a positive integer; wait must be positive")
deadline = time.monotonic() + wait
lock_key = f"{key}:lock"
while time.monotonic() < deadline:
v = r.get(key)
if v is not None:
return v
token = uuid4().hex
if r.set(lock_key, token, nx=True, px=2000):
try:
v = r.get(key) # check again
if v is None:
remaining = deadline - time.monotonic()
if remaining <= 0:
raise TimeoutError("cache fill deadline")
v = load(timeout=remaining)
if time.monotonic() >= deadline:
raise TimeoutError("cache fill deadline")
if not PUBLISH(keys=[lock_key, key],
args=[token, v, ttl]):
raise TimeoutError("cache fill lease lost")
return v
finally:
RELEASE(keys=[lock_key], args=[token])
time.sleep(min(0.02, max(0, deadline - time.monotonic())))
raise TimeoutError("cache fill wait expired")

Two details matter here.

  • If you release with a plain DEL, then after your lock outlives its 2-second PX and someone else takes it, you can delete their lock. So the lock holds a token, and a Lua script deletes it only if the token is still yours, in one step.
  • Right after taking the lock, check the cache once more. A request that takes the lock just after the previous holder filled the cache and released it will hit the database again without this check. Without that line, 500 and 1000 requests produced 1 to 2 backend calls; with it, exactly 1 in both cases.

The chart compares basic cache-aside with locking and a re-check. The code above adds a wait deadline and ownership-checked publication. It avoids unbounded waiting and prevents a loader that lost its lease from overwriting the cache later.

r is a redis-py client with finite connection and socket timeouts. load(timeout=...) must enforce that timeout in its database query or HTTP request. Python clock checks cannot cancel a function that never stops. Values are bytes or strings Redis can store directly. Redis and database errors propagate to the caller.

I checked the revised example with npm run test:cache -- --docker against Redis 6.2.6. The test executes the post’s Python fence directly and evaluates its Lua in Redis. It checks concurrent misses, empty-value hits, wait deadlines, ownership loss, database errors, rejection of late results, and OOM behaviour, and compares the Korean and English examples’ ASTs. These checks were run when revising the post, separately from the original measurements above.

In Cluster, both Lua keys must share a slot. A value key of {user:42}:value produces a lock key of {user:42}:value:lock, using the same hash tag. Lease expiry, lock-key eviction, or failover can allow overlapping loaders. This lock reduces duplicate cache fills. It does not provide exactly-once payments or inventory updates; use database transactions and idempotency keys for those.

QuizCheckpoint: a loader loses its lease

Choose an answer to see the explanation.

0 of 1 right

  1. The loader pauses until its lock expires. Does its token still give it exclusive access?

Fix 2: refresh early. XFetch (probabilistic early expiration) stores how long the value took to compute and increases the chance of an early refresh as expiry approaches1. It reduces clumping without guaranteeing exactly one refresher. Scheduled background warming can fail or run late too, so keep a path for requests after expiry.

Serve something slightly old instead of waiting

For data such as a news list, separate soft TTL from hard TTL. Before the soft deadline, serve the value. Between soft and hard, serve the old value while one loader refreshes it. After hard expiry, stop using the old value and choose bounded waiting or an error. This is stale-while-revalidate.

Before soft TTL Between soft and hard After hard TTL Request Value age Serve current value Serve stale value One loader refreshes Bounded wait or error

Use Redis EX for the hard TTL and put the soft deadline in the value’s metadata. Do not automatically extend hard TTL after a failed refresh. This policy is inappropriate for reads such as permission revocation where staleness is dangerous. Apply the same freshness budget to local caches so multiple layers do not keep extending stale data.

2. Cache penetration

If you keep asking for keys that are not in the database either, the cache never fills and every request goes to the database. It happens when a crawler walks nonexistent product IDs or an attacker sends random IDs.

  • Cache the absence too (negative caching). If the database has nothing, store an empty marker with a short TTL. If the key is created later, the writer deletes the marker.
  • Put a Bloom filter in front. Inserted elements have no false negatives. But if a newly created database ID has not reached the filter, it can reject a valid request. Trust “definitely absent” only after initial population and updates have caught up. If the filter is not ready, use a concurrency-limited database path. The structure is covered in Bloom versus cuckoo filters.
  • Rejecting nonsense IDs (negative, out of range) with input validation is cheapest of all.

3. Cache avalanche

Many keys expire at the same moment. If you filled the cache all at once right after a deploy, or set a 24-hour TTL on everything in a nightly job, it all expires together at the next midnight. A stampede is one key; an avalanche is thousands at once. The same name is used when the Redis server itself dies and the whole cache goes empty.

  • Add jitter to TTLs. The illustrative ex=3600 + random.randint(0, 600) spreads expiry over ten minutes. Use it only for data that tolerates a maximum TTL of 4,200 seconds.
  • For server death, set up failover with replicas and Sentinel (or Cluster), and put a circuit breaker or a concurrency limit in front of the database. That lets in only as much as the database can take while the cache is empty.

4. Hot keys and big keys

Redis runs commands one after another on a single thread. The I/O threads added in 6.0 (io-threads) only share socket reads and writes; commands still execute on the one main thread. So when requests pile onto one key (a hot key), only the shard holding it heats up, even in a cluster. Adding nodes does not help.

  • Cache it again in application memory with a very short TTL (a local or near cache). With client-side caching in 6.0 (CLIENT TRACKING), the server sends an invalidation message when that key changes. If a disconnect loses invalidations, recovery must discard or stop trusting the local cache. Keyspace notifications are not a replayable event log either; connection loss belongs in the consistency design.
  • For read-only data, copy it into keys in different slots, such as hot:item:42:copy:0 and hot:item:42:copy:1, and read one at random. Giving every copy the same {item:42} hash tag leaves them in the same slot and does not spread load across shards. Copies also increase update and invalidation work.
  • To find hot keys, use redis-cli --hotkeys. It reads LFU counters, so it only works under an LFU policy.

A big key is a hash with millions of fields or a string of hundreds of megabytes. The trouble comes when you delete it. Freeing millions of elements blocks the main thread, and every other command waits.

  • Use UNLINK instead of DEL. It detaches the key immediately, then lazyfree.c evaluates the freeing cost. It hands off only when effort exceeds 64 (LAZYFREE_THRESHOLD) and the reference count is 1. It helps structures with many allocations, such as a large hash. Strings and single-allocation encodings have effort 1, so a large byte size alone does not cause asynchronous freeing.
  • Expiry and eviction have the same problem. Turning on lazyfree-lazy-expire and lazyfree-lazy-eviction sends those to the background too. In 6.2 both default to no.
  • To find big keys, use redis-cli --bigkeys or MEMORY USAGE key. Splitting them from the start is best.

--bigkeys scans the keyspace with SCAN, so limit its rate while watching production load. Avoid diagnosing with KEYS * or reading an entire large hash with HGETALL. UNLINK addresses freeing work; it does not remove the cost of serializing and sending a huge response.

Persistence, even for a cache

“It’s a cache, losing it is fine.” But a restart starts from an empty cache, and that is an avalanche. So persistence settings matter even on a cache server.

  • RDB forks a child process with fork() to write a snapshot. Parent and child share memory pages, and only pages the parent changes are copied (copy-on-write). On a write-heavy server, memory can approach double in the worst case during a snapshot, and the larger the dataset, the longer fork() itself takes.
  • AOF logs write commands. The Redis 6.2.6 configuration defaults to appendonly no. Once AOF is enabled, the default fsync policy is everysec; normally plan for roughly a second of data loss. Disk or fsync stalls mean that is not a strict upper bound.
  • Replication is asynchronous. A successful write reply does not mean replicas have received it or a disk fsync completed. Failover can lose acknowledged writes. Waiting for replica acknowledgements with WAIT still does not turn Redis into a consensus store or guarantee disk durability.

Set budgets before setting TTLs

A product description may tolerate delay; a payment amount may not. Decide the allowed staleness, request deadline, and load the database can accept without the cache for each kind of data. TTL follows those decisions.

TTL jitter must stay within the freshness limit. If an example allows at most 300 seconds, 300 + random.randint(0, 60) can reach 360 and break that limit. Use a bounded range such as random.randint(240, 300). These are illustrative budget assumptions, not measurements.

Include every input that changes the result in the cache key. The same user ID can yield different responses by tenant, language, or authorization scope. Change the namespace when serialization changes, for example user:v2:.... Returning another user’s response is not fixed by a short TTL.

Negative entries must distinguish absence from an empty string, an empty list, and a Redis miss. Converting a failed database query into “not found” caches an outage as a normal response. Check the path that removes a negative entry when data is created.

Distinguish single-flight, which combines loaders, from pipelining, which batches commands. A pipeline sends multiple Redis commands without waiting for a round trip after each one. It is not a transaction and does not resolve another client’s update between GET and SET. Bound the batch size so response memory and waiting do not grow without limit.

QuizCheckpoint: fewer round trips, but atomic?

Choose an answer to see the explanation.

0 of 1 right

  1. You pipeline GET and SET. Does that eliminate races with another request?

A simple load model is

QDB≈Qread(1−h)Q_{\mathrm{DB}} \approx Q_{\mathrm{read}}(1-h)

Assume 10,000 reads per second. A 99% hit rate yields about 100 database queries; 90% yields about 1,000. These are substitutions into the model, assuming one database query per miss. Retries, refreshes, local caching, and writes are excluded. A modest-looking hit-rate drop can multiply database load.

An average hit rate alone is insufficient. Many cheap hits and a few expensive misses can still overload the database. Compare misses and backend cost by endpoint before changing memory or TTL. Look for cache pollution from results that are stored but never read again.

What to inspect during an outage

Inspection commands for Redis 6.2.6
redis-cli INFO stats
redis-cli INFO memory
redis-cli INFO replication
redis-cli INFO persistence
redis-cli CONFIG GET maxmemory
redis-cli CONFIG GET maxmemory-policy
redis-cli SLOWLOG GET 20
redis-cli LATENCY LATEST

LATENCY LATEST shows events collected with latency-monitor-threshold enabled. An empty result is not proof of no latency. SLOWLOG measures command execution, not the full network round trip or client wait. Production ACLs may disallow CONFIG GET.

ObservationCheck alongside itDecision
Rising keyspace_missesRequest volume, expired_keys, evicted_keys, endpoint missesSeparate expiry, eviction, and nonexistent lookups
Rising evicted_keysmaxmemory, memory usage, hit rateEviction can be normal for a cache; investigate when hits collapse
SET returns OOMLimit, policy, TTL candidates, actual errorInspect available space and policy instead of blindly retrying
RSS stays highused_memory, mem_fragmentation_ratio, lazyfree_pending_objectsSeparate live data, allocator retention, and pending frees
Redis is fast but the app is slowPool waits, round trips, payload size, DB missesCommand time is only part of the request
Miss surge after failoverReplication lag, cold cache, DB concurrencyRestore the cache while protecting the database

INFO stats hits and misses are cumulative. Compute Δhits / (Δhits + Δmisses) over the same interval; if the denominator is zero, do not calculate a hit rate. This is a Redis lookup statistic, not local-cache hits or the application’s overall request hit rate.

maxmemory is not an absolute limit on process RSS or container memory. Redis 6.2.6’s freeMemoryGetNotCountedMemory excludes AOF and replica output buffers from eviction accounting. Allocator slack and RDB copy-on-write need room too. Filling data up to the container limit can let the OS kill Redis before eviction has a chance to help.

Sending every timed-out request to the database can turn a cache outage into a database outage. Set connection and read timeouts, bounded retries, and a backend concurrency limit together. A circuit breaker stops repeatedly sending work down a failing path; an allowed stale response is another option. Avoid unlimited fallback for every endpoint.

After recovery, warm popular data first at a limited rate. TTL jitter spreads expiry times, single-flight combines loaders for the same key, and concurrency limits protect the database even from misses for different keys. Each limits a different source of work.

Interview glossary

TermIn one line
Cache-asideApp checks cache first, fills from DB on a miss. On write, update DB then delete the key
Write-throughUpdate cache and DB together, synchronously
Write-behindWrite to cache first, flush to DB asynchronously in batches
TTLLifetime from cache insertion, not a staleness bound from the database commit
Lazy expiryCheck and delete an expired key when it is accessed
Active expiryDefault target of 20 keys; repeat based on expired share and time budget
Approximate LRURefill a candidate pool with a default sample count of 5; evict an idle candidate
LFU counter8-bit logarithmic counter, starts at 5, decays per minute
noevictionDefault policy; reject denyoom commands such as SET if space is insufficient
StampedeRequests rush the DB the moment a popular key expires
PenetrationLookups for nonexistent keys pass through the cache to the DB
AvalancheMany keys expire at once, or the cache server goes empty
Hot keyOne key gets so many requests that one shard overheats
Big keyA key large enough to block the main thread when deleted or sent
UNLINKDetach now, choose asynchronous freeing by effort and reference count
Copy-on-writeDuring an RDB fork, only pages the parent changes are copied

Quiz

Quiz

Choose an answer to see the explanation.

0 of 13 right

  1. You set maxmemory on Redis 6.2 and keep the default policy. It cannot relieve the over-limit condition. What happens?

  2. Active expiry removes keys that nobody reads. What does the source establish?

  3. Is maxmemory-samples=1 the same algorithm as allkeys-random?

  4. Why does the LFU counter start at 5 instead of 0?

  5. In a stampede lock, why check the cache again after taking the lock?

  6. A nightly job sets a 24-hour TTL on every product key at midnight. What worries you most?

  7. You need to delete a hash with 5 million fields. Which command blocks the main thread least?

  8. What happens when you GET an expired key on a replica?

  9. Should permission checks immediately after revocation use stale-while-revalidate?

  10. In an example with a maximum allowed TTL of 300 seconds, which expression spreads expiry safely?

  11. A product exists in the database but its Bloom filter update is delayed. Can trusting 'absent' cause trouble?

  12. You copy a hot key to hot:{item:42}:0 and hot:{item:42}:1. Does that spread Cluster load?

  13. Every request that times out on Redis falls back to the database. What protection is needed first?

Flash cards

Tap a card to flip it. Made for a quick pass the night before an interview.

Flash cards

Click a card to flip it.

0 of 15 flipped

Summary

  • With a cache, update the database, delete the cache key, and always set a TTL, even a short one.
  • Expiry is handled in lookup paths and active scans. Time budgets can leave expired keys around for a while.
  • Set the memory limit and eviction policy together. Under default noeviction, insufficient space makes SET fail.
  • LRU uses a candidate pool; LFU uses a probabilistic counter. Measured survivor shares and saturation counts are not guarantees.
  • Same-key misses, absent-key requests, mass expiry, and large-object freeing need different protections. TTL and one lock do not solve all of them.
  • Set freshness and wait budgets, and protect the database. After the exercises, draw the failure path of an actual service.

Notes

  1. Andrea Vattani, Flavio Chierichetti, Keegan Lowenstein, “Optimal Probabilistic Cache Stampede Prevention”, VLDB 2015. ↩

ko.mdx

단축키Keyboard

/ ⌘K
글 검색Search posts
t
밝은/어두운 테마Light or dark theme
l
한국어/영어, 읽던 자리 유지Korean or English, keeping your place
[ ]
이전/다음 절Previous or next section
?
이 목록This list

↑↓ 이동move↵ 열기openesc 닫기close