Nâng CaoRedis iconRedis

Redis Cluster: hash slots, MOVED/ASK redirects và cách sharding hoạt động?

Redis Cluster chia key space thành 16384 hash slots (0-16383).

  • Mỗi key được assign slot = CRC16(key) % 16384.
  • Cluster có N master nodes, mỗi master sở hữu một range slot (ví dụ: 3 masters mỗi master có ~5461 slots). MOVED redirect: khi client gửi request đến node sai (key không thuộc slot của node đó), node trả về MOVED <slot> <ip:port> — client phải resend đến đúng node.
  • Smart client cache slot map để tránh redirect (redis-py, Jedis, ioredis tự handle). ASK redirect: trong quá trình resharding (migrate slot giữa nodes), một số key đã migrate, số khác chưa.
  • Node cũ trả ASK <slot> <new_node> cho key đã migrate — client gửi ASKING + request đến node mới một lần (không cache như MOVED). Key tags: {user_id}.orders — chỉ hash user_id phần trong {}, đảm bảo keys liên quan cùng slot để dùng multi-key commands.
  • Cluster yêu cầu ít nhất 6 nodes (3 master + 3 replica) để đảm bảo HA.

Xem toàn bộ Redis cùng filter theo level & chủ đề con.

Mở danh sách Redis