<?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: Gauransh Gupta</title>
    <description>The latest articles on DEV Community by Gauransh Gupta (@gauransh28).</description>
    <link>https://dev.to/gauransh28</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%2F3852447%2F88be2874-c6c7-457d-b24b-8b7cb51afd5f.jpg</url>
      <title>DEV Community: Gauransh Gupta</title>
      <link>https://dev.to/gauransh28</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gauransh28"/>
    <language>en</language>
    <item>
      <title>CAP Theorem Explained: What I Learned Setting Up MySQL Replication</title>
      <dc:creator>Gauransh Gupta</dc:creator>
      <pubDate>Fri, 08 May 2026 22:18:16 +0000</pubDate>
      <link>https://dev.to/gauransh28/cap-theorem-explained-what-i-learned-setting-up-mysql-replication-5291</link>
      <guid>https://dev.to/gauransh28/cap-theorem-explained-what-i-learned-setting-up-mysql-replication-5291</guid>
      <description>&lt;p&gt;I recently started my journey into system design and data-intensive applications. This experience has completely changed how I look at software — especially around the hard tradeoffs of consistency, availability, and what happens when things go wrong at scale.&lt;/p&gt;

&lt;p&gt;Let me start with a scenario that surprised me when I first encountered it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Imagine you just inserted a record in your app. A millisecond later, a read on a different server returns the old data. How? Welcome to distributed systems.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  What is CAP Theorem?
&lt;/h2&gt;

&lt;p&gt;CAP stands for &lt;strong&gt;Consistency&lt;/strong&gt;, &lt;strong&gt;Availability&lt;/strong&gt;, and &lt;strong&gt;Partition Tolerance&lt;/strong&gt;. The theorem states that a distributed system can only guarantee 2 out of these 3 properties at any given time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consistency&lt;/strong&gt; — Every read returns the most recent write, or an error. All nodes in the system see the same data at the same time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Availability&lt;/strong&gt; — Every request gets a response (not an error), even if some nodes are down. The system is always up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Partition Tolerance&lt;/strong&gt; — The system keeps running even when network communication between nodes is lost or delayed.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Real Choice: CP vs AP
&lt;/h2&gt;

&lt;p&gt;Here is the most important thing to understand: &lt;strong&gt;you cannot actually drop Partition Tolerance.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Network partitions are not a design choice — they are a reality. Hardware fails, packets drop, data centers lose connectivity. Any system that runs across multiple machines will eventually face a partition.&lt;/p&gt;

&lt;p&gt;So the real decision every distributed system makes is: &lt;em&gt;when a partition happens, do you stay consistent or stay available?&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  A Simple Analogy: The Bank Branch
&lt;/h2&gt;

&lt;p&gt;Imagine a bank with its HQ in New York and a branch in Boston. The branch always calls HQ before confirming any transaction.&lt;/p&gt;

&lt;p&gt;A customer in Boston deposits $500. Right at that moment, the network between Boston and New York goes down.&lt;/p&gt;

&lt;p&gt;The Boston branch now has two options:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1 — CP (Consistency + Partition Tolerance):&lt;/strong&gt;&lt;br&gt;
The branch refuses to process any transactions until it can reach HQ. The data stays consistent, but the service is unavailable during the outage. If you walk up to the counter, you get turned away.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 2 — AP (Availability + Partition Tolerance):&lt;/strong&gt;&lt;br&gt;
The branch processes the deposit locally and syncs with HQ once the network recovers. The service stays available, but if someone checks the account balance at the New York branch before the sync, they see the old amount — temporarily inconsistent data.&lt;/p&gt;

&lt;p&gt;Neither option is wrong. The right choice depends entirely on what your application can tolerate.&lt;/p&gt;




&lt;h2&gt;
  
  
  Hands-On: MySQL Replication as an AP System
&lt;/h2&gt;

&lt;p&gt;To make this concrete, I built a small master-slave replication setup using Docker to see these tradeoffs in action.&lt;/p&gt;

&lt;p&gt;You can find the full setup in &lt;a href="https://github.com/g3x-gauransh/MySQL--Master-slave-replication" rel="noopener noreferrer"&gt;this repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One MySQL container acts as the &lt;strong&gt;master&lt;/strong&gt; — it accepts all writes&lt;/li&gt;
&lt;li&gt;A second container acts as the &lt;strong&gt;slave&lt;/strong&gt; — it monitors the master and replays every change&lt;/li&gt;
&lt;li&gt;The master records every write to a &lt;strong&gt;binary log (binlog)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The slave runs two threads: an &lt;strong&gt;IO thread&lt;/strong&gt; that pulls the binlog from the master, and a &lt;strong&gt;SQL thread&lt;/strong&gt; that replays those events locally&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What I observed:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When I inserted rows on the master, they showed up on the slave within milliseconds under normal conditions. But when I inserted 10,000 rows rapidly, I could watch the &lt;code&gt;Seconds_Behind_Master&lt;/code&gt; value in &lt;code&gt;SHOW SLAVE STATUS&lt;/code&gt; grow and then shrink back to zero as the slave caught up.&lt;/p&gt;

&lt;p&gt;That gap — &lt;strong&gt;replication lag&lt;/strong&gt; — is the tradeoff in action. During those seconds, a read on the slave returns stale data. The slave is always available to serve reads, but it cannot guarantee it has the latest write.&lt;/p&gt;

&lt;p&gt;This makes the setup an &lt;strong&gt;AP system&lt;/strong&gt;: Available and Partition Tolerant, but not strongly Consistent.&lt;/p&gt;

&lt;p&gt;If you wanted CP behaviour, you would switch to synchronous replication — the master waits for the slave to confirm every write before acknowledging it to the client. You get consistency, but every write now pays the latency cost of that round-trip.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Choose CP vs AP
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Prefer CP when a wrong answer is worse than no answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bank balances and financial transactions&lt;/li&gt;
&lt;li&gt;Seat reservation systems (cinema, flights)&lt;/li&gt;
&lt;li&gt;Distributed locks and coordination&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Prefer AP when brief staleness is acceptable:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Social media feeds&lt;/li&gt;
&lt;li&gt;Product catalogs and shopping carts&lt;/li&gt;
&lt;li&gt;DNS resolution&lt;/li&gt;
&lt;li&gt;Caching layers&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Beyond CAP: Consistency is a Spectrum
&lt;/h2&gt;

&lt;p&gt;CAP theorem is a useful mental model, but it is a simplification. In practice, consistency is not binary — there is a whole spectrum:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Linearizability&lt;/strong&gt; — the strongest guarantee; reads always reflect the latest write&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sequential consistency&lt;/strong&gt; — operations appear in the same order across all nodes, but may lag&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Eventual consistency&lt;/strong&gt; — all nodes will converge to the same value, given enough time (this is what MySQL async replication gives you)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to go deeper, &lt;em&gt;Designing Data-Intensive Applications&lt;/em&gt; by Martin Kleppmann covers replication in Chapter 5 and consistency models in Chapter 9. It is genuinely the best resource I have found on this topic.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;CAP is not a theorem you apply once and move on — it is a lens for thinking about tradeoffs. Every distributed system you design makes a CAP choice, whether you make it explicitly or not. Understanding that choice, and being able to justify it, is what separates a system that works in production from one that only works in theory.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>mysql</category>
      <category>distributedsystems</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
