Redis Transactions, Pub/Sub, and Streams Cheat Sheet

Atomic transactions, optimistic locking, pub/sub messaging, streams, consumer groups, geo commands, bitmaps, and HyperLogLog.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Transactions and optimistic locking

Atomic batches and CAS-style updates.

Start a transaction

Queue subsequent commands for EXEC.

bashANYredistransactionmulti
bash
MULTI

Execute queued transaction

Run all queued commands atomically.

bashANYredistransactionexec
bash
EXEC

Abandon queued transaction

Clear queued transaction commands.

bashANYredistransactiondiscard
bash
DISCARD

Watch keys for changes

Abort EXEC if watched keys change before commit.

bashANYredistransactionwatch
bash
WATCH account:1 account:2

Stop watching keys

Clear optimistic-lock watches.

bashANYredistransactionwatch
bash
UNWATCH

Optimistic transfer transaction

Common check-and-set transaction workflow.

bashANYredistransactionworkflow
bash
WATCH account:1
GET account:1
MULTI
DECRBY account:1 50
INCRBY account:2 50
EXEC

In practice, the client should validate account state after GET and before MULTI/EXEC.

Pub/Sub

Publish messages and subscribe to channels or patterns.

Publish a message

Send a message to all subscribers of a channel.

bashANYredispubsubpublish
bash
PUBLISH events:user '{"userId":1,"action":"login"}'

Subscribe to channels

Listen for messages on one or more channels.

bashANYredispubsubsubscribe
bash
SUBSCRIBE events:user events:system

Subscribe by pattern

Listen on channels that match a pattern.

bashANYredispubsubpattern
bash
PSUBSCRIBE events:*

Unsubscribe from channels

Stop listening to one or more channels.

bashANYredispubsubunsubscribe
bash
UNSUBSCRIBE events:user

Unsubscribe from patterns

Stop listening to pattern subscriptions.

bashANYredispubsubunsubscribe
bash
PUNSUBSCRIBE events:*

List active Pub/Sub channels

Show active channels with subscribers.

bashANYredispubsubinspect
bash
PUBSUB CHANNELS

Count subscribers per channel

Return subscriber counts for channels.

bashANYredispubsubinspect
bash
PUBSUB NUMSUB events:user events:system

Count pattern subscriptions

Return the total number of active pattern subscriptions.

bashANYredispubsubinspect
bash
PUBSUB NUMPAT

Streams

Append-only logs, consumer groups, and event processing.

Append an entry to a stream

Write a new stream message with autogenerated ID.

bashANYredisstreamxadd
bash
XADD orders * user_id 1 total 19.99 status paid

Get stream length

Return how many entries exist in a stream.

bashANYredisstreamlength
bash
XLEN orders

Read stream entries forward

Read entries in ascending ID order.

bashANYredisstreamread
bash
XRANGE orders - + COUNT 10

Read stream entries backward

Read entries in descending ID order.

bashANYredisstreamread
bash
XREVRANGE orders + - COUNT 10

Read from one or more streams

Block and read new entries from multiple streams.

bashANYredisstreamread
bash
XREAD COUNT 10 BLOCK 5000 STREAMS orders payments $ $

Trim a stream

Keep the stream approximately within a max length.

bashANYredisstreamtrim
bash
XTRIM orders MAXLEN ~ 10000

Create a consumer group

Create a consumer group, creating the stream if missing.

bashANYredisstreamconsumer-group
bash
XGROUP CREATE orders order-workers $ MKSTREAM

Read via consumer group

Claim new messages for a consumer in a group.

bashANYredisstreamconsumer-group
bash
XREADGROUP GROUP order-workers worker-1 COUNT 10 BLOCK 5000 STREAMS orders >

Acknowledge processed entries

Acknowledge handled messages in a consumer group.

bashANYredisstreamconsumer-group
bash
XACK orders order-workers 1710000000000-0

Inspect pending group messages

View pending entries awaiting acknowledgement.

bashANYredisstreamconsumer-group
bash
XPENDING orders order-workers

Claim idle pending messages

Take ownership of stale pending messages.

bashANYredisstreamconsumer-group
bash
XCLAIM orders order-workers worker-2 60000 1710000000000-0

Auto-claim idle messages

Incrementally claim stale pending entries.

bashANYredisstreamconsumer-group
bash
XAUTOCLAIM orders order-workers worker-2 60000 0-0 COUNT 100

Inspect stream metadata

Show stream length, first/last IDs, and group counts.

bashANYredisstreaminspect
bash
XINFO STREAM orders

Inspect consumer groups

Show metadata for all groups on a stream.

bashANYredisstreaminspect
bash
XINFO GROUPS orders

Delete stream entries

Delete one or more stream entries by ID.

bashANYredisstreamdelete
bash
XDEL orders 1710000000000-0

Geo, bitmaps, and HyperLogLog

Approximate counts, geospatial indexing, and bit operations.

Add geospatial members

Store longitude/latitude points in a sorted set.

bashANYredisgeolocation
bash
GEOADD cities -122.4194 37.7749 sf -74.0060 40.7128 nyc

Read geospatial coordinates

Return stored coordinates for members.

bashANYredisgeolocation
bash
GEOPOS cities sf nyc

Measure distance between members

Compute the distance between two members.

bashANYredisgeodistance
bash
GEODIST cities sf nyc km

Find nearby members

Find nearby members around coordinates.

bashANYredisgeosearch
bash
GEORADIUS cities -122.4194 37.7749 500 km WITHDIST

Modern geo search

Query nearby members using GEOSEARCH.

bashANYredisgeosearch
bash
GEOSEARCH cities FROMLONLAT -122.4194 37.7749 BYRADIUS 500 km WITHDIST

Add elements to HyperLogLog

Approximate cardinality of unique items.

bashANYredishyperloglogcount
bash
PFADD unique:visitors user:1 user:2 user:3

Estimate unique count

Read approximate unique cardinality.

bashANYredishyperloglogcount
bash
PFCOUNT unique:visitors

Merge HyperLogLogs

Merge approximate cardinality structures.

bashANYredishyperloglogmerge
bash
PFMERGE unique:all unique:web unique:mobile

Bitwise AND across keys

Compute a new bitmap from source bitmaps.

bashANYredisbitopbitmap
bash
BITOP AND flags:all flags:a flags:b

Find first bit with a value

Find the offset of the first set bit.

bashANYredisbitmapbitpos
bash
BITPOS feature:flags 1

Recommended next

No recommendations yet.