MULTIRedis Transactions, Pub/Sub, and Streams Cheat Sheet
Atomic transactions, optimistic locking, pub/sub messaging, streams, consumer groups, geo commands, bitmaps, and HyperLogLog.
Transactions and optimistic locking
Atomic batches and CAS-style updates.
EXECDISCARDWatch keys for changes
Abort EXEC if watched keys change before commit.
WATCH account:1 account:2UNWATCHOptimistic transfer transaction
Common check-and-set transaction workflow.
WATCH account:1
GET account:1
MULTI
DECRBY account:1 50
INCRBY account:2 50
EXECIn practice, the client should validate account state after GET and before MULTI/EXEC.
Pub/Sub
Publish messages and subscribe to channels or patterns.
PUBLISH events:user '{"userId":1,"action":"login"}'SUBSCRIBE events:user events:systemPSUBSCRIBE events:*UNSUBSCRIBE events:userPUNSUBSCRIBE events:*PUBSUB CHANNELSPUBSUB NUMSUB events:user events:systemCount pattern subscriptions
Return the total number of active pattern subscriptions.
PUBSUB NUMPATStreams
Append-only logs, consumer groups, and event processing.
Append an entry to a stream
Write a new stream message with autogenerated ID.
XADD orders * user_id 1 total 19.99 status paidXLEN ordersXRANGE orders - + COUNT 10XREVRANGE orders + - COUNT 10Read from one or more streams
Block and read new entries from multiple streams.
XREAD COUNT 10 BLOCK 5000 STREAMS orders payments $ $XTRIM orders MAXLEN ~ 10000Create a consumer group
Create a consumer group, creating the stream if missing.
XGROUP CREATE orders order-workers $ MKSTREAMRead via consumer group
Claim new messages for a consumer in a group.
XREADGROUP GROUP order-workers worker-1 COUNT 10 BLOCK 5000 STREAMS orders >Acknowledge processed entries
Acknowledge handled messages in a consumer group.
XACK orders order-workers 1710000000000-0Inspect pending group messages
View pending entries awaiting acknowledgement.
XPENDING orders order-workersClaim idle pending messages
Take ownership of stale pending messages.
XCLAIM orders order-workers worker-2 60000 1710000000000-0Auto-claim idle messages
Incrementally claim stale pending entries.
XAUTOCLAIM orders order-workers worker-2 60000 0-0 COUNT 100Inspect stream metadata
Show stream length, first/last IDs, and group counts.
XINFO STREAM ordersXINFO GROUPS ordersXDEL orders 1710000000000-0Geo, bitmaps, and HyperLogLog
Approximate counts, geospatial indexing, and bit operations.
GEOADD cities -122.4194 37.7749 sf -74.0060 40.7128 nycGEOPOS cities sf nycGEODIST cities sf nyc kmGEORADIUS cities -122.4194 37.7749 500 km WITHDISTGEOSEARCH cities FROMLONLAT -122.4194 37.7749 BYRADIUS 500 km WITHDISTPFADD unique:visitors user:1 user:2 user:3PFCOUNT unique:visitorsPFMERGE unique:all unique:web unique:mobileBITOP AND flags:all flags:a flags:bBITPOS feature:flags 1