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

消费者组简介

了解消费者组如何让多个消费者协同且可靠地处理流

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

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

What are Consumer Groups?

Redis Streams are powerful for logging and event sourcing. But what if multiple applications or instances need to process the same stream data, without duplicating work?

This is where Consumer Groups come in. They are a core feature of Redis Streams designed for reliable, shared message processing.

The Challenge: Multiple Readers

Imagine two services, Service A and Service B, both needing to process every new order from an orders_stream.

If both services simply use XREAD, they would both receive and process all messages, leading to duplicated effort or inconsistent states.

  • How do they know which messages are already processed?
  • How do they share the workload?

Solving Shared Stream Processing

Consumer Groups provide a way for multiple clients (consumers) to jointly read from the same Redis Stream.

They ensure that:

  • Each message is delivered to only one consumer within the group.
  • The state (which messages are pending, which are acknowledged) is managed by Redis.
  • Consumers can recover and resume processing from where they left off.

Key Concepts: Group, Consumer, PEL

Let's define the core parts of a Consumer Group:

  • Stream: The Redis Stream holding the messages.
  • Consumer Group: A logical group of consumers processing a stream.
  • Consumer: An individual client instance within a group.
  • Pending Entry List (PEL): A list maintained by Redis for each consumer, tracking messages that have been delivered but not yet acknowledged.

Setting Up Your First Group

Before consumers can join, a Consumer Group must be created on a specific stream. We use the XGROUP CREATE command.

The MKSTREAM option automatically creates the stream if it doesn't exist.

XGROUP CREATE my_stream my_group 0 MKSTREAM

Populating the Stream

Let's add a few messages to our my_stream so we have something to read. Remember, XADD is how you add entries to a stream.

XADD my_stream * sensor_id 1 temp 25.5
XADD my_stream * sensor_id 2 temp 24.9
XADD my_stream * sensor_id 1 temp 26.1

Consumers Joining the Group

Consumers read from a group using the XREADGROUP command. You specify the group name, the consumer name, and the stream ID to start reading from.

The special ID > means "read only new, unread messages that have not been delivered to any other consumer in this group yet."

XREADGROUP GROUP my_group consumer1 COUNT 1 STREAMS my_stream >

Confirming Message Processing (XACK)

After a consumer processes a message, it must acknowledge it using XACK. This tells Redis the message is handled and can be removed from the consumer's Pending Entry List (PEL).

This is crucial for reliability and prevents messages from being re-delivered if a consumer crashes.

XACK my_stream my_group 1678881234567-0

Why XACK Matters

Acknowledging messages is vital for two main reasons:

  • Reliability: If a consumer fails before acknowledging, the message remains in its PEL and can be claimed by another consumer later.
  • Resource Management: Unacknowledged messages stay in the PEL, consuming memory. Acknowledging keeps the PEL clean and efficient.

Consumer Group Basics

Which Redis command is used to create a new Consumer Group for a stream?

Recap: Consumer Groups

In this lesson, you learned about Redis Consumer Groups and how they enable multiple consumers to reliably process messages from a single stream, ensuring each message is handled only once.

You now understand the roles of groups, consumers, and the Pending Entry List (PEL), and how to use XGROUP CREATE, XREADGROUP, and XACK.

Next, we'll dive deeper into implementing consumer group logic and handling pending messages!

常见问题解答

「消费者组简介」课时是免费的吗?

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

「消费者组简介」这节课中我会学到什么?

了解消费者组如何让多个消费者协同且可靠地处理流 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「消费者组简介」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 消费者组简介
  2. 实现消费者组逻辑
  3. 处理待处理消息与故障
  4. 监控消费者组延迟
← 返回 Redis Caching & Messaging (Pub/Sub, Streams)