Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Streams in Redis | Advanced Data Structures
Quizzes & Challenges
Quizzes
Challenges
/
Redis Intermediate Concepts

bookStreams in Redis

Redis Streams provide a powerful way to manage and process real-time data flows within Redis. Streams allow you to store, consume, and analyze sequences of messages, making them an ideal solution for event sourcing, messaging, and log aggregation scenarios. In this chapter, you will explore how to work with streams in Redis and unlock their full potential for handling time-ordered data.

Using Redis Streams for Order Processing

Suppose you are building an order processing system where new orders are added to a queue, and multiple workers process these orders. Redis streams provide an efficient way to handle this workflow.

  • You add new orders to the stream using the XADD command;
  • You read and process new orders with the XREAD command;
  • You manage multiple workers safely using the XGROUP command.

Step 1: Add Orders to the Stream

When a customer places an order, you add it to the stream:

XADD orders_stream * customer_id 12345 item "laptop" quantity 1

This command adds a new entry to the orders_stream. The * tells Redis to generate a unique ID for the order. The fields after the stream name describe the order details.

Step 2: Create a Consumer Group

To let multiple workers process orders without duplicating work, create a consumer group:

XGROUP CREATE orders_stream order_workers $ MKSTREAM

This command creates a consumer group called order_workers for the orders_stream. The $ means the group will start reading only new messages added after the group is created. MKSTREAM ensures the stream is created if it does not exist yet.

Step 3: Workers Read and Process Orders

Each worker reads new orders from the stream using the consumer group:

XREADGROUP GROUP order_workers worker-1 COUNT 1 STREAMS orders_stream >

This command tells Redis that the consumer named worker-1 in the order_workers group wants to read one new order from orders_stream. The > symbol means the worker only receives messages that have not yet been delivered to any other worker in the group.

By combining XADD, XGROUP, and XREADGROUP, you create a reliable, scalable system for processing orders where each order is handled only once by a single worker.

question mark

Which Redis command is used to add a new entry to a stream data structure?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 9.09

bookStreams in Redis

Deslize para mostrar o menu

Redis Streams provide a powerful way to manage and process real-time data flows within Redis. Streams allow you to store, consume, and analyze sequences of messages, making them an ideal solution for event sourcing, messaging, and log aggregation scenarios. In this chapter, you will explore how to work with streams in Redis and unlock their full potential for handling time-ordered data.

Using Redis Streams for Order Processing

Suppose you are building an order processing system where new orders are added to a queue, and multiple workers process these orders. Redis streams provide an efficient way to handle this workflow.

  • You add new orders to the stream using the XADD command;
  • You read and process new orders with the XREAD command;
  • You manage multiple workers safely using the XGROUP command.

Step 1: Add Orders to the Stream

When a customer places an order, you add it to the stream:

XADD orders_stream * customer_id 12345 item "laptop" quantity 1

This command adds a new entry to the orders_stream. The * tells Redis to generate a unique ID for the order. The fields after the stream name describe the order details.

Step 2: Create a Consumer Group

To let multiple workers process orders without duplicating work, create a consumer group:

XGROUP CREATE orders_stream order_workers $ MKSTREAM

This command creates a consumer group called order_workers for the orders_stream. The $ means the group will start reading only new messages added after the group is created. MKSTREAM ensures the stream is created if it does not exist yet.

Step 3: Workers Read and Process Orders

Each worker reads new orders from the stream using the consumer group:

XREADGROUP GROUP order_workers worker-1 COUNT 1 STREAMS orders_stream >

This command tells Redis that the consumer named worker-1 in the order_workers group wants to read one new order from orders_stream. The > symbol means the worker only receives messages that have not yet been delivered to any other worker in the group.

By combining XADD, XGROUP, and XREADGROUP, you create a reliable, scalable system for processing orders where each order is handled only once by a single worker.

question mark

Which Redis command is used to add a new entry to a stream data structure?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3
some-alt