0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · درس

تطبيق منطق مجموعات المستهلكين

تعلّموا إنشاء المجموعات وقراءة الرسائل باستخدام `XREADGROUP` والإقرار بمعالجة الرسائل باستخدام `XACK`.

تطبيق منطق مجموعات المستهلكين درس مجاني في Redis Caching & Messaging (Pub/Sub, Streams) على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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) وفتح باقي دورة Redis Caching & Messaging (Pub/Sub, Streams)، انتقل إلى CoddyKit PRO. تتضمن دورة Redis Caching & Messaging (Pub/Sub, Streams) 4 دروس في المجموع.

ماذا ستتعلم في «تطبيق منطق مجموعات المستهلكين»؟

تعلّموا إنشاء المجموعات وقراءة الرسائل باستخدام `XREADGROUP` والإقرار بمعالجة الرسائل باستخدام `XACK`. تتمرن على Redis Caching & Messaging (Pub/Sub, Streams) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Redis Caching & Messaging (Pub/Sub, Streams)؟

لا تُشترط خبرة سابقة. Redis Caching & Messaging (Pub/Sub, Streams) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «تطبيق منطق مجموعات المستهلكين»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Redis Caching & Messaging (Pub/Sub, Streams) هذا؟

نعم. كل درس في Redis Caching & Messaging (Pub/Sub, Streams) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. مقدمة إلى مجموعات المستهلكين
  2. تطبيق منطق مجموعات المستهلكين
  3. التعامل مع الرسائل المعلّقة والأعطال
  4. مراقبة تأخر مجموعات المستهلكين
← العودة إلى Redis Caching & Messaging (Pub/Sub, Streams)