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.
Official reference: Redis docs
- 1.easy
What does the last command return?
SET session:42 "alice" EX 600 SET session:42 "alice-v2" TTL session:42- A
(integer) 600or slightly less - B
(integer) -1 - C
(integer) -2 - D
(integer) 0
Show answer
Answer: B (
(integer) -1)A plain
SETreplaces the value and discards any existing expiry, so the key now lives forever andTTLreturns-1. PassKEEPTTL(Redis 6+) to keep the old expiry.-2would mean the key does not exist. - A
- 2.easy
What does the last command return?
SET hits 10 EX 100 INCR hits TTL hits- A
(integer) -1, because INCR rewrites the key - BAbout
(integer) 100 - C
(integer) -2 - DAn error: hits is a string
Show answer
Answer: B (About
(integer) 100)Commands that modify a value in place, such as
INCR,HSETorLPUSH, keep the key's TTL. Only commands that replace the whole value (SET,GETSET) or delete it clear the expiry.INCRworks on strings that hold integers. - A
- 3.easy
What does
LRANGEreturn?LPUSH letters a b c LRANGE letters 0 -1- A
a b c - B
c b a - C
a c b - DOnly
c
Show answer
Answer: B (
c b a)LPUSHwith several values inserts them one at a time at the head, left to right, socends up first.RPUSH letters a b cwould givea b c.0 -1means the whole list. - A
- 4.easy
On a new key, what does this
SADDreply?SADD tags redis cache redis- A
(integer) 3 - B
(integer) 2 - C
(integer) 1 - D
OK
Show answer
Answer: B (
(integer) 2)SADDreturns the number of members actually added. Sets hold unique members, so the secondredisis ignored and the reply is 2. Running the same command again would reply 0. - A
- 5.mid
What does the last command return?
ZADD lb 100 alice 250 bob 175 carol ZREVRANK lb carol- A
(integer) 2 - B
(integer) 1 - C
(integer) 175 - D
(integer) 3
Show answer
Answer: B (
(integer) 1)ZREVRANKranks from the highest score, zero-based: bob (250) is 0, carol (175) is 1, alice (100) is 2.ZRANKcounts from the lowest score instead, and neither returns the score itself. - A
- 6.mid
greetingholds a string andcntdoes not exist. After this transaction, what doesGET cntreturn?MULTI INCR cnt LPUSH greeting x INCR cnt EXEC- A
(nil): the error rolls the transaction back - B
"1": execution stops at the error - C
"2": the other commands still apply - DEXEC fails with EXECABORT and nothing runs
Show answer
Answer: C (
"2": the other commands still apply)LPUSHon a string fails only at execution time withWRONGTYPE. Redis transactions have no rollback:EXECreturns that error in its reply array and the twoINCRs still run. EXECABORT happens only for errors detected while queuing, such as an unknown command. - A
- 7.mid
What happens at
EXEC?MULTI SET a 1 SETT b 2 EXEC- A
ais set, then an error is reported forSETT - BEXEC returns
EXECABORTandais not set - CBoth keys are set because
SETTis an alias - DThe connection is closed
Show answer
Answer: B (EXEC returns
EXECABORTandais not set)SETTis an unknown command, so it is rejected while queuing. Since Redis 2.6.5, a queue-time error makesEXECrefuse the whole transaction withEXECABORT, so nothing runs. Runtime errors likeWRONGTYPEbehave differently: the other commands still apply. - A
- 8.mid
Client A runs the commands below. Between
GETandMULTI, client B runsSET balance 50. What does A'sEXECreturn?WATCH balance GET balance # "100" MULTI DECRBY balance 30 EXEC- A
1) (integer) 20 - B
1) (integer) 70 - CA null reply: the transaction was not executed
- DAn error: B must release the WATCH first
Show answer
Answer: C (A null reply: the transaction was not executed)
WATCHis optimistic locking: if any watched key changes beforeEXEC, the transaction is discarded andEXECreturns a null reply, so A re-reads and retries. B is never blocked; Redis has no lock to release. - A
- 9.hard
What does this command reply?
EVAL "return 3.99" 0- A
"3.99" - B
(integer) 3 - C
(integer) 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: Luafalsebecomes a nil reply and a table is cut at its first nil. - A
- 10.easy
Two workers run these commands within the same second. What do they get?
# worker 1 SET lock:report "w1-8f3a" NX PX 30000 # worker 2 SET lock:report "w2-c41d" NX PX 30000- ABoth get
OK; the second overwrites the first - BWorker 1 gets
OK, worker 2 gets(nil) - CBoth get
(nil)until one callsEXPIRE - DWorker 2 blocks until worker 1 releases
Show answer
Answer: B (Worker 1 gets
OK, worker 2 gets(nil))NXsets the key only if it does not exist, atomically, so exactly one caller getsOKand the other gets a nil reply.PX 30000sets a 30-second expiry in the same command. Redis never blocks aSET; the loser must retry or give up. - ABoth get
- 11.mid
A worker acquires the lock, then runs a 45-second job. What can go wrong with this release?
ok = r.set("lock:report", token, nx=True, px=30000) if ok: run_report() # sometimes takes 45 s r.delete("lock:report")- ANothing: DEL is atomic
- BThe lock expired at 30 s, another worker took it, and this DEL removes their lock
- CDEL fails because the key has a TTL
- 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
DELthen 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.mid
Which
MSETsucceeds on Redis Cluster without aCROSSSLOTerror?- A
MSET user:1000:cart x user:1000:profile y - B
MSET {user:1000}:cart x {user:1000}:profile y - C
MSET user:{1000}:cart x order:{2000}:cart y - 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 hashuser:1000and share slot 1649. Without tags,user:1000:cart(slot 7185) anduser: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. - A
- 13.mid
A cluster client sends
GET fooand receives-MOVED 12182 10.0.0.3:6379. What should it do?- ASend
ASKING, then retry on 10.0.0.3 once without changing anything - BRetry on 10.0.0.3 and update its slot map so slot 12182 points there
- CWait for the migration to finish and retry the same node
- 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)
MOVEDmeans slot 12182 (the slot offoo) is permanently served by another node, so the client retries there and updates its routing table, usually by refreshing the whole map.ASKINGplus a one-off retry is the response to anASKredirect during a slot migration. - ASend
- 14.hard
An instance has
maxmemory 2gbandmaxmemory-policy volatile-lru. Memory is full and no key has a TTL. What happens on the nextSET?- ARedis evicts the least recently used key anyway
- BThe write fails with an OOM error
- CRedis grows past maxmemory and logs a warning
- 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 likenoevictionand rejects writes withOOM command not allowed when used memory > maxmemory. Useallkeys-lrufor a pure cache, or set TTLs on cache keys. - 15.easy
What is the default
maxmemory-policyin Redis 7.4 and 8?- A
allkeys-lru - B
volatile-lru - C
noeviction - D
allkeys-lfu
Show answer
Answer: C (
noeviction)The default is
noeviction: whenmaxmemoryis reached, writes return an OOM error while reads keep working. That is safe for data you cannot lose, but a cache should usually switch toallkeys-lruorallkeys-lfu. - A
- 16.mid
With
appendonly yesand the defaultappendfsync, roughly how much acknowledged data can a power loss destroy?- ANothing: every write is fsynced
- BAbout one second of writes
- CEverything since the last RDB snapshot
- 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.alwaysfsyncs every write batch;noleaves flushing to the OS, which is the 30-second case. - 17.easy
A support script runs
KEYS session:*against a production instance with 40 million keys. What is the risk?- ANone: KEYS runs in a background thread
- BIt blocks the server while it walks every key, stalling all clients
- CIt only returns the first 1,000 keys
- DIt deletes expired keys and can lose data
Show answer
Answer: B (It blocks the server while it walks every key, stalling all clients)
KEYSis O(N) over the whole keyspace and runs on the single command thread, so every other client waits. UseSCAN 0 MATCH session:* COUNT 1000and follow the cursor: it does a little work per call and returns every key that exists for the whole iteration at least once. - 18.mid
A subscriber to channel
ordersrestarts and is offline for 10 seconds while 500 messages are published. What does it receive when it resubscribes?- AAll 500 messages, in order
- BOnly messages published after it resubscribes
- CThe last message only
- 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
XADDand a consumer group, which remembers each consumer's position and pending entries. - 19.mid
Which statement about this redis-py pipeline is true?
pipe = r.pipeline(transaction=False) pipe.get("stock:42") pipe.decr("stock:42") pipe.execute()- ABoth commands run atomically, with nothing in between
- BThey go in one round trip, but another client's command may run between them
- CDECR is skipped if GET returns nil
- 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=Truewraps them inMULTI/EXECfor isolation, and conditional logic needsWATCHor a Lua script. Nothing is ever rolled back. - 20.hard
You must remove a set with 15 million members from a busy primary. Which command avoids a long stall of the main thread?
- A
DEL big:set - B
UNLINK big:set - C
EXPIRE big:set 0, which deletes nothing until read - D
FLUSHDB
Show answer
Answer: B (
UNLINK big:set)UNLINKremoves the key from the keyspace immediately and frees its memory in a background thread, so clients are not blocked.DELfrees all 15 million members synchronously (unlesslazyfree-lazy-user-delis enabled).EXPIREwith 0 does not wait for a read: it deletes the key at once. - A
No questions match these filters. Try a different subtopic or clear the filters.