🔴 Redis Intermediate

How do you work around cross-slot transaction limitations in Redis Cluster?

Answer

Redis Cluster restricts multi-key commands (MGET, MSET, SUNION, MULTI/EXEC with multiple keys) to keys that reside in the same hash slot. The primary workaround is hash tags: enclose the common part of the key name in {} — e.g., {user:42}:profile and {user:42}:settings both hash on user:42, guaranteeing they land on the same slot. This allows atomic multi-key operations on logically related data. Alternative approaches: (1) redesign to avoid multi-key operations, using a single Hash for related fields instead of multiple keys. (2) Use Lua scripts — EVAL can access multiple keys only if they all hash to the same slot (enforced by Cluster). (3) Use Redis Cluster's WAIT and application-level coordination for operations that genuinely span slots, accepting eventual consistency.