Multi-key transactions for consistency in Redis - Time & Space Complexity
When using multi-key transactions in Redis, it is important to understand how the time to complete the transaction changes as more keys are involved.
We want to know how the number of keys affects the work Redis does to keep data consistent.
Analyze the time complexity of the following Redis transaction code.
MULTI
SET key1 value1
SET key2 value2
SET key3 value3
EXEC
This code starts a transaction, sets values for three keys, and then executes all commands atomically.
Look for repeated actions inside the transaction.
- Primary operation: Setting a key-value pair with SET command.
- How many times: Once for each key involved in the transaction.
As the number of keys increases, Redis performs more SET operations inside the transaction.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 SET commands inside one transaction |
| 10 | 10 SET commands inside one transaction |
| 100 | 100 SET commands inside one transaction |
Pattern observation: The total work grows directly with the number of keys you update.
Time Complexity: O(n)
This means the time to complete the transaction grows linearly with the number of keys involved.
[X] Wrong: "Using MULTI/EXEC makes all key updates happen instantly, no matter how many keys."
[OK] Correct: Even though the commands run atomically, Redis still processes each key update one by one, so more keys mean more work and more time.
Understanding how multi-key transactions scale helps you explain how Redis handles atomic updates and consistency in real applications.
What if we replaced multiple SET commands with a single Lua script that updates all keys? How would the time complexity change?
Practice
MULTI and EXEC commands in Redis transactions?Solution
Step 1: Understand MULTI command
TheMULTIcommand starts a transaction block in Redis, grouping commands.Step 2: Understand EXEC command
TheEXECcommand executes all commands queued afterMULTIatomically, ensuring consistency.Final Answer:
To group multiple commands so they execute atomically for data consistency -> Option AQuick Check:
Atomic execution = Data consistency [OK]
- Thinking MULTI speeds up commands individually
- Confusing transactions with backups
- Assuming EXEC deletes keys
Solution
Step 1: Identify Redis transaction start command
Redis usesMULTIto begin a transaction block.Step 2: Check other options
EXECends the transaction,BEGINandSTARTare not Redis commands.Final Answer:
MULTI -> Option DQuick Check:
Start transaction = MULTI [OK]
- Using EXEC to start instead of end
- Using SQL-like BEGIN or START commands
- Confusing transaction commands with key commands
MULTI SET key1 "value1" SET key2 "value2" EXECWhat will be the state of
key1 and key2 after EXEC?Solution
Step 1: Understand MULTI and EXEC behavior
Commands betweenMULTIandEXECare queued and executed atomically.Step 2: Analyze the commands
BothSET key1 "value1"andSET key2 "value2"will be applied together whenEXECruns.Final Answer:
key1andkey2will both have their new values set atomically -> Option AQuick Check:
Atomic set of keys = Both keys updated [OK]
- Thinking commands run immediately after MULTI
- Assuming partial execution on EXEC failure
- Confusing EXEC with DISCARD
MULTI SET key1 "val1" GET key1 EXECBut the
GET key1 command returns null inside the transaction. Why?Solution
Step 1: Understand command queuing in MULTI
Inside a transaction, commands are queued and do not execute immediately.Step 2: Effect on GET command
GET returnsnullbecause the SET has not executed yet; actual results come after EXEC.Final Answer:
GET inside MULTI queues the command and returns null until EXEC runs -> Option CQuick Check:
Queued commands return null before EXEC [OK]
- Thinking GET runs immediately inside MULTI
- Assuming SET failed without checking
- Forgetting to call EXEC
account:balance and account:points atomically in Redis. Which approach ensures both keys update together or not at all?Solution
Step 1: Understand atomic updates need transactions
To update multiple keys together, Redis transactions withMULTIandEXECensure atomicity.Step 2: Analyze other options
Separate SET commands risk partial updates; WATCH helps optimistic locking but needs MULTI for atomicity.Final Answer:
Use MULTI, queue both SET commands, then EXEC to apply atomically -> Option BQuick Check:
Atomic multi-key update = MULTI + EXEC [OK]
- Running SET commands separately risking partial updates
- Using WATCH without MULTI for atomicity
- Relying on GET before SET without transaction
Ready to prove it?
Pass the test and this pattern moves unknown
