<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://itsrishub.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://itsrishub.github.io/" rel="alternate" type="text/html" /><updated>2026-01-06T22:09:02+00:00</updated><id>https://itsrishub.github.io/feed.xml</id><title type="html">Rishub Kumar</title><subtitle></subtitle><author><name>Rishub Kumar</name></author><entry><title type="html">Raft Consensus Alogorithm</title><link href="https://itsrishub.github.io/raft-consensus-alogorithm.html" rel="alternate" type="text/html" title="Raft Consensus Alogorithm" /><published>2026-01-07T00:00:00+00:00</published><updated>2026-01-07T00:00:00+00:00</updated><id>https://itsrishub.github.io/raft-consensus-alogorithm</id><content type="html" xml:base="https://itsrishub.github.io/raft-consensus-alogorithm.html"><![CDATA[<p>Raft is a <strong>consensus algorithm</strong> designed to help a group of machines agree on the same sequence of actions (a <strong>log</strong>) even if some machines fail. It’s widely used in systems like <strong>distributed databases</strong>, <strong>key-value stores</strong>, and <strong>Raft storage</strong>.</p>

<p><strong>Step-by-step mental model</strong> of how Raft works.</p>

<hr />

<h2 id="1-core-idea-one-line">1 Core Idea (One-Line)</h2>

<blockquote>
  <p>Raft keeps multiple servers in sync by <strong>electing one leader</strong>, and <strong>only the leader decides what gets written</strong>.</p>
</blockquote>

<hr />

<h2 id="2-roles-in-raft">2 Roles in Raft</h2>

<p>Every node is always in <strong>one</strong> of these roles:</p>

<table>
  <thead>
    <tr>
      <th>Role</th>
      <th>What it does</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><strong>Leader</strong></td>
      <td>Handles all client requests and log replication</td>
    </tr>
    <tr>
      <td><strong>Follower</strong></td>
      <td>Passive, just listens and applies updates</td>
    </tr>
    <tr>
      <td><strong>Candidate</strong></td>
      <td>Tries to become leader during elections</td>
    </tr>
  </tbody>
</table>

<p>At any time, <strong>there is at most one leader</strong>.</p>

<hr />

<h2 id="3-leader-election-how-a-leader-is-chosen">3 Leader Election (How a Leader Is Chosen)</h2>

<p><img src="https://eli.thegreenplace.net/images/2020/raft-highlevel-state-machine.png" alt="Image" /></p>

<p><img src="https://miro.medium.com/v2/resize%3Afit%3A1400/0%2AQxpldrGD9Ft3xBsN" alt="Image" /></p>

<h3 id="step-by-step">Step-by-step</h3>

<ol>
  <li>All nodes start as <strong>followers</strong></li>
  <li>
    <p>If a follower doesn’t hear from a leader (heartbeat timeout):</p>

    <ul>
      <li>It becomes a <strong>candidate</strong></li>
    </ul>
  </li>
  <li>
    <p>Candidate:</p>

    <ul>
      <li>Increments its <strong>term</strong></li>
      <li>Asks others for votes</li>
    </ul>
  </li>
  <li>
    <p>If it gets <strong>majority votes</strong>:</p>

    <ul>
      <li>It becomes the <strong>leader</strong></li>
    </ul>
  </li>
  <li>
    <p>If not:</p>

    <ul>
      <li>Election restarts with randomized timeouts</li>
    </ul>
  </li>
</ol>

<p><strong>Majority is mandatory</strong> → prevents split-brain.</p>

<hr />

<h2 id="4-log-replication-how-data-is-safely-written">4 Log Replication (How Data Is Safely Written)</h2>

<p><img src="/images/raft.png" alt="Image" /></p>

<p><img src="https://miro.medium.com/v2/resize%3Afit%3A1400/1%2AqC8d8hu2LjIY_sH6vwODdQ.png" alt="Image" /></p>

<h3 id="writing-data-works-like-this">Writing data works like this:</h3>

<ol>
  <li>Client sends request → <strong>Leader</strong></li>
  <li>
    <p>Leader:</p>

    <ul>
      <li>Appends entry to its log</li>
      <li>Sends <strong>AppendEntries RPC</strong> to followers</li>
    </ul>
  </li>
  <li>
    <p>Followers:</p>

    <ul>
      <li>Write entry to their logs</li>
      <li>Send ACK back</li>
    </ul>
  </li>
  <li>
    <p>Once <strong>majority ACKs</strong>:</p>

    <ul>
      <li>Leader <strong>commits</strong> the entry</li>
      <li>Followers apply it to their state machine</li>
    </ul>
  </li>
</ol>

<p>Only committed entries are considered durable.</p>

<hr />

<h2 id="5-terms-rafts-logical-clock">5 Terms (Raft’s Logical Clock)</h2>

<p>Raft uses <strong>terms</strong> to maintain order.</p>

<table>
  <thead>
    <tr>
      <th>Term</th>
      <th>Meaning</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Integer counter</td>
      <td>Increases monotonically</td>
    </tr>
    <tr>
      <td>New election</td>
      <td>New term</td>
    </tr>
    <tr>
      <td>Higher term wins</td>
      <td>Old leaders step down</td>
    </tr>
  </tbody>
</table>

<p>If a node sees a <strong>higher term</strong>, it immediately becomes a follower.</p>

<hr />

<h2 id="6-safety-guarantees-why-raft-is-reliable">6 Safety Guarantees (Why Raft Is Reliable)</h2>

<p>Raft guarantees:</p>

<h3 id="leader-completeness">Leader Completeness</h3>

<ul>
  <li>If an entry is committed, <strong>all future leaders will have it</strong></li>
</ul>

<h3 id="log-matching">Log Matching</h3>

<ul>
  <li>If two logs have same index + term → logs before it are identical</li>
</ul>

<h3 id="no-split-brain">No Split Brain</h3>

<ul>
  <li>Only one leader per term (majority rule)</li>
</ul>

<hr />

<h2 id="7-failure-scenarios-what-happens-if">7 Failure Scenarios (What Happens If…)</h2>

<h3 id="leader-crashes">Leader crashes</h3>

<ul>
  <li>Followers stop receiving heartbeats</li>
  <li>New election starts</li>
  <li>New leader takes over</li>
</ul>

<h3 id="follower-crashes">Follower crashes</h3>

<ul>
  <li>Leader continues (as long as majority exists)</li>
  <li>Follower catches up later</li>
</ul>

<h3 id="network-partition">Network partition</h3>

<ul>
  <li>Side with <strong>majority</strong> continues</li>
  <li>Minority side becomes read-only</li>
</ul>

<hr />

<h2 id="8-why-raft-is-easier-than-paxos">8 Why Raft Is Easier Than Paxos</h2>

<p>Raft was designed to be <strong>understandable</strong>, unlike Paxos.</p>

<table>
  <thead>
    <tr>
      <th>Paxos</th>
      <th>Raft</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Hard to reason about</td>
      <td>Easy mental model</td>
    </tr>
    <tr>
      <td>Abstract</td>
      <td>Practical roles</td>
    </tr>
    <tr>
      <td>Leader implicit</td>
      <td>Leader explicit</td>
    </tr>
  </tbody>
</table>

<hr />

<h2 id="9-one-diagram-mental-model">9 One Diagram Mental Model</h2>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Client
  |
  v
Leader  ---&gt;  Follower
  |            Follower
  |            Follower
  |
(commit after majority)
</code></pre></div></div>

<hr />

<h2 id="tldr">TL;DR</h2>

<ul>
  <li>Raft = <strong>Leader-based consensus</strong></li>
  <li>Leader is elected by <strong>majority</strong></li>
  <li>All writes go through leader</li>
  <li>Logs are replicated and committed safely</li>
  <li>Designed to be <strong>simple and predictable</strong></li>
</ul>]]></content><author><name>Rishub Kumar</name></author><category term="algorithms" /><summary type="html"><![CDATA[Raft is a consensus algorithm designed to help a group of machines agree on the same sequence of actions (a log) even if some machines fail. It’s widely used in systems like distributed databases, key-value stores, and Raft storage.]]></summary></entry><entry><title type="html">Strange Post</title><link href="https://itsrishub.github.io/strange-post.html" rel="alternate" type="text/html" title="Strange Post" /><published>2020-07-06T00:00:00+00:00</published><updated>2020-07-06T00:00:00+00:00</updated><id>https://itsrishub.github.io/strange-post</id><content type="html" xml:base="https://itsrishub.github.io/strange-post.html"><![CDATA[<p>This post is strange. It also has some custom js.</p>]]></content><author><name>Rishub Kumar</name></author><summary type="html"><![CDATA[This post is strange. It also has some custom js.]]></summary></entry></feed>