pencils ready ✎

Redis MCQs multiple-choice questions with answers & explanations

All 20 Redis quiz questions on one page. Pick an answer in your head, then open Show answer to check it and read why. Want a score and a timer? Take them as a quiz instead.

20 questions
  1. 1.

    What does the last command return?

    easy
    SET session:42 "alice" EX 600
    SET session:42 "alice-v2"
    TTL session:42
    1. A(integer) 600 or slightly less
    2. B(integer) -1
    3. C(integer) -2
    4. D(integer) 0
    Show answer

    Answer: B ((integer) -1)

    A plain SET replaces the value and discards any existing expiry, so the key now lives forever and TTL returns -1. Pass KEEPTTL (Redis 6+) to keep the old expiry. -2 would mean the key does not exist.

  2. 2.

    What does the last command return?

    easy
    SET hits 10 EX 100
    INCR hits
    TTL hits
    1. A(integer) -1, because INCR rewrites the key
    2. BAbout (integer) 100
    3. C(integer) -2
    4. DAn error: hits is a string
    Show answer

    Answer: B (About (integer) 100)

    Commands that modify a value in place, such as INCR, HSET or LPUSH, keep the key's TTL. Only commands that replace the whole value (SET, GETSET) or delete it clear the expiry. INCR works on strings that hold integers.

  3. 3.

    What does LRANGE return?

    easy
    LPUSH letters a b c
    LRANGE letters 0 -1
    1. Aa b c
    2. Bc b a
    3. Ca c b
    4. DOnly c
    Show answer

    Answer: B (c b a)

    LPUSH with several values inserts them one at a time at the head, left to right, so c ends up first. RPUSH letters a b c would give a b c. 0 -1 means the whole list.

  4. 4.

    On a new key, what does this SADD reply?

    easy
    SADD tags redis cache redis
    1. A(integer) 3
    2. B(integer) 2
    3. C(integer) 1
    4. DOK
    Show answer

    Answer: B ((integer) 2)

    SADD returns the number of members actually added. Sets hold unique members, so the second redis is ignored and the reply is 2. Running the same command again would reply 0.

  5. 5.

    What does the last command return?

    mid
    ZADD lb 100 alice 250 bob 175 carol
    ZREVRANK lb carol
    1. A(integer) 2
    2. B(integer) 1
    3. C(integer) 175
    4. D(integer) 3
    Show answer

    Answer: B ((integer) 1)

    ZREVRANK ranks from the highest score, zero-based: bob (250) is 0, carol (175) is 1, alice (100) is 2. ZRANK counts from the lowest score instead, and neither returns the score itself.

  6. 6.

    greeting holds a string and cnt does not exist. After this transaction, what does GET cnt return?

    mid
    MULTI
    INCR cnt
    LPUSH greeting x
    INCR cnt
    EXEC
    1. A(nil): the error rolls the transaction back
    2. B"1": execution stops at the error
    3. C"2": the other commands still apply
    4. DEXEC fails with EXECABORT and nothing runs
    Show answer

    Answer: C ("2": the other commands still apply)

    LPUSH on a string fails only at execution time with WRONGTYPE. Redis transactions have no rollback: EXEC returns that error in its reply array and the two INCRs still run. EXECABORT happens only for errors detected while queuing, such as an unknown command.

  7. 7.

    What happens at EXEC?

    mid
    MULTI
    SET a 1
    SETT b 2
    EXEC
    1. Aa is set, then an error is reported for SETT
    2. BEXEC returns EXECABORT and a is not set
    3. CBoth keys are set because SETT is an alias
    4. DThe connection is closed
    Show answer

    Answer: B (EXEC returns EXECABORT and a is not set)

    SETT is an unknown command, so it is rejected while queuing. Since Redis 2.6.5, a queue-time error makes EXEC refuse the whole transaction with EXECABORT, so nothing runs. Runtime errors like WRONGTYPE behave differently: the other commands still apply.

  8. 8.

    Client A runs the commands below. Between GET and MULTI, client B runs SET balance 50. What does A's EXEC return?

    mid
    WATCH balance
    GET balance        # "100"
    MULTI
    DECRBY balance 30
    EXEC
    1. A1) (integer) 20
    2. B1) (integer) 70
    3. CA null reply: the transaction was not executed
    4. DAn error: B must release the WATCH first
    Show answer

    Answer: C (A null reply: the transaction was not executed)

    WATCH is optimistic locking: if any watched key changes before EXEC, the transaction is discarded and EXEC returns a null reply, so A re-reads and retries. B is never blocked; Redis has no lock to release.

  9. 9.

    What does this command reply?

    hard
    EVAL "return 3.99" 0
    1. A"3.99"
    2. B(integer) 3
    3. C(integer) 4
    4. D(double) 3.99
    Show answer

    Answer: B ((integer) 3)

    A Lua number is converted to a Redis integer reply by truncation, so 3.99 becomes 3. Return tostring(x) (or a string) to keep the fraction. Similar surprises: Lua false becomes a nil reply and a table is cut at its first nil.

  10. 10.

    Two workers run these commands within the same second. What do they get?

    easy
    # worker 1
    SET lock:report "w1-8f3a" NX PX 30000
    # worker 2
    SET lock:report "w2-c41d" NX PX 30000
    1. ABoth get OK; the second overwrites the first
    2. BWorker 1 gets OK, worker 2 gets (nil)
    3. CBoth get (nil) until one calls EXPIRE
    4. DWorker 2 blocks until worker 1 releases
    Show answer

    Answer: B (Worker 1 gets OK, worker 2 gets (nil))

    NX sets the key only if it does not exist, atomically, so exactly one caller gets OK and the other gets a nil reply. PX 30000 sets a 30-second expiry in the same command. Redis never blocks a SET; the loser must retry or give up.

  11. 11.

    A worker acquires the lock, then runs a 45-second job. What can go wrong with this release?

    mid
    ok = r.set("lock:report", token, nx=True, px=30000)
    if ok:
        run_report()          # sometimes takes 45 s
        r.delete("lock:report")
    1. ANothing: DEL is atomic
    2. BThe lock expired at 30 s, another worker took it, and this DEL removes their lock
    3. CDEL fails because the key has a TTL
    4. DRedis extends the TTL automatically while the client is connected
    Show answer

    Answer: B (The lock expired at 30 s, another worker took it, and this DEL removes their lock)

    After 30 seconds the key expires and a second worker can acquire it. The first worker's unconditional DEL then deletes the second worker's lock, letting a third in. Release with a Lua script that deletes only if the value still equals your token, and size or renew the TTL to outlast the job.

  12. 12.

    Which MSET succeeds on Redis Cluster without a CROSSSLOT error?

    mid
    1. AMSET user:1000:cart x user:1000:profile y
    2. BMSET {user:1000}:cart x {user:1000}:profile y
    3. CMSET user:{1000}:cart x order:{2000}:cart y
    4. DNone: MSET is not supported in Cluster
    Show answer

    Answer: B (MSET {user:1000}:cart x {user:1000}:profile y)

    When a key contains {...}, only the text inside the first braces is hashed, so both keys hash user:1000 and share slot 1649. Without tags, user:1000:cart (slot 7185) and user:1000:profile (slot 8918) differ, and the third option uses two different tags. Multi-key commands are allowed in Cluster when all keys share a slot.

  13. 13.

    A cluster client sends GET foo and receives -MOVED 12182 10.0.0.3:6379. What should it do?

    mid
    1. ASend ASKING, then retry on 10.0.0.3 once without changing anything
    2. BRetry on 10.0.0.3 and update its slot map so slot 12182 points there
    3. CWait for the migration to finish and retry the same node
    4. DTreat it as a failure: the key was deleted
    Show answer

    Answer: B (Retry on 10.0.0.3 and update its slot map so slot 12182 points there)

    MOVED means slot 12182 (the slot of foo) is permanently served by another node, so the client retries there and updates its routing table, usually by refreshing the whole map. ASKING plus a one-off retry is the response to an ASK redirect during a slot migration.

  14. 14.

    An instance has maxmemory 2gb and maxmemory-policy volatile-lru. Memory is full and no key has a TTL. What happens on the next SET?

    hard
    1. ARedis evicts the least recently used key anyway
    2. BThe write fails with an OOM error
    3. CRedis grows past maxmemory and logs a warning
    4. DRedis evicts a random key
    Show answer

    Answer: B (The write fails with an OOM error)

    volatile-* policies only consider keys with an expiry. With none available, Redis behaves like noeviction and rejects writes with OOM command not allowed when used memory > maxmemory. Use allkeys-lru for a pure cache, or set TTLs on cache keys.

  15. 15.

    What is the default maxmemory-policy in Redis 7.4 and 8?

    easy
    1. Aallkeys-lru
    2. Bvolatile-lru
    3. Cnoeviction
    4. Dallkeys-lfu
    Show answer

    Answer: C (noeviction)

    The default is noeviction: when maxmemory is reached, writes return an OOM error while reads keep working. That is safe for data you cannot lose, but a cache should usually switch to allkeys-lru or allkeys-lfu.

  16. 16.

    With appendonly yes and the default appendfsync, roughly how much acknowledged data can a power loss destroy?

    mid
    1. ANothing: every write is fsynced
    2. BAbout one second of writes
    3. CEverything since the last RDB snapshot
    4. DUp to 30 seconds, whatever the OS has not flushed
    Show answer

    Answer: B (About one second of writes)

    The default is appendfsync everysec: writes go to the AOF immediately, but a background thread fsyncs once per second, so about a second of writes can be lost. always fsyncs every write batch; no leaves flushing to the OS, which is the 30-second case.

  17. 17.

    A support script runs KEYS session:* against a production instance with 40 million keys. What is the risk?

    easy
    1. ANone: KEYS runs in a background thread
    2. BIt blocks the server while it walks every key, stalling all clients
    3. CIt only returns the first 1,000 keys
    4. DIt deletes expired keys and can lose data
    Show answer

    Answer: B (It blocks the server while it walks every key, stalling all clients)

    KEYS is O(N) over the whole keyspace and runs on the single command thread, so every other client waits. Use SCAN 0 MATCH session:* COUNT 1000 and follow the cursor: it does a little work per call and returns every key that exists for the whole iteration at least once.

  18. 18.

    A subscriber to channel orders restarts and is offline for 10 seconds while 500 messages are published. What does it receive when it resubscribes?

    mid
    1. AAll 500 messages, in order
    2. BOnly messages published after it resubscribes
    3. CThe last message only
    4. DAll 500, but possibly out of order
    Show answer

    Answer: B (Only messages published after it resubscribes)

    Pub/Sub does not store messages: they go to whoever is subscribed at publish time and are otherwise dropped. For replay and at-least-once processing, use a Stream with XADD and a consumer group, which remembers each consumer's position and pending entries.

  19. 19.

    Which statement about this redis-py pipeline is true?

    mid
    pipe = r.pipeline(transaction=False)
    pipe.get("stock:42")
    pipe.decr("stock:42")
    pipe.execute()
    1. ABoth commands run atomically, with nothing in between
    2. BThey go in one round trip, but another client's command may run between them
    3. CDECR is skipped if GET returns nil
    4. DThe pipeline is rolled back if DECR fails
    Show answer

    Answer: B (They go in one round trip, but another client's command may run between them)

    Pipelining only batches network traffic: the server runs the commands in order but may interleave other clients. transaction=True wraps them in MULTI/EXEC for isolation, and conditional logic needs WATCH or a Lua script. Nothing is ever rolled back.

esc