<?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>Forem: rajat</title>
    <description>The latest articles on Forem by rajat (@rajat10).</description>
    <link>https://forem.com/rajat10</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%2F924078%2F2cb3e4fb-97f3-4d73-b342-b7e6182e9607.jpg</url>
      <title>Forem: rajat</title>
      <link>https://forem.com/rajat10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rajat10"/>
    <language>en</language>
    <item>
      <title>Why We Don't Use Socket Connections for Everything</title>
      <dc:creator>rajat</dc:creator>
      <pubDate>Fri, 06 Mar 2026 17:00:25 +0000</pubDate>
      <link>https://forem.com/rajat10/why-we-dont-use-socket-connections-for-everything-3j6g</link>
      <guid>https://forem.com/rajat10/why-we-dont-use-socket-connections-for-everything-3j6g</guid>
      <description>&lt;p&gt;Sockets (such as TCP sockets or WebSockets) enable &lt;strong&gt;persistent, two-way communication&lt;/strong&gt; between a client and a server. Because of this, they are extremely useful for &lt;strong&gt;real-time applications&lt;/strong&gt; like chat systems, live notifications, and online games.&lt;/p&gt;

&lt;p&gt;However, in most web applications we &lt;strong&gt;do not use socket connections for every request&lt;/strong&gt;. Instead, many systems still rely on &lt;strong&gt;HTTP-based communication&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let’s explore why.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Persistent Connections Consume More Resources
&lt;/h2&gt;

&lt;p&gt;A socket connection remains &lt;strong&gt;open for a long time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client ⇄ Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server must maintain this connection in memory and keep track of its state.&lt;/p&gt;

&lt;p&gt;If a system has &lt;strong&gt;1 million users connected simultaneously&lt;/strong&gt;, the server must maintain &lt;strong&gt;1 million open connections&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This requires:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Memory for each connection&lt;/li&gt;
&lt;li&gt;CPU for connection management&lt;/li&gt;
&lt;li&gt;File descriptors from the operating system&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of these costs, keeping many persistent connections can become &lt;strong&gt;expensive and difficult to scale&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In contrast, HTTP connections are usually &lt;strong&gt;short-lived&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client → Request → Server → Response → Connection closed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the response is sent, the server can release the resources.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Many Applications Do Not Need Real-Time Communication
&lt;/h2&gt;

&lt;p&gt;Socket connections are best suited for &lt;strong&gt;real-time systems&lt;/strong&gt;, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chat applications&lt;/li&gt;
&lt;li&gt;Multiplayer games&lt;/li&gt;
&lt;li&gt;Stock trading dashboards&lt;/li&gt;
&lt;li&gt;Live notifications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, most web operations follow a simple &lt;strong&gt;request–response pattern&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User login&lt;/li&gt;
&lt;li&gt;Fetching product lists&lt;/li&gt;
&lt;li&gt;Loading a webpage&lt;/li&gt;
&lt;li&gt;Submitting a form&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In these cases, HTTP is &lt;strong&gt;simpler and more efficient&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Load Balancing Becomes More Difficult
&lt;/h2&gt;

&lt;p&gt;With HTTP, every request is independent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client → Load Balancer → Any Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Requests can be distributed easily across many servers.&lt;/p&gt;

&lt;p&gt;Socket connections, however, are &lt;strong&gt;long-lived&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client ⇄ Server (persistent connection)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once a socket is established, the client must continue communicating with the &lt;strong&gt;same server&lt;/strong&gt;. This can make &lt;strong&gt;load balancing and horizontal scaling more complex&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Operating System Limits
&lt;/h2&gt;

&lt;p&gt;Operating systems impose limits on the number of simultaneous socket connections a server can handle.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maximum open file descriptors&lt;/li&gt;
&lt;li&gt;Network port limits&lt;/li&gt;
&lt;li&gt;Memory limits per connection&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a server tries to maintain millions of open sockets, it may &lt;strong&gt;hit these system limits&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Increased Development Complexity
&lt;/h2&gt;

&lt;p&gt;Socket-based systems require additional logic to handle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Connection management&lt;/li&gt;
&lt;li&gt;Reconnection strategies&lt;/li&gt;
&lt;li&gt;Heartbeats and keep-alive signals&lt;/li&gt;
&lt;li&gt;Message ordering&lt;/li&gt;
&lt;li&gt;State synchronization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;HTTP APIs are generally &lt;strong&gt;much simpler to implement and maintain&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Comparison: HTTP vs Socket Connections
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;HTTP&lt;/th&gt;
&lt;th&gt;Socket / WebSocket&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Connection Type&lt;/td&gt;
&lt;td&gt;Short-lived&lt;/td&gt;
&lt;td&gt;Persistent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Communication&lt;/td&gt;
&lt;td&gt;Request–response&lt;/td&gt;
&lt;td&gt;Bi-directional&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server Resource Usage&lt;/td&gt;
&lt;td&gt;Lower&lt;/td&gt;
&lt;td&gt;Higher&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best Use Cases&lt;/td&gt;
&lt;td&gt;APIs, web pages&lt;/td&gt;
&lt;td&gt;Real-time applications&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Complexity&lt;/td&gt;
&lt;td&gt;Simple&lt;/td&gt;
&lt;td&gt;More complex&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




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

&lt;p&gt;Although socket connections enable powerful &lt;strong&gt;real-time communication&lt;/strong&gt;, they are not suitable for every situation.&lt;/p&gt;

&lt;p&gt;Most modern architectures use a &lt;strong&gt;hybrid approach&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;HTTP&lt;/strong&gt; for standard API communication&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WebSockets or sockets&lt;/strong&gt; for real-time features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By combining both approaches, systems can achieve &lt;strong&gt;efficient resource usage while still supporting real-time interactions when needed&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>networking</category>
      <category>performance</category>
    </item>
    <item>
      <title>How Vector Clocks Work in Distributed Systems</title>
      <dc:creator>rajat</dc:creator>
      <pubDate>Fri, 06 Mar 2026 16:45:28 +0000</pubDate>
      <link>https://forem.com/rajat10/how-vector-clocks-work-in-distributed-systems-5b4j</link>
      <guid>https://forem.com/rajat10/how-vector-clocks-work-in-distributed-systems-5b4j</guid>
      <description>&lt;p&gt;In distributed databases, multiple servers replicate the same data to improve &lt;strong&gt;availability and scalability&lt;/strong&gt;. However, concurrent updates to the same data item can lead to &lt;strong&gt;conflicts&lt;/strong&gt; because different replicas may process writes independently.&lt;/p&gt;

&lt;p&gt;To detect and resolve these conflicts, distributed systems often use &lt;strong&gt;vector clocks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;vector clock&lt;/strong&gt; helps determine whether two versions of data are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ordered&lt;/strong&gt; (one happened after another)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrent&lt;/strong&gt; (a conflict exists)&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  What Is a Vector Clock?
&lt;/h1&gt;

&lt;p&gt;A &lt;strong&gt;vector clock&lt;/strong&gt; is a collection of &lt;strong&gt;[server, version] pairs&lt;/strong&gt; associated with a data item.&lt;/p&gt;

&lt;p&gt;It tracks which server modified the data and how many updates that server has made.&lt;/p&gt;

&lt;p&gt;A vector clock can be represented as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D([S1, v1], [S2, v2], …, [Sn, vn])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;D&lt;/strong&gt; → data item&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Si&lt;/strong&gt; → server ID&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;vi&lt;/strong&gt; → version counter for that server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When a server writes a data item:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;[Si, vi]&lt;/code&gt; already exists → increment &lt;code&gt;vi&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Otherwise → create a new entry &lt;code&gt;[Si, 1]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Example: How Vector Clocks Work
&lt;/h1&gt;

&lt;p&gt;Let’s walk through the example shown in the diagram.&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%2F9rs22azus7y2n6mcjpay.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%2F9rs22azus7y2n6mcjpay.png" alt=" " width="800" height="742"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  1. First Write
&lt;/h2&gt;

&lt;p&gt;A client writes data item &lt;strong&gt;D1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The write is handled by server &lt;strong&gt;Sx&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Vector clock becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D1([Sx, 1])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means server &lt;strong&gt;Sx&lt;/strong&gt; has processed the first version of the data.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Second Update
&lt;/h2&gt;

&lt;p&gt;Another client reads &lt;strong&gt;D1&lt;/strong&gt;, updates it, and writes it back.&lt;/p&gt;

&lt;p&gt;The write is again handled by &lt;strong&gt;Sx&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The counter for &lt;strong&gt;Sx&lt;/strong&gt; increments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D2([Sx, 2])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This version &lt;strong&gt;descends from D1&lt;/strong&gt;, so it overwrites the previous version.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Update by Another Server
&lt;/h2&gt;

&lt;p&gt;A client reads &lt;strong&gt;D2&lt;/strong&gt;, modifies it, and the write is handled by &lt;strong&gt;Sy&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Vector clock becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D3([Sx, 2], [Sy, 1])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This indicates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two updates occurred on &lt;strong&gt;Sx&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;One update occurred on &lt;strong&gt;Sy&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Concurrent Update
&lt;/h2&gt;

&lt;p&gt;Another client also reads &lt;strong&gt;D2&lt;/strong&gt;, modifies it, and writes through &lt;strong&gt;Sz&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Vector clock becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D4([Sx, 2], [Sz, 1])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the system has &lt;strong&gt;two different versions&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D3([Sx,2], [Sy,1])
D4([Sx,2], [Sz,1])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These updates occurred &lt;strong&gt;independently&lt;/strong&gt;, creating a &lt;strong&gt;conflict&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Conflict Resolution
&lt;/h2&gt;

&lt;p&gt;When a client reads both &lt;strong&gt;D3&lt;/strong&gt; and &lt;strong&gt;D4&lt;/strong&gt;, it detects a conflict.&lt;/p&gt;

&lt;p&gt;This happened because &lt;strong&gt;D2 was modified by both Sy and Sz&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The client reconciles the conflict and writes a new version handled by &lt;strong&gt;Sx&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Vector clock becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D5([Sx, 3], [Sy, 1], [Sz, 1])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This new version incorporates both updates.&lt;/p&gt;




&lt;h1&gt;
  
  
  Detecting Conflicts Using Vector Clocks
&lt;/h1&gt;

&lt;p&gt;Vector clocks allow systems to determine relationships between versions.&lt;/p&gt;




&lt;h2&gt;
  
  
  Ancestor Relationship (No Conflict)
&lt;/h2&gt;

&lt;p&gt;Version &lt;strong&gt;X is an ancestor of Y&lt;/strong&gt; if every counter in &lt;strong&gt;Y&lt;/strong&gt; is greater than or equal to &lt;strong&gt;X&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D([s0,1], [s1,1])
D([s0,1], [s1,2])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since every counter in the second version is greater or equal, the first version &lt;strong&gt;preceded&lt;/strong&gt; the second.&lt;/p&gt;

&lt;p&gt;Therefore:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No conflict exists.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Sibling Relationship (Conflict)
&lt;/h2&gt;

&lt;p&gt;Two versions are &lt;strong&gt;siblings&lt;/strong&gt; if neither dominates the other.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D([s0,1], [s1,2])
D([s0,2], [s1,1])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First version has higher &lt;strong&gt;s1&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Second version has higher &lt;strong&gt;s0&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because neither version fully dominates the other, they are &lt;strong&gt;concurrent&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Therefore:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A conflict exists.&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Downsides of Vector Clocks
&lt;/h1&gt;

&lt;p&gt;Although vector clocks are powerful, they have some drawbacks.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Client Complexity
&lt;/h3&gt;

&lt;p&gt;Clients must implement &lt;strong&gt;conflict resolution logic&lt;/strong&gt;, which increases application complexity.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Growing Vector Size
&lt;/h3&gt;

&lt;p&gt;The number of &lt;code&gt;[server, version]&lt;/code&gt; pairs may grow quickly as more servers update the data.&lt;/p&gt;

&lt;p&gt;To control this, systems often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set a &lt;strong&gt;maximum vector size&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Remove the &lt;strong&gt;oldest entries&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, removing entries can make it harder to determine &lt;strong&gt;exact ancestor relationships&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;Vector clocks are a fundamental technique used in distributed systems to detect &lt;strong&gt;causal relationships between updates&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;They help distributed databases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Detect &lt;strong&gt;concurrent writes&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Identify &lt;strong&gt;conflicting versions&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Allow applications to &lt;strong&gt;reconcile conflicts safely&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Systems such as &lt;strong&gt;Amazon DynamoDB&lt;/strong&gt; introduced vector clocks to maintain &lt;strong&gt;eventual consistency while preserving high availability&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Despite their complexity, vector clocks remain one of the most effective mechanisms for &lt;strong&gt;conflict detection in distributed databases&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>vectorclock</category>
      <category>replica</category>
      <category>backend</category>
    </item>
    <item>
      <title>Inconsistency Resolution in Distributed Systems: Versioning</title>
      <dc:creator>rajat</dc:creator>
      <pubDate>Fri, 06 Mar 2026 16:34:28 +0000</pubDate>
      <link>https://forem.com/rajat10/inconsistency-resolution-in-distributed-systems-versioning-331b</link>
      <guid>https://forem.com/rajat10/inconsistency-resolution-in-distributed-systems-versioning-331b</guid>
      <description>&lt;p&gt;Modern distributed databases such as &lt;strong&gt;Amazon DynamoDB&lt;/strong&gt; and &lt;strong&gt;Apache Cassandra&lt;/strong&gt; replicate data across multiple servers to improve &lt;strong&gt;scalability, fault tolerance, and availability&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;However, replication introduces a major challenge: &lt;strong&gt;data inconsistency during concurrent updates&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To address this problem, distributed systems use a technique called &lt;strong&gt;versioning&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we’ll explore &lt;strong&gt;why versioning is needed, how it works, and how it helps resolve conflicts in eventually consistent systems.&lt;/strong&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%2F6rj5saeyau0n9r10j6d1.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%2F6rj5saeyau0n9r10j6d1.png" alt=" " width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Versioning Is Needed
&lt;/h2&gt;

&lt;p&gt;In an &lt;strong&gt;eventually consistent system&lt;/strong&gt;, updates do not reach all replicas at the same time due to &lt;strong&gt;network delays or partitions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Consider a system with three replicas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;N = 3
Replicas: s0, s1, s2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two clients update the same key at the same time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client A → put(key1 = 10)
Client B → put(key1 = 20)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because of network delays, the replicas may temporarily store &lt;strong&gt;different values&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s0 = 10
s1 = 20
s2 = 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the system contains &lt;strong&gt;conflicting values for the same key&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This situation is called a &lt;strong&gt;write conflict&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Versioning Does
&lt;/h2&gt;

&lt;p&gt;Versioning allows the system to &lt;strong&gt;track the history of writes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each update is assigned a &lt;strong&gt;version identifier&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(key1, value = 10, version = v1)
(key1, value = 20, version = v2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of immediately overwriting data, the system may &lt;strong&gt;temporarily store multiple versions&lt;/strong&gt; of the same value.&lt;/p&gt;

&lt;p&gt;This helps the system determine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which write happened earlier&lt;/li&gt;
&lt;li&gt;Whether two writes occurred &lt;strong&gt;concurrently&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Two Possible Scenarios
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. One Version Is Newer
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;v1 → value = 10
v2 → value = 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the system detects that &lt;strong&gt;v2 happened after v1&lt;/strong&gt;, then the newer version replaces the older one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;v2 replaces v1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;strong&gt;no conflict occurs&lt;/strong&gt; because the system knows the correct order of writes.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Concurrent Versions
&lt;/h3&gt;

&lt;p&gt;Sometimes the system &lt;strong&gt;cannot determine the order of writes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;v1 → value = 10
v2 → value = 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These updates are considered &lt;strong&gt;concurrent&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of choosing one automatically, the database may return &lt;strong&gt;both versions&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;key1:
 value1 = 10
 value2 = 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the &lt;strong&gt;client application must resolve the conflict&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Client Reconciliation
&lt;/h2&gt;

&lt;p&gt;The application decides how to resolve conflicting values.&lt;/p&gt;

&lt;p&gt;Common strategies include:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Last Write Wins
&lt;/h3&gt;

&lt;p&gt;The system selects the value with the &lt;strong&gt;latest timestamp&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Latest timestamp → chosen value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  2. Merging Values
&lt;/h3&gt;

&lt;p&gt;Sometimes values can be &lt;strong&gt;combined instead of overwritten&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example in a shopping cart system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cart1 = {apple}
cart2 = {banana}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Merged result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cart = {apple, banana}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach ensures that &lt;strong&gt;no data is lost&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Application Logic
&lt;/h3&gt;

&lt;p&gt;The application can apply &lt;strong&gt;custom business rules&lt;/strong&gt; to determine the correct value.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Priority-based updates&lt;/li&gt;
&lt;li&gt;User-based conflict resolution&lt;/li&gt;
&lt;li&gt;Domain-specific merge logic&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Versioning with Vector Clocks
&lt;/h2&gt;

&lt;p&gt;Some distributed databases use &lt;strong&gt;vector clocks&lt;/strong&gt; to track the history of updates.&lt;/p&gt;

&lt;p&gt;A vector clock stores a &lt;strong&gt;list of node counters representing update history&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;v1 = {s0:1}
v2 = {s1:1}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If two versions have &lt;strong&gt;different histories&lt;/strong&gt;, the system treats them as &lt;strong&gt;concurrent updates&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Vector clocks help databases &lt;strong&gt;automatically detect conflicts&lt;/strong&gt; without relying only on timestamps.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;Versioning allows distributed databases to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Detect &lt;strong&gt;conflicting updates&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Temporarily &lt;strong&gt;store multiple versions of data&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Resolve conflicts through &lt;strong&gt;reconciliation strategies&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach enables &lt;strong&gt;eventual consistency&lt;/strong&gt; while keeping the system &lt;strong&gt;highly available and scalable&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Versioning is a &lt;strong&gt;fundamental technique in distributed systems&lt;/strong&gt;. It allows modern databases to maintain consistency &lt;strong&gt;without blocking writes&lt;/strong&gt;, which is critical for systems operating at large scale.&lt;/p&gt;

&lt;p&gt;By tracking the history of updates and allowing conflict resolution later, versioning ensures that distributed systems remain &lt;strong&gt;reliable, flexible, and highly performant&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>database</category>
      <category>distributedsystems</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Understanding WebSocket Connection (Simple Explanation)</title>
      <dc:creator>rajat</dc:creator>
      <pubDate>Fri, 06 Mar 2026 16:25:39 +0000</pubDate>
      <link>https://forem.com/rajat10/understanding-websocket-connection-simple-explanation-5b15</link>
      <guid>https://forem.com/rajat10/understanding-websocket-connection-simple-explanation-5b15</guid>
      <description>&lt;p&gt;WebSocket is a protocol used for &lt;strong&gt;real-time communication between a client and a server&lt;/strong&gt;. Unlike normal HTTP communication, WebSocket allows &lt;strong&gt;continuous, two-way communication over a single connection&lt;/strong&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%2F64blkrynbi7n8p0xr5qa.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%2F64blkrynbi7n8p0xr5qa.png" alt="Socket_Image" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  1. WebSocket Connection is Initiated by the Client
&lt;/h2&gt;

&lt;p&gt;A WebSocket connection always starts from the &lt;strong&gt;client side&lt;/strong&gt; (browser, mobile app, or any client application).&lt;/p&gt;

&lt;p&gt;The client sends a request to the server asking to &lt;strong&gt;upgrade the connection to WebSocket&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example HTTP request headers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the server supports WebSocket, it responds with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This process is called the &lt;strong&gt;WebSocket Handshake&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;After this handshake, the protocol &lt;strong&gt;switches from HTTP to WebSocket&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. WebSocket is Bi-Directional
&lt;/h2&gt;

&lt;p&gt;In traditional HTTP communication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client → Request
Server → Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server cannot send data unless the client first sends a request.&lt;/p&gt;

&lt;p&gt;But in WebSocket:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client ⇄ Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both the &lt;strong&gt;client and the server can send messages at any time&lt;/strong&gt; without waiting for a request.&lt;/p&gt;

&lt;p&gt;This makes WebSocket ideal for &lt;strong&gt;real-time applications&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chat applications&lt;/li&gt;
&lt;li&gt;Online gaming&lt;/li&gt;
&lt;li&gt;Live notifications&lt;/li&gt;
&lt;li&gt;Stock price updates&lt;/li&gt;
&lt;li&gt;Real-time dashboards&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. WebSocket Connection is Persistent
&lt;/h2&gt;

&lt;p&gt;A WebSocket connection remains &lt;strong&gt;open for a long time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Unlike HTTP where the connection is closed after each response, WebSocket maintains a &lt;strong&gt;persistent connection&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This allows continuous communication like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client ⇄ Server
Client ⇄ Server
Client ⇄ Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because the connection stays open, it reduces the overhead of repeatedly creating new HTTP connections.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. WebSocket Works Through Firewalls
&lt;/h2&gt;

&lt;p&gt;Many networks use firewalls that restrict unknown ports.&lt;/p&gt;

&lt;p&gt;However, WebSocket usually runs on the same ports used by HTTP and HTTPS:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Port 80&lt;/strong&gt; → &lt;code&gt;ws://&lt;/code&gt; (WebSocket)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port 443&lt;/strong&gt; → &lt;code&gt;wss://&lt;/code&gt; (Secure WebSocket)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since these ports are commonly allowed by firewalls, WebSocket connections generally work without issues.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Why WebSocket is Important
&lt;/h2&gt;

&lt;p&gt;WebSocket is widely used in modern applications because it enables &lt;strong&gt;low-latency and efficient communication&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Benefits include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time data updates&lt;/li&gt;
&lt;li&gt;Lower network overhead&lt;/li&gt;
&lt;li&gt;Persistent connection&lt;/li&gt;
&lt;li&gt;Full duplex communication (two-way communication)&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;A WebSocket connection:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is &lt;strong&gt;initiated by the client&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Starts as an &lt;strong&gt;HTTP connection&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Uses a &lt;strong&gt;handshake to upgrade to WebSocket&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Becomes a &lt;strong&gt;persistent connection&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Allows &lt;strong&gt;bi-directional communication&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Works easily through firewalls using &lt;strong&gt;ports 80 and 443&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of these features, WebSocket is one of the most important technologies used for building &lt;strong&gt;real-time web applications&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>socket</category>
      <category>microservices</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Convert any Github Repo to VS Code Environment</title>
      <dc:creator>rajat</dc:creator>
      <pubDate>Sun, 09 Jul 2023 16:05:35 +0000</pubDate>
      <link>https://forem.com/rajat10/convert-any-github-repo-to-vs-code-environment-3eb0</link>
      <guid>https://forem.com/rajat10/convert-any-github-repo-to-vs-code-environment-3eb0</guid>
      <description>&lt;p&gt;To open a GitHub repository in GitHub.dev, you can follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to the GitHub website and find the repository you want to open. For example, let's say you want to open the repository &lt;code&gt;[example/repo](https://github.com/example/repo)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the URL, locate the &lt;code&gt;.com&lt;/code&gt; portion, which in this case is &lt;code&gt;github.com&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Swap the &lt;code&gt;.com&lt;/code&gt; with &lt;code&gt;.dev&lt;/code&gt; in the URL. So, in our example, &lt;code&gt;github.com&lt;/code&gt; becomes &lt;code&gt;github.dev&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The updated URL would be &lt;code&gt;[https://github.dev/example/repo](https://github.dev/example/repo)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open a new tab in your web browser and paste the updated URL into the address bar.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hit Enter or Return to navigate to the URL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The GitHub repository will now open in GitHub.dev, providing you with a web-based code editor environment powered by Visual Studio Code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From here, you can edit the code, view branches, open pull requests, and collaborate with others in real-time, all within the GitHub.dev environment.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Remember, this process allows you to open a GitHub repository directly in GitHub.dev without needing to download or install any additional software. It provides a convenient way to quickly access and work on your code within a web-based IDE.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
