Bird
Raised Fist0
Redisquery~20 mins

EXEC to execute in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis EXEC Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this Redis transaction?
Consider the following Redis commands executed in a transaction using MULTI and EXEC:

MULTI
SET key1 value1
INCR counter
GET key1
EXEC

Assuming counter was initially 5, what will EXEC return?
Redis
MULTI
SET key1 value1
INCR counter
GET key1
EXEC
A["OK", 6, "value1"]
B["OK", "6", "value1"]
C["OK", 5, "value1"]
D["OK", 6, null]
Attempts:
2 left
💡 Hint
Remember that INCR returns the new incremented value as an integer.
🧠 Conceptual
intermediate
1:30remaining
What does EXEC do in a Redis transaction?
In Redis, after starting a transaction with MULTI and queuing commands, what is the role of the EXEC command?
AIt executes all queued commands atomically and returns their results.
BIt cancels the transaction and discards all queued commands.
CIt queues another command to be executed later.
DIt resets the database to its initial state.
Attempts:
2 left
💡 Hint
Think about what happens after queuing commands with MULTI.
📝 Syntax
advanced
2:00remaining
Which option shows correct syntax to start and execute a Redis transaction?
Select the option that correctly starts a transaction, queues two commands, and executes them.
A
SET key1 val1
MULTI
INCR counter
EXEC
B
MULTI
SET key1 val1
EXEC
INCR counter
C
MULTI
SET key1 val1
INCR counter
EXEC
D
MULTI
EXEC
SET key1 val1
INCR counter
Attempts:
2 left
💡 Hint
Commands must be queued between MULTI and EXEC.
🔧 Debug
advanced
2:30remaining
Why does EXEC return null in this Redis transaction?
Given the commands:

MULTI
SET key1 value1
WATCH key1
INCR counter
EXEC

Why might EXEC return null?
ABecause INCR counter is invalid syntax inside MULTI.
BBecause SET key1 value1 failed due to wrong arguments.
CBecause EXEC was called before queuing any commands.
DBecause WATCH was called after MULTI, causing the transaction to fail.
Attempts:
2 left
💡 Hint
Remember the order of WATCH and MULTI matters.
❓ optimization
expert
3:00remaining
How to optimize multiple Redis commands for atomic execution?
You want to increment a counter and set a key's value atomically in Redis. Which approach is best for performance and atomicity?
AUse MULTI, queue INCR and SET commands, then EXEC.
BUse Lua scripting to perform both commands atomically.
CSend INCR and SET commands separately without MULTI/EXEC.
DUse WATCH on the key, then send INCR and SET commands.
Attempts:
2 left
💡 Hint
Lua scripts run atomically and can combine commands efficiently.

Practice

(1/5)
1. What does the EXEC command do in Redis transactions?
easy
A. It runs all queued commands in a transaction at once.
B. It starts queuing commands for a transaction.
C. It cancels all queued commands in a transaction.
D. It checks the status of the Redis server.

Solution

  1. Step 1: Understand Redis transaction commands

    The MULTI command starts queuing commands, and EXEC runs them all together.
  2. Step 2: Identify the role of EXEC

    EXEC executes all commands queued after MULTI in one atomic operation.
  3. Final Answer:

    It runs all queued commands in a transaction at once. -> Option A
  4. Quick Check:

    EXEC runs queued commands = B [OK]
Hint: Remember: MULTI queues, EXEC runs all queued commands [OK]
Common Mistakes:
  • Confusing EXEC with MULTI
  • Thinking EXEC cancels commands
  • Assuming EXEC checks server status
2. Which of the following is the correct sequence to run multiple commands atomically in Redis?
easy
A. SET key value, EXEC, MULTI
B. EXEC, MULTI, SET key value
C. MULTI, SET key value, EXEC
D. SET key value, MULTI, EXEC

Solution

  1. Step 1: Recall the transaction command order

    First, MULTI starts the transaction, then commands like SET queue, and finally EXEC runs them.
  2. Step 2: Match the correct order

    MULTI, SET key value, EXEC follows this order: MULTI, then SET, then EXEC.
  3. Final Answer:

    MULTI, SET key value, EXEC -> Option C
  4. Quick Check:

    Start with MULTI, end with EXEC = A [OK]
Hint: MULTI starts, EXEC ends the transaction [OK]
Common Mistakes:
  • Putting EXEC before MULTI
  • Running SET before MULTI
  • Mixing command order
3. Given the following Redis commands executed in order:
MULTI
SET user:1:name "Alice"
INCR user:1:visits
EXEC
What will be the result of the EXEC command?
medium
A. An error because INCR on a string key
B. ["OK", 1]
C. ["QUEUED", "QUEUED"]
D. Empty array []

Solution

  1. Step 1: Analyze queued commands

    SET user:1:name "Alice" sets a string key, returns "OK"; INCR user:1:visits increments a numeric key, returns the new value.
  2. Step 2: Understand EXEC output

    EXEC returns an array with results of each command: ["OK", 1].
  3. Final Answer:

    ["OK", 1] -> Option B
  4. Quick Check:

    EXEC returns results array = C [OK]
Hint: EXEC returns results of all queued commands in order [OK]
Common Mistakes:
  • Expecting QUEUED responses from EXEC
  • Thinking INCR causes error on string key
  • Assuming EXEC returns empty array
4. You run the following Redis commands:
MULTI
SET key1 "value1"
GET key1
EXEC
What will be the result of the EXEC command?
medium
A. An error because GET command is not allowed inside MULTI block
B. You forgot to call MULTI before queuing commands
C. The GET command returns QUEUED, causing EXEC to fail
D. No error; EXEC always succeeds if MULTI was called

Solution

  1. Step 1: Understand command behavior in transactions

    Some commands like GET are allowed but return QUEUED during MULTI, and EXEC returns their results after execution.
  2. Step 2: Identify that no error occurs

    GET is fully supported inside MULTI/EXEC blocks, so EXEC succeeds and returns ["OK", "value1"].
  3. Final Answer:

    No error; EXEC always succeeds if MULTI was called -> Option D
  4. Quick Check:

    GET inside MULTI causes no error = D [OK]
Hint: Check if commands are allowed inside MULTI before EXEC [OK]
Common Mistakes:
  • Assuming all commands work inside MULTI
  • Forgetting to call MULTI before EXEC
  • Expecting EXEC to never fail
5. You want to increment a counter atomically with optimistic locking (aborting if the key was modified meanwhile), using Redis transactions. Which sequence correctly uses WATCH, MULTI, and EXEC to do this safely?
hard
A.
WATCH counter
MULTI
INCR counter
EXEC
B.
MULTI
WATCH counter
INCR counter
EXEC
C.
MULTI
INCR counter
EXEC
WATCH counter
D.
INCR counter
WATCH counter
MULTI
EXEC

Solution

  1. Step 1: Understand WATCH usage

    WATCH monitors keys for changes before starting a transaction with MULTI.
  2. Step 2: Correct command order for safe increment

    First WATCH counter, then MULTI, queue INCR counter, and finally EXEC to run if no changes occurred.
  3. Final Answer:

    WATCH counter MULTI INCR counter EXEC -> Option A
  4. Quick Check:

    WATCH before MULTI for safe transactions = D [OK]
Hint: Always WATCH keys before MULTI to ensure safe EXEC [OK]
Common Mistakes:
  • Calling WATCH after MULTI
  • Running INCR before WATCH
  • Not using WATCH for conditional updates
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