0Pricing
Caching Strategies: Redis + CDN + Edge Computing · บทเรียน

การเผยแพร่และสมัครรับข้อมูลในรีดิสเพื่อทำให้แคชเป็นโมฆะ

สำรวจการใช้การเผยแพร่และสมัครรับข้อมูลของรีดิสเพื่อทำให้แคชเป็นโมฆะแบบเรียลไทม์ระหว่างอินสแตนซ์ของแอปพลิเคชันหลายรายการ

การเผยแพร่และสมัครรับข้อมูลในรีดิสเพื่อทำให้แคชเป็นโมฆะ เป็นบทเรียน Caching Strategies: Redis + CDN + Edge Computing ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Caching Strategies: Redis + CDN + Edge Computing และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Caching Strategies: Redis + CDN + Edge Computing มีบทเรียนทั้งหมด 4 บทเรียน

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

Real-time Cache Updates

Imagine you have multiple copies of your application running, all using a local cache. When data changes in the database, how do you tell all these application instances to update their caches immediately?

Redis Publish/Subscribe (Pub/Sub) is a powerful messaging pattern that allows you to send real-time notifications to multiple clients, making it perfect for distributed cache invalidation.

Beyond Time-To-Live (TTL)

While Time-To-Live (TTL) is great for automatically expiring old data, it doesn't guarantee instant freshness. If critical data changes, you don't want to wait for the TTL to expire.

Pub/Sub provides a way to force immediate invalidation. When data is updated in your primary data store (like a database), one application instance can broadcast a message, and all other instances listening will receive it and invalidate their specific cache entries.

The Publisher Role

In the Pub/Sub model, a Publisher is an entity (like one of your application instances) that sends messages to a specific channel.

  • When a significant data change occurs (e.g., a product's price is updated in the database), the application instance that made the change acts as a publisher.
  • It doesn't care who receives the message, only that it's sent to the designated channel.

The Subscriber Role

A Subscriber is an entity (another application instance) that listens for messages on one or more specific channels.

  • All other application instances would be subscribers to the 'cache-invalidation' channel.
  • When a message arrives on a channel they're subscribed to, they receive it and can then react, for example, by removing the corresponding item from their local cache.

Redis Pub/Sub Commands

Redis provides two main commands for Pub/Sub:

  • PUBLISH channel message: Sends message to the specified channel. All subscribers to that channel will receive it.
  • SUBSCRIBE channel [channel ...]: This client subscribes to one or more channels. Once subscribed, it will continuously listen for messages.

Remember, Pub/Sub messages are fire-and-forget; Redis doesn't store them.

Publishing a Cache Invalidation

Here's how an application instance can publish an invalidation message using Java and the Jedis client. This example sends a message to invalidate a specific product.

import redis.clients.jedis.Jedis;

public class CachePublisher {
  public static void main(String[] args) {
    // Connect to Redis (default localhost:6379)
    Jedis jedis = new Jedis("localhost", 6379);

    String channel = "product-updates";
    String message = "invalidate:product:456"; // Key to invalidate

    // Publish the message
    jedis.publish(channel, message);
    System.out.println("Published: '" + message + "' to channel '" + channel + "'");

    // Close the connection
    jedis.close();
  }
}

Setting Up a Cache Subscriber

Subscribers use a special listener class to handle incoming messages. The onMessage method is where your invalidation logic goes.

Note: The jedis.subscribe() call is blocking and keeps the connection open to listen. In a real app, this runs in a dedicated thread.

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

public class CacheSubscriberSetup {
  public static void main(String[] args) {
    System.out.println("Preparing Redis Pub/Sub subscriber...");

    // Define your listener logic
    JedisPubSub listener = new JedisPubSub() {
      @Override
      public void onMessage(String channel, String message) {
        System.out.println("Received: '" + message + "' on channel '" + channel + "'");
        // Here, you would implement your cache invalidation logic
        // e.g., myLocalCache.remove(message.split(":")[1]);
      }

      @Override
      public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println("Successfully subscribed to: " + channel);
      }
      // Other methods like onUnsubscribe, onPMessage, etc., can be overridden
    };

    // In a real application, you'd run:
    // try (Jedis jedis = new Jedis("localhost", 6379)) {
    //   jedis.subscribe(listener, "product-updates"); // This blocks!
    // }
    System.out.println("Subscriber listener defined. To truly listen, run a blocking subscribe call.");
    System.out.println("This runnable example exits to demonstrate setup.");
  }
}

End-to-End Invalidation Flow

Let's see the full picture:

  1. App A updates a product in the database.
  2. App A publishes an invalidation message ('invalidate:product:456') to the 'product-updates' channel in Redis.
  3. Redis receives the message and broadcasts it to all clients subscribed to 'product-updates'.
  4. App B, App C (and App A itself if subscribed) receive the message.
  5. Each app's subscriber logic removes 'product:456' from its local cache, ensuring fresh data on next request.

Designing Invalidation Messages

What should you include in your invalidation message?

  • Specific Key: 'invalidate:user:123' is ideal for precise invalidation.
  • Category: 'invalidate:all:products' for broader invalidation (use with caution).
  • Timestamp/Version: Can help subscribers decide if their cached data is older than the update.

Keep messages concise. Subscribers should have enough info to know what to invalidate.

Pros and Cons of Pub/Sub

Benefits:

  • Real-time: Immediate cache updates across instances.
  • Decoupled: Publishers don't need to know about subscribers.
  • Scalable: Redis handles message distribution efficiently.

Considerations:

  • No Persistence: If a subscriber is offline, it misses messages.
  • At-Most-Once: Redis Pub/Sub doesn't guarantee delivery. For critical systems, consider other messaging patterns or a combination.

Pub/Sub Invalidation Quiz

You've learned how Redis Pub/Sub helps with real-time cache invalidation. Let's test your understanding!

Pub/Sub for Fresh Data

Great job! You've explored how Redis Publish/Subscribe is a vital tool for maintaining data freshness in distributed caching environments.

  • Pub/Sub allows real-time broadcasting of invalidation messages.
  • Publishers send messages, and subscribers listen to channels.
  • This pattern enables immediate cache updates across all application instances when data changes, improving consistency and user experience.
  • While powerful, remember its 'fire-and-forget' nature and consider persistence needs for critical systems.

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

บทเรียน “การเผยแพร่และสมัครรับข้อมูลในรีดิสเพื่อทำให้แคชเป็นโมฆะ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเผยแพร่และสมัครรับข้อมูลในรีดิสเพื่อทำให้แคชเป็นโมฆะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Caching Strategies: Redis + CDN + Edge Computing ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Caching Strategies: Redis + CDN + Edge Computing มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเผยแพร่และสมัครรับข้อมูลในรีดิสเพื่อทำให้แคชเป็นโมฆะ”

สำรวจการใช้การเผยแพร่และสมัครรับข้อมูลของรีดิสเพื่อทำให้แคชเป็นโมฆะแบบเรียลไทม์ระหว่างอินสแตนซ์ของแอปพลิเคชันหลายรายการ คุณปฏิบัติ Caching Strategies: Redis + CDN + Edge Computing ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Caching Strategies: Redis + CDN + Edge Computing หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Caching Strategies: Redis + CDN + Edge Computing บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การเผยแพร่และสมัครรับข้อมูลในรีดิสเพื่อทำให้แคชเป็นโมฆะ” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Caching Strategies: Redis + CDN + Edge Computing นี้ได้ไหม

ได้ บทเรียน Caching Strategies: Redis + CDN + Edge Computing ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การคงอยู่ของข้อมูลและ HA ในรีดิส
  2. การแคชแบบกระจายด้วยรีดิส
  3. การเผยแพร่และสมัครรับข้อมูลในรีดิสเพื่อทำให้แคชเป็นโมฆะ
  4. Redis Cluster และการแบ่งชาร์ด
← กลับไปที่ Caching Strategies: Redis + CDN + Edge Computing