Redis Caching & Messaging (Pub/Sub, Streams) · บทเรียน

การป้องกันแคชถล่มและฝูงคำขอถล่ม

เรียนรู้ว่าแคชถล่มคืออะไร เหตุใดจึงทำให้ฐานข้อมูลรับภาระหนักเมื่อคีย์ยอดนิยมหมดอายุ และเทคนิคการล็อกกับการรีเฟรชใดช่วยให้แบ็กเอนด์ปลอดภัยเมื่อมีโหลดสูง

บทเรียน 4 จาก 413 ขั้นตอน

การป้องกันแคชถล่มและฝูงคำขอถล่ม เป็นบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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
เริ่มต้นได้ฟรี

เรียนรู้ Redis Caching & Messaging (Pub/Sub, Streams) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การป้องกันแคชถล่มและฝูงคำขอถล่ม” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การป้องกันแคชถล่มและฝูงคำขอถล่ม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Redis Caching & Messaging (Pub/Sub, Streams) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การป้องกันแคชถล่มและฝูงคำขอถล่ม”

เรียนรู้ว่าแคชถล่มคืออะไร เหตุใดจึงทำให้ฐานข้อมูลรับภาระหนักเมื่อคีย์ยอดนิยมหมดอายุ และเทคนิคการล็อกกับการรีเฟรชใดช่วยให้แบ็กเอนด์ปลอดภัยเมื่อมีโหลดสูง คุณปฏิบัติ Redis Caching & Messaging (Pub/Sub, Streams) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Redis Caching & Messaging (Pub/Sub, Streams) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Redis Caching & Messaging (Pub/Sub, Streams) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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)