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
Start a transaction
MULTI

# Queue subsequent commands for EXEC.

Execute queued transaction
EXEC

# Run all queued commands atomically.

Abandon queued transaction
DISCARD

# Clear queued transaction commands.

Watch keys for changes
WATCH account:1 account:2

# Abort EXEC if watched keys change before commit.

Stop watching keys
UNWATCH

# Clear optimistic-lock watches.

Optimistic transfer transaction
WATCH account:1
GET account:1
MULTI
DECRBY account:1 50
INCRBY account:2 50
EXEC

# Common check-and-set transaction workflow.

## Pub/Sub
Publish a message
PUBLISH events:user '{"userId":1,"action":"login"}'

# Send a message to all subscribers of a channel.

Subscribe to channels
SUBSCRIBE events:user events:system

# Listen for messages on one or more channels.

Subscribe by pattern
PSUBSCRIBE events:*

# Listen on channels that match a pattern.

Unsubscribe from channels
UNSUBSCRIBE events:user

# Stop listening to one or more channels.

Unsubscribe from patterns
PUNSUBSCRIBE events:*

# Stop listening to pattern subscriptions.

List active Pub/Sub channels
PUBSUB CHANNELS

# Show active channels with subscribers.

Count subscribers per channel
PUBSUB NUMSUB events:user events:system

# Return subscriber counts for channels.

Count pattern subscriptions
PUBSUB NUMPAT

# Return the total number of active pattern subscriptions.

## Streams
Append an entry to a stream
XADD orders * user_id 1 total 19.99 status paid

# Write a new stream message with autogenerated ID.

Get stream length
XLEN orders

# Return how many entries exist in a stream.

Read stream entries forward
XRANGE orders - + COUNT 10

# Read entries in ascending ID order.

Read stream entries backward
XREVRANGE orders + - COUNT 10

# Read entries in descending ID order.

Read from one or more streams
XREAD COUNT 10 BLOCK 5000 STREAMS orders payments $ $

# Block and read new entries from multiple streams.

Trim a stream
XTRIM orders MAXLEN ~ 10000

# Keep the stream approximately within a max length.

Create a consumer group
XGROUP CREATE orders order-workers $ MKSTREAM

# Create a consumer group, creating the stream if missing.

Read via consumer group
XREADGROUP GROUP order-workers worker-1 COUNT 10 BLOCK 5000 STREAMS orders >

# Claim new messages for a consumer in a group.

Acknowledge processed entries
XACK orders order-workers 1710000000000-0

# Acknowledge handled messages in a consumer group.

Inspect pending group messages
XPENDING orders order-workers

# View pending entries awaiting acknowledgement.

Claim idle pending messages
XCLAIM orders order-workers worker-2 60000 1710000000000-0

# Take ownership of stale pending messages.

Auto-claim idle messages
XAUTOCLAIM orders order-workers worker-2 60000 0-0 COUNT 100

# Incrementally claim stale pending entries.

Inspect stream metadata
XINFO STREAM orders

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

Inspect consumer groups
XINFO GROUPS orders

# Show metadata for all groups on a stream.

Delete stream entries
XDEL orders 1710000000000-0

# Delete one or more stream entries by ID.

## Geo, bitmaps, and HyperLogLog
Add geospatial members
GEOADD cities -122.4194 37.7749 sf -74.0060 40.7128 nyc

# Store longitude/latitude points in a sorted set.

Read geospatial coordinates
GEOPOS cities sf nyc

# Return stored coordinates for members.

Measure distance between members
GEODIST cities sf nyc km

# Compute the distance between two members.

Find nearby members
GEORADIUS cities -122.4194 37.7749 500 km WITHDIST

# Find nearby members around coordinates.

Modern geo search
GEOSEARCH cities FROMLONLAT -122.4194 37.7749 BYRADIUS 500 km WITHDIST

# Query nearby members using GEOSEARCH.

Add elements to HyperLogLog
PFADD unique:visitors user:1 user:2 user:3

# Approximate cardinality of unique items.

Estimate unique count
PFCOUNT unique:visitors

# Read approximate unique cardinality.

Merge HyperLogLogs
PFMERGE unique:all unique:web unique:mobile

# Merge approximate cardinality structures.

Bitwise AND across keys
BITOP AND flags:all flags:a flags:b

# Compute a new bitmap from source bitmaps.

Find first bit with a value
BITPOS feature:flags 1

# Find the offset of the first set bit.

Recommended next

No recommendations yet.