Caching Strategies: Redis + CDN + Edge Computing · 강의

쇄도하는 요청 방어

인기 키가 만료될 때 캐시 쇄도가 발생하는 원리와 요청 병합, 잠금, 조기 재계산, 지터를 적용한 TTL 등 이를 방지하는 기법을 학습합니다.

레슨 4/413개 단계

쇄도하는 요청 방어은(는) CoddyKit의 무료 Caching Strategies: Redis + CDN + Edge Computing 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Caching Strategies: Redis + CDN + Edge Computing 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Caching Strategies: Redis + CDN + Edge Computing 강의에는 총 4개의 강의가 포함되어 있습니다.

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

The Thundering Herd Problem

When a hot cache key expires, every concurrent request misses at once and rushes the origin together. This cache stampede (or thundering herd) can overwhelm the database in an instant.

Why It Is Dangerous

A single popular item serving 10,000 requests/second normally hits the cache. The moment it expires, those 10,000 requests all hit the database simultaneously, often causing a spike that takes the origin down.

Cause: Synchronized Expiry

The root cause is many keys (or many requests on one key) expiring at the same moment. The fix strategies all aim to spread or serialize the resulting recomputation.

Fix 1: Request Coalescing

Let only the first request recompute the value; everyone else waits for that result. This is also called single-flight.

in_flight = {}

def get(key, compute):
    if key in in_flight:
        return 'waiting for in-flight result'
    in_flight[key] = True
    return compute()

print(get('hot', lambda: 'computed once'))

Fix 2: Mutex Lock

Use a distributed lock (e.g. a Redis key with NX) so only one process recomputes. Others briefly serve stale data or retry after a short wait.

lock = None

def acquire_lock(holder):
    global lock
    if lock is None:
        lock = holder
        return True
    return False

print(acquire_lock('worker-1'))
print(acquire_lock('worker-2'))

Fix 3: Jittered TTL

Add randomness to each entry's TTL so they do not all expire together. A base TTL plus random jitter spreads recomputation over time.

import random
base_ttl = 300
jitter = random.randint(0, 60)
print('TTL for this entry:', base_ttl + jitter, 'seconds')

Fix 4: Early Recomputation

Refresh a value before it expires. When an entry is close to its TTL, a background task (or a probabilistic check) recomputes it so it never actually goes cold for users.

Probabilistic Early Expiration

A clever trick: as a key nears expiry, give each request a small, growing probability of recomputing early. One lucky request refreshes the value while others still serve the cached copy.

import random
time_left = 5
beta = 1.0
should_refresh = random.random() < (1 / max(time_left, 1)) * beta
print('Refresh early?', should_refresh)

Fix 5: Serve Stale While Revalidating

Return the expired value immediately while a background job fetches fresh data. Users get a fast (slightly stale) response and the origin sees only one refresh request.

Combining Defenses

Real systems layer these: jittered TTLs to avoid synchronized expiry, plus coalescing or a lock to serialize the inevitable misses, plus stale-while-revalidate for the best user experience.

Watch for Cache Penetration Too

A related issue is penetration: requests for keys that never exist always miss and hit the origin. Cache negative results (or use a bloom filter) so missing keys are also absorbed.

Quick Check

Which technique prevents a cache stampede by ensuring only one request recomputes the value while the rest wait for that result?

Recap

You learned to defend against the thundering herd:

  • Stampedes happen when hot keys expire and many requests miss at once.
  • Coalescing and locks serialize recomputation.
  • Jittered TTLs and early recomputation spread the load.
  • Stale-while-revalidate keeps responses fast.

Combine these to keep your origin safe under load.

무료로 시작

AI 튜터와 함께 Caching Strategies: Redis + CDN + Edge Computing을(를) 배우세요 — 무료

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

코스
12
레슨
48

자주 묻는 질문

“쇄도하는 요청 방어” 강의는 무료인가요?

네 — “쇄도하는 요청 방어” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Caching Strategies: Redis + CDN + Edge Computing 강의 전체를 잠금 해제할 수 있습니다. Caching Strategies: Redis + CDN + Edge Computing 강의에는 총 4개의 강의가 포함되어 있습니다.

“쇄도하는 요청 방어”에서 뭘 배우나요?

인기 키가 만료될 때 캐시 쇄도가 발생하는 원리와 요청 병합, 잠금, 조기 재계산, 지터를 적용한 TTL 등 이를 방지하는 기법을 학습합니다. 브라우저에서 직접 실행하는 실습 코드로 Caching Strategies: Redis + CDN + Edge Computing을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Caching Strategies: Redis + CDN + Edge Computing을(를) 시작하는 데 경험이 필요한가요?

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

“쇄도하는 요청 방어” 강의는 얼마나 걸리나요?

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

이 Caching Strategies: Redis + CDN + Edge Computing 강의에서 코드를 작성하고 실행할 수 있나요?

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

이 강의의 모든 강의

  1. 일반적인 캐싱 패턴
  2. 캐시 무효화 전략
  3. 캐시 제거 정책
  4. 쇄도하는 요청 방어
← Caching Strategies: Redis + CDN + Edge Computing(으)로 돌아가기