Target key does not exist or is not a stream
Production Risk
Medium — stream commands silently fail or error until the key is created; consumer groups cannot be set up.
Raised by stream commands when the target key does not exist and MKSTREAM was not specified, or when the key exists but holds a non-stream data type. Commands such as XLEN, XRANGE, XINFO STREAM, and XGROUP CREATE can produce this error.
- 1The stream key was never created (no XADD has been called on it).
- 2The key was deleted with DEL or expired via TTL.
- 3XGROUP CREATE called without MKSTREAM on a non-existent key.
- 4A typo in the key name.
A consumer tries to inspect or read a stream that does not exist yet.
# Key does not exist XLEN nonexistent_stream # or XGROUP CREATE nonexistent_stream mygroup $
expected output
(error) ERR The XGROUP subcommand requires the key to exist. # For XLEN on a non-existent key it returns 0, but XINFO STREAM raises: (error) ERR no such key
Fix 1
Use MKSTREAM to auto-create the stream
WHEN Creating a consumer group before any producers have written to the stream
# MKSTREAM creates the stream automatically if it does not exist XGROUP CREATE mystream mygroup $ MKSTREAM
Why this works
The MKSTREAM flag instructs XGROUP CREATE to create an empty stream before registering the group.
Fix 2
Seed the stream with a dummy message
WHEN You need the stream to exist before consumers start
# Write a placeholder entry so the stream exists XADD mystream * _init true XGROUP CREATE mystream mygroup 0
Why this works
XADD creates the stream key; XGROUP CREATE then succeeds because the key exists.
Fix 3
Check key existence before operating
WHEN The stream may legitimately not exist and the operation should be skipped
if redis.exists("mystream"):
info = redis.xinfo_stream("mystream")Why this works
EXISTS returns 1 if the key is present; avoids the error when the stream is genuinely absent.
✕ Assume a stream exists simply because a producer should have written to it
Producers may not have run yet, or the key may have been evicted; always handle the absent-key case explicitly.
Streams introduced; NOSTREAM / ERR no such key raised for missing stream keys.
MKSTREAM support improved across more subcommands.
Redis Streams — key existence and MKSTREAM documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev