Redis Caching & Messaging (Pub/Sub, Streams) · 강의

캐시 스탬피드와 폭주 요청 방지

인기 키가 만료될 때 데이터베이스를 압도하는 캐시 스탬피드가 무엇인지, 그리고 부하 상황에서도 백엔드를 안전하게 유지하는 잠금 및 갱신 기법을 배워 보세요.

레슨 4/413개 단계

캐시 스탬피드와 폭주 요청 방지은(는) CoddyKit의 무료 Redis Caching & Messaging (Pub/Sub, Streams) 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Redis Caching & Messaging (Pub/Sub, Streams) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What Is a Cache Stampede?

A cache stampede (or thundering herd) happens when a popular cached key expires and many requests miss the cache at the same moment. They all rush to the database to rebuild the value, overwhelming it.

Why It Is So Dangerous

Under high traffic, hundreds or thousands of concurrent misses can hit the database in milliseconds. The backend that normally serves zero load for that key suddenly takes the full firehose, causing latency spikes or outages.

The Naive Cache-Aside Flow

The basic pattern reads the cache, and on a miss queries the DB and stores the result. Every concurrent miss runs this DB query — that is the vulnerability.

Solution 1: A Recompute Lock

Let only the first missing request rebuild the value while others wait or serve stale. Redis SET NX grants a short-lived lock to exactly one client.

SET lock:product:42 "1" NX EX 10

How the Lock Flow Works

On a miss:

  • Try to acquire the lock with SET NX
  • If you got it, query the DB and repopulate the cache
  • If not, briefly wait and re-read the cache

Only one DB query runs per expiry.

Solution 2: Stale-While-Revalidate

Store the value with a logical expiry earlier than its physical TTL. When the logical time passes, serve the stale value immediately and refresh it in the background, so users never wait on a miss.

Solution 3: Early Probabilistic Expiry

Each request, with a small growing probability as expiry nears, voluntarily refreshes the key before it dies. This spreads recomputation across time so the herd never forms.

Adding Jitter to TTLs

If many keys are written together (e.g. on deploy) they expire together, causing a synchronized stampede. Add random jitter to each TTL so expiries spread out.

SET product:42 "..." EX 305
SET product:43 "..." EX 318

Releasing the Lock Safely

After rebuilding, delete the lock. Give it a short TTL too, so a crashed worker does not hold it forever and block refreshes.

DEL lock:product:42

Choosing a Strategy

Guidelines:

  • Lock: simplest, briefly delays some requests
  • Stale-while-revalidate: best UX, needs background refresh
  • Probabilistic + jitter: smooths load, no waiting

Combine them for very hot keys.

Negative Caching

A related danger is the cache penetration miss: many requests for a key that does not exist in the DB always miss the cache and hammer the backend. Cache the not-found result briefly too, so repeated lookups are absorbed.

SET product:9999 "__NULL__" EX 30

Quick Check

Test your stampede-prevention knowledge.

Recap

You learned to stop cache stampedes:

  • A stampede floods the DB when a hot key expires
  • A SET NX recompute lock limits rebuilds to one client
  • Stale-while-revalidate serves old data while refreshing
  • Probabilistic early expiry spreads recomputation out
  • TTL jitter prevents synchronized mass expiry
무료로 시작

AI 튜터와 함께 Redis Caching & Messaging (Pub/Sub, Streams)을(를) 배우세요 — 무료

브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.

코스
12
레슨
48

자주 묻는 질문

“캐시 스탬피드와 폭주 요청 방지” 강의는 무료인가요?

네 — “캐시 스탬피드와 폭주 요청 방지” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Redis Caching & Messaging (Pub/Sub, Streams) 강의 전체를 잠금 해제할 수 있습니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.

“캐시 스탬피드와 폭주 요청 방지”에서 뭘 배우나요?

인기 키가 만료될 때 데이터베이스를 압도하는 캐시 스탬피드가 무엇인지, 그리고 부하 상황에서도 백엔드를 안전하게 유지하는 잠금 및 갱신 기법을 배워 보세요. 브라우저에서 직접 실행하는 실습 코드로 Redis Caching & Messaging (Pub/Sub, Streams)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Redis Caching & Messaging (Pub/Sub, Streams)을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Redis Caching & Messaging (Pub/Sub, Streams)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.

“캐시 스탬피드와 폭주 요청 방지” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Redis Caching & Messaging (Pub/Sub, Streams) 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 왜 캐시를 사용할까요? 캐싱 소개
  2. 기본 캐시 패턴 구현
  3. 캐시 제거 및 만료
  4. 캐시 스탬피드와 폭주 요청 방지
← Redis Caching & Messaging (Pub/Sub, Streams)(으)로 돌아가기