0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · 课时

流消息持久化

探索 Streams 如何提供消息持久化和有序传递,以及它们与发布/订阅的区别

流消息持久化 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Redis Caching & Messaging (Pub/Sub, Streams) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What is Message Persistence?

In messaging systems, persistence means messages aren't lost, even if the recipient isn't ready to receive them immediately.

Think of it like leaving a note on a fridge instead of shouting it to someone who might not be home. The note stays until it's read.

This is crucial for reliability in many applications.

Streams Retain Your Data

Unlike some other Redis messaging features, Redis Streams are designed to be persistent. Every message (entry) you add to a stream is stored in Redis.

This storage acts like an append-only log. New entries are always added to the end, creating a historical record of events.

Unique Entry IDs for Order

Every entry in a Redis Stream gets a unique Entry ID. This ID is automatically generated by Redis and consists of two parts:

  • Timestamp (milliseconds): The time the entry was added.
  • Sequence Number: A counter for entries added within the same millisecond.

Example: 1678886400000-0

Adding Messages to a Stream

We use the XADD command to add new entries to a stream. The * tells Redis to auto-generate the Entry ID, ensuring uniqueness and proper ordering.

Each entry also holds field-value pairs, like a mini-hash.

XADD mystream * sensor-id 123 temperature 25.5

Reading from a Stream

To read entries from a stream, we use the XREAD command. You specify the stream name and the ID from which to start reading.

Crucially, reading an entry does not remove it from the stream. It stays there for other consumers or for future review.

XREAD COUNT 2 STREAMS mystream 0

Streams vs. Pub/Sub: Persistence

This is where Redis Streams significantly differ from Redis Pub/Sub (Publish/Subscribe).

  • Streams: Messages are persistent. They are stored and can be read multiple times, even by consumers who connect later.
  • Pub/Sub: Messages are ephemeral. They are delivered once to active subscribers and then disappear.

Pub/Sub: Fire and Forget

With Pub/Sub, if no client is subscribed to a channel when a message is published, that message is simply lost. There's no backlog or history.

It's like a live broadcast: if you're not tuned in, you miss it. This is great for real-time notifications where history isn't needed.

Streams Ensure Order

The automatically generated Entry IDs in Redis Streams guarantee strict chronological order. Messages are always stored and retrieved in the exact order they were added.

This is vital for applications where the sequence of events matters, such as logging, auditing, or event sourcing.

When to Use Persistent Streams

Choose Redis Streams when you need:

  • Reliable Event Logs: A complete, ordered history of events.
  • Message Replay: Ability for new consumers to process past events.
  • Auditing & Analytics: Historical data for analysis.
  • Decoupled Microservices: Services can process events at their own pace.

Stream Persistence Check

You're building an application that needs to ensure every event is processed, even if a consumer is temporarily offline. Which Redis feature is best suited for this requirement?

Recap: Streams' Core Strengths

In this lesson, we explored the crucial aspects of Redis Streams: message persistence and guaranteed ordered delivery.

  • Streams store all entries, forming an immutable log.
  • Each entry has a unique, time-based ID ensuring strict order.
  • This persistence and ordering are key differentiators from Redis Pub/Sub, making Streams ideal for reliable event processing and historical data.

常见问题解答

「流消息持久化」课时是免费的吗?

是的 — 「流消息持久化」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

「流消息持久化」这节课中我会学到什么?

探索 Streams 如何提供消息持久化和有序传递,以及它们与发布/订阅的区别 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「流消息持久化」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?

能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 什么是 Redis Streams?
  2. 流数据结构与命令
  3. 流消息持久化
  4. 限制与裁剪流
← 返回 Redis Caching & Messaging (Pub/Sub, Streams)