Reference

SQL & Redis

SQLite, PostgreSQL, Snowflake and Redis syntax, queries and everyday operations.

Scope and conventions

SQL examples name their database and assume the sample schema where indicated. Redis examples are native redis-cli commands, with optional Search/JSON features labeled. Run independent examples in your own scratch database; driver binding syntax and transaction guarantees differ.

Redis structures

Use a Redis string key

Redis

SET stores one byte string, GET reads it and MGET reads several. NX/XX select absence/presence conditions; EX/PX sets a lifetime at the same write. INCR is atomic for a valid integer string. Ordinary SET replaces the previous value and normally clears an existing TTL unless instructed otherwise.

Fragment

Redis · Use a Redis string key
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SET ref:greeting "north gate"
GET ref:greeting
SET ref:claim "owner-a" NX EX 60
SET ref:counter 0
INCR ref:counter

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Read and change named hash fields

Redis

A hash maps field names to byte strings. HSET changes selected fields; HMGET preserves the requested field order and uses nil for missing fields. HGETALL returns all fields, with protocol-dependent reply shape. HINCRBY atomically changes a valid integer field.

Fragment

Redis · Read and change named hash fields
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
HSET ref:profile name north visits 0
HMGET ref:profile name visits absent
HINCRBY ref:profile visits 1
HDEL ref:profile visits

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Push and pop an ordered list

Redis

LPUSH/RPUSH add to the left/right; LPOP/RPOP remove from those ends. LRANGE indexes start at zero and the stop index is inclusive; -1 means the last element. A destructive pop is not durable job acknowledgement by itself.

Fragment

Redis · Push and pop an ordered list
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
RPUSH ref:queue north west
LRANGE ref:queue 0 -1
LPOP ref:queue

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Keep unique set membership

Redis

A set keeps unique string members without an ordering guarantee. SADD/SREM change membership; SISMEMBER tests it; SCARD returns the count. SMEMBERS returns the entire set, while SSCAN allows cursor iteration for a large collection.

Fragment

Redis · Keep unique set membership
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SADD ref:tags paper new paper
SISMEMBER ref:tags paper
SCARD ref:tags
SREM ref:tags new

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Order members by score

Redis

A sorted set gives each unique member a floating-point score. ZADD inserts/updates; ZINCRBY changes a score. ZRANGE by index uses zero-based inclusive positions; BYSCORE (Redis 6.2+) uses score bounds. Equal-score members have lexicographic member order. This is not a SQL window frame.

Fragment

Redis · Order members by score
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
ZADD ref:ranking 10 north 20 west 20 east
ZRANGE ref:ranking 0 -1 WITHSCORES
ZRANGE ref:ranking 10 20 BYSCORE WITHSCORES
ZRANK ref:ranking west

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Append and read a Redis Stream

Redis

Streams retain ordered entries with IDs. XADD appends; XREAD reads after the supplied ID. Store the last processed ID when resuming; $ means only entries arriving after that read begins.

Fragment

Redis · Append and read a Redis Stream
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
XADD ref:events * kind created item 1
XREAD COUNT 10 STREAMS ref:events 0-0
# Later reads use the last observed entry ID.

Native commands in redis-cli; explanatory # lines are not commands. Replace observed-ID placeholders and use disposable ref:* keys.

Receive and acknowledge stream work

Redis

A consumer group tracks delivery and pending acknowledgements. > requests entries not yet delivered to this group. XACK removes a pending acknowledgement record; it does not make a business side effect atomic.

Fragment

Redis · Receive and acknowledge stream work
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
XGROUP CREATE ref:jobs workers 0 MKSTREAM
XADD ref:jobs * task render
XREADGROUP GROUP workers worker-1 COUNT 1 STREAMS ref:jobs >
XACK ref:jobs workers ID_FROM_READ

Native commands in redis-cli; explanatory # lines are not commands. Replace observed-ID placeholders and use disposable ref:* keys.

Inspect and reclaim pending stream entries

Redis

XPENDING inspects unacknowledged work. XAUTOCLAIM transfers sufficiently idle pending entries to another consumer (Redis 6.2+); continue with its returned scan cursor. Reprocessing can occur, so consumers need an idempotency policy.

Fragment

Redis · Inspect and reclaim pending stream entries
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
XPENDING ref:jobs workers
XAUTOCLAIM ref:jobs workers worker-2 60000 0-0 COUNT 10
XTRIM ref:jobs MAXLEN ~ 1000
# Trimming can remove payloads still referenced by pending work.

Native commands in redis-cli; explanatory # lines are not commands. Replace observed-ID placeholders and use disposable ref:* keys.

Publish live notifications with Pub/Sub

Redis

Pub/Sub delivers to currently connected subscribers with at-most-once delivery. It does not retain messages for replay or acknowledge business processing; use a Stream or another durable protocol when needed.

Fragment

Redis · Publish live notifications with Pub/Sub
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
# Session 1:
SUBSCRIBE ref:notifications
# Session 2:
PUBLISH ref:notifications hello

Native commands in redis-cli; explanatory # lines are not commands. Replace observed-ID placeholders and use disposable ref:* keys.

Batch round trips without implying transaction atomicity

Redis

Pipelining sends multiple commands without waiting for each reply. It reduces round trips; it does not make the batch atomic. redis-py pipelines use transaction behavior by default, so disable that explicitly for this example.

Fragment

Redis · Batch round trips without implying transaction atomicity
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import redis
client = redis.Redis(host="localhost", port=6379)
with client.pipeline(transaction=False) as pipe:
    pipe.set("ref:a", "1")
    pipe.get("ref:a")
    replies = pipe.execute()

Python redis-py client example, requiring that optional package. Bound batch sizes and account for per-command replies/errors.