0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · Lección

Introducción a los grupos de consumidores

Comprenda cómo los grupos de consumidores permiten que varios consumidores procesen un stream de forma cooperativa y fiable.

Introducción a los grupos de consumidores es una lección gratuita de Redis Caching & Messaging (Pub/Sub, Streams) en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Redis Caching & Messaging (Pub/Sub, Streams), y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Redis Caching & Messaging (Pub/Sub, Streams) incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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 MKSTREAM

Populating 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.1

Consumers 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-0

Why 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!

Preguntas frecuentes

¿La lección «Introducción a los grupos de consumidores» es gratis?

Sí — el texto completo de «Introducción a los grupos de consumidores» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Redis Caching & Messaging (Pub/Sub, Streams), actualiza a CoddyKit PRO. El curso de Redis Caching & Messaging (Pub/Sub, Streams) incluye 4 lecciones en total.

¿Qué aprenderé en «Introducción a los grupos de consumidores»?

Comprenda cómo los grupos de consumidores permiten que varios consumidores procesen un stream de forma cooperativa y fiable. Practicas Redis Caching & Messaging (Pub/Sub, Streams) con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Redis Caching & Messaging (Pub/Sub, Streams)?

No se requiere experiencia previa. Redis Caching & Messaging (Pub/Sub, Streams) en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.

¿Cuánto tiempo toma la lección «Introducción a los grupos de consumidores»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Redis Caching & Messaging (Pub/Sub, Streams)?

Sí. Cada lección de Redis Caching & Messaging (Pub/Sub, Streams) incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Introducción a los grupos de consumidores
  2. Implementación de la lógica de grupos de consumidores
  3. Gestión de mensajes pendientes y fallos
  4. Supervisión del retraso de grupos de consumidores
← Volver a Redis Caching & Messaging (Pub/Sub, Streams)