0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · 강의

소비자 그룹 로직 구현

그룹을 만들고 `XREADGROUP`을 사용해 메시지를 읽으며 `XACK`로 처리 완료를 알리는 방법을 배웁니다.

소비자 그룹 로직 구현은(는) CoddyKit의 무료 Redis Caching & Messaging (Pub/Sub, Streams) 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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!

자주 묻는 질문

“소비자 그룹 로직 구현” 강의는 무료인가요?

네 — “소비자 그룹 로직 구현” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Redis Caching & Messaging (Pub/Sub, Streams) 강의 전체를 잠금 해제할 수 있습니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.

“소비자 그룹 로직 구현”에서 뭘 배우나요?

그룹을 만들고 `XREADGROUP`을 사용해 메시지를 읽으며 `XACK`로 처리 완료를 알리는 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Redis Caching & Messaging (Pub/Sub, Streams)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Redis Caching & Messaging (Pub/Sub, Streams)을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Redis Caching & Messaging (Pub/Sub, Streams)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“소비자 그룹 로직 구현” 강의는 얼마나 걸리나요?

대부분의 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)(으)로 돌아가기