<?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: Sriram R</title>
    <description>The latest articles on Forem by Sriram R (@sriramr98).</description>
    <link>https://forem.com/sriramr98</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%2F214005%2Fea1f630c-b4af-4008-963e-9fb1c84d1c93.jpeg</url>
      <title>Forem: Sriram R</title>
      <link>https://forem.com/sriramr98</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sriramr98"/>
    <language>en</language>
    <item>
      <title>Vector Clocks</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Mon, 27 Feb 2023 05:35:12 +0000</pubDate>
      <link>https://forem.com/sriramr98/vector-clocks-3ehi</link>
      <guid>https://forem.com/sriramr98/vector-clocks-3ehi</guid>
      <description>&lt;p&gt;The main limitation of &lt;a href="https://dev.to/sriramr98/the-notion-of-logical-time-311j"&gt;Lamport Clocks&lt;/a&gt; was that they were unable to assist us in determining the causal relationship between two events.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you're not sure what a causal relationship is, read my article on &lt;a href="https://dev.to/sriramr98/message-ordering-23k8"&gt;Message Ordering&lt;/a&gt; first.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The reason for this is that we had a single timestamp that was being updated globally, which did not provide us with a complete picture of the sequence of events that occurred.&lt;/p&gt;

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

&lt;p&gt;Instead of each node maintaining a single timestamp, we maintain a list of timestamps - one for each node in every node.&lt;/p&gt;

&lt;p&gt;For example, if we have a three-node system, the vector timestamp would look like [ T1, T2, T3 ], where T1 is the logical time of Node 1, T2 is the logical time of Node 2, and T3 is the logical time of Node 3.&lt;/p&gt;

&lt;p&gt;The entire list forms a single logical clock, known as vector clocks.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do Vector Clocks work?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;When a node boots up, it determines the number of nodes in the cluster (N) and fills an array of size N with 0's.&lt;/li&gt;
&lt;li&gt;Before executing an event, the node increments the clock of that node's time in the list. &lt;code&gt;N[i] = N[i] + 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;When a message is sent, the time for that node is incremented, and the vector clock is attached to the message.&lt;/li&gt;
&lt;li&gt;When a message is received, 

&lt;ol&gt;
&lt;li&gt;It performs the action&lt;/li&gt;
&lt;li&gt;Update each list element by taking the maximum of that element's own list and the list received.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u3WgwZuf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m14kyprb6jufd65jofn4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u3WgwZuf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m14kyprb6jufd65jofn4.png" alt="Vector Clock Movement" width="880" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We say Event A caused Event B if A's vector timestamp is strictly less than B's vector timestamp.&lt;br&gt;
We can also say that Event A and Event B are concurrent if their timestamps cannot be compared.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pseudocode
&lt;/h2&gt;

&lt;p&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;on initialisation at node Ni do
    T := [0,0,..0] //One element for each node
end on

on any event occuring at node Ni do
    T[i] := T[i] + 1
end on

on request to send message m from node Ni do
    T[i] := T[i] + 1
    send_message(m, T)
end on

on receiving message (m, T') at node Ni do
    T[j] := max(T[j], T'[j]) for j in (1..n)
    T[i] := T[i] + 1;
    process_message(m)
end on
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;The main problem with vector clocks is that they require a large amount of storage because each node must store N timestamps based on the number of nodes.&lt;/p&gt;

&lt;p&gt;Furthermore, as the number of nodes increases, we will be sending a significant amount of data because the entire vector clock with all node timestamps must be sent even if only two nodes are interacting.&lt;/p&gt;

</description>
      <category>distributedsystems</category>
      <category>computerscience</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Lamport Clocks</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Fri, 24 Feb 2023 12:40:07 +0000</pubDate>
      <link>https://forem.com/sriramr98/lamport-clocks-2jke</link>
      <guid>https://forem.com/sriramr98/lamport-clocks-2jke</guid>
      <description>&lt;p&gt;We looked at &lt;a href="https://dev.to/sriramr98/the-notion-of-logical-time-311j"&gt;Logical Time&lt;/a&gt; in the previous article and how it can help us reason about time in a single machine. Let's now think about how we can think about this in a distributed system.&lt;/p&gt;

&lt;p&gt;Consider sending an email to a friend and having him read it. If we think about it, any action you took before sending the email, such as writing it, checking for grammatical errors, and attaching an attachment, must have occurred prior to your friend reading the email, as must any action that follows it, such as forwarding the mail to someone else and printing it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/sriramr98/the-notion-of-logical-time-311j"&gt;Logical Time&lt;/a&gt; is simply a counter that is incremented from an arbitrary point in time for a specific node. It means that the logical timestamps of two distinct machines cannot be compared.&lt;/p&gt;

&lt;p&gt;If this is the case, how do we determine the order of these events when logical timestamps cannot be compared?&lt;/p&gt;

&lt;h2&gt;
  
  
  Lamport Clocks
&lt;/h2&gt;

&lt;p&gt;The Lamport Clock, invented by Leslie Lamport, was one of the first and simplest types of Logical Clocks.&lt;/p&gt;

&lt;p&gt;This is how Lamport Clocks function.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When a node is first started, it keeps a local logical time that starts at zero.&lt;/li&gt;
&lt;li&gt;Prior to performing an event, the node increases its local logical time counter by one ('Ci = Ci + 1').&lt;/li&gt;
&lt;li&gt;The incremented logical timestamp is sent along with the message when it is sent over the network.&lt;/li&gt;
&lt;li&gt;The message's receiver does the following:

&lt;ol&gt;
&lt;li&gt;It updates its own Logical Clock with the maximum of its own time and the time of the received message.&lt;/li&gt;
&lt;li&gt;It performs the required action&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Sx6VJ52M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fa45r9cl0qtmxqdur68n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Sx6VJ52M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fa45r9cl0qtmxqdur68n.png" alt="Lamport Clocks Message Transfer" width="880" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This allows us to directly compare and order the logical timestamps of multiple nodes.&lt;/p&gt;

&lt;p&gt;The order is determined by&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the timestamp of Event A is less than Event B, then Event A happened before Event B&lt;/li&gt;
&lt;li&gt;If the timestamps are same, then they are concurrent.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Lamports clocks are designed for a &lt;em&gt;Crash Stop&lt;/em&gt; model, but they are simple to implement in a &lt;em&gt;Crash Recovery&lt;/em&gt; model where the timestamp must be persisted on disk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pseduo Code for Lamport Clocks
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;on initialisation do
    t := 0
end on

on any event occuring in the node do
    t := t + 1
end on

on request to send message m do
    t := t + 1;
    send_message(m, t)
end on

on receiving (m, t1) do
    t := max(t, t1) + 1
    process_message(m)
end on
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Limitations of Lamport Clocks
&lt;/h2&gt;

&lt;p&gt;Lamport Clocks are easy to implement and assist us in reasoning about the happens before relationship in a distributed system.&lt;/p&gt;

&lt;p&gt;According to Lamport Clocks, if Event A caused Event B, then Event A's Lamport Time will be less than Event B's. However, we cannot reverse it and say that if Event A's timestamp is less than Event B's, then they are causally related.&lt;/p&gt;

&lt;p&gt;Because Lamport Timestamps cannot be used to infer causal relationships, we require a new type of Logical Clock that can assist us in determining causal relationships.&lt;/p&gt;

&lt;p&gt;In the next article, we'll look at Vector Clocks, which will help us detect causal relationships.&lt;/p&gt;

</description>
      <category>distributedsystems</category>
      <category>computerscience</category>
      <category>backendengineering</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>The Notion of Logical Time</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Thu, 23 Feb 2023 12:52:40 +0000</pubDate>
      <link>https://forem.com/sriramr98/the-notion-of-logical-time-311j</link>
      <guid>https://forem.com/sriramr98/the-notion-of-logical-time-311j</guid>
      <description>&lt;p&gt;In the previous two articles, we learned about &lt;a href="https://dev.to/sriramr98/physical-time-1nm9"&gt;Physical Time&lt;/a&gt; and the nuances of time.&lt;/p&gt;

&lt;p&gt;We concluded the thought by attempting to synchronise two clocks, but this presents a new challenge.&lt;/p&gt;

&lt;p&gt;The very idea of synchronising time means that when a drift is found, time can jump forward or backward depending on the drift.&lt;/p&gt;

&lt;p&gt;Consider the following scenario:&lt;br&gt;
![[Screenshot 2023-02-19 at 7.47.25 PM.png]]&lt;/p&gt;

&lt;p&gt;We wanted to calculate the time it takes to execute a function, but NTP intervenes and pushes time back.&lt;br&gt;
Now, when we figure out how much time has passed, startTime is greater than endTime, so the amount of time that has passed is negative.&lt;/p&gt;

&lt;p&gt;This is a significant issue because many critical usecases rely on calculating the time elapsed between two events.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use of timeouts&lt;/li&gt;
&lt;li&gt;TTL expiration&lt;/li&gt;
&lt;li&gt;Retry Timers&lt;/li&gt;
&lt;li&gt;Performance Evaluation, and so on.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Logical Time
&lt;/h2&gt;

&lt;p&gt;Looking at the usecase, all we need to know is how long something took. When we think about it, it has nothing to do with the exact date and time. We only need something like 'This function took 100ms to execute'.&lt;/p&gt;

&lt;p&gt;As a result, we created Logical Time. The time measured from an arbitrary point is known as logical time. In a computer, for example, we can consider booting up to be an initial action with time 'T=0' and increment this by one every second.&lt;/p&gt;

&lt;p&gt;This time does not indicate the date or time. It only knows that &lt;em&gt;X&lt;/em&gt; time has passed since something happened.&lt;/p&gt;

&lt;p&gt;This concept, it turns out, is very useful in detecting time differences between two points because there is no concept of time drift or skew here. This time can only go up and never down.&lt;/p&gt;

&lt;p&gt;We also discussed how to order events in &lt;a href="https://dev.to/sriramr98/message-ordering-23k8"&gt;Message Ordering&lt;/a&gt; and how &lt;a href="https://dev.to/sriramr98/physical-time-1nm9"&gt;Physical Time&lt;/a&gt; is ineffective for determining the order of two messages.&lt;/p&gt;

&lt;p&gt;Because logical time is only incremental, we can deduce that Event A occurs before Event B if Event A's logical timestamp is less than Event B's.&lt;/p&gt;

&lt;p&gt;Logical Time can also be used to detect Causality between two messages.&lt;/p&gt;

&lt;p&gt;There are two major algorithms of Logical Time&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lamport Clocks&lt;/li&gt;
&lt;li&gt;Vector Clocks&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both clocks will be examined in depth in subsequent articles.&lt;/p&gt;

</description>
      <category>help</category>
    </item>
    <item>
      <title>Message Ordering</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Wed, 22 Feb 2023 05:42:38 +0000</pubDate>
      <link>https://forem.com/sriramr98/message-ordering-23k8</link>
      <guid>https://forem.com/sriramr98/message-ordering-23k8</guid>
      <description>&lt;p&gt;The order in which messages are processed determines the final outcome of the actions in any distributed system. This is actually more difficult than it appears to be.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is the order of messages important?
&lt;/h2&gt;

&lt;p&gt;Consider the following scenario: a user dislikes a post but quickly realises they want to like it and does so almost immediately.&lt;/p&gt;

&lt;p&gt;Because network delays can vary, suppose the request for like arrives before the request for dislike. We would dislike the post if we processed the messages in the order they were received, but the user's intent was to like the post.&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%2Fma4ktuxmyazsyx1hkem8.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%2Fma4ktuxmyazsyx1hkem8.png" alt="Order Mismatch" width="800" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thus, determining the order of two messages is critical for our applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Ordering
&lt;/h2&gt;

&lt;p&gt;There are two ways to order.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Total Order&lt;/li&gt;
&lt;li&gt;Partial Order&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Total Order
&lt;/h3&gt;

&lt;p&gt;Given a set of events 'A,B,C,D,' there exists a single order of all messages in total order.&lt;/p&gt;

&lt;p&gt;For instance, if we have a set of numbers &lt;code&gt;7,4,5,9,1&lt;/code&gt; and want to order them using the &lt;code&gt;&amp;lt;&lt;/code&gt; relation, there is only one order &lt;code&gt;1,4,5,7,9&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Total order is used in single machine systems where it is easy to determine the order of events as they occur sequentially, and a global clock (the machine's clock) can be used for any parallel events.&lt;/p&gt;

&lt;p&gt;Total ordering, on the other hand, is not possible in distributed systems because different machines have different clocks and concurrent events cannot be ordered.&lt;/p&gt;

&lt;h3&gt;
  
  
  Partial Order
&lt;/h3&gt;

&lt;p&gt;Only some events can be ordered in a partial order, while others cannot. As a result, multiple orders are generated for a given set of values.&lt;/p&gt;

&lt;p&gt;For example, if we have a list of events 'A,B,C,D' and we know that events A and B are ordered, but Event C occurred at the exact same millisecond as Event A and Event D occurred at the exact same millisecond as Event B, there is no way to order these four events in a single order because they occurred concurrently.&lt;/p&gt;

&lt;p&gt;Partial Order is commonly used in Distributed Systems because some events occur in order while others do not, which is consistent with the theme of Partial Ordering.&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%2Fdme3unytfymas6ngfyva.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%2Fdme3unytfymas6ngfyva.png" alt="Partial Order" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Happens Before Relation
&lt;/h2&gt;

&lt;p&gt;Given two events A and B, we say that A happens before B ('A -&amp;gt; B') if&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Event A occurs before Event B in the Node Execution Order&lt;/li&gt;
&lt;li&gt;Event A is the transmission of Message M, and Event B is the reception of the same message, because a message cannot be received before it is transmitted.&lt;/li&gt;
&lt;li&gt;There is a Message C such that A occurs before C and C occurs before B.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If we are unable to establish a &lt;em&gt;happens-before&lt;/em&gt; relationship between two messages, it indicates that they occurred concurrently. ( &lt;code&gt;a || b&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%2Frkulgirz5hquos8i0t3z.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%2Frkulgirz5hquos8i0t3z.png" alt="Happens Before" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this diagram &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Events A and B occurred in the same node, and A occurred before B in the execution order of the node.&lt;/li&gt;
&lt;li&gt;Event A is the transmission of Message M, and Event B is the arrival of the same message, because a message cannot be received before it is transmitted.&lt;/li&gt;
&lt;li&gt;There is a Message C such that A occurs before C and C occurs before B.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Causality
&lt;/h2&gt;

&lt;p&gt;Take, for example, social media. Do we really care about the order in which we see two unrelated posts? Most likely not.&lt;br&gt;
As a result, the system could use Partial Ordering here, where posts that cannot be ordered are displayed in a random order.&lt;/p&gt;

&lt;p&gt;However, there are some events that must be shown in chronological order. For example, if User A responds to Comment C1 by User B, User A's comment must appear after User B's comment. The conversation would be difficult to follow otherwise.&lt;/p&gt;

&lt;p&gt;What we described in the preceding example is the concept of &lt;em&gt;Causality&lt;/em&gt;, which states that one event contributes to the occurrence of another, i.e. Event B occurred solely because Event A occurred.&lt;/p&gt;

&lt;p&gt;Based on what we've seen so far, we can also conclude that if Event A happened before Event B, then Event A may have caused Event B. It is critical to emphasise that this is a possibility, not a guarantee.&lt;br&gt;
However, if Events A and B occur concurrently, we can be certain that Event A did not cause Event B and vice versa.&lt;/p&gt;

&lt;h3&gt;
  
  
  Establishing Causal Ordering
&lt;/h3&gt;

&lt;p&gt;Assume we want to determine the causal ordering of two events. Is it accurate to rely on &lt;a href="https://dev.to/sriramr98/physical-time-1nm9"&gt;Physical Time&lt;/a&gt;, especially since two nodes can drift at any point in time?&lt;/p&gt;

&lt;p&gt;This gives rise to the concept of Logical Time, in which we don't need the date and time to order events causally. In the following article, we'll go over the specifics of Logical Time.&lt;/p&gt;

</description>
      <category>express</category>
      <category>node</category>
      <category>developer</category>
      <category>motivation</category>
    </item>
    <item>
      <title>Physical Time</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Tue, 21 Feb 2023 05:37:52 +0000</pubDate>
      <link>https://forem.com/sriramr98/physical-time-1nm9</link>
      <guid>https://forem.com/sriramr98/physical-time-1nm9</guid>
      <description>&lt;h2&gt;
  
  
  Physical Time
&lt;/h2&gt;

&lt;p&gt;Have you ever thought about how time works? How do your wall clocks and phone clocks know when a second has passed?&lt;/p&gt;

&lt;p&gt;Why do different timezones around the world have different times for the same event?&lt;br&gt;&lt;br&gt;
Why does a clock in India show 10:30 AM on February 19th, but a clock in the United States shows 11:00 PM on February 18th?&lt;/p&gt;

&lt;p&gt;Even if we are convinced that there are differences in how the sun rises and sets and have adjusted the time accordingly, how do you justify your watch and wall clock showing different times, even if the difference is only in minutes?&lt;/p&gt;

&lt;p&gt;This is strange. Time was supposed to be something we could rely on, but it turns out to be more complicated than we thought.&lt;/p&gt;

&lt;p&gt;Understanding how time works and its nuances is critical when developing distributed systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  History of Time
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Egyptian Standard Time
&lt;/h3&gt;

&lt;p&gt;The first recorded time measurements were made in Egypt around 1500 B.C. They divided the time between sunrise and sunset into 12 equal parts. However, this meant that each section was not identical, as sunrise and sunset are not identical.&lt;br&gt;&lt;br&gt;
This was the first definition of time based on the motion of the earth and the sun.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quartz Clock
&lt;/h3&gt;

&lt;p&gt;As time passed, scientists began to notice an unusual behaviour in a material known as &lt;em&gt;Quartz&lt;/em&gt;. Quartz is a piezoelectric material, which means that when electricity is passed through it, it generates mechanical stress, causing it to vibrate.&lt;/p&gt;

&lt;p&gt;Scientists discovered that under static conditions, the vibration frequency remained constant no matter where you went. Quartz’s frequency can be controlled by the crystal’s size, shape, and temperature.&lt;br&gt;&lt;br&gt;
This made the material ideal for creating a new mechanical clock because you could adjust its vibration and define time based on it.&lt;/p&gt;

&lt;p&gt;This was a revolution in timekeeping. Quartz crystals are inexpensive to produce, and minor changes in environmental conditions have little effect on frequency.&lt;/p&gt;

&lt;p&gt;Even today, Quartz Clocks are used in computers, watches, clocks, microwaves, and pretty much any other device that keeps time.&lt;/p&gt;

&lt;h4&gt;
  
  
  Cons of Quartz Clocks
&lt;/h4&gt;

&lt;p&gt;This, however, was not a perfect solution. The size, shape, and temperature of a Quartz Crystal determine its frequency. It is impossible to make identical crystals because they must be cut. There will always be minor differences and manufacturing defects, no matter how hard we try.&lt;/p&gt;

&lt;p&gt;This difference causes time differences between the two clocks, also known as &lt;em&gt;Skew&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This is why we rarely notice a difference in time between two devices.&lt;/p&gt;

&lt;h3&gt;
  
  
  Atomic Clocks
&lt;/h3&gt;

&lt;p&gt;As science progressed, scientists discovered atoms and discovered that each atom has its own frequency. Because these frequencies are inherently stable over space and time, the idea of using atomic frequency to build clocks was proposed.&lt;/p&gt;

&lt;p&gt;Many atoms were tested, but &lt;em&gt;Ceasium&lt;/em&gt; emerged as the winner in accurately measuring time, and clocks using &lt;em&gt;Ceasium&lt;/em&gt; to tell time were built.&lt;br&gt;&lt;br&gt;
These timepieces were known as &lt;strong&gt;Atomic Clocks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The frequency was so stable that it was formally recognised as the international time unit.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;1 second = 9,192,631,770 Ceasium-113 atom frequency oscillations&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is so precise that Atomic Clocks have an error of one over three million years.&lt;/p&gt;

&lt;h4&gt;
  
  
  Problems with Atomic Clock
&lt;/h4&gt;

&lt;p&gt;You should have realised by now that nothing is perfect, including atomic clocks. The issue with atomic clocks is not their margin of error, but rather their high cost. Atomic clocks cost around $1500, making them prohibitively expensive for use in every device. Cheap atomic clocks are available, but they are not as accurate.&lt;/p&gt;

&lt;p&gt;This is why, even today, we use Quartz Clocks, which are less expensive and more convenient.&lt;/p&gt;

&lt;p&gt;Satellites and GPS systems use atomic clocks.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, what’s up with time zones?
&lt;/h2&gt;

&lt;p&gt;They recognised that this still does not work with the motion of the earth as we built highly accurate atomic clocks.&lt;br&gt;&lt;br&gt;
When you define AM as morning and PM as evening, you must be consistent.&lt;br&gt;&lt;br&gt;
However, because of the earth’s motion, when one half of the world experiences day, the other half experiences night.&lt;/p&gt;

&lt;p&gt;By this point, we had two definitions of time: * GMT — Time defined by the rotation of the Earth, and * TAI — International Atomic Time based on Atomic Clocks.&lt;/p&gt;

&lt;p&gt;We needed a means to strike a balance between these two formats of time, and thus the UTC was born.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coordinated Universal Time — UTC
&lt;/h2&gt;

&lt;p&gt;UTC is essentially TAI with corrections for the TAI definition of a second and the rotation of the Earth. This is known as &lt;em&gt;Leap Seconds&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The difference is calculated on a regular basis, and extra seconds are either added to or subtracted from TAI depending on how many Leap Seconds occurred in that timeframe.&lt;/p&gt;

&lt;p&gt;Understanding how it’s calculated and how these differences are applied isn’t critical for us, but it is important to know that something like this occurs in the real world.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time Synchronization
&lt;/h2&gt;

&lt;p&gt;We saw how time can differ between devices, causing Skew. However, as you may have noticed, this drift no longer occurs in digital systems. This is due to NTP (Network Time Protocol).&lt;/p&gt;

&lt;p&gt;Network Time Protocol (NTP)&lt;br&gt;&lt;br&gt;
This protocol was designed to synchronise time in devices all over the world.&lt;br&gt;&lt;br&gt;
As previously stated, Atomic Clocks provide the most accurate time measurement, but they are too expensive to include in every device.&lt;/p&gt;

&lt;p&gt;So, instead of having it in every machine, why not have it in thousands of machines all over the world, with normal machines communicating with these special servers to synchronise time?&lt;/p&gt;

&lt;p&gt;On a high level, this is how NTP operates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Skew Adjustment
&lt;/h3&gt;

&lt;p&gt;Assume your computer communicated with an NTP server and discovered a 200ms skew from the standard time.&lt;/p&gt;

&lt;p&gt;Your computer’s NTP client adjusts the time without your knowledge. This is accomplished through the use of Time Smearing.&lt;/p&gt;

&lt;p&gt;Because softwares rely on time and instructions on a computer run in nanoseconds, a time correction in the order of milliseconds can be disastrous. As a result, when a drift is detected, the difference is applied incrementally over several minutes.&lt;br&gt;&lt;br&gt;
Consider adding 200ms over the course of 10 minutes. The time difference will be insignificant.&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>ui</category>
      <category>mobile</category>
      <category>chatgpt</category>
    </item>
    <item>
      <title>Failure Detectors</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Mon, 20 Feb 2023 02:16:44 +0000</pubDate>
      <link>https://forem.com/sriramr98/failure-detectors-5hcc</link>
      <guid>https://forem.com/sriramr98/failure-detectors-5hcc</guid>
      <description>&lt;p&gt;In distributed systems, it is crucial to understand failure and how to mitigate it in order to maintain high availability.&lt;br&gt;
As we saw in &lt;a href="https://dev.to/sriramr98/distributed-system-models-1k2a"&gt;System Models&lt;/a&gt;, the characteristics of Nodes and Network in distributed systems make it difficult to detect failure, particularly given the asynchronous nature of networks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is it so difficult?
&lt;/h2&gt;

&lt;p&gt;Most of the time, we find out about failures by sending a request to a node and waiting for a response for a certain amount of time. If we try X times and don't get a response, we say that the node has failed.&lt;/p&gt;

&lt;p&gt;But it's not that easy. We only know that our requests to the node went unanswered X times. We don't know if the node has crashed, if there's a problem with the network between nodes, or if the node is just slow to respond because it's busy with other work.&lt;/p&gt;

&lt;p&gt;A node might not respond for more than one reason. If that's the case, how do we tell the difference between these scenarios to know for sure if a node has crashed?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JvLwTO4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2p5ryq0ay099b03wkzh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JvLwTO4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2p5ryq0ay099b03wkzh.png" alt="Fault Detector Scenario" width="722" height="827"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we rely on timeouts, we can also get false positives. Think about this situation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fsiOpjia--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gw3lku2m900snu45fqm6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fsiOpjia--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gw3lku2m900snu45fqm6.png" alt="Timeout problem" width="605" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, Node B crashed as soon as it replied. But since Node B answered before the crash, Node A now thinks that Node B is still alive, even though it isn't.&lt;/p&gt;

&lt;p&gt;The timeout you choose is also a very important part of how accurate the results are.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you choose a short timeout, there will be a lot more false positives because the node might still be alive, but it took longer than the set timeout to process the message. Choosing a longer timeout is also not a good idea because the node could have crashed and you could be waiting for a response that will never come.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Failure Detector
&lt;/h2&gt;

&lt;p&gt;A failure detector is an algorithm that checks to see if other nodes in the network have failed.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Failure Detectors Work
&lt;/h3&gt;

&lt;p&gt;We can tell the different types of failure detectors apart by looking at two basic features that show the different trade-offs.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Completeness&lt;/strong&gt; is the percentage of crashed nodes that a "failure detector" is able to find in a certain amount of time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accuracy&lt;/strong&gt; is the number of mistakes that a failure detector makes in a certain amount of time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Perfect Failure Detector
&lt;/h4&gt;

&lt;p&gt;A perfect failure detector is an algorithm that is both Complete and Accurate to the highest degree. This detector detects every faulty process without assuming a node has crashed before it actually does and has no margin of error.&lt;/p&gt;

&lt;p&gt;Unfortunately, as we saw earlier, building a Perfect Failure Detector in purely Asynchronous Systems is impossible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Eventually Perfect Failure Detectors
&lt;/h3&gt;

&lt;p&gt;Perfect Failure Detectors are impossible in Asynchronous Distributed Systems, so we use Eventually Perfect Failure Detectors instead.&lt;/p&gt;

&lt;p&gt;These detectors have the following qualities&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A node can be marked as "crashed" even if it's still alive&lt;/li&gt;
&lt;li&gt;A node may be temporarily labelled as alive even if it has crashed.&lt;/li&gt;
&lt;li&gt;Eventually, a node will be marked as crashed only if it has really crashed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A perfect example of an Eventually Perfect Failure Detector is a timeout. When a timeout ends, we might not know right away if the node has crashed, but if we keep trying and the node doesn't respond for more than X times, we know it has crashed and mark it as crashed.&lt;/p&gt;

</description>
      <category>distributedsystems</category>
    </item>
    <item>
      <title>Exactly Once Processing</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Sun, 19 Feb 2023 04:58:44 +0000</pubDate>
      <link>https://forem.com/sriramr98/exactly-once-processing-56ed</link>
      <guid>https://forem.com/sriramr98/exactly-once-processing-56ed</guid>
      <description>&lt;h2&gt;
  
  
  Multiple Deliveries of a Message
&lt;/h2&gt;

&lt;p&gt;Nodes in a distributed system talk to each other over a network.&lt;br&gt;
As we saw in &lt;a href="https://dev.to/sriramr98/distributed-system-models-2gh6"&gt;System Models&lt;/a&gt;, most networks are Fair Loss Links, where data can be lost while in transit.&lt;/p&gt;

&lt;p&gt;We use retries to deal with this. When a node finds out that the message it tried to send might not have reached its destination, it tries again in the hopes that the message will eventually get through.&lt;/p&gt;

&lt;p&gt;This seems like a good way to solve the problem, but there's a catch.&lt;br&gt;
When a node sends a message, it waits for the receiving node to confirm that it got the message. There is a chance, though, that the message was received at its destination but that the acknowledgment message was lost on the way.&lt;/p&gt;

&lt;p&gt;Imagine this happening!&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%2Frdv7scd5p1n6mzg3su6b.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%2Frdv7scd5p1n6mzg3su6b.png" alt="Retry Error Scenario" width="603" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the message was received by Node B, but the acknowledgement got lost along the way. Not knowing this, Node A did a retry, which means Node B got the same message twice and processed it twice.&lt;/p&gt;
&lt;h3&gt;
  
  
  Real World Example
&lt;/h3&gt;

&lt;p&gt;Imagine that you're sending money to someone.&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%2Fp9z7hl3kgyt10qo5mux5.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%2Fp9z7hl3kgyt10qo5mux5.png" alt="Retry Real World Example" width="748" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a bad situation because you're transferring money twice when you only meant to do it once.&lt;/p&gt;
&lt;h2&gt;
  
  
  Difference between Delivery and Processing
&lt;/h2&gt;

&lt;p&gt;Before we can figure out how to avoid these kinds of situations, we need to know how Delivery and Processing are different.&lt;/p&gt;

&lt;p&gt;Delivery is the hardware-level event of a message arriving in a specific node while processing is taking action on the received message on the Software Layer.&lt;/p&gt;

&lt;p&gt;For example, if you send a request to a server, it is called "Delivery" when the server gets the request, and "Processing" when the server does something with the request.&lt;/p&gt;

&lt;p&gt;This is important to know because we can't stop duplicate messages from being sent. Networks aren't reliable, and it's impossible to stop duplicate messages from being sent.&lt;/p&gt;

&lt;p&gt;But we can use algorithms that act on duplicate messages to make sure that duplicate actions are only processed once.&lt;/p&gt;

&lt;p&gt;So it's important to know that "exactly-once delivery" is not possible, but "exactly-once processing" is.&lt;/p&gt;
&lt;h2&gt;
  
  
  How do we avoid this?
&lt;/h2&gt;

&lt;p&gt;To deal with these kinds of situations, we take precautions to make sure that a node only processes a message once, even if it is sent more than once.&lt;/p&gt;
&lt;h3&gt;
  
  
  Idempotency
&lt;/h3&gt;

&lt;p&gt;An operation is idempotent if an action can be repeated multiple times with the same result.&lt;/p&gt;
&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;p&gt;Let's say you work for a social media site and your job is to build the "likes" feature.&lt;/p&gt;

&lt;p&gt;There is a chance that you could like a post more than once because of retries, which we talked about above.&lt;/p&gt;

&lt;p&gt;Let's look at two ways to do this and see which one is better.&lt;/p&gt;
&lt;h5&gt;
  
  
  Method 1
&lt;/h5&gt;

&lt;p&gt;We keep track of how many people like each post, and when we get a new like, we add one.&lt;/p&gt;

&lt;p&gt;This operation is not retryable because if you use the increment operation more than once, you will end up with duplicate likes.&lt;/p&gt;

&lt;p&gt;This operation is not retryable because if you use the increment operation more than once, you will end up with duplicate likes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;newLikeCount = oldLikeCount + 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Method 2
&lt;/h5&gt;

&lt;p&gt;We keep a set of users who have liked a specific post, and for each new like, we add the 'userId' to that list.&lt;br&gt;
The property of a set is that it will not contain any duplicates.&lt;/p&gt;

&lt;p&gt;This operation is idempotent because even if you try it more than once, the result won't change. It will still show that the user has liked the post once, since a set doesn't have any duplicates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;usersLiked = post.getExistingLikedUsers().add(userId);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Remove duplicates
&lt;/h3&gt;

&lt;p&gt;There are times when it's hard to get idempotency. In these cases, the caller module will send a unique ID to the processing module, which will keep track of every action it takes. If it gets a duplicate request with the same ID, the processing module throws away the message.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;Sending an email isn't an idempotent action, and you can't make it one. Instead, when a new email is sent, you can send a UniqueID for that particular email. Because it tracks the process using the Unique ID, the application sending the mail won't send it again if it has already sent it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Idempotency and Deduplication: Pros and Cons
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Idempotency is easier to set up and manage because nodes don't have to work together. Deduplication, on the other hand, needs all the nodes in your system to work together. This is because the original request and the retry request could end up in different nodes. &lt;/li&gt;
&lt;/ol&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%2Ftjd7nor0xmfenixcf084.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%2Ftjd7nor0xmfenixcf084.png" alt="Deduplication" width="671" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Since idempotency is built into the processing module, it can be used even if you don't have control over the caller module. But for DeDuplication, you need to be in charge of the caller module because it generates IDs for every request.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>biotech</category>
      <category>science</category>
    </item>
    <item>
      <title>Byzantine Generals Problem</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Sat, 18 Feb 2023 12:49:25 +0000</pubDate>
      <link>https://forem.com/sriramr98/byzantine-generals-problem-3adf</link>
      <guid>https://forem.com/sriramr98/byzantine-generals-problem-3adf</guid>
      <description>&lt;p&gt;Up until now, we thought that none of the nodes could be malicious and try to do something other than what they were supposed to do. Unfortunately, because there is inherent evil in the real world, any node has the potential to turn evil and attempt to upset the peace among other nodes.&lt;/p&gt;

&lt;p&gt;In this example, we will look at what could go wrong if a node turned out to be bad.&lt;/p&gt;

&lt;p&gt;You might be asking. What do you mean when you say someone is malicious? How does this affect building systems? Here's a good example:&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Similar to the &lt;a href="https://dev.to/sriramr98/two-generals-problem-2ifk"&gt;Two Generals Problem&lt;/a&gt; we saw in the previous article, let's imagine a theoretical situation where multiple generals want to attack a fort.&lt;/p&gt;

&lt;p&gt;In the Two Generals Problem, we saw that the messages between two generals could be intercepted. Let's assume, for the sake of simplicity, that in this situation, all messages are sent to the other generals and none are lost.&lt;/p&gt;

&lt;p&gt;But it's possible that one of the generals is a traitor who really works for the fort and is trying to stop the plan of the other generals to attack. Unluckily for the honest generals, there's no way to know which one or possibly more than one general is a traitor.&lt;/p&gt;

&lt;p&gt;Let's say a successful attack is when all honest generals attack, and a successful retreat is when all honest generals retreat. Even if one honest general attacks when others retreat or retreats when others attack, our solution is flawed.&lt;/p&gt;

&lt;p&gt;Given these restrictions, the honest generals will have to agree on whether to attack or retreat.&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%2Fc8twzahctb91x320sulw.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%2Fc8twzahctb91x320sulw.png" alt="Byzantine Problem" width="800" height="675"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario
&lt;/h2&gt;

&lt;p&gt;Let's think about these two situations.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;General 1&lt;/em&gt; tells both &lt;em&gt;General 2&lt;/em&gt; and &lt;em&gt;General 3&lt;/em&gt; to attack, but &lt;em&gt;General 2&lt;/em&gt; tells &lt;em&gt;General 3&lt;/em&gt; that &lt;em&gt;General 1&lt;/em&gt; told him to retreat.  In this situation, &lt;em&gt;General 2&lt;/em&gt; is the traitor. &lt;/li&gt;
&lt;/ol&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%2F0scakdrutw3bgocuccil.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%2F0scakdrutw3bgocuccil.png" alt="General 1 being a traitor" width="800" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;"General 1" told "General 3" to attack, but "General 2" was instructed to retreat. Since &lt;em&gt;General 2&lt;/em&gt; has to tell &lt;em&gt;General 3&lt;/em&gt;, &lt;em&gt;General 3&lt;/em&gt; again gets two instructions from &lt;em&gt;General 1&lt;/em&gt; that differ from one another. In this situation, &lt;em&gt;General 1&lt;/em&gt; is the traitor. &lt;/li&gt;
&lt;/ol&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%2F89azjeioqgxn1voxnlrj.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%2F89azjeioqgxn1voxnlrj.png" alt="General 2 being a traitor" width="800" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we are the central entity monitoring all communications, it is clear to us who the traitor is. But when there isn't a central entity keeping an eye on all messages, it's impossible for &lt;em&gt;General 3&lt;/em&gt; to tell who the traitor is and, by extension, what the valid message is.&lt;/p&gt;

&lt;p&gt;This is called "Byzantine Behaviour," and we saw a little bit of it in &lt;a href="https://dev.to/sriramr98/distributed-system-models-2gh6"&gt;System Models&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Real Life Scenario
&lt;/h2&gt;

&lt;p&gt;The most well-known Byzantine System is Blockchain. Because it is distributed and decentralised by design, it is bound to have nodes that behave in a Byzantine way.&lt;/p&gt;

&lt;p&gt;User A claims to have given User B $200 while User B claims to have received $100 from User A. Which one of these is the Byzantine Node?&lt;/p&gt;

&lt;p&gt;Byzantine systems are also used when you add a third party to your system, like a payment gateway.&lt;br&gt;
Byzantine systems are also used when you add a third party to your system, like a payment gateway.&lt;br&gt;
Let's say you're building an online wallet solution and the user says he added Rs 200 to the wallet but the payment gateway says he only added Rs 100. &lt;br&gt;
Who is the Byzantine one here? &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%2Fe8p13ojg63920io9oqce.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%2Fe8p13ojg63920io9oqce.png" alt="Real Life Byzantine" width="800" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To handle these behaviours, &lt;a href="https://www.educative.io/answers/what-is-quorum-in-distributed-systems" rel="noopener noreferrer"&gt;Quorum&lt;/a&gt; is used, where the truth is what the majority of nodes agree to. Blockchain uses &lt;a href="https://cointelegraph.com/blockchain-for-beginners/how-does-blockchain-solve-the-byzantine-generals-problem" rel="noopener noreferrer"&gt;Proof of Work&lt;/a&gt; to deal with these kinds of mistakes.&lt;/p&gt;

</description>
      <category>challenge</category>
    </item>
    <item>
      <title>Two Generals Problem</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Fri, 17 Feb 2023 17:12:24 +0000</pubDate>
      <link>https://forem.com/sriramr98/two-generals-problem-2ifk</link>
      <guid>https://forem.com/sriramr98/two-generals-problem-2ifk</guid>
      <description>&lt;p&gt;This is a thought experiment designed to help us understand a very basic problem in distributed systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario
&lt;/h2&gt;

&lt;p&gt;There are three cities named A, B, and C. Both City B and City C's generals want to attack City A, but City A is well-defended enough that neither City B nor City C can win an invasion against City A on their own.&lt;/p&gt;

&lt;p&gt;The smart generals in Cities B and C come up with a plan. They think that if they both attack City A at the same time, they will be able to take it down.&lt;/p&gt;

&lt;p&gt;Both generals agree to do their own reconnaissance and send a messenger when they find the best time to attack.&lt;/p&gt;

&lt;p&gt;The forest between City B and City C is also connected to City A. When City A hears about this plan, it sends people to stop any messengers going through the forest to stop them from carrying out their plan.&lt;/p&gt;

&lt;p&gt;This means that it's possible that City C will never get a message sent by City B, and the same is true for City B.&lt;/p&gt;

&lt;p&gt;Our challenge is to come up with a way for both cities to agree on when to attack City A.&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%2Fswbjsa1xavad79aonhdd.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%2Fswbjsa1xavad79aonhdd.png" alt="Problem Statement" width="522" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Solution 1
&lt;/h3&gt;

&lt;p&gt;City B knows that City A might catch its messenger, so it sends more than one, hoping that one of them will get through. Then, City B attacks City A without hearing back from City A, hoping that at least one of its messengers would have reached City C.&lt;/p&gt;

&lt;p&gt;But there's a good chance that City A will catch all the messengers, and City B will lose because City C never got the message.&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%2Fce678rvnre66umli7v6h.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%2Fce678rvnre66umli7v6h.png" alt="Solution 1" width="562" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution 2
&lt;/h3&gt;

&lt;p&gt;What if City B waited until it heard back from City C that it got the message before attacking? So, City B won't have to fight a battle it can't win, right?&lt;/p&gt;

&lt;p&gt;City B might not be fighting a losing battle, but City C might be, since the messenger sent by City C to confirm the invasion date could be caught by City A, and City C has no way of knowing if their confirmation got through.&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%2F1bkdlz2cumq1vsvwm8qj.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%2F1bkdlz2cumq1vsvwm8qj.png" alt="Solution 2" width="639" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Is this practical?
&lt;/h2&gt;

&lt;p&gt;Sure, and it happens more often than you think.&lt;/p&gt;

&lt;p&gt;Imagine we are building an online shop and want to connect it to a Payment Service.&lt;/p&gt;

&lt;p&gt;Customer, Online Shop, and Payment Service must all agree on a common message ( Money Charged ).&lt;/p&gt;

&lt;p&gt;It's possible that the money was charged to the customer, but when the payment service tried to tell you, the message got lost. This is a problem because money is taken from the customer but our Online Shop doesn't know it.&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%2F5m7yrae10pmidfq31upm.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%2F5m7yrae10pmidfq31upm.png" alt="Practical Scenario" width="564" height="642"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  If this really happens, how do online shops work?
&lt;/h3&gt;

&lt;p&gt;In real life, there are reconciliation systems that track these types of extra money movement and delay your refunds unlike our thought experiment where there was no scope of fixing things later.&lt;/p&gt;

&lt;p&gt;If you've ever had a problem with Amazon or Swiggy where your payment went through but your order wasn't placed, even if Swiggy didn't know about it right away and couldn't place your order, it eventually learned about it and processed your refund, and you'd have received your money back.&lt;/p&gt;

&lt;p&gt;There's nothing we can do to keep messages from getting lost, but we can figure out what went wrong and fix it later.&lt;/p&gt;

</description>
      <category>vibecoding</category>
    </item>
    <item>
      <title>Distributed System Models</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Fri, 17 Feb 2023 03:18:44 +0000</pubDate>
      <link>https://forem.com/sriramr98/distributed-system-models-1k2a</link>
      <guid>https://forem.com/sriramr98/distributed-system-models-1k2a</guid>
      <description>&lt;h2&gt;
  
  
  System Models
&lt;/h2&gt;

&lt;p&gt;As we saw in the first part of this series, a distributed system is made up of two parts: the Node and the Network. Based on how these two parts work, we can create different kinds of behaviour that should be taken into account when building distributed systems. We call these &lt;em&gt;System Models.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The behaviours we use to create variations are usually based on two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How the different parts of a distributed system work together.&lt;/li&gt;
&lt;li&gt;How the parts of a distributed system fail.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Network Behavior
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Reliable Link
&lt;/h4&gt;

&lt;p&gt;A reliable link says that if you send a message across a network, it will be delivered to its destination every time.&lt;br&gt;
Most of the time, these links are used in single-machine systems where components can talk to each other reliably.&lt;/p&gt;

&lt;h4&gt;
  
  
  Fair Loss Link
&lt;/h4&gt;

&lt;p&gt;A fair loss link means that a message sent across a network might get lost, duplicated, or reordered. This can be changed to a Reliable Link if we keep trying in case we lose the connection. If we keep trying, the message will eventually get to its destination, but there's no way to know when it will get there by the latest. In theory, it could take up to 100 years.&lt;/p&gt;

&lt;h4&gt;
  
  
  Arbitrary Link
&lt;/h4&gt;

&lt;p&gt;This link says that any party can intercept messages sent between nodes and change, spoof, eavesdrop on, block, or replay messages.&lt;/p&gt;

&lt;p&gt;This model is a very good representation of what happens when we use the internet in places like Starbucks or a Coffee Shop that aren't very reliable. The owner of the Coffee Shop can easily exploit the network packets and use them in a bad way.&lt;/p&gt;

&lt;p&gt;With the arrival of &lt;a href="https://www.cloudflare.com/en-gb/learning/ssl/transport-layer-security-tls" rel="noopener noreferrer"&gt;TLS&lt;/a&gt;, it's no longer possible to intercept packets, but that doesn't stop a third party from blocking the communication.&lt;/p&gt;

&lt;h3&gt;
  
  
  Node Behaviour
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Crash Stop
&lt;/h4&gt;

&lt;p&gt;This model says that when a node becomes faulty, it will never recover and cease to function permanently.&lt;br&gt;
For example, if you drop your phone under a train, it won't work again.&lt;/p&gt;

&lt;h4&gt;
  
  
  Crash Recovery
&lt;/h4&gt;

&lt;p&gt;In this model, a node that stops working properly can get back to normal after any amount of time.&lt;br&gt;
For example, if the operating system in a virtual machine (VM) crashes, a machine restart can fix it and make the node healthy again.&lt;/p&gt;

&lt;h4&gt;
  
  
  Byzantine
&lt;/h4&gt;

&lt;p&gt;If a node departs from what it is supposed to perform, it is deemed to be defective. A byzantine node can break for no clear reason or reason at all.&lt;/p&gt;

&lt;p&gt;This typically occurs when a hostile actor or a flaw compromises the node's algorithm.&lt;/p&gt;

&lt;p&gt;There's a famous thought experiment called the &lt;a href="https://medium.com/coinmonks/a-note-from-anthony-if-you-havent-already-please-read-the-article-gaining-clarity-on-key-787989107969" rel="noopener noreferrer"&gt;Byzantine General Problem&lt;/a&gt; that explains byzantine behaviour in detail.&lt;/p&gt;

&lt;p&gt;Blockchains are a great example of a Byzantine system, and all of the algorithms built for them assume that they will behave in a Byzantine way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Timing Behaviour
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Synchronous System
&lt;/h4&gt;

&lt;p&gt;Every synchronous system sets a maximum time limit for a message to reach its destination and a maximum expected duration for a message to be processed.&lt;/p&gt;

&lt;p&gt;For example, if you want to write something into RAM, the RAM has guarantees about how long it could take in the worst case.&lt;/p&gt;

&lt;p&gt;Creating a synchronous distributed system is nearly impossible, and presuming Synchrony can be devastating.&lt;/p&gt;

&lt;p&gt;Let's say you think a node can process a message in 5ms, but in the middle of the process, the Operating System does a &lt;a href="https://www.javatpoint.com/what-is-the-context-switching-in-the-operating-system" rel="noopener noreferrer"&gt;Context Switch&lt;/a&gt; or a [long GC pause](&lt;a href="https://docs.datastax.com/en/dse-trblshoot/doc/t" rel="noopener noreferrer"&gt;https://docs.datastax.com/en/dse-trblshoot/doc/t&lt;/a&gt;&lt;br&gt;
In this case, the assumption is wrong, and your system goes down with it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Asynchronous System
&lt;/h4&gt;

&lt;p&gt;We make no assumptions about processing time or message delivery time across a network in this model. Any message can be delayed at random and without warning.&lt;/p&gt;

&lt;p&gt;Algorithms built for asynchronous systems are very strong because they are not affected by network delays or latency. However, building asynchronous systems is hard.&lt;/p&gt;

&lt;h4&gt;
  
  
  Partially Synchronous System
&lt;/h4&gt;

&lt;p&gt;In this model, we assume that the system is mostly synchronous, but that it can randomly change into an asynchronous system. This provides a decent compromise between synchronous and asynchronous systems.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Why should you care about System Models?
&lt;/h2&gt;

&lt;p&gt;Your Distributed System is only as strong as the assumptions you use to develop it. These models show how different systems can work, and if you make a wrong assumption, your system could break.&lt;/p&gt;

&lt;p&gt;For instance, when blockchain algorithms were developed, they assumed a Byzantine Node Behaviour, which means any node might be evil because blockchain algorithms must be precise even if someone tries to tamper with the ledger. If they had assumed a Crash-Stop model rather than Byzantine, Blockchain algorithms would have been flawed since they would not have handled the scenario in which a node may be bad, and malicious people would have exploited that weakness.&lt;/p&gt;

&lt;p&gt;As a result, understanding System Models and selecting an accurate System Model based on your use case is critical when designing large-scale systems.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Fallacies of Distributed System</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Thu, 16 Feb 2023 09:18:15 +0000</pubDate>
      <link>https://forem.com/sriramr98/fallacies-of-distributed-system-2mb2</link>
      <guid>https://forem.com/sriramr98/fallacies-of-distributed-system-2mb2</guid>
      <description>&lt;p&gt;The fallacies of distributed computing are a list of false assumptions that architects and developers of distributed systems may make. In this post, we'll look at what these fallacies are, how they came to be, and how to avoid them to build reliable distributed systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  The network is reliable
&lt;/h2&gt;

&lt;p&gt;When we build systems with only one machine, we usually assume that the components can talk to each other in a reliable way. All the parts are in one computer, so talking to RAM or HDD is easy and doesn't cause a lot of communication errors.&lt;/p&gt;

&lt;p&gt;When we build distributed systems, our computers are spread out over a network, which is not very reliable.&lt;/p&gt;

&lt;p&gt;There have been times when Google Cloud's network went down, and when it was looked into, it was found that &lt;a href="https://www.theguardian.com/technology/2014/aug/14/google-undersea-fibre-optic-cables-shark-attacks" rel="noopener noreferrer"&gt;sharks were nibbling on the underwater optic cable&lt;/a&gt; that powers their network.&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%2Fhz16iwxdlyzgwlt5race.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%2Fhz16iwxdlyzgwlt5race.png" alt="Shark Attack" width="573" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are many things that could cause a network to go down. Because of this, whenever we build distributed systems, we should always assume that the network is not reliable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Latency is Zero
&lt;/h2&gt;

&lt;p&gt;When we build systems with just one computer, we assume there is no latency because reading from RAM, HDD, or SSD takes almost no time.&lt;/p&gt;

&lt;p&gt;But when it comes to distributed systems, networks do have latency because the machines can be in different places and the signals have to travel from one machine to the next.&lt;/p&gt;

&lt;p&gt;There's also a chance that your computers are on different continents, which makes the difference in latency even more obvious.&lt;/p&gt;

&lt;p&gt;Always think of latency as a cost that you have to try to avoid, and we should try to keep this cost as low as possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bandwidth is Infinite
&lt;/h2&gt;

&lt;p&gt;It's a mistake to think that you can add more data to a network channel without taking its limits into account.&lt;/p&gt;

&lt;p&gt;Let's say you make a website that pulls 5MB of data from your servers every time it loads. As the size of your business grows, your systems will soon reach a limit because of bandwidth limits.&lt;/p&gt;

&lt;p&gt;This problem can't be fixed by adding more nodes, so be careful with every bit you send or receive.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Network is Secure
&lt;/h2&gt;

&lt;p&gt;Every system with many parts needs a network. Without a network, nodes can't talk to each other. But thinking that the network is safe and that no one can get into it is false and often disastrous. If you don't think about security in your system, hackers will be very happy to hear that from you.&lt;/p&gt;

&lt;p&gt;Every system needs to take security seriously and keep looking for security vulnerabilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Topology doesn't change
&lt;/h2&gt;

&lt;p&gt;We think that once a system is set up and working perfectly, it will continue to work the same way in the future, but nothing could be further from the truth. Any node can break, and any network between nodes can temporarily stop working. The topology of your system can change.&lt;/p&gt;

&lt;p&gt;Always ask yourself, "Does my system have one place where it could go wrong?"&lt;br&gt;
If you have a system, ask yourself, "If this part of the system fails, will my whole system fail?" What would happen if this part of my system stopped working?&lt;/p&gt;

&lt;p&gt;This problem is so common that tools like Zookeeper and Consul were made to detect topological changes and respond to them.&lt;/p&gt;

&lt;p&gt;It can be hard, but building systems that can adapt to changes in their topology makes for a strong system.&lt;/p&gt;

&lt;h2&gt;
  
  
  There is one administrator
&lt;/h2&gt;

&lt;p&gt;This mistake says that you can't be in charge of everything.&lt;/p&gt;

&lt;p&gt;In a single-machine system, there will be one administrator who knows everything and can control every part of the system.&lt;/p&gt;

&lt;p&gt;But in a distributed system, as your system grows, it will start relying on systems that you don't control.&lt;/p&gt;

&lt;p&gt;For example, if you are building an online store, you will add a payment gateway that will be run by another company. If that goes down for a short time, you can't do much. The payment gateway will have to fix it, and all you can do is wait until their system is back up and running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Transport cost is Zero
&lt;/h2&gt;

&lt;p&gt;Cloud service providers will charge you based on how much data your node sends and receives. Each bit you send between your nodes costs you money.&lt;/p&gt;

&lt;p&gt;As your systems grow, it makes sense to optimise them for these costs.&lt;br&gt;
For example, if you have two systems that talk to each other using JSON, you might want to look into transfer-optimized formats like &lt;a href="https://protobuf.dev/" rel="noopener noreferrer"&gt;Protobuf&lt;/a&gt;. These transfer-optimized formats can help you save a lot of money and time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Global Clock Fallacy
&lt;/h2&gt;

&lt;p&gt;In a single machine, any part that reads the date or time at the same point will always get the same date or time.&lt;/p&gt;

&lt;p&gt;But because of how clocks work, there can be a difference in time between two or more machines. This is often called a "clock skew."&lt;/p&gt;

&lt;p&gt;In distributed systems, you cannot simply assume that time on one machine will be the same as time on other machines.&lt;/p&gt;

</description>
      <category>vibecoding</category>
    </item>
    <item>
      <title>Introduction to Distributed Systems</title>
      <dc:creator>Sriram R</dc:creator>
      <pubDate>Wed, 15 Feb 2023 16:54:25 +0000</pubDate>
      <link>https://forem.com/sriramr98/introduction-to-distributed-systems-1np5</link>
      <guid>https://forem.com/sriramr98/introduction-to-distributed-systems-1np5</guid>
      <description>&lt;h2&gt;
  
  
  What's a Distributed System?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;distributed system&lt;/strong&gt; is a system whose components are on different computers connected by a network. These computers send messages to each other to talk to each other and coordinate their actions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B0iKhoFA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p0206jrnsqsgxeojzth.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B0iKhoFA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p0206jrnsqsgxeojzth.png" alt="Distributed System" width="880" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Components of a Distributed System
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Nodes - The various components that comprise a distributed system&lt;/li&gt;
&lt;li&gt;Network: The way that the nodes of Distributed Systems talk to each other&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why do we need Distributed Systems?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Performance
&lt;/h3&gt;

&lt;p&gt;There are limits to what a single node can do. Each machine has hardware-based limits.&lt;br&gt;
We can scale up the hardware of a machine by adding more RAM and CPU, but it gets very expensive after a certain point to improve the performance of a single computer.&lt;/p&gt;

&lt;p&gt;Instead, we can get the same results with fewer, less expensive machines.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scalability
&lt;/h3&gt;

&lt;p&gt;Most computer systems deal with information. They are in charge of storing and processing data.&lt;br&gt;
Since a single machine's performance can only be scaled to a certain point, we need more than one machine to handle the amount of data we get today.&lt;br&gt;
One computer won't be able to handle all of your requests.&lt;/p&gt;

&lt;p&gt;With multiple machines, we'll be able to store and process data more efficiently by splitting them up.&lt;/p&gt;

&lt;h3&gt;
  
  
  Availability
&lt;/h3&gt;

&lt;p&gt;Most services need to be available 24 hours a day, 7 days a week, which is a big challenge. Any time, a single machine can break down.&lt;br&gt;
If your service goes down, you'll lose money right away.&lt;br&gt;
If you store all of your data on a single machine, and that machine crashes, you lose all of your data.&lt;/p&gt;

&lt;p&gt;To be highly available, we need multiple machines so that if one fails, we can quickly switch to another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Difficulties Designing Distributed Systems
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Network Asynchrony
&lt;/h3&gt;

&lt;p&gt;Communication networks have a property called &lt;em&gt;asynchrony&lt;/em&gt;, which says that there is no way to know how long it will take to transfer an event from one machine to another. Sometimes things can occur out of order.&lt;br&gt;
This makes it hard to build systems that are spread out.&lt;/p&gt;

&lt;p&gt;To understand better, let's take an example.&lt;br&gt;
Let's say a user disliked a post on a social media site, but then realised they meant to like it and changed their vote.&lt;br&gt;
Since the network is asynchronous, it's possible that the like was received and processed first.&lt;br&gt;
The real goal was for the post to be liked, but since the messages were sent out of order and the dislike was the second message sent, the system marked the post as disliked.&lt;/p&gt;

&lt;h3&gt;
  
  
  Partial Failures
&lt;/h3&gt;

&lt;p&gt;Failure of some components of your system is called "partial failure." If the application doesn't take this into account, it could lead to bad results.&lt;/p&gt;

&lt;p&gt;For example, let's say you have multiple machines where your users' data is spread out, and you lose connection with one of them. Users whose data was stored on that machine will have to wait for it to come back up.&lt;/p&gt;

&lt;p&gt;It also makes things much more complicated when we need to do atomic transactions while some nodes are down.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---KbHQ1W9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkhsc5zxwgp24a638nr4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---KbHQ1W9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkhsc5zxwgp24a638nr4.png" alt="Partial Failure" width="426" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Concurrency
&lt;/h3&gt;

&lt;p&gt;Concurrency is the ability to do more than one computation at a time, possibly on a single data set. This makes things more complicated because the two computations can mess with each other and cause bad results.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measuring Correctness
&lt;/h2&gt;

&lt;p&gt;How do we know that a system is correct or working as it should?&lt;br&gt;
There are two main factors that determine whether a system is right or wrong:&lt;/p&gt;

&lt;h3&gt;
  
  
  Safety
&lt;/h3&gt;

&lt;p&gt;A safety property says that something in the system must never happen.&lt;/p&gt;

&lt;p&gt;If we think of a bicycle as a system, for example, the safety property says that the wheel must always be attached to the bike when it is running. If the wheel is taken off the cycle while it is running, bad things can happen.&lt;/p&gt;

&lt;h3&gt;
  
  
  Liveliness
&lt;/h3&gt;

&lt;p&gt;A liveliness property defines something that must eventually occur in a system.&lt;/p&gt;

&lt;p&gt;In the case of a bicycle system, the liveliness might mean that the bike should move when pedalled. The cycle should stop when breaks are applied.&lt;/p&gt;

</description>
      <category>distributedsystems</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
