Redis Caching & Messaging (Pub/Sub, Streams) · 课时

客户端连接韧性

通过重试、超时、连接池和感知集群的重定向处理,让客户端能够经受故障转移和拓扑变化

第 4 / 4 课13 个步骤

客户端连接韧性 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Redis Caching & Messaging (Pub/Sub, Streams) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

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

HA Is a Two-Sided Deal

You configured replication, Sentinel, and Cluster on the server. But high availability only works if the client reacts correctly to failovers, moved slots, and dropped connections. This lesson covers client-side resilience.

Connection Pools

Opening a TCP connection per command is slow. A connection pool reuses a set of connections across requests, bounded by a maximum size to protect the server.

pool = redis.ConnectionPool(max_connections=50)
client = redis.Redis(connection_pool=pool)

Timeouts Matter

Without timeouts, a stalled node can hang your whole app. Set both a connect timeout and a socket/command timeout so failed nodes fail fast.

client = redis.Redis(socket_connect_timeout=2, socket_timeout=2)

Retries with Backoff

Transient errors (a brief failover) should be retried, ideally with exponential backoff to avoid hammering a recovering node.

for attempt in range(3):
    try:
        return client.get('key')
    except ConnectionError:
        time.sleep(2 ** attempt)

Sentinel-Aware Clients

With Sentinel, the client asks Sentinel for the current master address rather than hardcoding it. After a failover, the client re-queries and reconnects to the new master.

from redis.sentinel import Sentinel
s = Sentinel([('s1', 26379)], socket_timeout=0.5)
master = s.master_for('mymaster')

Reading from Replicas

For read-heavy workloads, route reads to replicas to offload the master. Be aware replicas may be slightly behind (eventual consistency).

replica = s.slave_for('mymaster')
value = replica.get('key')

Handling MOVED in Cluster

In a cluster, a key may live on a different node. The server replies MOVED with the correct node. A cluster-aware client follows the redirect and updates its slot map.

# (error) MOVED 3999 127.0.0.1:7002

Handling ASK Redirects

During slot migration the server may reply ASK, a one-time redirect. The client should send ASKING then the command to the target node, without permanently updating its slot map.

# (error) ASK 3999 127.0.0.1:7003
# client sends ASKING then retries on 7003

Refreshing the Topology

Cluster-aware clients periodically refresh their slot-to-node map and on receiving redirects, so they keep routing to the right node as the cluster reshards.

CLUSTER SLOTS
CLUSTER SHARDS

Idempotency and Retries

Retrying writes is risky if the first attempt actually succeeded. Prefer idempotent operations (SET, INCR with a dedup key) so a retry cannot double-apply an effect.

Putting It Together

Resilient clients combine pools, timeouts, backoff retries, Sentinel/cluster awareness, replica reads where safe, and idempotent writes. Together they turn server-side HA into end-to-end availability.

Quick Check

Test your understanding of client resilience.

Recap

You learned client-side resilience: connection pools, connect and command timeouts, exponential backoff retries, Sentinel-aware master discovery, replica reads, and handling MOVED/ASK redirections in a cluster. Combine these with idempotent writes for true end-to-end availability.

免费开始

用 AI 导师学习 Redis Caching & Messaging (Pub/Sub, Streams) — 免费

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

课程
12
课程
48

常见问题解答

「客户端连接韧性」课时是免费的吗?

是的 — 「客户端连接韧性」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

「客户端连接韧性」这节课中我会学到什么?

通过重试、超时、连接池和感知集群的重定向处理,让客户端能够经受故障转移和拓扑变化 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「客户端连接韧性」课时需要多长时间?

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

我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?

能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 Redis 复制实现冗余
  2. 使用 Redis Sentinel 实现高可用
  3. 使用 Redis Cluster 实现分片
  4. 客户端连接韧性
← 返回 Redis Caching & Messaging (Pub/Sub, Streams)