Caching Strategies: Redis + CDN + Edge Computing · 课时

Redis 集群与分片

了解 Redis Cluster 如何利用哈希槽将缓存水平扩展到多个节点、如何路由键,以及如何使用哈希标签让相关键保持在一起。

第 4 / 4 课13 个步骤

Redis 集群与分片 是 CoddyKit 上的免费 Caching Strategies: Redis + CDN + Edge Computing 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Caching Strategies: Redis + CDN + Edge Computing 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Caching Strategies: Redis + CDN + Edge Computing 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Scale Beyond One Node?

A single Redis instance is limited by one server's memory and throughput. Redis Cluster spreads data across many nodes so the cache can grow far beyond a single machine.

Hash Slots

Redis Cluster divides the keyspace into 16384 hash slots. Each node owns a range of slots. A key belongs to exactly one slot, determined by hashing the key.

TOTAL_SLOTS = 16384

def slot_for(crc):
    return crc % TOTAL_SLOTS

print('Slot:', slot_for(123456789))

CRC16 Routing

The slot is computed as CRC16(key) mod 16384. The client (or cluster) uses this to know which node holds the key, so reads and writes go directly to the right place.

Slot Distribution

With three nodes, slots split roughly into thirds. Adding a node means moving some slots to it; removing one redistributes its slots. This is how the cluster rebalances.

nodes = 3
per_node = 16384 // nodes
print('Approx slots per node:', per_node)

MOVED and ASK Redirects

If a client contacts the wrong node, the node replies with a MOVED redirect pointing to the correct node. During slot migration, a temporary ASK redirect is used. Smart clients cache the slot map.

The Multi-Key Limitation

Operations across multiple keys (like MGET or transactions) only work if all keys live in the same slot. Otherwise the cluster rejects the command, because the keys may be on different nodes.

Hash Tags

Hash tags solve this. Only the part inside curly braces is hashed, so keys sharing a tag land in the same slot. user:{42}:profile and user:{42}:cart are co-located.

def hash_part(key):
    if '{' in key and '}' in key:
        start = key.index('{') + 1
        end = key.index('}')
        return key[start:end]
    return key

print(hash_part('user:{42}:cart'))

Replicas for Availability

Each master node can have replicas. If a master fails, a replica is promoted automatically, so the slot range stays available. This combines sharding with high availability.

Cluster vs Client-Side Sharding

Before Redis Cluster, apps sharded manually by hashing keys to instances in client code. Cluster mode moves that logic into Redis itself, handling rebalancing and failover automatically.

Trade-offs of Clustering

Clustering adds power but also constraints: cross-slot operations are limited, multi-key Lua scripts need same-slot keys, and operations are more complex. Use it when a single node truly is not enough.

When to Cluster

Reach for Redis Cluster when memory or throughput exceeds one node, when you need automatic failover at scale, or when a region demands many nodes. For smaller caches, a single primary with a replica is simpler.

Quick Check

You need MGET to fetch two related keys atomically in a Redis Cluster. How do you guarantee they live on the same node?

Recap

You learned Redis Cluster and sharding:

  • The keyspace splits into 16384 hash slots owned by nodes.
  • CRC16 routing plus MOVED/ASK redirects locate keys.
  • Multi-key ops need same-slot keys; hash tags co-locate them.
  • Replicas add automatic failover.

Clustering scales a cache horizontally when one node is not enough.

免费开始

用 AI 导师学习 Caching Strategies: Redis + CDN + Edge Computing — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「Redis 集群与分片」课时是免费的吗?

是的 — 「Redis 集群与分片」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Caching Strategies: Redis + CDN + Edge Computing 课程的其余内容,请升级到 CoddyKit PRO。 Caching Strategies: Redis + CDN + Edge Computing 课程共包含 4 节课。

「Redis 集群与分片」这节课中我会学到什么?

了解 Redis Cluster 如何利用哈希槽将缓存水平扩展到多个节点、如何路由键,以及如何使用哈希标签让相关键保持在一起。 你通过在浏览器中直接运行的动手代码来练习 Caching Strategies: Redis + CDN + Edge Computing,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Caching Strategies: Redis + CDN + Edge Computing 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Caching Strategies: Redis + CDN + Edge Computing 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「Redis 集群与分片」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Caching Strategies: Redis + CDN + Edge Computing 课中编写并运行代码吗?

能。每节 Caching Strategies: Redis + CDN + Edge Computing 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Redis 持久化与 HA
  2. 使用 Redis 实现分布式缓存
  3. 使用 Redis 发布/订阅实现缓存失效
  4. Redis 集群与分片
← 返回 Caching Strategies: Redis + CDN + Edge Computing