0
0
Redisquery~3 mins

XLEN for stream length in Redis

Choose your learning style9 modes available
Introduction
XLEN helps you find out how many entries are in a Redis stream. It tells you the stream's length quickly.
You want to know how many messages are waiting in a chat message stream.
You need to check if a stream has any data before reading it.
You want to monitor the size of a stream to avoid it growing too large.
You want to display the number of events stored in a stream to users.
You want to decide if you should trim a stream based on its length.
Syntax
Redis
XLEN key
Replace 'key' with the name of your Redis stream.
The command returns the number of entries in the stream as an integer.
Examples
Returns the number of entries in the stream named 'mystream'.
Redis
XLEN mystream
Returns how many order events are stored in the 'orders' stream.
Redis
XLEN orders
Sample Program
First, we add two entries to the stream 'mystream'. Then, XLEN returns the total number of entries, which is 2.
Redis
XADD mystream * name Alice
XADD mystream * name Bob
XLEN mystream
OutputSuccess
Important Notes
If the stream does not exist, XLEN returns 0.
XLEN is very fast because Redis keeps track of the stream length internally.
Use XLEN before reading a stream to know how much data you will get.
Summary
XLEN returns the number of entries in a Redis stream.
It helps you quickly check the size of a stream.
If the stream is empty or missing, XLEN returns 0.