Caching Strategies: Redis + CDN + Edge Computing · Lekcja

Redis Cluster i sharding

Poznaj sposób skalowania cache poziomo przez Redis Cluster na wielu węzłach z użyciem slotów haszujących, routing kluczy oraz grupowanie powiązanych kluczy za pomocą hash tags.

Lekcja 4 z 413 kroki

Redis Cluster i sharding to bezpłatna lekcja Caching Strategies: Redis + CDN + Edge Computing na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Caching Strategies: Redis + CDN + Edge Computing, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Caching Strategies: Redis + CDN + Edge Computing zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Bezpłatny start

Ucz się Caching Strategies: Redis + CDN + Edge Computing dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Redis Cluster i sharding” jest bezpłatna?

Tak — pełny tekst „Redis Cluster i sharding” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Caching Strategies: Redis + CDN + Edge Computing, przejdź na CoddyKit PRO. Kurs Caching Strategies: Redis + CDN + Edge Computing zawiera 4 lekcji w sumie.

Co nauczysz się w „Redis Cluster i sharding”?

Poznaj sposób skalowania cache poziomo przez Redis Cluster na wielu węzłach z użyciem slotów haszujących, routing kluczy oraz grupowanie powiązanych kluczy za pomocą hash tags. Ćwiczysz Caching Strategies: Redis + CDN + Edge Computing z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Caching Strategies: Redis + CDN + Edge Computing?

Nie wymagamy żadnego doświadczenia. Caching Strategies: Redis + CDN + Edge Computing w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Redis Cluster i sharding”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Caching Strategies: Redis + CDN + Edge Computing?

Tak. Każda lekcja Caching Strategies: Redis + CDN + Edge Computing zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Trwałość danych i wysoka dostępność Redis
  2. Rozproszone buforowanie za pomocą Redis
  3. Redis Pub/Sub do unieważniania pamięci podręcznej
  4. Redis Cluster i sharding
← Powrót do Caching Strategies: Redis + CDN + Edge Computing