0Pricing
System Design Basics for Backend Developers · บทเรียน

รูปแบบการทำให้แคชไม่ถูกต้อง

สำรวจกลยุทธ์ต่าง ๆ สำหรับทำให้ข้อมูลในแคชไม่ถูกต้อง เพื่อให้ข้อมูลใหม่และสอดคล้องกัน

รูปแบบการทำให้แคชไม่ถูกต้อง เป็นบทเรียน System Design Basics for Backend Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน System Design Basics for Backend Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน

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

What is Cache Invalidation?

Caching helps speed up applications by storing frequently accessed data closer to where it's needed. But what happens when the original data changes?

Cache invalidation is the process of removing or updating cached data when the original source data has changed. It ensures that users always see the most up-to-date information.

The Problem of Stale Data

Imagine you're viewing a product's price on an e-commerce site. If the price changes in the database but your browser (or an intermediate cache) still shows the old price, that's stale data.

Stale data can lead to incorrect information, bad user experiences, or even financial losses. Effective invalidation is key to preventing this.

Time-Based Invalidation (TTL)

The simplest invalidation strategy is Time-To-Live (TTL). Each cached item is given an expiry time. After this time, the item is considered stale and will be removed or refreshed upon the next request.

It's easy to implement but doesn't guarantee immediate freshness if the data changes *before* the TTL expires.

// Example: Setting a cache entry with a TTL
cache.put("user:123", userData, 300); // Cache for 300 seconds

// When requesting "user:123" after 300 seconds,
// the cache will return null or a stale indicator.

TTL: Simple but Limited

Pros of TTL:

  • Easy to implement and manage.
  • Automatically handles removal of old data.
  • Reduces load on the database periodically.

Cons of TTL:

  • Data can be stale for the duration of the TTL.
  • Choosing an optimal TTL can be tricky.
  • Not suitable for data requiring immediate consistency.

Explicit Invalidation: On-Demand

Explicit invalidation means directly removing a cached item when its corresponding source data changes. This ensures immediate freshness.

When an update occurs in the database, the application explicitly tells the cache to delete the affected item(s). The next read request will then fetch the fresh data from the database and re-populate the cache.

// When an item is updated in the database
function updateProduct(productId, newPrice) {
  database.update("products", productId, newPrice);
  cache.delete("product:" + productId); // Explicitly remove from cache
}

Cache-Aside & Explicit Invalidation

This pattern combines the Cache-Aside strategy (where the application manages caching) with explicit invalidation. It's very common.

How it works:

  • Read: Check cache first. If not found, fetch from DB, then store in cache.
  • Write: Update DB first, then explicitly invalidate (delete) the item from the cache.
// Read operation
function getProduct(productId) {
  let product = cache.get("product:" + productId);
  if (product === null) {
    product = database.fetch("products", productId);
    cache.put("product:" + productId, product);
  }
  return product;
}

// Write operation (as seen in previous scene)
// updateProduct(productId, newPrice) {...}

Event-Driven Invalidation (Pub/Sub)

In distributed systems, Event-Driven Invalidation uses a Publish/Subscribe (Pub/Sub) model. When data changes, the service responsible publishes an "update" event to an event bus.

Other services or cache instances that hold a copy of that data subscribe to these events and invalidate their local caches accordingly. This decouples services and ensures consistency across many components.

Versioning for Cache Freshness

Another approach is to use version numbers or timestamps. Each cached item and its corresponding database record can carry a version.

When fetching data, you can compare the cached item's version with the database's version. If the cached version is older, it's stale and needs to be refreshed. This is useful for optimistic concurrency control as well.

// Conceptual check for data freshness
function isCacheStale(cachedItem, dbItem) {
  return cachedItem.version < dbItem.version;
}

// Or using a timestamp
function isCacheStale(cachedItem, dbItem) {
  return cachedItem.lastModified < dbItem.lastModified;
}

Choosing the Right Invalidation

The best invalidation pattern depends on your application's needs:

  • Data Freshness: How critical is it for users to see the absolute latest data?
  • Update Frequency: How often does the data change?
  • System Complexity: How many services share the data?
  • Performance Impact: What's the cost of invalidation vs. the cost of stale data?

Often, a combination of patterns is used.

Invalidation Challenge

You are designing a system for a real-time stock trading platform. Stock prices update very frequently, and showing stale prices could lead to significant financial issues for users.

Which cache invalidation strategy would be most appropriate to ensure users always see the most up-to-date stock prices?

Cache Invalidation Recap

In this lesson, we explored crucial cache invalidation patterns:

  • Time-To-Live (TTL): Simple, time-based expiry.
  • Explicit Invalidation: Direct removal upon data change.
  • Event-Driven (Pub/Sub): For distributed systems to notify changes.
  • Versioning: Comparing data versions/timestamps for freshness.

Choosing the right strategy ensures data consistency and a reliable user experience in your systems.

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

บทเรียน “รูปแบบการทำให้แคชไม่ถูกต้อง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “รูปแบบการทำให้แคชไม่ถูกต้อง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส System Design Basics for Backend Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน

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

สำรวจกลยุทธ์ต่าง ๆ สำหรับทำให้ข้อมูลในแคชไม่ถูกต้อง เพื่อให้ข้อมูลใหม่และสอดคล้องกัน คุณปฏิบัติ System Design Basics for Backend Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน System Design Basics for Backend Developers หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน System Design Basics for Backend Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “รูปแบบการทำให้แคชไม่ถูกต้อง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน System Design Basics for Backend Developers นี้ได้ไหม

ได้ บทเรียน System Design Basics for Backend Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. รูปแบบการทำให้แคชไม่ถูกต้อง
  2. การผสานรวม CDN และการแคชที่ขอบเครือข่าย
  3. การแคชแบบกระจายด้วย Redis
  4. นโยบายการนำข้อมูลออกจากแคช
← กลับไปที่ System Design Basics for Backend Developers