DeployU
Interviews / Databases / How do you do sharding in Redis?

How do you do sharding in Redis?

practical Sharding Interactive Quiz Code Examples

The Scenario

You are a database administrator at a social media company. You are responsible for a Redis database that is growing very quickly. The database is starting to experience performance issues, and you have identified that the bottleneck is the single server that is hosting the database.

You need to find a way to scale the database horizontally to handle the increasing load.

The Challenge

Explain how you would do sharding in Redis. What is a sharded cluster, and what are the different components of a sharded cluster?

Wrong Approach

A junior engineer might not be aware of sharding. They might try to solve this problem by just adding more resources to the single server, which would not be a very scalable solution.

Right Approach

A senior engineer would know that sharding is a critical part of database administration. They would be able to explain what a sharded cluster is and would have a clear plan for how to set up sharding for a production database.

Step 1: Understand What a Sharded Cluster Is

A sharded cluster is a group of Redis servers that work together to store and process data. A sharded cluster provides horizontal scalability by distributing the data across multiple servers.

Step 2: The Different Components of a Sharded Cluster

ComponentDescription
ShardEach shard is a separate Redis server that stores a subset of the data.
Redis Cluster BusA communication bus that is used by the nodes in the cluster to exchange information about the state of the cluster.

Step 3: Set Up a Sharded Cluster

Here’s how we can set up a simple sharded cluster with three masters and three slaves:

1. Create the redis.conf files:

Create a separate redis.conf file for each node in the cluster.

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

2. Start the servers:

Start six Redis servers on different machines, each with its own redis.conf file.

3. Create the cluster:

Use the redis-cli tool to create the cluster.

redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 \
127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 \
--cluster-replicas 1

Hash Slots

Redis Cluster uses a concept called hash slots to distribute the data across the shards. There are 16384 hash slots in a Redis Cluster.

When you set a key, Redis calculates a hash of the key and then uses the result to determine which hash slot the key belongs to. It then sends the key to the shard that is responsible for that hash slot.

Practice Question

You are designing a sharded cluster for a new application. Which of the following would be the most important consideration when choosing a sharding strategy?