Back to results for “Redis as a Message Queue”
Redis › Streams

How do you add a new entry to a Redis Stream, and how do you read entries back from it?

Beginner

Answer

You use XADD to add a new entry, providing a set of field value pairs representing the message data, and XRANGE to read a range of entries from the stream, specifying starting and ending id boundaries, letting you retrieve all or a specific portion of the stream's stored history.
XADD orders '*' orderId '1001' status 'created'
XRANGE orders - +
Real-world example An order processing system adds a new stream entry every time an order's status changes, and a monitoring dashboard reads the full history of status changes for a specific order using XRANGE.

Common follow-ups: What is the format of the id automatically generated for each stream entry?;How do you read only the newest entries added since your last read?