0Pricing
Caching Strategies: Redis + CDN + Edge Computing · บทเรียน

Redis Cluster และการแบ่งชาร์ด

เรียนรู้ว่า Redis Cluster ขยายแคชในแนวนอนไปยังหลายโหนดด้วยช่องแฮชได้อย่างไร คีย์ถูกส่งไปยังโหนดใดอย่างไร และทำอย่างไรให้คีย์ที่เกี่ยวข้องกันอยู่รวมกันด้วยแท็กแฮช

Redis Cluster และการแบ่งชาร์ด เป็นบทเรียน Caching Strategies: Redis + CDN + Edge Computing ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.

คำถามที่พบบ่อย

บทเรียน “Redis Cluster และการแบ่งชาร์ด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “Redis Cluster และการแบ่งชาร์ด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Caching Strategies: Redis + CDN + Edge Computing ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Caching Strategies: Redis + CDN + Edge Computing มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “Redis Cluster และการแบ่งชาร์ด”

เรียนรู้ว่า Redis Cluster ขยายแคชในแนวนอนไปยังหลายโหนดด้วยช่องแฮชได้อย่างไร คีย์ถูกส่งไปยังโหนดใดอย่างไร และทำอย่างไรให้คีย์ที่เกี่ยวข้องกันอยู่รวมกันด้วยแท็กแฮช คุณปฏิบัติ Caching Strategies: Redis + CDN + Edge Computing ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Caching Strategies: Redis + CDN + Edge Computing หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Caching Strategies: Redis + CDN + Edge Computing บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “Redis Cluster และการแบ่งชาร์ด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Caching Strategies: Redis + CDN + Edge Computing นี้ได้ไหม

ได้ บทเรียน Caching Strategies: Redis + CDN + Edge Computing ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การคงอยู่ของข้อมูลและ HA ในรีดิส
  2. การแคชแบบกระจายด้วยรีดิส
  3. การเผยแพร่และสมัครรับข้อมูลในรีดิสเพื่อทำให้แคชเป็นโมฆะ
  4. Redis Cluster และการแบ่งชาร์ด
← กลับไปที่ Caching Strategies: Redis + CDN + Edge Computing