샤딩을 위한 Redis Cluster
Redis Cluster를 구현해 여러 노드에 데이터를 분산하고 수평 확장성과 장애 내성을 확보합니다.
샤딩을 위한 Redis Cluster은(는) CoddyKit의 무료 Redis Caching & Messaging (Pub/Sub, Streams) 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Redis Caching & Messaging (Pub/Sub, Streams) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Scaling Beyond a Single Node
So far, we've learned about Redis replication and Sentinel for high availability. These are great for redundancy and read scaling, but what about write scaling or handling datasets larger than a single server's memory?
This is where Redis Cluster comes in. It's designed to provide horizontal scaling and fault tolerance by distributing your data across multiple Redis instances.
What is Sharding?
At its core, Redis Cluster uses sharding (also known as data partitioning). Imagine you have a massive library of books.
- Instead of one giant shelf, you split the books across many smaller shelves.
- Each shelf holds a portion of the books, and a librarian manages that shelf.
In Redis Cluster, each Redis instance (or node) acts like a 'librarian' managing a 'shelf' of your data. This allows you to scale both memory and CPU.
Redis Cluster Architecture
A Redis Cluster is a collection of interconnected Redis instances. Each instance can be a master or a replica.
- Master Nodes: These nodes hold and manage a portion of the dataset.
- Replica Nodes: These are copies of master nodes, providing high availability. If a master fails, one of its replicas can be promoted to take its place.
The cluster ensures data is distributed, and it can continue operating even if some nodes fail.
Hash Slots: How Data is Distributed
How does Redis know which node stores which piece of data? It uses hash slots.
- The entire dataset is divided into 16,384 logical slots (from 0 to 16383).
- Each master node in the cluster is responsible for a subset of these hash slots.
- When you store a key, Redis calculates a hash of the key to determine which slot it belongs to.
- This slot then maps to a specific master node.
This system allows for flexible data distribution and easy rebalancing.
Creating a Redis Cluster
To create a cluster, you need at least three master nodes for a fault-tolerant setup (each master can have replicas). Here's a conceptual CLI command using redis-cli:
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 --cluster-replicas 1
- This command creates a cluster with 3 masters (on ports 7000, 7001, 7002).
--cluster-replicas 1means each master will have one replica node.
The redis-cli tool automatically assigns hash slots to the master nodes.
Client Interaction & Redirection
When a client wants to interact with Redis Cluster, it can connect to any node. If the key the client requests doesn't belong to the connected node's hash slots, the node will respond with a MOVED redirection error.
Modern Redis client libraries (like Jedis for Java) are cluster-aware. They automatically handle these redirections by:
- Learning the cluster topology (which node owns which hash slots).
- Directly connecting to the correct node for a given key.
This makes interacting with a cluster surprisingly seamless for developers.
Connecting with a Java Client
Let's see how a Java client (using the Jedis library) connects to a Redis Cluster. The client needs to know at least one node in the cluster to discover the full topology.
Try running this example:
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.util.HashSet;
import java.util.Set;
public class RedisClusterClient {
public static void main(String[] args) {
// Provide at least one node to connect to the cluster
Set<HostAndPort> jedisClusterNodes = new HashSet<>();
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000)); // Example node
try (JedisCluster jc = new JedisCluster(jedisClusterNodes)) {
System.out.println("Connected to Redis Cluster!");
String key = "user:100:name";
String value = "Alice";
jc.set(key, value);
System.out.println("Set: " + key + " = " + value);
String retrievedValue = jc.get(key);
System.out.println("Get: " + key + " = " + retrievedValue);
// Client automatically handles sharding for different keys
jc.set("product:5:price", "99.99");
System.out.println("Set: product:5:price = " + jc.get("product:5:price"));
} catch (Exception e) {
System.err.println("Error connecting to Redis Cluster: " + e.getMessage());
System.err.println("Ensure a Redis Cluster is running on 127.0.0.1:7000");
}
}
}Resharding & Rebalancing
One of the powerful features of Redis Cluster is its ability to reshard. This means you can dynamically add or remove nodes from the cluster while it's running.
- When you add new master nodes, you can migrate hash slots from existing masters to the new ones.
- When you remove nodes, their hash slots can be moved to other remaining masters.
This allows for flexible scaling up or down of your cluster resources without downtime.
Fault Tolerance in Cluster
Redis Cluster provides robust fault tolerance:
- If a master node fails, its assigned replica is automatically promoted to become the new master.
- The cluster continues to operate, as the hash slots previously managed by the failed master are now handled by its promoted replica.
However, if a master and all its replicas fail, the portion of the data managed by that master's hash slots becomes unavailable, and the cluster might cease to operate unless configured otherwise (cluster-require-full-coverage no).
Check Your Cluster Knowledge
Which of the following are primary benefits or characteristics of Redis Cluster?
Recap: Redis Cluster for Scale
In this lesson, we explored Redis Cluster, a powerful solution for scaling Redis horizontally. We learned:
- Cluster uses sharding to distribute data across multiple master nodes.
- Hash slots determine which node stores which key.
- Client libraries are cluster-aware, handling redirections automatically.
- It offers fault tolerance by promoting replicas upon master failure.
- The ability to reshard allows for dynamic scaling.
Redis Cluster is your go-to when a single Redis instance can no longer meet your application's data size or throughput demands.
AI 튜터와 함께 Redis Caching & Messaging (Pub/Sub, Streams)을(를) 배우세요 — 무료
브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.
- 코스
- 12
- 레슨
- 48
자주 묻는 질문
“샤딩을 위한 Redis Cluster” 강의는 무료인가요?
네 — “샤딩을 위한 Redis Cluster” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Redis Caching & Messaging (Pub/Sub, Streams) 강의 전체를 잠금 해제할 수 있습니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.
“샤딩을 위한 Redis Cluster”에서 뭘 배우나요?
Redis Cluster를 구현해 여러 노드에 데이터를 분산하고 수평 확장성과 장애 내성을 확보합니다. 브라우저에서 직접 실행하는 실습 코드로 Redis Caching & Messaging (Pub/Sub, Streams)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Redis Caching & Messaging (Pub/Sub, Streams)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Redis Caching & Messaging (Pub/Sub, Streams)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“샤딩을 위한 Redis Cluster” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Redis Caching & Messaging (Pub/Sub, Streams) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 중복성을 위한 Redis 복제
- 고가용성을 위한 Redis Sentinel
- 샤딩을 위한 Redis Cluster
- 클라이언트 측 연결 복원력