Search code examples
redisredis-sentinel

Is the SETNX always executed on redis master in the sentinel system?


wanted to ask

  1. if the SETNX command (both the NX test and the SET) guaranteed to execute on the redis master in the context of the "Redis Sentinel System" ?
  2. if it is guaranteed to to be atomic in the context of the "Redis Sentinel System"?

My understanding after reading the documentation is YES to both, because:

  • Ad. 1 only the master can accept writes and since SETNX has a set/write component it has to go to the master (because all writes go through the master).
  • Ad. 2 since the SET will be executed on the master it only make sense to check the NX part also on the master (no slaves queried, ever), otherwise it would be unnecessarily time consuming and could undermine atomicity.

Can someone confirm with 100% certainty, maybe point me to some documentation that clears my doubts. Thanks in advance!


Solution

  • I can confirm the above with %99.97 (3 sigmas) certainty.

    Ad. 1 only the master can accept writes and since SETNX has a set/write component it has to go to the master (because all writes go through the master).

    Correct, excluding the scenario where you deliberately enable writing to replicas and connect to a replica.

    Ad. 2 since the SET will be executed on the master it only make sense to check the NX part also on the master (no slaves queried, ever), otherwise it would be unnecessarily time consuming and could undermine atomicity.

    Yep.