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

Ketahanan Koneksi di Sisi Klien

Buat klien tetap berjalan saat terjadi failover dan perubahan topologi dengan percobaan ulang, batas waktu, kumpulan koneksi, serta penanganan pengalihan yang memahami klaster.

Pelajaran 4 dari 413 langkah

Ketahanan Koneksi di Sisi Klien adalah pelajaran Redis Caching & Messaging (Pub/Sub, Streams) gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Redis Caching & Messaging (Pub/Sub, Streams), dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Redis Caching & Messaging (Pub/Sub, Streams) mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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.

Gratis untuk memulai

Belajar Redis Caching & Messaging (Pub/Sub, Streams) dengan tutor AI — gratis

Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.

Kursus
12
Pelajaran
48

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Ketahanan Koneksi di Sisi Klien” gratis?

Ya — teks lengkap “Ketahanan Koneksi di Sisi Klien” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Redis Caching & Messaging (Pub/Sub, Streams), upgrade ke CoddyKit PRO. Kursus Redis Caching & Messaging (Pub/Sub, Streams) mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Ketahanan Koneksi di Sisi Klien”?

Buat klien tetap berjalan saat terjadi failover dan perubahan topologi dengan percobaan ulang, batas waktu, kumpulan koneksi, serta penanganan pengalihan yang memahami klaster. Kamu berlatih Redis Caching & Messaging (Pub/Sub, Streams) dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Redis Caching & Messaging (Pub/Sub, Streams)?

Tidak diperlukan pengalaman sebelumnya. Redis Caching & Messaging (Pub/Sub, Streams) di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.

Berapa lama pelajaran “Ketahanan Koneksi di Sisi Klien” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Redis Caching & Messaging (Pub/Sub, Streams) ini?

Ya. Setiap pelajaran Redis Caching & Messaging (Pub/Sub, Streams) menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Replikasi Redis untuk Redundansi
  2. Redis Sentinel untuk Ketersediaan Tinggi
  3. Redis Cluster untuk Sharding
  4. Ketahanan Koneksi di Sisi Klien
← Kembali ke Redis Caching & Messaging (Pub/Sub, Streams)