skip to content

How do you inspect which hash slots each Redis Cluster node owns, verify that two specific keys will be served by the same node, and how does a client library know where to send a command?

level: seniorimportance: should knowfreq 40%

answer

  1. CLUSTER SHARDS (7.0+) > CLUSTER SLOTS (legacy)
  2. CLUSTER NODES = raw view + migration markers
  3. CLUSTER KEYSLOT proves co-location
  4. COUNTKEYSINSLOT / GETKEYSINSLOT explain a fat slot
  5. clients cache a 16384-entry map, compute slots locally

basics

~20 s

Use CLUSTER SHARDS (or the older CLUSTER SLOTS) for slot-range ownership, CLUSTER NODES for the raw view, and CLUSTER KEYSLOT on each key to compare slots. Smart clients fetch that map once, cache it, compute slots locally with CRC16, and refresh when the server says a slot moved.

solid answer

~50 s

**Ownership:** `CLUSTER SHARDS` (Redis 7.0+) returns each shard with its slot ranges and the health and role of every node in it; `CLUSTER SLOTS` is the older, flatter form kept for compatibility. `CLUSTER NODES` gives the raw gossip view - node id, address, flags, master/replica links, slot ranges, and migration markers. `CLUSTER INFO` summarises state and how many slots are assigned. **Key placement:** `CLUSTER KEYSLOT <key>` runs the same CRC16-and-hash-tag rule the server uses; equal results prove co-location. `CLUSTER COUNTKEYSINSLOT <slot>` and `CLUSTER GETKEYSINSLOT <slot> <count>` inspect what actually lives there. **Clients:** a cluster-aware client bootstraps from any seed node, fetches the slot map, and caches it. For each command it computes the slot locally from the key and dials the owning master directly - there is no proxy, so latency matches a standalone instance. When the server reports that the slot has moved, the client retries against the indicated node and refreshes its map.

code

text · 16 lines
text
> CLUSTER INFO | head -3
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384

> CLUSTER SHARDS            # 7.0+: slot ranges + node roles/health per shard
> CLUSTER NODES             # raw view; look for [slot->-<id>] migration markers

> CLUSTER KEYSLOT "{acct:42}:balance"
(integer) 8461
> CLUSTER KEYSLOT "{acct:42}:ledger"
(integer) 8461      # same slot -> same node
> CLUSTER COUNTKEYSINSLOT 8461
(integer) 2

$ redis-cli --cluster check 127.0.0.1:7000   # validates full, unique slot coverage

go deeper

for a junior

Know the commands by name - CLUSTER KEYSLOT for a key, CLUSTER SHARDS or CLUSTER NODES for ownership.

for a middle

Add that clients cache a 16384-entry slot map and compute slots locally, and that SCAN/DBSIZE are per-node.

for a senior

Show diagnostic judgment: cross-check nodes because each reports its own belief, use COUNTKEYSINSLOT/GETKEYSINSLOT to explain skew, and validate coverage with --cluster check.

for a principal

Discuss the client-assisted design - map as cache, server as authority - and what that implies for client library choice, refresh storms, and observability tooling across shards.

## The three views of cluster topology ### CLUSTER SHARDS (Redis 7.0 and later) The modern command. It returns one entry per shard: the slot ranges the shard owns, plus a list of its nodes with id, endpoint, port, role (master/replica), replication offset and health (`online`, `failed`, `loading`). Grouping by shard - rather than by slot range as the old command did - makes it natural to see a master together with its replicas, which is what most tooling actually wants. ### CLUSTER SLOTS (legacy) Returns an array of `[start, end, master, replica...]` tuples. It is deprecated in favour of `CLUSTER SHARDS` but still widely produced and consumed because older client libraries speak it. It carries no health field, which is precisely why it was superseded. ### CLUSTER NODES The raw text dump of the node's own view of the cluster: one line per node with `<id> <ip:port@busport> <flags> <master-id> <ping-sent> <pong-recv> <config-epoch> <link-state> <slot ranges>`. Two details make it the debugging tool of choice. First, flags show `myself`, `master`, `slave`, `fail?` (suspected) and `fail` (agreed). Second, slot entries can carry migration markers such as `[slot-><-nodeid]` (importing) and `[slot->-nodeid]` (migrating), which is how you see an in-flight slot move. Importantly, each of these commands reports **that node's belief** about the cluster. During a partition or a failover, different nodes can disagree transiently; if a diagnosis surprises you, ask a second node before concluding anything. ### CLUSTER INFO Summary counters: `cluster_state` (ok/fail), `cluster_slots_assigned`, `cluster_slots_ok`, `cluster_slots_pfail`, `cluster_slots_fail`, `cluster_known_nodes`, `cluster_size` (masters serving at least one slot), and epoch/stats fields. It is the fastest health probe to script. ## Verifying placement of specific keys `CLUSTER KEYSLOT <key>` asks the server to apply the exact rule - CRC16 over the key, or over the hash tag if present - and return the slot. Comparing the result for two keys is the authoritative co-location check; do not eyeball key names, because tag-parsing edge cases (empty `{}`, nested braces, a `}` before the `{`) are easy to get wrong. Watch shell quoting: unquoted braces may be mangled before they reach the server. Once you know the slot, `CLUSTER COUNTKEYSINSLOT <slot>` returns how many keys the queried node holds in it, and `CLUSTER GETKEYSINSLOT <slot> <count>` samples the actual names - invaluable when you are hunting the key family that made one slot enormous. Both must be run against the node that owns the slot. For cluster-wide sweeps, remember that `SCAN`, `KEYS`, `DBSIZE` and `INFO keyspace` are per-node: to survey the whole keyspace you iterate over every master. `redis-cli --cluster call <node> <command>` and `redis-cli --cluster info/check` automate the fan-out and also validate that all 16384 slots are covered exactly once. ## How a client library routes A cluster-aware ("smart") client works like this: 1. **Bootstrap.** Connect to any seed node and issue `CLUSTER SHARDS` (or `CLUSTER SLOTS`). Build an in-memory array of 16384 entries mapping slot to a connection pool for the owning master, plus its replicas. 2. **Route.** For each command, extract the key positions (from the command's arity metadata or a built-in table), compute `CRC16 mod 16384` locally applying hash-tag rules, and send the command straight to the owning master over a persistent connection. Because routing is local, a cluster command costs one round trip, just like standalone Redis - there is no proxy hop. 3. **Adapt.** If the slot map is stale, the contacted node replies with a redirect naming the correct node. The client follows it and schedules a map refresh. (The precise redirect semantics during migration are the resharding topic's material; what matters here is that the map is a *cache* and the server is the authority.) 4. **Multi-key and pipelines.** Since all keys of one command must share a slot, clients validate that up front; for pipelines they group commands per node and fan out, so a batch touching many slots becomes a handful of parallel round trips. 5. **Replica reads.** Optional and opt-in; the map already contains the replicas from the shard listing. ## Practical checklist - Health probe: `CLUSTER INFO` -> `cluster_state:ok` and `cluster_slots_assigned:16384`. - Ownership: `CLUSTER SHARDS`, cross-checked with `CLUSTER NODES` on a second node when something looks odd. - Placement: `CLUSTER KEYSLOT` per key; `COUNTKEYSINSLOT`/`GETKEYSINSLOT` to explain a fat slot. - Whole-cluster validation: `redis-cli --cluster check <host:port>`. - Remember every per-node command needs fan-out to be cluster-wide.

  • You run CLUSTER NODES on two different nodes and get different pictures. What does that tell you?
    Each node reports its own gossiped belief, so a transient disagreement means the cluster is converging - typically during a failover, a slot migration, or a partition where one side has marked a node pfail/fail and the other has not. Query several nodes, and use redis-cli --cluster check to test whether coverage is actually consistent before acting.
  • Why can't you just run SCAN once to list all keys in a cluster?
    SCAN, KEYS, DBSIZE and INFO keyspace are node-local: each master only knows its own slots. A cluster-wide sweep means iterating SCAN independently against every master, which cluster-aware tooling such as redis-cli --cluster call does for you.

saying these in an interview costs you the question

  • Assuming CLUSTER SLOTS or CLUSTER NODES output is globally authoritative rather than one node's belief
  • Thinking a proxy resolves the node so clients need no slot map
  • Running SCAN or DBSIZE on one node and reporting it as the cluster total
  • Computing co-location by inspecting key names instead of comparing CLUSTER KEYSLOT results
  • Believing the client re-fetches the slot map on every command

context