コンシューマーグループ入門
コンシューマーグループによって、複数のコンシューマーがストリームを協調して信頼性高く処理できる仕組みを理解します。
「コンシューマーグループ入門」はCoddyKit上の無料Redis Caching & Messaging (Pub/Sub, Streams)レッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 MKSTREAMPopulating 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.1Consumers 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-0Why 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!
よくある質問
「コンシューマーグループ入門」レッスンは無料ですか?
はい。「コンシューマーグループ入門」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Redis Caching & Messaging (Pub/Sub, Streams)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Redis Caching & Messaging (Pub/Sub, Streams)コースには全4レッスンが含まれています。
「コンシューマーグループ入門」で何を学びますか?
コンシューマーグループによって、複数のコンシューマーがストリームを協調して信頼性高く処理できる仕組みを理解します。 ブラウザで直接実行するハンズオンコードでRedis Caching & Messaging (Pub/Sub, Streams)を演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。