Redis Caching & Messaging (Pub/Sub, Streams) · Lekcja

Obsługa oczekujących komunikatów i awarii

Poznają Państwo strategie zarządzania oczekującymi komunikatami, odzyskiwania po awariach konsumentów i ponownego przetwarzania.

Lekcja 3 z 411 kroki

Obsługa oczekujących komunikatów i awarii to bezpłatna lekcja Redis Caching & Messaging (Pub/Sub, Streams) na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Redis Caching & Messaging (Pub/Sub, Streams), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Redis Caching & Messaging (Pub/Sub, Streams) zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Understanding Pending Messages

In Redis Streams, a Consumer Group allows multiple consumers to process messages from a stream.

When a consumer reads a message using XREADGROUP, the message isn't immediately removed. Instead, it's marked as pending for that consumer.

If a consumer crashes or fails to acknowledge a message (using XACK), that message remains pending. This ensures no data loss, but it also means the message isn't fully processed.

Inspecting Pending Messages

Redis provides the XPENDING command to see which messages are pending for a specific consumer group, or even for a specific consumer within that group.

  • It helps identify messages that haven't been acknowledged.
  • You can see which consumer is responsible for a message.
  • It reveals how long a message has been pending.

This is crucial for monitoring the health and progress of your stream processing.

Example: `XPENDING` in Action

Let's simulate a scenario where a consumer reads a message but fails to acknowledge it. Then, we'll use XPENDING to see it.

First, set up a stream and group, add a message, and read it without XACK:

XGROUP CREATE mystream mygroup 0 MKSTREAM
XADD mystream * sensor-id 1 value 10
XREADGROUP GROUP mygroup myconsumer COUNT 1 STREAMS mystream >
XPENDING mystream mygroup

Deciphering `XPENDING` Output

The XPENDING output provides vital information about each pending message:

  • Message ID: The unique ID of the pending message.
  • Consumer Name: The consumer currently assigned this message.
  • Idle Time: How long the message has been pending (in milliseconds).
  • Delivery Counter: How many times this message has been delivered. A high count might indicate a problematic message.

This data helps you decide if a message needs manual intervention or re-processing.

Manually Claiming Messages (`XCLAIM`)

If a consumer fails permanently, its pending messages will never be acknowledged. To prevent these messages from being stuck, another healthy consumer can claim them.

The XCLAIM command allows you to transfer ownership of one or more pending messages from a "stuck" consumer to a new consumer.

You specify the stream, group, new owner, minimum idle time, and the message IDs to claim.

Demonstrating `XCLAIM`

Continuing from our previous example, suppose myconsumer failed. We can claim its pending message for a newconsumer:

(Replace 1678881234567-0 with an actual Message ID from your XPENDING output.)

XCLAIM mystream mygroup newconsumer 3600000 1678881234567-0
XACK mystream mygroup 1678881234567-0
XPENDING mystream mygroup

Automated Recovery with `XAUTOCLAIM`

Manually identifying and claiming messages can be tedious, especially with many consumers or messages. Redis 6.2+ introduced XAUTOCLAIM for a more automated approach.

XAUTOCLAIM automatically identifies messages that have been pending for longer than a specified idle time and transfers them to a new consumer.

It's ideal for quickly reassigning messages from failed consumers without needing to list individual message IDs.

Using `XAUTOCLAIM`

Let's add another message, read it with myconsumer, let it go pending. Then we'll use XAUTOCLAIM to move it:

XADD mystream * sensor-id 2 value 20
XREADGROUP GROUP mygroup myconsumer COUNT 1 STREAMS mystream >
XPENDING mystream mygroup
XAUTOCLAIM mystream mygroup newconsumer 0 COUNT 1
XPENDING mystream mygroup

Guaranteed Delivery & Idempotency

Redis Streams, with consumer groups, guarantees at-least-once delivery. This means a message is guaranteed to be delivered, but it might be delivered more than once (e.g., if a consumer fails before acknowledging).

For reliable processing, your application logic should be idempotent. An idempotent operation produces the same result regardless of how many times it's executed with the same input.

  • Check if a record already exists before inserting.
  • Use unique transaction IDs to prevent duplicate processing.

Quick Check: Message Recovery

A consumer in your Redis Stream group processed a message but crashed before acknowledging it. What are the consequences and how can you resolve this?

Recap: Robust Stream Processing

We've learned how to handle messages that become pending due to consumer failures in Redis Streams:

  • XPENDING helps identify unacknowledged messages.
  • XCLAIM allows manual transfer of message ownership.
  • XAUTOCLAIM provides an automated way to reassign messages.
  • Understanding at-least-once delivery and designing idempotent consumers is key for reliable processing.

These tools ensure your stream processing remains resilient even when consumers fail.

Bezpłatny start

Ucz się Redis Caching & Messaging (Pub/Sub, Streams) dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Obsługa oczekujących komunikatów i awarii” jest bezpłatna?

Tak — pełny tekst „Obsługa oczekujących komunikatów i awarii” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Redis Caching & Messaging (Pub/Sub, Streams), przejdź na CoddyKit PRO. Kurs Redis Caching & Messaging (Pub/Sub, Streams) zawiera 4 lekcji w sumie.

Co nauczysz się w „Obsługa oczekujących komunikatów i awarii”?

Poznają Państwo strategie zarządzania oczekującymi komunikatami, odzyskiwania po awariach konsumentów i ponownego przetwarzania. Ćwiczysz Redis Caching & Messaging (Pub/Sub, Streams) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Redis Caching & Messaging (Pub/Sub, Streams)?

Nie wymagamy żadnego doświadczenia. Redis Caching & Messaging (Pub/Sub, Streams) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Obsługa oczekujących komunikatów i awarii”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Redis Caching & Messaging (Pub/Sub, Streams)?

Tak. Każda lekcja Redis Caching & Messaging (Pub/Sub, Streams) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Wprowadzenie do grup konsumentów
  2. Implementacja logiki grup konsumentów
  3. Obsługa oczekujących komunikatów i awarii
  4. Monitorowanie opóźnienia grup konsumentów
← Powrót do Redis Caching & Messaging (Pub/Sub, Streams)