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.

Transactions & conflicts

Group writes and choose commit or rollback

Redis

MULTI queues commands on one connection. A queue-time refusal causes EXECABORT: none of that attempt executes. Otherwise EXEC runs the group without interleaving and returns replies in command order; inspect every reply, because a runtime error does not roll back successful commands. A WATCH conflict returns nil and executes none. DISCARD abandons a queued attempt before EXEC.

Fragment

Redis · Group writes and choose commit or rollback
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
MULTI
HINCRBY ref:items:1 qty 1
HSET ref:items:1 note reviewed
EXEC

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

Make the write check the current value

Redis

WATCH makes EXEC abort if a watched key changes before execution. Read, decide, then MULTI/EXEC on the same connection; a nil EXEC reply requires a bounded retry of the whole decision. WATCH is key-level optimistic concurrency, not a SQL row lock.

Fragment

Redis · Make the write check the current value
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
WATCH ref:items:1
HGET ref:items:1 qty
# If the observed quantity permits the update:
MULTI
HINCRBY ref:items:1 qty -1
EXEC

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

Recognize contention and retry boundaries

Redis

Redis has no transactional SQL row-lock wait cycle or SQL deadlock schedule. WATCH/EXEC is optimistic and Lua/functions execute without command interleaving. A distributed lock built from keys has a separate ownership/lease/failure contract.

Different native mechanism; no interchangeable statement is implied.

Different native mechanism; no interchangeable operation is implied.

Run a bounded Redis server-side decision

Redis

EVAL passes keys separately from arguments; KEYS/ARGV are Lua arrays indexed from 1. The script executes without command interleaving, but runtime errors do not roll back earlier writes. Keep work bounded. All accessed keys must be supplied, and cluster scripts must meet key-slot rules.

Fragment

Redis · Run a bounded Redis server-side decision
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
EVAL "local n = redis.call('HINCRBY', KEYS[1], 'qty', ARGV[1]); return n" 1 ref:items:1 1

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

Roll back part of a transaction with savepoints

Redis

MULTI/DISCARD and WATCH are not savepoints or rollback of partially executed writes. Use a bounded atomic decision/script where that fits the required invariant.

Different transaction mechanism.

Different native mechanism; no interchangeable operation is implied.