Advertisement

Streams interview questions and answers

7 Streams interview questions for Redis roles, each with a model answer, examples and likely follow-ups.

7 questions
Advertisement

7 questions

Q1 Beginner Save

What is a Redis Stream, and how does it differ from a regular Redis list when used as a messaging system?

A Redis Stream is an append only log data structure specifically designed for messaging, where every added entry receives a unique id and remains stored in the stream even after being read, unlike a list where items are typically removed once processed, letting multiple different consumers read and replay the same messages independently.
Example

XADD mystream '*' sensor 'temp1' value '22.5' XRANGE mystream - +

Real-world example
An event logging system uses a Redis Stream to record every user action, allowing multiple different downstream services to independently process the same events at their own pace without consuming or removing them for other consumers.
Likely follow-ups
  • What does the asterisk represent when adding an entry to a stream?
  • Can entries be manually removed from a stream, or do they stay forever?
Redis as a Message Queue Pub/Sub
Q2 Beginner Save

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

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.
Example

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.
Likely 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?
Redis as a Message Queue Data Types
Q3 Intermediate Save

What is a consumer group in Redis Streams, and how does it enable multiple workers to process the same stream cooperatively?

A consumer group lets multiple different consumers share the workload of processing a single stream, with Redis ensuring each message is only delivered to one consumer within that specific group, while different consumer groups can independently receive and process every message in the stream separately from each other.
Example

XGROUP CREATE orders processing_group '$' XREADGROUP GROUP processing_group worker1 COUNT 1 STREAMS orders '>'

Real-world example
An order processing system runs several worker instances all belonging to the same consumer group, with Redis automatically distributing each new order event to only one of the available workers, effectively load balancing the processing work.
Likely follow-ups
  • What happens if a worker in a consumer group crashes while processing a message?
  • Can you have multiple independent consumer groups reading the exact same stream?
Redis as a Message Queue Distributed Locks with Redis
Q4 Intermediate Save

How do you acknowledge that a message read from a consumer group has been successfully processed?

You use the XACK command, specifying the stream, the consumer group, and the specific message id, which marks that message as successfully processed for that group, removing it from the group's pending entries list, letting Redis know it does not need to be redelivered to another consumer if something goes wrong.
Example

XACK orders processing_group 1725800000-0

Real-world example
A worker processing an order event calls XACK immediately after successfully completing its work, confirming to Redis that this specific message does not need to be redelivered to another worker in the group.
Likely follow-ups
  • What happens to a message that is never acknowledged?
  • How do you view the list of currently pending, unacknowledged messages for a consumer group?
Redis as a Message Queue Distributed Locks with Redis
Q5 Advanced Save

How would you handle a message that was delivered to a consumer but never acknowledged, perhaps because that consumer crashed while processing it?

You use the XPENDING command to identify messages that have been delivered but not yet acknowledged, and XCLAIM to transfer ownership of a specific pending message to a different, healthy consumer after enough time has passed, ensuring that a crashed worker's unfinished work is not permanently lost and can be picked up by another available consumer.
Example

XPENDING orders processing_group XCLAIM orders processing_group worker2 60000 1725800000-0

Real-world example
A monitoring process periodically checks for messages that have been pending for too long using XPENDING, automatically reclaiming them with XCLAIM and assigning them to a healthy worker whenever the original consumer appears to have crashed.
Likely follow-ups
  • How long should you typically wait before claiming a pending message from another consumer?
  • What happens if the original consumer eventually does complete and try to acknowledge a message that has already been claimed?
Redis as a Message Queue Distributed Locks with Redis
Q6 Advanced Save

How would you design a robust event sourcing system using Redis Streams as the primary event log?

You would use a single stream, or several streams organized by event category, as the authoritative append only log of every event that has occurred, use consumer groups to let different services independently process events relevant to them, ensure consumers handle messages idempotently in case of redelivery, and consider periodically trimming very old entries once they are no longer needed for replay purposes.
Example

XADD events '*' type 'OrderCreated' orderId '1001' XGROUP CREATE events analytics_service '$' XGROUP CREATE events email_service '$'

Real-world example
An e-commerce platform uses a single events stream as its source of truth for everything that happens, with separate consumer groups for their analytics service and their email notification service, each independently processing the exact same underlying events at their own pace.
Likely follow-ups
  • How do you decide when it is safe to trim old entries from an event sourcing stream?
  • What idempotency strategies help handle the rare case of a message being processed more than once?
Redis as a Message Queue Data Types
Q7 Intermediate Save

How do you limit the size of a Redis Stream to prevent it from growing indefinitely and consuming unlimited memory?

You use the MAXLEN option when adding entries with XADD, either specifying an exact maximum length or using the approximate trimming option for better performance, which automatically removes the oldest entries once the stream exceeds the specified length, keeping its memory usage bounded over time.
Example

XADD mystream MAXLEN '~' 10000 '*' field 'value'

Real-world example
A high volume logging system caps its stream at approximately ten thousand entries, automatically discarding the oldest log entries as new ones arrive, preventing the stream from consuming unbounded memory over time.
Likely follow-ups
  • What is the difference between exact and approximate trimming for performance reasons?
  • How do you decide an appropriate maximum length for a specific stream's use case?
Redis Memory Optimization Data Types