Bird
Raised Fist0
Redisquery~10 mins

Multi-key transactions for consistency in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Multi-key transactions for consistency
Start Transaction
↓
Queue Commands on Keys
↓
Execute All Commands Atomically
↓
Check for Success or Failure
↓
Commit if Success
No→Discard Transaction
↓
End Transaction
This flow shows how Redis queues multiple commands on different keys and executes them all at once to keep data consistent.
Execution Sample
Redis
MULTI
SET key1 "value1"
SET key2 "value2"
EXEC
This Redis transaction sets two keys atomically to keep data consistent.
Execution Table
StepCommandActionState ChangeResult
1MULTIStart transaction modeCommands queued, no changes yetOK
2SET key1 "value1"Queue SET for key1key1 not changed yetQUEUED
3SET key2 "value2"Queue SET for key2key2 not changed yetQUEUED
4EXECExecute all queued commands atomicallykey1 = "value1", key2 = "value2"["OK", "OK"]
5EndTransaction endsAll changes committedTransaction complete
💡 EXEC runs all queued commands atomically, ensuring both keys update together or not at all.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
key1nullnullnull"value1""value1"
key2nullnullnull"value2""value2"
Transaction StateNot startedStartedStartedCommittedCommitted
Key Moments - 2 Insights
Why don't the keys change immediately after SET commands inside MULTI?
Because inside MULTI, commands are only queued (see steps 2 and 3 in execution_table). Actual changes happen only after EXEC runs all commands atomically (step 4).
What happens if EXEC fails or is not called?
If EXEC is not called or fails, none of the queued commands run, so keys remain unchanged, ensuring consistency (exit_note explains atomic execution).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of key1 after step 3?
AQUEUED
B"value1"
Cnull
DError
💡 Hint
Check the 'State Change' column at step 3 where commands are queued but keys not changed yet.
At which step do both keys get their new values?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'State Change' columns in step 4 where EXEC runs all commands atomically.
If EXEC was never called, what would be the final state of the keys?
ABoth keys updated
BNo keys updated
COnly key1 updated
DOnly key2 updated
💡 Hint
Refer to key_moments about what happens if EXEC is not called.
Concept Snapshot
Redis MULTI starts a transaction.
Commands like SET are queued, not executed immediately.
EXEC runs all queued commands atomically.
This ensures multi-key updates happen together or not at all.
If EXEC is not called, no changes happen.
Use this to keep data consistent across keys.
Full Transcript
This visual execution shows how Redis multi-key transactions work. First, MULTI starts the transaction mode where commands are queued but not executed. Then SET commands for different keys are queued. Only when EXEC is called, all queued commands run together atomically, updating all keys at once. This prevents partial updates and keeps data consistent. If EXEC is not called, no changes happen. The variable tracker shows keys remain unchanged until EXEC runs. This method is useful to update multiple keys safely in Redis.

Practice

(1/5)
1. What is the main purpose of using MULTI and EXEC commands in Redis transactions?
easy
A. To group multiple commands so they execute atomically for data consistency
B. To speed up the execution of individual Redis commands
C. To create backups of the Redis database automatically
D. To delete multiple keys at once

Solution

  1. Step 1: Understand MULTI command

    The MULTI command starts a transaction block in Redis, grouping commands.
  2. Step 2: Understand EXEC command

    The EXEC command executes all commands queued after MULTI atomically, ensuring consistency.
  3. Final Answer:

    To group multiple commands so they execute atomically for data consistency -> Option A
  4. Quick Check:

    Atomic execution = Data consistency [OK]
Hint: MULTI starts, EXEC runs all commands atomically [OK]
Common Mistakes:
  • Thinking MULTI speeds up commands individually
  • Confusing transactions with backups
  • Assuming EXEC deletes keys
2. Which of the following is the correct syntax to start a Redis transaction?
easy
A. EXEC
B. START
C. BEGIN
D. MULTI

Solution

  1. Step 1: Identify Redis transaction start command

    Redis uses MULTI to begin a transaction block.
  2. Step 2: Check other options

    EXEC ends the transaction, BEGIN and START are not Redis commands.
  3. Final Answer:

    MULTI -> Option D
  4. Quick Check:

    Start transaction = MULTI [OK]
Hint: Start with MULTI, end with EXEC [OK]
Common Mistakes:
  • Using EXEC to start instead of end
  • Using SQL-like BEGIN or START commands
  • Confusing transaction commands with key commands
3. Consider the following Redis commands executed in a transaction:
MULTI
SET key1 "value1"
SET key2 "value2"
EXEC
What will be the state of key1 and key2 after EXEC?
medium
A. key1 and key2 will both have their new values set atomically
B. Only key1 will be set, key2 will remain unchanged
C. Neither key1 nor key2 will be set because EXEC fails
D. key1 will be set, but key2 will be deleted

Solution

  1. Step 1: Understand MULTI and EXEC behavior

    Commands between MULTI and EXEC are queued and executed atomically.
  2. Step 2: Analyze the commands

    Both SET key1 "value1" and SET key2 "value2" will be applied together when EXEC runs.
  3. Final Answer:

    key1 and key2 will both have their new values set atomically -> Option A
  4. Quick Check:

    Atomic set of keys = Both keys updated [OK]
Hint: EXEC applies all queued commands together [OK]
Common Mistakes:
  • Thinking commands run immediately after MULTI
  • Assuming partial execution on EXEC failure
  • Confusing EXEC with DISCARD
4. You wrote this Redis transaction:
MULTI
SET key1 "val1"
GET key1
EXEC
But the GET key1 command returns null inside the transaction. Why?
medium
A. SET command failed, so GET returns null
B. GET command syntax is incorrect
C. GET inside MULTI queues the command and returns null until EXEC runs
D. EXEC was not called, so commands did not run

Solution

  1. Step 1: Understand command queuing in MULTI

    Inside a transaction, commands are queued and do not execute immediately.
  2. Step 2: Effect on GET command

    GET returns null because the SET has not executed yet; actual results come after EXEC.
  3. Final Answer:

    GET inside MULTI queues the command and returns null until EXEC runs -> Option C
  4. Quick Check:

    Queued commands return null before EXEC [OK]
Hint: GET returns null inside MULTI before EXEC [OK]
Common Mistakes:
  • Thinking GET runs immediately inside MULTI
  • Assuming SET failed without checking
  • Forgetting to call EXEC
5. You want to update two related keys account:balance and account:points atomically in Redis. Which approach ensures both keys update together or not at all?
hard
A. Use GET to check keys, then SET commands separately
B. Use MULTI, queue both SET commands, then EXEC to apply atomically
C. Use WATCH on both keys, then run SET commands without MULTI
D. Run two separate SET commands without MULTI

Solution

  1. Step 1: Understand atomic updates need transactions

    To update multiple keys together, Redis transactions with MULTI and EXEC ensure atomicity.
  2. Step 2: Analyze other options

    Separate SET commands risk partial updates; WATCH helps optimistic locking but needs MULTI for atomicity.
  3. Final Answer:

    Use MULTI, queue both SET commands, then EXEC to apply atomically -> Option B
  4. Quick Check:

    Atomic multi-key update = MULTI + EXEC [OK]
Hint: MULTI + EXEC ensures all-or-nothing updates [OK]
Common Mistakes:
  • Running SET commands separately risking partial updates
  • Using WATCH without MULTI for atomicity
  • Relying on GET before SET without transaction
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard