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

实现消费者组逻辑

学习创建组、使用 `XREADGROUP` 读取消息,并使用 `XACK` 确认处理完成

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

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

Welcome to Consumer Groups!

In the previous lesson, we learned what Redis Streams are. Now, let's dive into Consumer Groups! They are a powerful feature that lets multiple clients process messages from a stream cooperatively.

Imagine a team of workers processing tasks from a single to-do list. Each worker gets unique tasks, and if one fails, others can pick up pending tasks. That's what Consumer Groups enable!

Set Up Your Group

Before consumers can join, you need to create a Consumer Group for your stream. This is done using the XGROUP CREATE command.

It tells Redis: "Hey, for this stream, make a new group." You also specify an ID, which is typically 0 or $ to start reading from the beginning or end of the stream.

Create Your First Group!

Let's create a group named mygroup for a stream called mystream. The $ means "start reading from the latest entry." The MKSTREAM option is crucial: it creates the stream if it doesn't already exist!

Try it out:

XGROUP CREATE mystream mygroup $ MKSTREAM

Reading with `XREADGROUP`

Once a group is created, consumers can start reading messages using the XREADGROUP command. This command is designed specifically for consumer groups.

It ensures that each message is delivered to only one consumer within the group. If a consumer fails to acknowledge a message, it remains pending and can be claimed later.

Decoding `XREADGROUP`

The XREADGROUP command has several important parameters:

  • GROUP <groupname> <consumername>: Specifies which group and consumer are reading.
  • COUNT <N>: Optional. Limits the number of messages to read.
  • BLOCK <milliseconds>: Optional. Blocks the client if no messages are available.
  • STREAMS <streamname> <ID>: The stream to read from and the ID. Use > to get new messages that haven't been delivered to any other consumer in the group yet.

Let's Read Some Messages!

First, let's add a few messages to our mystream:

XADD mystream * event start task:1
XADD mystream * event process task:1

Consumer Reads New Messages

Now, let's have a consumer named consumer-1 from mygroup read from mystream. The > ID means "new, unread messages."

XREADGROUP GROUP mygroup consumer-1 STREAMS mystream >

Confirming Message Processing

After a consumer successfully processes a message, it's crucial to acknowledge it. This tells Redis that the message has been handled and can be removed from the consumer's Pending Entries List (PEL).

Acknowledgment prevents the same message from being re-delivered to another consumer if the current one crashes or goes offline before completing the task.

How to `XACK`

The command to acknowledge messages is XACK. Its syntax is straightforward:

XACK <streamname> <groupname> <ID> [ID ...]

You provide the stream name, the consumer group name, and one or more message IDs that have been successfully processed.

Put `XACK` into Practice

Let's assume you received message IDs 1678881234567-0 and 1678881234568-0 from the previous read. You would acknowledge them like this (replace with your actual IDs):

XACK mystream mygroup 1678881234567-0 1678881234568-0

Test Your Knowledge

You've learned how to create groups, read messages, and acknowledge them. Which of the following statements about Redis Consumer Groups is TRUE?

Recap: Consumer Group Logic

Great job! You've mastered the fundamentals of implementing Redis Consumer Group logic:

  • We used XGROUP CREATE to set up a new group for a stream.
  • You learned to read messages cooperatively as a consumer with XREADGROUP.
  • We saw how XACK is vital for acknowledging processed messages, managing the Pending Entries List (PEL).

Next, we'll explore how to handle pending messages and consumer failures more robustly!

常见问题解答

「实现消费者组逻辑」课时是免费的吗?

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

「实现消费者组逻辑」这节课中我会学到什么?

学习创建组、使用 `XREADGROUP` 读取消息,并使用 `XACK` 确认处理完成 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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