ช่องทางเทียบกับการแจ้งเตือนคีย์สเปซ
ทำความเข้าใจความแตกต่างระหว่างช่องทาง Pub/Sub ที่ระบุอย่างชัดเจนกับการแจ้งเตือนคีย์สเปซของ Redis และเรียนรู้ว่าควรเลือกใช้แต่ละแบบเมื่อใด
ช่องทางเทียบกับการแจ้งเตือนคีย์สเปซ เป็นบทเรียน 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 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Two Ways to Get Notified
So far you have published to explicit channels with PUBLISH. Redis also offers keyspace notifications, which automatically emit events when keys change. Both deliver messages to subscribers, but the source differs.
Recap: Explicit Channels
With explicit channels, your application decides what to publish and when. The channel name is arbitrary text.
PUBLISH news.sports "Match ended 2-1"
SUBSCRIBE news.sportsWhat Are Keyspace Notifications?
Keyspace notifications let Redis itself publish an event whenever a key is modified, expired, or evicted. You subscribe to special channels to observe data changes without your app having to publish anything.
Enabling Notifications
They are off by default. Enable them with the notify-keyspace-events config flag, where letters select event classes.
Kkeyspace eventsEkeyevent eventsAall command/event typesxexpired,ggeneric,$string
CONFIG SET notify-keyspace-events KEAKeyspace vs Keyevent
There are two channel families:
- Keyspace:
__keyspace@0__:mykeytells you which event happened to a key - Keyevent:
__keyevent@0__:deltells you which keys had a given event
The @0 is the database number.
PSUBSCRIBE __keyspace@0__:*
PSUBSCRIBE __keyevent@0__:expiredSubscribing to Expirations
A common use case is reacting when a key expires, for example to clean up a session. Enable Ex and subscribe to the expired keyevent channel.
CONFIG SET notify-keyspace-events Ex
SUBSCRIBE __keyevent@0__:expiredA Practical Flow
Set a session key with a TTL. When it expires, Redis publishes to the expired channel, and your worker reacts.
SET session:abc "data" EX 30
# 30s later, subscriber receives:
# __keyevent@0__:expired -> session:abcDelivery Caveats
Keyspace notifications use Pub/Sub, so they are fire-and-forget. If no client is subscribed when the event fires, the message is lost. They are also not guaranteed for keys that expire while idle until a read or the background expiry cycle touches them.
When to Use Each
Use explicit channels when your app controls the event semantics (chat, notifications, custom events). Use keyspace notifications when you want to react to data mutations you do not directly control, like cache invalidation or expiry cleanup.
Performance Note
Enabling A (all events) on a busy server generates a high volume of messages and adds overhead. Subscribe only to the specific event classes you need.
CONFIG SET notify-keyspace-events EgxSummary So Far
Explicit channels are app-driven; keyspace notifications are Redis-driven. Both ride on the same lightweight Pub/Sub transport, so neither persists messages.
Quick Check
Pick the correct statement about keyspace notifications.
Recap
You compared explicit Pub/Sub channels with keyspace notifications, learned how to enable notifications via notify-keyspace-events, the keyspace vs keyevent channel families, and the fire-and-forget delivery caveats. Reach for keyspace notifications to react to data changes without publishing them yourself.
คำถามที่พบบ่อย
บทเรียน “ช่องทางเทียบกับการแจ้งเตือนคีย์สเปซ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ช่องทางเทียบกับการแจ้งเตือนคีย์สเปซ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Redis Caching & Messaging (Pub/Sub, Streams) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ช่องทางเทียบกับการแจ้งเตือนคีย์สเปซ”
ทำความเข้าใจความแตกต่างระหว่างช่องทาง Pub/Sub ที่ระบุอย่างชัดเจนกับการแจ้งเตือนคีย์สเปซของ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่การเผยแพร่และรับสมาชิก
- กลไกการเผยแพร่และรับสมาชิกของ Redis
- การกระจายข้อความอย่างง่าย
- ช่องทางเทียบกับการแจ้งเตือนคีย์สเปซ