0
0
Redisquery~5 mins

SETNX for set-if-not-exists in Redis - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the Redis command SETNX do?

SETNX sets a key to a value only if the key does not already exist in the database.

Click to reveal answer
beginner
What will SETNX mykey "hello" return if mykey already exists?

It will return 0, meaning the key was not set because it already exists.

Click to reveal answer
intermediate
How can SETNX be useful in real-life applications?

It can be used to create locks or ensure a value is set only once, like setting a unique user ID or a one-time token.

Click to reveal answer
beginner
What is the difference between SET and SETNX in Redis?

SET always sets the key to the value, overwriting if it exists. SETNX sets the key only if it does not exist.

Click to reveal answer
beginner
If SETNX returns 1, what does that mean?

It means the key was successfully set because it did not exist before.

Click to reveal answer
What does the Redis command SETNX key value do?
ADeletes the key if it exists
BAlways sets the key to the value
CReturns the current value of the key
DSets the key only if it does not exist
What does SETNX return if the key already exists?
Anull
B1
C0
Derror
Which Redis command would you use to set a key only if it does not exist?
AGET
BSETNX
CSET
DDEL
Why might you use SETNX in a distributed system?
ATo create a lock preventing race conditions
BTo get the value of a key
CTo always overwrite values
DTo delete keys safely
If you want to set a key regardless of its existence, which command should you use?
ASET
BSETNX
CGET
DEXISTS
Explain how the Redis command SETNX works and give an example of when you might use it.
Think about setting a value only once.
You got /3 concepts.
    What is the difference between SET and SETNX in Redis?
    Consider what happens if the key already exists.
    You got /3 concepts.