# Revision and Interview Questions — Redis Pub/Sub & Streams

Source: https://www.skillbyai.com/en/redis-streams/p-revision

> Recall Redis Pub/Sub and Streams concepts for exams and interviews.

## Cheat sheet

**Pub/Sub**: `SUBSCRIBE`/`PSUBSCRIBE`/`PUBLISH`; live broadcast, nothing stored, at-most-once; `PUBLISH` returns receiver count; slow subscribers disconnected by output buffer limits; reconnect gaps; classic PUBLISH broadcasts to all cluster nodes; **sharded Pub/Sub** (`SSUBSCRIBE`/`SPUBLISH`, Redis 7) scales by hash slot; RESP3 allows other commands on a subscribed connection. **Keyspace notifications**: `notify-keyspace-events`, keyspace and keyevent channels, expired events fire on actual deletion and can be lost. **Streams**: append-only log in one key; IDs `ms-seq`; `XADD`, `XRANGE`/`XREVRANGE`, `XREAD` with `BLOCK` and `$`; trimming with `MAXLEN`/`MINID` and `~`; `XDEL`. **Consumer groups**: `XGROUP CREATE` (`$`, `0`, `MKSTREAM`), `XREADGROUP ... >`, PEL, `XACK`, `XPENDING`, ID `0` to recover own pending, `XAUTOCLAIM`/`XCLAIM`, delivery counts for dead-lettering, `XGROUP DELCONSUMER`; at-least-once, so idempotent consumers. **Patterns**: job queues (Streams or BLMOVE lists), outbox to stream, partition streams by key for scale. **Ops**: AOF/RDB, async replication and `WAIT`, `noeviction`, `XINFO GROUPS` lag, memory sizing.

## Common interview questions

Answer each with commands and a trade-off.

```text
1. Compare Redis Pub/Sub, lists and Streams as messaging tools.
2. What happens to Pub/Sub messages when a subscriber is offline or slow?
3. Why does sharded Pub/Sub scale better in Redis Cluster?
4. Explain stream IDs and how XREAD with BLOCK and $ works.
5. How do consumer groups distribute work and track progress?
6. What is the Pending Entries List, and how do you recover from a crashed consumer?
7. How would you handle poison messages in a Redis stream?
8. How do you bound memory use of a stream?
9. What durability guarantees does Redis give for stream entries?
10. When would you choose Kafka or RabbitMQ instead of Redis Streams?
```

## Always state the delivery guarantee

Interviewers check whether you know that Pub/Sub is at-most-once and consumer groups are at-least-once. Saying which one you rely on, and how you handle duplicates or losses, is what makes an answer strong.

**Quiz:** Which command acknowledges that a consumer in a group has processed a stream entry?

- [ ] XADD
- [ ] XTRIM
- [ ] PUBLISH
- [x] XACK

*Answer:* XACK. XACK removes the entry from the group's Pending Entries List.
