DEV Community

Roshan singh
Roshan singh

Posted on

Scaling Redis in Distributed Systems: From Local Cache Problems to Redis Cluster

**When building scalable backend systems, one common question arises:

“If we scale our application using multiple instances, how should caching work?”

This becomes especially important when using Redis in distributed architectures.

Let’s understand the problem step by step and then move toward advanced scaling concepts like sharding and replication.**


🚨 The Problem with Local Redis per Instance

Suppose we scale our backend application:

Load Balancer
     ↓
 ┌───────────┐
 │ Instance1 │
 └───────────┘
       │
   Local Redis 1

 ┌───────────┐
 │ Instance2 │
 └───────────┘
       │
   Local Redis 2
Enter fullscreen mode Exit fullscreen mode

At first glance, this looks fine.

But here’s the issue.

Imagine:

  • User data gets updated through Instance 2
  • Local Redis 2 gets updated
  • Local Redis 1 still contains old cached data

Now:

  • Requests routed to Instance 1 return stale data
  • Requests routed to Instance 2 return fresh data

This creates:

  • Cache inconsistency
  • Synchronization complexity
  • Stale reads

❌ Why Syncing Local Redis is Difficult

To keep multiple local Redis caches synchronized, we need:

  • Pub/Sub systems
  • Event broadcasting
  • Cache invalidation messaging
  • Distributed coordination

This becomes very complex at scale.

That’s why production systems usually avoid separate Redis instances per application server.


✅ The Correct Approach: Centralized Redis

Instead of separate caches, all backend instances use a shared Redis layer.

                        ┌─────────────────┐
Client → Load Balancer  → App Instances
                        └─────────────────┘
                              ↓
                     Shared Redis Cluster
                              ↓
                         PostgreSQL
Enter fullscreen mode Exit fullscreen mode

Now:

  • All instances read/write from the same Redis
  • Any update becomes immediately visible everywhere
  • No synchronization problem

Example:

await db.updateUser(id, data);
await redis.del(`user:${id}`);
Enter fullscreen mode Exit fullscreen mode

Once the cache is invalidated:

  • Any instance fetching the data gets the latest value

This architecture is simple, scalable, and production-friendly.


🤔 But What Happens When Redis Itself Needs Scaling?

A single Redis server has limitations:

  • Limited memory
  • Limited CPU
  • Single point of failure

For very large systems, we need:

  • Horizontal scaling
  • Fault tolerance
  • High availability

This is where Redis Cluster comes in.


🔥 Redis Cluster

A Redis Cluster distributes data across multiple Redis nodes.

It uses two important concepts:

  1. Sharding
  2. Replication

1️⃣ Sharding — Scaling Redis Horizontally

What is Sharding?

Sharding means:

Splitting data across multiple Redis nodes.

Instead of storing all data in one Redis server:

Redis Node 1
→ All users
→ All movies
→ All bookings
Enter fullscreen mode Exit fullscreen mode

Redis distributes keys across multiple nodes.

Example:

user:101    → Node 1
movie:201   → Node 2
booking:301 → Node 3
Enter fullscreen mode Exit fullscreen mode

Important:
Redis does NOT shard by entity type.

It shards based on:

  • Hash calculation of keys

Internally:

HASH(key) % 16384 = slot
Enter fullscreen mode Exit fullscreen mode

Each node owns a range of hash slots.

Example:

Node 1 → Slots 0–5000
Node 2 → Slots 5001–10000
Node 3 → Slots 10001–16383
Enter fullscreen mode Exit fullscreen mode

✅ Why Sharding is Important

Sharding helps with:

  • Memory scaling
  • Traffic distribution
  • Better performance
  • Horizontal scalability

Without sharding:

  • One Redis server becomes overloaded

2️⃣ Replication — High Availability & Failover

What is Replication?

Replication means:

Creating copies of Redis data on replica nodes.

Example:

Master 1 → Replica 1
Master 2 → Replica 2
Master 3 → Replica 3
Enter fullscreen mode Exit fullscreen mode

If:

Master 2 crashes
Enter fullscreen mode Exit fullscreen mode

Redis automatically promotes:

Replica 2 → New Master
Enter fullscreen mode Exit fullscreen mode

This process is called:

  • Automatic failover

✅ Why Replication is Needed

Sharding distributes data,
but it does NOT protect against node failure.

Without replication:

  • If a node crashes,
  • Data on that shard becomes unavailable

Replication provides:

  • High availability
  • Fault tolerance
  • Backup copies
  • Reduced downtime

⚔️ Sharding vs Replication

Feature Sharding Replication
Purpose Scalability High Availability
Data Split Copied
Increases Memory Capacity ✅ Yes ❌ No
Handles Node Failure ❌ No ✅ Yes
Improves Performance ✅ Yes Partial

🏗️ Final Production Architecture

Production systems usually combine both:

                Redis Cluster

Master 1 → Replica 1
Master 2 → Replica 2
Master 3 → Replica 3
Enter fullscreen mode Exit fullscreen mode

Here:

  • Masters handle sharded data
  • Replicas provide failover protection

🎯 Final Thoughts

When scaling distributed systems:

  • Avoid local Redis per instance
  • Use centralized Redis
  • Scale Redis using sharding
  • Ensure reliability using replication

This architecture helps achieve:

  • Low latency
  • High scalability
  • High availability
  • Fault tolerance

Which is exactly what modern distributed systems require.

Top comments (0)