# Streams and consumer groups

> XADD, XREAD, consumer groups, pending entries and acknowledgements.

---

LLMS index: [llms.txt](/llms.txt)

---

A Redis Stream stores append-only entries ordered by a two-part identifier (`milliseconds-sequence`). Each entry contains an ordered list of binary field/value pairs. Consumer groups allow multiple workers to partition a stream, track unacknowledged work in a Pending Entries List (PEL), and transfer stalled entries.

For an end-to-end transactional example combining SQL tables with Redis Streams, see the [Transactional outbox tutorial](/docs/redis/tutorial/outbox/).

## Entries, IDs, and trimming {#entries}

- **Full 128-bit ID domain:** Stream IDs consist of two unsigned 64-bit integers (`ms-seq`). `pgredis` preserves the full `uint64` range for both components inside `redis._stream`.
- **Entry payload fidelity:** Field/value pairs retain their exact insertion order, duplicate field names within the same entry, and arbitrary binary bytes (including `NUL`).
- **Write, read, and trim commands:** Use `XADD` to append entries (`NOMKSTREAM`, `MAXLEN`, `MINID`), `XRANGE` and `XREVRANGE` to query ID intervals (`-`, `+`, exclusive `(` bounds, `COUNT`), `XLEN` to read the entry count, and `XDEL` or `XTRIM` to remove entries.
- **Approximate trimming and `XSETID`:** Approximate trimming (`MAXLEN ~` or `MINID ~`) follows internal stream node boundaries configured by `stream-node-max-entries` and `stream-node-max-bytes`, so it may retain more entries than the requested threshold. `XSETID` updates stream metadata (`last_generated_id`, `entries_added`, `max_deleted_entry_id`) without inserting an entry or waking blocked readers.

## Consumer groups and pending entries {#groups}

- **Group lifecycle:** `XGROUP CREATE` (`MKSTREAM`, `ENTRIESREAD`), `XGROUP SETID`, `XGROUP DESTROY`, `XGROUP CREATECONSUMER`, and `XGROUP DELCONSUMER` manage consumer group state in `redis._stream_group`.
- **Reading and acknowledging:** `XREADGROUP GROUP <group> <consumer> ... STREAMS <key> >` delivers new entries and records them in `redis._stream_pel` unless `NOACK` is specified. `XACK` removes acknowledged entries from the PEL.
- **Inspecting and claiming stalled work:** `XPENDING` inspects unacknowledged entries by ID range, idle time (`IDLE`), and consumer. `XCLAIM` and `XAUTOCLAIM` transfer ownership of pending entries to another consumer and increment delivery counters.
- **Deleted entries in the PEL:** If an entry is deleted with `XDEL` while still referenced in `redis._stream_pel`, historical `XREADGROUP` (`0`) or `XCLAIM` calls return a nil/empty payload for that ID, matching Redis 7.2.4. Known gaps in `v0.1.0-rc1`: `XINFO GROUPS` computes `lag` from stream length rather than adjusting for `XDEL`, and `XAUTOCLAIM` has one cursor formatting difference when draining an empty queue (`99/101` in `unit/type/stream-cgroups`; see [Status](/docs/redis/status/#known-issues)).

## Blocking reads {#blocking}

- **RESP connections:** `XREAD BLOCK <ms>` and `XREADGROUP BLOCK <ms>` wait for matching `XADD` commits. Inside `MULTI`/`EXEC`, `BLOCK` options run non-blockingly as immediate reads; external clients blocked on a stream wake only after an `EXEC` batch or SQL transaction commits.
- **SQL transactions:** Ordinary `redis.command` calls cannot block and commit a caller's transaction while waiting. SQL callers use [`redis.blocking_begin`, `redis.blocking_poll`, and `redis.blocking_cancel`](/docs/redis/reference/sql/#sessions) across transactions.
- **Restart behavior:** Stream entries, consumer group offsets (`last_delivered_id`), and PEL records persist in PostgreSQL WAL and survive restarts and `pg_dump` / `pg_restore`, whereas active `BLOCK` network waits are transient and require clients to reconnect.

---

Backlinks:

- [Reference](/docs/redis/reference/)
