0
0
Redisquery~10 mins

Transactions vs Lua scripts in Redis - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Transactions vs Lua scripts
Start Transaction
Queue Commands
Execute All Commands
Commit or Discard
Start Lua Script
Run All Commands Atomically
Return Result
Transactions queue commands to run all at once, while Lua scripts run all commands inside a script atomically and return results immediately.
Execution Sample
Redis
MULTI
SET key1 val1
SET key2 val2
EXEC
This Redis transaction queues two SET commands and executes them atomically.
Execution Table
StepActionCommandStateOutput
1Start transactionMULTITransaction started, commands queuedOK
2Queue commandSET key1 val1Command queuedQUEUED
3Queue commandSET key2 val2Command queuedQUEUED
4Execute all commandsEXECCommands executed atomically["OK", "OK"]
5EndN/ATransaction completeN/A
💡 All queued commands executed atomically when EXEC is called
Variable Tracker
VariableStartAfter 1After 2After EXEC
key1nilnilnilval1
key2nilnilnilval2
transaction_statenonestartedstartednone
Key Moments - 3 Insights
Why do commands inside MULTI show QUEUED instead of immediate results?
Because commands are only queued during the transaction (see execution_table rows 2 and 3). They run only when EXEC is called (row 4).
How does a Lua script ensure atomic execution?
The entire Lua script runs as one atomic operation, so all commands inside it execute together without interruption, returning results immediately.
Can a Lua script return results while a transaction only queues commands?
Yes, Lua scripts return results after running all commands atomically, but transactions only return results after EXEC runs all queued commands.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after queuing the first SET command?
AOK
BQUEUED
Cnil
DError
💡 Hint
Check execution_table row 2, Output column
At which step do all queued commands actually run in the transaction?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
See execution_table row 4, Action column
If you want immediate results from multiple commands atomically, which should you use?
ALua script
BSeparate commands without transaction
CMULTI/EXEC transaction
DWATCH command
💡 Hint
Lua scripts run all commands atomically and return results immediately
Concept Snapshot
Transactions (MULTI/EXEC): queue commands, run all at once atomically, results after EXEC.
Lua scripts: run all commands inside script atomically, return results immediately.
Transactions show QUEUED until EXEC; Lua scripts execute fully in one step.
Use transactions for simple atomic batches; Lua scripts for complex logic atomically.
Full Transcript
This visual execution compares Redis transactions and Lua scripts. Transactions start with MULTI, queue commands showing QUEUED, then run all commands atomically with EXEC, returning results after. Lua scripts run all commands inside the script atomically and return results immediately. Variables like keys update only after EXEC in transactions, but update instantly in Lua scripts. Key confusions include why commands queue in transactions and how Lua scripts ensure atomicity. The quiz tests understanding of when commands run and outputs during transactions versus Lua scripts.