0
0
Redisquery~5 mins

XREAD for reading entries in Redis

Choose your learning style9 modes available
Introduction
XREAD helps you read new messages or data from a Redis stream, like checking new chat messages or logs.
When you want to get new chat messages as they arrive.
When you need to process new events from a stream of data.
When you want to read logs or updates that are continuously added.
When you want to build a simple message queue system.
When you want to read data from multiple streams at once.
Syntax
Redis
XREAD [COUNT count] [BLOCK milliseconds] STREAMS key [key ...] ID [ID ...]
COUNT limits how many entries you get at once.
BLOCK waits for new entries if none are available immediately.
Examples
Reads up to 2 entries from 'mystream' starting from the beginning (ID 0).
Redis
XREAD COUNT 2 STREAMS mystream 0
Waits up to 5 seconds for new entries in 'mystream' starting from the latest entry ($ means last).
Redis
XREAD BLOCK 5000 STREAMS mystream $
Reads entries from two streams 'mystream1' and 'mystream2' starting from the beginning.
Redis
XREAD STREAMS mystream1 mystream2 0 0
Sample Program
First, we add two entries to 'mystream' with names Alice and Bob. Then, we read up to 2 entries from the stream starting at the beginning.
Redis
127.0.0.1:6379> XADD mystream * name Alice
"1609459200000-0"
127.0.0.1:6379> XADD mystream * name Bob
"1609459200001-0"
127.0.0.1:6379> XREAD COUNT 2 STREAMS mystream 0
OutputSuccess
Important Notes
XREAD is efficient for reading new data without scanning the whole stream.
Using BLOCK helps to wait for new data instead of polling repeatedly.
Remember to use the correct ID to avoid missing or repeating entries.
Summary
XREAD reads new entries from Redis streams.
You can limit how many entries to read and wait for new ones.
It works well for real-time data like messages or logs.