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

กลยุทธ์การทำให้แคชใช้ไม่ได้

เรียนรู้การรักษาข้อมูลในแคชให้ใหม่และสอดคล้องกันด้วย TTL การเขียนผ่าน การเขียนภายหลัง และการทำให้ใช้ไม่ได้โดยขับเคลื่อนด้วยเหตุการณ์ใน Redis

บทเรียน 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 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Invalidation Matters

Caching speeds up reads, but a cache that serves stale data can be worse than no cache at all. Cache invalidation is the discipline of removing or refreshing entries when the underlying source of truth changes.

It is famously hard: there are only two hard things in computer science, and one of them is cache invalidation.

TTL-Based Expiry

The simplest strategy is time-to-live. You accept that data may be stale for at most N seconds.

  • SET key value EX 60 expires after 60 seconds
  • EXPIRE key 60 sets a TTL on an existing key
  • TTL key shows remaining seconds
SET user:42:profile "...json..." EX 60
TTL user:42:profile
EXPIRE user:42:profile 120

Write-Through Caching

In write-through caching, every write goes to both the database and the cache in the same operation. Reads are always fresh, at the cost of slower writes.

The application is responsible for keeping the two in lockstep.

def save_user(user):
    db.update(user)
    redis.set('user:' + user.id, serialize(user))
    return user

Write-Behind Caching

Write-behind (write-back) writes to the cache first and flushes to the database asynchronously. Writes are fast, but you risk data loss if Redis goes down before the flush.

Use a queue or stream to buffer pending writes.

redis.set('order:' + id, data)
redis.lpush('pending_writes', id)

Explicit Deletion on Update

The cache-aside + delete pattern: on every update to the database, delete the cached key. The next read repopulates it.

This avoids serving stale data and is simpler than keeping the cache value in sync.

def update_product(p):
    db.update(p)
    redis.delete('product:' + p.id)

Why Delete Beats Update

Deleting the key (instead of overwriting it) avoids a race condition: two concurrent updates could otherwise write values in the wrong order. With deletion, the next read always pulls the latest from the source of truth.

Versioned Keys

Instead of invalidating, bump a version number embedded in the key. Old keys expire naturally via TTL while new reads use the new key.

INCR config:version
GET config:version
SET config:v7:settings "..."

Event-Driven Invalidation

Publish an invalidation event whenever data changes. Other application nodes subscribe and clear their local caches. This pairs Redis Pub/Sub with caching.

redis.publish('invalidate', 'user:42')
# subscribers:
redis.subscribe('invalidate')

Tag-Based Invalidation

Group related keys under a tag set. When a tag becomes invalid, delete all member keys in one sweep.

  • SADD tag:user:42 product:1 product:2
  • On change: SMEMBERS tag:user:42 then DEL each
SADD tag:user:42 cart:42 wishlist:42
SMEMBERS tag:user:42

Avoiding Stampedes

When a hot key expires, many requests may hit the database at once (a cache stampede). Mitigate with a short lock, probabilistic early expiry, or serving slightly stale data while one worker refreshes.

SET lock:user:42 1 NX EX 5

Choosing a Strategy

There is no single best approach:

  • TTL for tolerable staleness
  • Delete-on-write for correctness
  • Event-driven for multi-node consistency
  • Versioning for bulk config changes

Quick Check

Test your understanding of invalidation strategies.

Recap

You learned the major cache invalidation strategies: TTL expiry, write-through, write-behind, delete-on-write, versioned keys, event-driven, and tag-based invalidation, plus how to avoid cache stampedes. Pick the strategy that matches your tolerance for staleness and your consistency needs.

เริ่มต้นได้ฟรี

เรียนรู้ 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 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “กลยุทธ์การทำให้แคชใช้ไม่ได้”

เรียนรู้การรักษาข้อมูลในแคชให้ใหม่และสอดคล้องกันด้วย TTL การเขียนผ่าน การเขียนภายหลัง และการทำให้ใช้ไม่ได้โดยขับเคลื่อนด้วยเหตุการณ์ใน Redis คุณปฏิบัติ 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. การจัดการเซสชันด้วย Redis
  3. การจำกัดอัตราและรูปแบบการใช้งานที่ควรหลีกเลี่ยง
  4. กลยุทธ์การทำให้แคชใช้ไม่ได้
← กลับไปที่ Redis Caching & Messaging (Pub/Sub, Streams)