<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gaurav Rathor</title>
    <description>The latest articles on DEV Community by Gaurav Rathor (@gaurav_rathor_c31123d3b4a).</description>
    <link>https://dev.to/gaurav_rathor_c31123d3b4a</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1763571%2Fc5cc1302-274d-4c87-8099-350d6075c662.jpg</url>
      <title>DEV Community: Gaurav Rathor</title>
      <link>https://dev.to/gaurav_rathor_c31123d3b4a</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gaurav_rathor_c31123d3b4a"/>
    <language>en</language>
    <item>
      <title>Redis Cluster Topology That Saves CPUs</title>
      <dc:creator>Gaurav Rathor</dc:creator>
      <pubDate>Thu, 30 Oct 2025 04:43:23 +0000</pubDate>
      <link>https://dev.to/gaurav_rathor_c31123d3b4a/redis-cluster-topology-that-saves-cpus-4d7a</link>
      <guid>https://dev.to/gaurav_rathor_c31123d3b4a/redis-cluster-topology-that-saves-cpus-4d7a</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Redis is a popular in-memory data store that has become an essential component of many modern applications. With its high performance, scalability, and reliability features, Redis has emerged as a top choice for caching, session management, and other use cases. In this article, we'll explore the deployment topology of Redis Cluster, specifically focusing on the master-replica approach utilizing all the cores on the vms, leveraging the single threaded behaviour of redis.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Is a Redis Cluster?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Redis Cluster is a distributed deployment that shards your dataset across multiple Redis nodes. It automatically handles data partitioning and replication, ensuring both high availability and horizontal scalability.&lt;/p&gt;

&lt;p&gt;Each cluster node manages a subset of hash slots, allowing the system to distribute data and load efficiently. When combined with replicas, Redis Cluster provides fault tolerance and performance benefits ideal for high-traffic workloads.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Typical Master-Replica Deployment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a standard Redis deployment, a master node handles all write operations while replica nodes replicate data and handle read operations.&lt;/p&gt;

&lt;p&gt;For example, a 3-node setup might consist of 1 master and 2 replicas.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Pros:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Availability: If the master fails, one of the replicas is automatically promoted.&lt;/li&gt;
&lt;li&gt;Fault Tolerance: Adding more replicas increases redundancy.&lt;/li&gt;
&lt;li&gt;Load Distribution: Reads and writes are separated across nodes for efficiency.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Cons:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limited scalability for writes, since only one master handles them.&lt;/li&gt;
&lt;li&gt;Under utilization of CPU resources, as Redis uses a single thread per process.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Multi-Master Cluster Deployment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To overcome the write scalability issue, Redis supports multiple masters, each with their own set of replicas.&lt;/p&gt;

&lt;p&gt;Let’s consider a 3-master and 2-replica setup, totaling 9 VMs.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Pros:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sharding: Each master handles a unique keyspace segment — distributing load effectively.&lt;/li&gt;
&lt;li&gt;High Write Throughput: Multiple masters process writes concurrently.&lt;/li&gt;
&lt;li&gt;Fault Isolation: A single master failure impacts only a subset of keys.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Cons:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Infrastructure Cost: Requires significantly more VMs or instances.&lt;/li&gt;
&lt;li&gt;Operational Complexity: Managing hash slot rebalancing and failover increases overhead.&lt;/li&gt;
&lt;li&gt;Potential Imbalance: Uneven key distribution can cause hotspots.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Optimized Deployment: CPU-Aware Containerized Redis Cluster&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To reduce infrastructure cost while maintaining performance, we can consolidate Redis masters and replicas on fewer VMs by using Docker Swarm and CPU pinning.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deployment Strategy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Three VMs, each with 4 CPU cores.&lt;br&gt;
Docker Swarm configured across all three VMs.&lt;br&gt;
On each VM, run three Redis containers:&lt;br&gt;
One master container (unique per VM).&lt;br&gt;
Two replica containers — each replicating masters from the other VMs.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In this topology:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Each Redis process (master or replica) runs on a dedicated CPU core.&lt;/p&gt;

&lt;p&gt;Redis’ single-threaded design ensures one vCPU per instance provides optimal performance.&lt;/p&gt;

&lt;p&gt;Each VM uses:&lt;br&gt;
1 core for its master.&lt;br&gt;
2 cores for replicas of the other masters.&lt;br&gt;
1 core for system operations.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Advantages:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CPU Efficiency: Fully utilizes available cores without over provisioning.&lt;/li&gt;
&lt;li&gt;Cost Optimization: Achieves multi-master performance using only 3 VMs instead of 9.&lt;/li&gt;
&lt;li&gt;Simplified Management: Fewer VMs to monitor, patch, and secure.&lt;/li&gt;
&lt;li&gt;Same High Availability: Replication ensures data redundancy across hosts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Implementation specific:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a docker swarm with those 3 vms&lt;/li&gt;
&lt;li&gt;Create a docker overlay network. (&lt;code&gt;--attachable&lt;/code&gt; flag, so that standalone containers can attach to this network)&lt;/li&gt;
&lt;li&gt;Open following ports on all the nodes: redis port to serve client (6379) and cluster bus port (16379, 16380, 16381)&lt;/li&gt;
&lt;li&gt;Next, create the docker redis container on all the vms, using the overlay network and exposing the ports, on all the 3 vms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc3qnluf8bd33u1ihzy7m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc3qnluf8bd33u1ihzy7m.png" alt="Redis Cluster Topology Diagram" width="800" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the docker redis container are up, we can create redis cluster using below command from any redis container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker exec -it redis-master1 redis-cli --cluster create &amp;lt;&amp;lt;redis-master1-ip&amp;gt;&amp;gt;:6379 &amp;lt;&amp;lt;redis-master2-ip&amp;gt;&amp;gt;:6379 &amp;lt;&amp;lt;redis-master3-ip&amp;gt;&amp;gt;:6379 \
&amp;lt;&amp;lt;redis-master1-ip&amp;gt;&amp;gt;:6380 &amp;lt;&amp;lt;redis-master2-ip&amp;gt;&amp;gt;:6380 &amp;lt;&amp;lt;redis-master3-ip&amp;gt;&amp;gt;:6380 \
&amp;lt;&amp;lt;redis-master1-ip&amp;gt;&amp;gt;:6381 &amp;lt;&amp;lt;redis-master2-ip&amp;gt;&amp;gt;:6381 &amp;lt;&amp;lt;redis-master3-ip&amp;gt;&amp;gt;:6381  \
--cluster-replicas 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you run above command, you can confirm cluster creation using following command:&lt;br&gt;
&lt;code&gt;docker exec -it redis-master1 redis-cli cluster nodes&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuuf63cm974nticsmmcaf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuuf63cm974nticsmmcaf.png" alt="Cluster Created" width="800" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Testing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We were able to validate using a standard python locust script, that only a single core is utilized when redis process (container) is deployed on a 2 core vm.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgnwqy4kppuoyq3tchaaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgnwqy4kppuoyq3tchaaw.png" alt="Redis CPU Utilization" width="800" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While running the same locust python script (with set/get to varied data-structures), we observed that redis master were handling the writes and only used one core. The load was evenly distributed across all 3 masters.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcpzljkbobn7807h8nuth.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcpzljkbobn7807h8nuth.png" alt="Locust Test" width="800" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjl342cjs857xn7a8jsa1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjl342cjs857xn7a8jsa1.png" alt="Locust Test Monitoring" width="800" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our performance validation on cluster topology confirmed that:&lt;/p&gt;

&lt;p&gt;Each Redis master process consistently utilized a single CPU core.&lt;br&gt;
Replicas used their assigned cores for replication tasks.&lt;br&gt;
Additional CPU cores remained available for system or Docker tasks.&lt;/p&gt;

&lt;p&gt;When comparing traditional vs optimized setups:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Configuration&lt;/th&gt;
&lt;th&gt;Nodes&lt;/th&gt;
&lt;th&gt;Total CPUs&lt;/th&gt;
&lt;th&gt;Observations&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Traditional 3-Master + 2-Replica (9 VMs)&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;~18&lt;/td&gt;
&lt;td&gt;Higher cost, underutilized CPUs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Optimized Swarm Deployment (3 VMs)&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;~12&lt;/td&gt;
&lt;td&gt;Efficient core utilization, same throughput&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Limitations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While this topology provides a balanced tradeoff between cost and performance, there are some practical constraints to be aware of:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Resource Contention Under Heavy Load&lt;/em&gt;&lt;br&gt;
Even though CPU cores are pinned, network and memory I/O are still shared at the VM level. Heavy workloads may cause contention, especially during replication bursts or snapshotting (RDB/AOF persistence).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Recovery Complexity&lt;/em&gt;&lt;br&gt;
Container or node failures require manual intervention or Swarm rebalancing to maintain master-replica pairing across hosts. Automated failover can be slower than in dedicated setups.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Operational Visibility&lt;/em&gt;&lt;br&gt;
Monitoring multiple Redis containers per VM demands robust observability — metrics, logs, and alerts should be aggregated using tools like Prometheus + Grafana or RedisInsight.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Persistence Overhead in Shared Storage&lt;/em&gt;&lt;br&gt;
If persistence is enabled and multiple containers share underlying disks, storage I/O may become a bottleneck, impacting latency.&lt;/p&gt;

&lt;p&gt;Despite these trade-offs, for many real-world workloads where cost efficiency and CPU utilization matter, this architecture delivers an excellent balance between performance, simplicity, and maintainability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Redis single-threaded nature makes CPU utilization a critical design factor. By leveraging containerization, Docker Swarm(or any other orchestration), and CPU pinning, it’s possible to achieve a multi-master Redis Cluster with high throughput and fault tolerance while using fewer VMs and fewer CPU cores overall.&lt;/p&gt;

&lt;p&gt;This topology proves that smart deployment design can save infrastructure cost and CPU resources without compromising Redis performance or availability.&lt;/p&gt;

</description>
      <category>performance</category>
      <category>architecture</category>
      <category>devops</category>
      <category>database</category>
    </item>
  </channel>
</rss>
