{"record":{"id":"101157458dc6a3d5","repo":"redis/redis-rb","slug":"msetnx-cannot-be-used-in-redis-distributed-becaus","errorCode":null,"errorMessage":"MSETNX cannot be used in Redis::Distributed because the keys involved need to be on the same server or because we cannot guarantee that the operation will be atomic.","messagePattern":"MSETNX cannot be used in Redis::Distributed because the keys involved need to be on the same server or because we cannot guarantee that the operation will be atomic\\.","errorType":"exception","errorClass":"Redis::Distributed::CannotDistribute","httpStatus":null,"severity":"error","filePath":"lib/redis/distributed.rb","lineNumber":336,"sourceCode":"    end\n\n    # Set the value of a key, only if the key does not exist.\n    def setnx(key, value)\n      node_for(key).setnx(key, value)\n    end\n\n    # Set multiple keys to multiple values.\n    def mset(*)\n      raise CannotDistribute, :mset\n    end\n\n    def mapped_mset(_hash)\n      raise CannotDistribute, :mapped_mset\n    end\n\n    # Set multiple keys to multiple values, only if none of the keys exist.\n    def msetnx(*)\n      raise CannotDistribute, :msetnx\n    end\n\n    def mapped_msetnx(_hash)\n      raise CannotDistribute, :mapped_msetnx\n    end\n\n    # Get the value of a key.\n    def get(key)\n      node_for(key).get(key)\n    end\n\n    # Get the value of a key and delete it.\n    def getdel(key)\n      node_for(key).getdel(key)\n    end\n\n    # Get the value of a key and sets its time to live based on options.\n    def getex(key, **options)","sourceCodeStart":318,"sourceCodeEnd":354,"githubUrl":"https://github.com/redis/redis-rb/blob/2ba9010b91dab9e0fde1fbae3a9aae003f8bc307/lib/redis/distributed.rb#L318-L354","documentation":"Error \"MSETNX cannot be used in Redis::Distributed because the keys involved need to be on the same server or because we cannot guarantee that the operation will be atomic.\" thrown in redis/redis-rb.","triggerScenarios":"Thrown at lib/redis/distributed.rb:336 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Do not use msetnx on Redis::Distributed; the keys may hash to different servers and atomicity cannot be guaranteed.","Use set with nx: true per key, accepting that the operation is no longer atomic across keys."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"2ba9010b91dab9e0fde1fbae3a9aae003f8bc307","analyzedAt":"2026-08-23T03:54:57.017Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}