A hot key in Redis is a single key that is accessed far more frequently than any other key. This creates a bottleneck because all read and write operations for that key are directed to a single Redis instance (the master, or a specific replica if read-only traffic is already sharded), overwhelming its CPU and memory.

Let’s see this in action. Imagine a leaderboard for a popular game. Every time a player scores, their score is updated, and the leaderboard is read to display the top players. If this game has millions of active players, the leaderboard:global key will be hammered.

Here’s a simplified view of how a typical Redis cluster without hot key mitigation might handle this:

Client A (Player 1 scores) -> Redis Master (writes `leaderboard:global`)
Client B (Player 2 scores) -> Redis Master (writes `leaderboard:global`)
Client C (Player 3 scores) -> Redis Master (writes `leaderboard:global`)

Client D (Player 1 reads leaderboard) -> Redis Master (reads `leaderboard:global`)
Client E (Player 2 reads leaderboard) -> Redis Master (reads `leaderboard:global`)
Client F (Player 3 reads leaderboard) -> Redis Master (reads `leaderboard:global`)

Notice how all traffic, reads and writes, funnels to the Redis Master. If the master can’t keep up, latency spikes for everyone, and operations start timing out.

The core problem is that Redis, by default, treats every key independently. It doesn’t inherently understand that one key is "hotter" than others and needs special handling.

To mitigate hot keys, we need to distribute the load associated with that key. Since writes must go to the master, we can’t directly shard writes of a single hot key across multiple masters. However, we can significantly alleviate the read pressure on the master by directing read traffic for that hot key to replicas.

Here’s the strategy:

  1. Identify the Hot Key: This is the first and often hardest step. You need monitoring tools that can show you key-level access patterns. Redis Enterprise’s built-in "Top Keys" feature or open-source tools like redis-cli --hot-keys (available in newer versions) are essential. Let’s assume you’ve identified leaderboard:global as your hot key.

  2. Configure Read-Only Replicas: Ensure your Redis cluster has replicas configured. A common setup is one master with at least one replica. In Redis Sentinel or Cluster, replicas are automatically managed.

  3. Client-Side Read Routing: This is where the magic happens. Your application clients need to be aware of the hot key and intelligently direct read requests for only that key to the replicas. Write requests for the hot key still go to the master.

    • How it works mechanically: When your application code sees a request for leaderboard:global, it checks if it’s a read operation. If it is, it picks one of the available read replicas (e.g., round-robin) and sends the GET or ZRANGE command there. If it’s a write operation (e.g., ZADD), it sends it to the master.

    • Example (Conceptual Python):

      import redis
      
      # Assume these are configured and accessible
      master_host = 'redis-master.example.com'
      replica_hosts = ['redis-replica-1.example.com', 'redis-replica-2.example.com']
      hot_key = 'leaderboard:global'
      
      def get_redis_connection(is_write=False):
          if is_write:
              return redis.StrictRedis(host=master_host, port=6379, db=0)
          else:
              # Simple round-robin for read replicas
              import random
              replica_host = random.choice(replica_hosts)
              return redis.StrictRedis(host=replica_host, port=6379, db=0)
      
      def get_leaderboard():
          conn = get_redis_connection(is_write=False)
          # This command is for a sorted set, common for leaderboards
          return conn.zrange(hot_key, 0, 9, desc=True)
      
      def update_score(player_id, score):
          conn = get_redis_connection(is_write=True)
          conn.zadd(hot_key, {f'player:{player_id}': score})
      
  4. Application Logic for Writes: Writes to the hot key must go to the master for consistency. If you’re using Redis Cluster, the client library or proxy handles this redirection automatically based on the key’s hash slot. For Sentinel or a standalone master/replica setup, your client code needs to explicitly know the master’s address for writes.

    • Important Note on Consistency: Reads from replicas are eventually consistent. There can be a small lag between a write to the master and that write appearing on the replicas. For a leaderboard, this is usually acceptable. If strict consistency is required for reads, this approach won’t work for the hot key itself.
  5. Cache Hot Key Data on Application Servers (Optional but Recommended): For extremely hot keys, even distributing reads across replicas might not be enough. Consider caching the results of frequently executed read commands for the hot key directly in your application servers’ memory. When a write occurs, invalidate the cache.

    • How it works mechanically: Your application reads the hot key. If it’s in the local cache, serve from there. If not, fetch from Redis (replica), populate the cache, and then serve. On a write, mark the cache entry as stale or remove it. This dramatically reduces Redis read load.

The most surprising aspect of hot key mitigation is how much you can offload by just being smart about read operations. You don’t need to re-architect your entire data model or shard every key; often, a targeted approach for a few critical keys is sufficient. The key is understanding that replicas are excellent for read-heavy workloads, and your application can act as a smart proxy to leverage them.

Once you’ve successfully offloaded read traffic for leaderboard:global to your replicas, you might notice that your other, less-hot keys are now also being served from the master, and the master’s CPU usage has dropped. The next challenge you’ll likely encounter is optimizing the management and discovery of these hot keys as your application scales and more keys become candidates for this treatment.

Want structured learning?

Take the full Redis course →