<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mananjethwani on Medium]]></title>
        <description><![CDATA[Stories by Mananjethwani on Medium]]></description>
        <link>https://medium.com/@mananjethwani02?source=rss-205d4339c08------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*SWMEmPa2y9vmUm2t</url>
            <title>Stories by Mananjethwani on Medium</title>
            <link>https://medium.com/@mananjethwani02?source=rss-205d4339c08------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 21:04:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mananjethwani02/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Illusion of Time: Why Physical Clocks Fail in Distributed Systems]]></title>
            <link>https://medium.com/@mananjethwani02/the-illusion-of-time-why-physical-clocks-fail-in-distributed-systems-f46b2e4a3238?source=rss-205d4339c08------2</link>
            <guid isPermaLink="false">https://medium.com/p/f46b2e4a3238</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <dc:creator><![CDATA[Mananjethwani]]></dc:creator>
            <pubDate>Sun, 24 Aug 2025 21:33:47 GMT</pubDate>
            <atom:updated>2025-08-24T21:33:47.819Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Time is one of the most fundamental yet often overlooked aspects of modern distributed systems.</strong> We look at a clock and assume it tells us the “correct” time, and that everyone else’s clock is reasonably close. This assumption works well enough when we’re coordinating a meeting with colleagues. But in the world of distributed systems — where computers are spread across different locations, communicating over a network — this simple notion of time breaks down completely.</p><h3>The Problem with Physical Clocks</h3><p>Consider you have two processes, process A and process B, which are running in different data centers on different servers. A user action triggers an event over process A at 10:00:00.<strong>100</strong> AM. Process A then sends a message to Process B, which receives it and triggers another event. When Process B logs this event, its local clock reads 10:00:00.<strong>095</strong> AM.</p><p>Umm, what? The event on Process B appears to have happened <em>before</em> the event on Process A that caused it. This is not a bug; it’s a fundamental problem with relying on physical clocks in a distributed environment, caused by two main factors:</p><ol><li><strong>Clock Drift:</strong> No two physical clocks are perfect. They are physical devices that will inevitably run at slightly different rates. This difference, known as “clock drift,” might be very small initially, but over time, it can lead to significant discrepancies between machines.</li><li><strong>Network Latency:</strong> The time it takes for a message to travel from one computer to another is not instantaneous and, more importantly, it’s not constant. Network congestion and physical distance introduce unpredictable delays.</li></ol><h3>Taming the Drift: The Quest for Physical Time Sync</h3><p>Ok for some systems, precise physical time is non-negotiable. Consider <strong>High-Frequency Trading (HFT)</strong>, where trades are executed in microseconds. A tiny discrepancy in timestamps can mean the difference between a profitable trade and a significant loss. Regulatory bodies often require financial institutions to have their clocks synchronized to a very high degree of accuracy.</p><p>To combat clock drift, systems use synchronization protocols. The most common is the <strong>Network Time Protocol (NTP)</strong>. NTP works by creating a hierarchy of time servers, with the most accurate ones (Stratum 0) being atomic clocks or GPS clocks. Lower-level servers synchronize with higher-level ones, and clients synchronize with them. This process constantly corrects the local clock, significantly reducing drift.</p><p>Plus, HFTs go to extreme measures for high Clock Sync, by placing there servers in same Data-centers as of the stock broker system, use specialized systems optimizing each Layer of OSI model according to their needs (just as a simple example consider they buy Optic fiber cables for transmiting data inside the data center.)</p><p>Cloud providers have also introduced some special functionalities. <strong>Amazon’s Time Sync Service</strong>, for example, uses a fleet of satellite-connected and atomic clocks in each AWS region to deliver highly accurate time to EC2 instances, mitigating the variability of internet-based NTP.</p><p>However, even with these measures, achieving perfect synchronization is impossible. There will always be a small, non-zero error bound.</p><h3>When Close Enough Isn’t the Point</h3><p>While HFT systems invest heavily in minimizing clock drift, not all distributed systems need that level of precision. For many applications, the exact wall-clock time is less important than understanding the causal relationship between events.</p><p>Consider a collaborative document editor like Google Docs. If two users type at the same time, does it matter if User A’s keystroke was at 10:00:01.123 and User B&#39;s was at 10:00:01.124? Not really. What matters is that the system can establish a consistent order for applying their changes to avoid conflicts. Insisting on microsecond-accurate physical time here would be overkill, adding complexity and cost for no real benefit. The trade-off is clear: accept a simpler model of time in exchange for a less complex and more resilient system.</p><p>This is where logical clocks come into play.</p><h3>Introducing Logical Clocks: The Lamport Timestamp</h3><p>I was inspired to write this blog because of a paper Leslie lamport wrote — <a href="https://lamport.azurewebsites.net/pubs/time-clocks.pdf"><strong><em>Time, Clocks, and the Ordering of Events in a Distributed System</em></strong></a></p><p>Leslie Lamport’s key insight was that if we can’t agree on the exact <em>time</em> an event happened, we can at least agree on the <em>order</em> in which events happened. Meaning, we should care about the <strong>“happened-before” </strong>relationship.</p><p>A Lamport clock is a simple counter maintained by each process. Here’s how it works:</p><ol><li><strong>Initialization:</strong> Each process starts with its clock at 0.</li><li><strong>Internal Events:</strong> Before a process executes an event, it increments its own clock. clock = clock + 1.</li><li><strong>Sending a Message:</strong> When sending a message, a process increments its clock and includes the new value.</li><li><strong>Receiving a Message:</strong> When a process receives a message, it updates its clock to be the maximum of its current clock and the received timestamp, and then increments it. clock = max(local_clock, received_timestamp) + 1.</li></ol><p>This ensures that if event A happened before event B, the Lamport timestamp of A will be less than the timestamp of B.</p><h3>A Practical Example in Python</h3><p>To see these concepts in action, I’ve built a simple client-server messaging application in Python. In this project:</p><ul><li>Each server and client maintains its own Lamport clock.</li><li>When a client sends a message, it ticks its clock and includes the timestamp.</li><li>When a server receives a message, it updates its clock based on the message’s timestamp, processes the message, and then ticks its clock again before broadcasting it to other clients.</li></ul><p>This simple simulation, which you can run using Docker, clearly demonstrates how a logical order is maintained across different processes, even with simulated network delays. It provides a hands-on way to observe the “happened-before” relationship being correctly captured by the logical timestamps.</p><h3>Going Deeper: Vector Clocks</h3><p>Lamport clocks are great for establishing a partial order, but they have a limitation. If two events have timestamps 4 and 5, we know 4 happened before 5. But if two events in different processes both have a timestamp of 4, we can&#39;t tell which happened first, or if they happened concurrently.</p><p><strong>Vector clocks</strong> solve this. Instead of a single counter, each process maintains a vector (or a list) of counters, with one entry for every process in the system.</p><ol><li><strong>Initialization:</strong> Each process starts with a vector of all zeros. [0, 0, 0] for a 3-process system.</li><li><strong>Internal Events:</strong> A process increments its <em>own</em> counter in the vector. Process 1: [0,0,0] -&gt; [1,0,0].</li><li><strong>Sending a Message:</strong> A process sends its entire vector along with the message.</li><li><strong>Receiving a Message:</strong> When a process receives a message, it updates its own vector by taking the element-wise maximum of its vector and the received vector. Then, it increments its <em>own</em> counter in the updated vector.</li></ol><p>With vector clocks, we can definitively determine the relationship between any two events:</p><ul><li>Event A happened before B iff A’s vector is strictly less than B’s in every position.</li><li>Events A and B are concurrent if neither vector is strictly less than the other.</li></ul><h3>Choosing the Right Clock: A Trade-off Analysis</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8Dea-rc1PlJXGvZ5jBZvNg.png" /></figure><h3>The Holy Grail: Google’s TrueTime</h3><p>What if you could have the best of both worlds? <strong>Google’s TrueTime API</strong>, used by its Spanner database, effectively does this. It doesn’t return a simple timestamp. Instead, it returns a <em>time interval</em> [earliest, latest], guaranteeing that the actual time of an event is within that interval. The intervals provided by TrueTime are extremely small (typically a few milliseconds) because Google uses its own infrastructure of atomic clocks and GPS receivers.</p><p>This allows Spanner to use physical time for ordering while still accounting for clock uncertainty. It can wait for a few milliseconds to be certain that one transaction’s time interval has passed before committing another, thus providing globally consistent reads at any point in time.</p><h3>Conclusion</h3><p>Time in distributed systems is not a single, straight line but a complex, branching reality. While physical clocks can be meticulously synchronized for applications that demand it, logical clocks like Lamport and vector clocks provide a powerful and often more practical way to reason about the order of events. By understanding the trade-offs, we can choose the right tool for the job, whether it’s the simple elegance of a Lamport clock or the conflict-resolving power of a vector clock.</p><h3>References and Further Reading</h3><ol><li><strong>Lamport, L. (1978). Time, Clocks, and the Ordering of Events in a Distributed System. Communications of the ACM, 21(7), 558–565. </strong>The foundational paper that introduced the concept of logical clocks. A must-read for anyone serious about distributed systems. <a href="https://lamport.azurewebsites.net/pubs/time-clocks.pdf">Link to paper</a></li><li><strong>Mattern, F. (1988). Virtual Time and Global States of Distributed Systems. </strong>One of the key papers that independently developed and formalized the concept of vector clocks.</li><li><strong>Corbett, J. C., et al. (2012). Spanner: Google’s Globally-Distributed Database. OSDI’12. </strong>The paper detailing Google’s Spanner database and its novel TrueTime API. <a href="https://research.google/pubs/pub39966/">Link to paper</a></li><li><strong>Amazon Web Services. (n.d.). Keeping Time With the Amazon Time Sync Service. </strong>Official documentation on how AWS provides accurate time to its cloud instances. <a href="https://www.google.com/search?q=https://aws.amazon.com/blogs/aws/keeping-time-with-the-amazon-time-sync-service/">Link to documentation</a></li></ol><p><em>A note on creation: This write-up was created with the assistance of AI to help organize, refine, and articulate the core concepts of distributed time.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f46b2e4a3238" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>