0
0
Redisquery~10 mins

Lua vs transactions comparison in Redis - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Lua vs transactions comparison
Start Client Command
Choose: Use Lua Script?
NoUse MULTI/EXEC Transaction
Queue Commands
Run Lua Script Atomically
EXEC Executes All
Atomic Execution Complete
Return Result to Client
This flow shows how Redis handles atomic operations either by running a Lua script atomically or by queuing commands in a MULTI/EXEC transaction.
Execution Sample
Redis
MULTI
SET key1 100
INCR key1
EXEC
This Redis transaction queues commands and executes them atomically with EXEC.
Execution Table
StepActionCommandState BeforeState AfterResult
1Start TransactionMULTIkey1 does not existTransaction startedOK
2Queue CommandSET key1 100Transaction startedSET queuedQUEUED
3Queue CommandINCR key1SET queuedINCR queuedQUEUED
4Execute TransactionEXECCommands queuedkey1=101["OK", 101]
5EndN/ATransaction executedNo active transactionN/A
💡 EXEC runs all queued commands atomically, then transaction ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
key1NULLNULLNULL101101
Transaction StateNoneStartedStartedExecutedNone
Key Moments - 2 Insights
Why does the key1 value only change after EXEC?
Commands are only queued during MULTI; actual changes happen atomically when EXEC runs, as shown in execution_table step 4.
How is Lua script execution different from MULTI/EXEC?
Lua scripts run all commands atomically in one go without queuing, unlike MULTI/EXEC which queues commands before execution.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the state of the transaction?
ATransaction executed
BNo active transaction
CTransaction started and commands queued
DTransaction aborted
💡 Hint
Refer to execution_table row 3, 'State After' column shows 'INCR queued'.
At which step does key1 get its new value?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check execution_table step 4, 'State After' shows 'key1=101'.
If we replace MULTI/EXEC with a Lua script doing the same commands, what changes?
ACommands execute atomically without queuing
BCommands are queued before execution
CCommands execute one by one with delays
DCommands cannot modify keys
💡 Hint
See key_moments explanation about Lua scripts running atomically in one go.
Concept Snapshot
Redis MULTI/EXEC queues commands and runs them atomically on EXEC.
Lua scripts run all commands atomically in one execution without queuing.
MULTI/EXEC is multi-step; Lua script is single-step atomic.
Both ensure atomicity but differ in execution style and complexity.
Use Lua for complex logic; MULTI/EXEC for simple grouped commands.
Full Transcript
This visual execution compares Redis Lua scripts and MULTI/EXEC transactions. MULTI starts a transaction, queues commands like SET and INCR, then EXEC runs all queued commands atomically, updating key1 from NULL to 101. Lua scripts run all commands atomically in one step without queuing. The key difference is that MULTI/EXEC separates queuing and execution, while Lua scripts execute all logic in one atomic call. This ensures atomicity in both but with different workflows. Beginners often confuse when changes happen in MULTI/EXEC; changes only apply at EXEC. Lua scripts simplify atomic execution by running all commands together. This comparison helps understand when to use each method in Redis.