Q15 Redis Domain 2 · 25–30% of exam Priority · Real Exam

Which command atomically sets a key only if it does not exist, with a 30-second expiry, forming the basis of a distributed lock?

  1. A SET key value NX EX 30 (correct answer)
  2. B SETNX key value
  3. C SETEX key 30 value
  4. D MSETNX

Shortcut: SET with NX and EX in a single call is atomic. Separate SETNX and EXPIRE calls risk leaving a lock without a TTL if the client crashes between them.