<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tanu Priya</title>
    <description>The latest articles on DEV Community by Tanu Priya (@tanu_priya).</description>
    <link>https://dev.to/tanu_priya</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4073510%2F9e5a8f12-290d-4fb5-b80e-0b5e53c01a70.png</url>
      <title>DEV Community: Tanu Priya</title>
      <link>https://dev.to/tanu_priya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tanu_priya"/>
    <language>en</language>
    <item>
      <title>Virtual DOM vs Real DOM: Is the Virtual DOM Actually Faster?</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Mon, 31 Aug 2026 02:32:19 +0000</pubDate>
      <link>https://dev.to/tanu_priya/virtual-dom-vs-real-dom-is-the-virtual-dom-actually-faster-1n2p</link>
      <guid>https://dev.to/tanu_priya/virtual-dom-vs-real-dom-is-the-virtual-dom-actually-faster-1n2p</guid>
      <description>&lt;p&gt;You've probably heard this before:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"React is fast because it uses a Virtual DOM."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It sounds reasonable.&lt;/p&gt;

&lt;p&gt;The browser's DOM is often described as expensive, React uses a Virtual DOM, and therefore it's tempting to conclude:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Virtual DOM
     ↓
Faster than the Real DOM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But that's not quite how it works.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Virtual DOM is not inherently faster than the Real DOM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In fact, if you know exactly which DOM element needs to change, a direct DOM update can sometimes be faster than going through React's rendering and reconciliation process.&lt;/p&gt;

&lt;p&gt;So why does React use a Virtual DOM?&lt;/p&gt;

&lt;p&gt;What problem is it actually solving?&lt;/p&gt;

&lt;p&gt;And if the Virtual DOM isn't simply "a faster DOM," what makes it useful?&lt;/p&gt;

&lt;p&gt;Let's build the right mental model from the ground up.&lt;/p&gt;




&lt;h1&gt;
  
  
  1. What Is the Real DOM?
&lt;/h1&gt;

&lt;p&gt;DOM stands for &lt;strong&gt;Document Object Model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When a browser parses HTML, it creates an in-memory representation of the document as a tree.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Click me&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conceptually, the structure looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div
├── h1
│   └── "Hello"
└── button
    └── "Click me"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the &lt;strong&gt;Real DOM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It's the actual document structure that the browser works with when displaying your webpage.&lt;/p&gt;

&lt;p&gt;JavaScript can modify it directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, Nayan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That statement changes the actual DOM node.&lt;/p&gt;

&lt;p&gt;And this leads to an important point:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Direct DOM manipulation isn't inherently bad or slow.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you know exactly what needs to change, a direct DOM update can be extremely efficient.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Updated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is very little abstraction involved.&lt;/p&gt;

&lt;p&gt;You already know:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What changed
     ↓
Which element changed
     ↓
Update that element
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Real DOM = Slow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is simply too simplistic.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. Why Does the DOM Have a Reputation for Being Slow?
&lt;/h1&gt;

&lt;p&gt;The DOM itself isn't necessarily the problem.&lt;/p&gt;

&lt;p&gt;The browser often has additional work to perform after DOM changes.&lt;/p&gt;

&lt;p&gt;A simplified rendering pipeline looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DOM Change
    │
    ▼
Style Calculation
    │
    ▼
Layout
    │
    ▼
Paint
    │
    ▼
Compositing
    │
    ▼
Pixels on Screen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The amount of work depends heavily on what changed.&lt;/p&gt;

&lt;p&gt;Changing the text of a small element might be relatively inexpensive.&lt;/p&gt;

&lt;p&gt;Changing a property that affects the layout of a large part of the page can require substantially more work.&lt;/p&gt;

&lt;p&gt;So when developers say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"DOM operations are expensive."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;what they usually mean is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Frequent or poorly managed DOM changes can cause additional browser work.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The important distinction is that the cost isn't simply:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DOM API call = Expensive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, it can be closer to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DOM Change
     ↓
Browser determines what is affected
     ↓
Style
     ↓
Layout
     ↓
Paint
     ↓
Composite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact pipeline varies depending on the type of change.&lt;/p&gt;

&lt;p&gt;And there's one thing we can't escape:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Eventually, the browser has to update the Real DOM and render the result.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  3. So What Is the Virtual DOM?
&lt;/h1&gt;

&lt;p&gt;The Virtual DOM is a &lt;strong&gt;JavaScript representation of UI structure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Consider this React UI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click me&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conceptually, React can represent the UI as a tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Virtual UI Tree

div
├── h1
│   └── "Hello"
└── button
    └── "Click me"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But there is a crucial distinction:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The Virtual DOM is not the browser's actual DOM.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's a representation of what the UI should look like.&lt;/p&gt;

&lt;p&gt;When your application state changes, React can calculate another representation of the UI.&lt;/p&gt;

&lt;p&gt;It then reconciles the previous and next representations to determine what work needs to be committed to the actual UI.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Previous UI
     │
     ▼
New UI
     │
     ▼
Reconciliation
     │
     ▼
Determine necessary changes
     │
     ▼
Commit DOM updates
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This process is commonly called &lt;strong&gt;reconciliation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And this is where the Virtual DOM becomes useful.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. The Biggest Myth: "The Virtual DOM Is Faster"
&lt;/h1&gt;

&lt;p&gt;Let's look at a simple example.&lt;/p&gt;

&lt;p&gt;Suppose we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"count"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;0&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we know that the value needs to become &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With direct DOM manipulation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;count&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a very targeted operation.&lt;/p&gt;

&lt;p&gt;The program already knows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The value changed
       ↓
The &amp;lt;h1&amp;gt; changed
       ↓
Update the &amp;lt;h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's no need to calculate an entire UI representation just to perform that specific update.&lt;/p&gt;

&lt;p&gt;So in this situation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A direct DOM update can be faster.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is why the claim:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The Virtual DOM is faster than the Real DOM."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;is misleading.&lt;/p&gt;

&lt;p&gt;The Virtual DOM isn't designed simply to make one known DOM update faster.&lt;/p&gt;

&lt;p&gt;Its value becomes much clearer when UI complexity increases.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. The Real Problem: Managing Complex UI State
&lt;/h1&gt;

&lt;p&gt;Now imagine a real application.&lt;/p&gt;

&lt;p&gt;Your dashboard might depend on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
Products
Cart
Notifications
Permissions
Filters
Search
Loading state
Authentication state
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A single state change might affect several different parts of the UI.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Dashboard&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;notifications&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Complex UI */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine managing this entire interface with manual DOM operations.&lt;/p&gt;

&lt;p&gt;You may need to determine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Which element changed?
        │
        ▼
Which DOM node depends on it?
        │
        ▼
Should an element be added?
        │
        ▼
Should another element be removed?
        │
        ▼
Did an attribute change?
        │
        ▼
Does an event handler need updating?
        │
        ▼
Which elements should remain untouched?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As applications grow, manually keeping the DOM synchronized with application state becomes increasingly difficult.&lt;/p&gt;

&lt;p&gt;This is where React's declarative model becomes valuable.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. Declarative UI Changes the Way We Think
&lt;/h1&gt;

&lt;p&gt;With imperative DOM manipulation, you tell the browser &lt;strong&gt;how to change the UI&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;display&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;block&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome back!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;display&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;none&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please log in&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You're describing the steps required to change the interface.&lt;/p&gt;

&lt;p&gt;React allows you to describe the desired UI instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Dashboard&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;

      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&lt;/span&gt;
          &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome back!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please log in&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference is important.&lt;/p&gt;

&lt;h3&gt;
  
  
  Imperative
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How should I change the UI?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Declarative
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What should the UI look like?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React handles the transition between those states.&lt;/p&gt;

&lt;p&gt;This is one of the biggest reasons React is useful for building complex interfaces.&lt;/p&gt;




&lt;h1&gt;
  
  
  7. How React Uses the Virtual DOM
&lt;/h1&gt;

&lt;p&gt;Let's use a simple counter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
        &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Increment
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initially, the UI can be thought of as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Fragment
├── h1
│   └── 0
└── button
    └── "Increment"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the user clicks the button.&lt;/p&gt;

&lt;p&gt;The state changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;count = 0
    │
    ▼
count = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React calculates the next UI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Previous UI          Next UI

h1 → 0               h1 → 1
button → Increment   button → Increment
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important difference is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 → 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React can then commit the necessary update to the actual DOM.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;State Change
     │
     ▼
Calculate Next UI
     │
     ▼
Reconcile
     │
     ▼
Determine Changes
     │
     ▼
Commit DOM Updates
     │
     ▼
Browser Rendering
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something important:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;React does not replace the Real DOM with the Virtual DOM.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Virtual DOM is an abstraction used as part of React's UI update process.&lt;/p&gt;

&lt;p&gt;The browser still ultimately renders the Real DOM.&lt;/p&gt;




&lt;h1&gt;
  
  
  8. But Doesn't Reconciliation Add Extra Work?
&lt;/h1&gt;

&lt;p&gt;Yes.&lt;/p&gt;

&lt;p&gt;And this is another reason the phrase:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"The Virtual DOM is always faster."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;doesn't make sense.&lt;/p&gt;

&lt;p&gt;React itself has work to perform.&lt;/p&gt;

&lt;p&gt;Depending on the update, React may need to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Run component functions
        │
        ▼
Create React elements
        │
        ▼
Reconcile the UI
        │
        ▼
Determine changes
        │
        ▼
Prepare the update
        │
        ▼
Commit changes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the browser performs its own rendering work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React
  │
  ▼
DOM Updates
  │
  ▼
Browser
  │
  ├── Style
  ├── Layout
  ├── Paint
  └── Composite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the Virtual DOM isn't a magic shortcut that eliminates rendering work.&lt;/p&gt;

&lt;p&gt;It is an abstraction.&lt;/p&gt;

&lt;p&gt;And abstractions have costs.&lt;/p&gt;

&lt;p&gt;The question is whether the abstraction provides enough value to justify those costs.&lt;/p&gt;

&lt;p&gt;For complex applications, it often does.&lt;/p&gt;




&lt;h1&gt;
  
  
  9. Can Direct DOM Manipulation Be Faster?
&lt;/h1&gt;

&lt;p&gt;Absolutely.&lt;/p&gt;

&lt;p&gt;Suppose you have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Updated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you already know exactly which element needs to change, that's a very direct operation.&lt;/p&gt;

&lt;p&gt;React may instead involve a higher-level process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;State Update
     │
     ▼
Render
     │
     ▼
Reconciliation
     │
     ▼
Commit
     │
     ▼
DOM Update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So why not simply use direct DOM manipulation everywhere?&lt;/p&gt;

&lt;p&gt;Because raw performance isn't the only thing we're optimizing.&lt;/p&gt;

&lt;p&gt;We're also optimizing for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Developer productivity&lt;/li&gt;
&lt;li&gt;Predictability&lt;/li&gt;
&lt;li&gt;Maintainability&lt;/li&gt;
&lt;li&gt;Scalability&lt;/li&gt;
&lt;li&gt;Correctness&lt;/li&gt;
&lt;li&gt;Code organization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A tiny application might be perfectly manageable with direct DOM manipulation.&lt;/p&gt;

&lt;p&gt;A large application is a different story.&lt;/p&gt;




&lt;h1&gt;
  
  
  10. The Trade-Off React Makes
&lt;/h1&gt;

&lt;p&gt;Imagine manually managing a large application.&lt;/p&gt;

&lt;p&gt;You would need to keep application state synchronized with DOM state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application State
       │
       ▼
Which elements depend on it?
       │
       ▼
What changed?
       │
       ▼
What needs updating?
       │
       ▼
What should remain?
       │
       ▼
What should be removed?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the application becomes more complex, the number of relationships increases.&lt;/p&gt;

&lt;p&gt;React introduces a higher-level model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application State
       │
       ▼
Describe the UI
       │
       ▼
React determines changes
       │
       ▼
DOM is updated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You're giving up some low-level control in exchange for a more predictable programming model.&lt;/p&gt;

&lt;p&gt;That's the trade-off.&lt;/p&gt;

&lt;p&gt;And for many applications, it's a worthwhile one.&lt;/p&gt;




&lt;h1&gt;
  
  
  11. A Better Comparison: Imperative vs Declarative
&lt;/h1&gt;

&lt;p&gt;This is why the Virtual DOM discussion is often framed incorrectly.&lt;/p&gt;

&lt;p&gt;We tend to compare:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Virtual DOM
     vs
Real DOM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But a more useful comparison is often:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Imperative UI
     vs
Declarative UI
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With imperative code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;status&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Logged in&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You're explicitly describing the operations.&lt;/p&gt;

&lt;p&gt;With React:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Logged in&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Logged out&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You're describing the desired result.&lt;/p&gt;

&lt;p&gt;The conceptual difference is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Imperative
    │
    ▼
Tell the UI how to change


Declarative
    │
    ▼
Describe what the UI should be
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a much more useful way to understand React.&lt;/p&gt;




&lt;h1&gt;
  
  
  12. Is the Virtual DOM the Fastest Rendering Strategy?
&lt;/h1&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Different UI technologies use different approaches.&lt;/p&gt;

&lt;p&gt;Some rely on a Virtual DOM.&lt;/p&gt;

&lt;p&gt;Others use techniques such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fine-grained reactivity&lt;/li&gt;
&lt;li&gt;Signals&lt;/li&gt;
&lt;li&gt;Compile-time optimizations&lt;/li&gt;
&lt;li&gt;Direct targeted DOM updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if a framework knows that a particular piece of state affects exactly one DOM node, it may be able to update that node directly.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;State
  │
  ▼
Known dependency
  │
  ▼
Specific DOM node
  │
  ▼
Update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No large UI tree comparison is necessarily required.&lt;/p&gt;

&lt;p&gt;This doesn't make the Virtual DOM "bad."&lt;/p&gt;

&lt;p&gt;It simply means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The Virtual DOM is one strategy for managing UI updates — not the only strategy and not automatically the fastest.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is no single rendering strategy that wins every possible performance scenario.&lt;/p&gt;




&lt;h1&gt;
  
  
  13. React's Strength Is Bigger Than the Virtual DOM
&lt;/h1&gt;

&lt;p&gt;Reducing React to:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"React is fast because of the Virtual DOM."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;misses the bigger picture.&lt;/p&gt;

&lt;p&gt;React's strength comes from a combination of ideas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Declarative UI
      +
Component Architecture
      +
State-Driven Rendering
      +
Reconciliation
      +
Scheduling
      +
Reusable Abstractions
      +
Large Ecosystem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Virtual DOM is one piece of that larger model.&lt;/p&gt;

&lt;p&gt;It shouldn't be treated as a magical performance feature.&lt;/p&gt;

&lt;p&gt;You can still build a slow React application.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Huge unnecessary renders
Heavy calculations
Large unoptimized lists
Expensive component trees
Poor state architecture
Large JavaScript bundles
Slow network requests
Unoptimized images
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Virtual DOM doesn't automatically solve these problems.&lt;/p&gt;




&lt;h1&gt;
  
  
  14. The Virtual DOM Doesn't Make Expensive JavaScript Disappear
&lt;/h1&gt;

&lt;p&gt;Consider this component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductList&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sortedProducts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inStock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;sortedProducts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ProductCard&lt;/span&gt;
          &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;product&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Imagine &lt;code&gt;products&lt;/code&gt; contains thousands of items.&lt;/p&gt;

&lt;p&gt;If this filtering and sorting happens repeatedly, you can still create a performance problem.&lt;/p&gt;

&lt;p&gt;The Virtual DOM doesn't magically make those calculations free.&lt;/p&gt;

&lt;p&gt;The flow is still:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Component Render
       │
       ▼
JavaScript Calculations
       │
       ▼
UI Reconciliation
       │
       ▼
DOM Updates
       │
       ▼
Browser Rendering
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every stage can potentially become a bottleneck.&lt;/p&gt;

&lt;p&gt;That's why good React performance requires looking beyond the Virtual DOM.&lt;/p&gt;




&lt;h1&gt;
  
  
  15. What Should You Actually Optimize?
&lt;/h1&gt;

&lt;p&gt;Instead of obsessing over the Virtual DOM, pay attention to the things that actually affect your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  State Architecture
&lt;/h2&gt;

&lt;p&gt;Where state lives matters.&lt;/p&gt;

&lt;p&gt;Putting frequently changing state unnecessarily high in the component tree can cause more work than necessary.&lt;/p&gt;




&lt;h2&gt;
  
  
  Re-render Behavior
&lt;/h2&gt;

&lt;p&gt;Understand what causes components to render.State updates, props, context, and parent renders can all influence rendering behavior.&lt;/p&gt;




&lt;h2&gt;
  
  
  Expensive Calculations
&lt;/h2&gt;

&lt;p&gt;Heavy JavaScript work can block the main thread regardless of how efficiently DOM updates are handled.&lt;/p&gt;




&lt;h2&gt;
  
  
  Large Lists
&lt;/h2&gt;

&lt;p&gt;Rendering thousands of DOM nodes is still expensive.&lt;/p&gt;

&lt;p&gt;The Virtual DOM doesn't make a massive DOM free.&lt;/p&gt;




&lt;h2&gt;
  
  
  Network Performance
&lt;/h2&gt;

&lt;p&gt;Slow API requests can make an application feel slow even if rendering is extremely efficient.&lt;/p&gt;




&lt;h2&gt;
  
  
  Images and Assets
&lt;/h2&gt;

&lt;p&gt;Large images and unnecessary assets can have a significant effect on loading and runtime performance.&lt;/p&gt;




&lt;h2&gt;
  
  
  Bundle Size
&lt;/h2&gt;

&lt;p&gt;Shipping unnecessary JavaScript increases the amount of work the browser has to download, parse, and execute.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real User Experience
&lt;/h2&gt;

&lt;p&gt;Most importantly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Optimize based on measurable problems, not assumptions.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If a component renders three times, that doesn't automatically mean your application has a performance problem.&lt;/p&gt;

&lt;p&gt;Measure first.&lt;/p&gt;

&lt;p&gt;Then optimize the bottleneck.&lt;/p&gt;




&lt;h1&gt;
  
  
  16. The Right Mental Model
&lt;/h1&gt;

&lt;p&gt;If there's one thing you remember from this article, don't remember:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Virtual DOM is faster than Real DOM."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead, remember:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"The Virtual DOM is an abstraction that helps React manage UI changes declaratively."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The overall process is closer to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application State Changes
          │
          ▼
   React Calculates
      Next UI
          │
          ▼
    Reconciliation
          │
          ▼
  Necessary DOM Changes
          │
          ▼
    Browser Rendering
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Virtual DOM is part of that process.&lt;/p&gt;

&lt;p&gt;It is not a replacement for the Real DOM.&lt;/p&gt;

&lt;p&gt;It does not eliminate browser rendering work.&lt;/p&gt;

&lt;p&gt;And it is not automatically faster than direct DOM manipulation.&lt;/p&gt;




&lt;h1&gt;
  
  
  17. So, Is the Virtual DOM Actually Faster?
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Not by itself.
&lt;/h2&gt;

&lt;p&gt;That's the important distinction.&lt;/p&gt;

&lt;p&gt;If you know exactly which DOM node needs to change, a direct DOM update can be extremely efficient.The Virtual DOM introduces additional work because React needs to calculate and reconcile UI changes before committing the necessary updates.&lt;/p&gt;

&lt;p&gt;So the value of the Virtual DOM isn't simply:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Virtual DOM
      ↓
Faster DOM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A better mental model is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Virtual DOM
      │
      ▼
Representation of UI
      │
      ▼
Reconciliation
      │
      ▼
Determine Necessary Changes
      │
      ▼
Commit to Real DOM
      │
      ▼
Browser Renders UI
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  18. The One-Sentence Explanation
&lt;/h1&gt;

&lt;p&gt;If someone asks you:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Why does React use the Virtual DOM?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;you don't need to say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Because it's faster than the Real DOM."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A better answer is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"React uses a Virtual DOM as part of a declarative rendering model that helps it calculate and manage changes to the UI."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And if someone says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"But isn't the Virtual DOM faster than the Real DOM?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Not necessarily. A direct DOM update can be faster when you already know exactly what needs to change. The Virtual DOM's real value is helping React manage complex, state-driven UI updates in a predictable way."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's the mental model worth remembering.&lt;/p&gt;




&lt;h1&gt;
  
  
  Final Takeaway
&lt;/h1&gt;

&lt;p&gt;The Virtual DOM isn't a &lt;strong&gt;faster version of the Real DOM&lt;/strong&gt;.&lt;br&gt;
It's an abstraction.Its purpose is not to make every DOM operation magically faster.&lt;/p&gt;

&lt;p&gt;Its purpose is to help React manage complex, state-driven interfaces using a declarative programming model.The browser still needs the Real DOM.React still needs to do work.&lt;/p&gt;

&lt;p&gt;And performance still depends on how your application is designed.&lt;/p&gt;

&lt;p&gt;So instead of thinking:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Virtual DOM
     ≠
Automatically faster than Real DOM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application State
       │
       ▼
Declarative UI
       │
       ▼
Virtual UI Representation
       │
       ▼
Reconciliation
       │
       ▼
Necessary DOM Updates
       │
       ▼
Browser Rendering
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a much more accurate — and much more useful — way to understand the Virtual DOM.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>performance</category>
      <category>react</category>
    </item>
    <item>
      <title>Why Does Your React App Re-render So Much?</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Sun, 30 Aug 2026 07:19:31 +0000</pubDate>
      <link>https://dev.to/tanu_priya/why-does-your-react-app-re-render-so-much-45co</link>
      <guid>https://dev.to/tanu_priya/why-does-your-react-app-re-render-so-much-45co</guid>
      <description>&lt;p&gt;You update one small piece of state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And suddenly your console looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Header rendered
Sidebar rendered
ProductList rendered
ProductCard rendered
Footer rendered
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your first thought might be:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Why did half of my React app render again? Isn't this bad for performance?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not necessarily.&lt;/p&gt;

&lt;p&gt;One of the biggest misconceptions about React performance is that &lt;strong&gt;every re-render is a problem&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It isn't.&lt;/p&gt;

&lt;p&gt;React components are designed to render when their inputs change. A render is simply part of how React determines what the UI should look like.&lt;/p&gt;

&lt;p&gt;The real question is not:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"How do I stop React from re-rendering?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Is this re-render causing unnecessary expensive work?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That distinction is the foundation of understanding React performance.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Does a Re-render Actually Mean?
&lt;/h2&gt;

&lt;p&gt;Consider this simple component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Counter rendered&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Increment
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the button is clicked, &lt;code&gt;count&lt;/code&gt; changes.&lt;/p&gt;

&lt;p&gt;React then needs to calculate what the component should produce with the new state.&lt;/p&gt;

&lt;p&gt;A simplified mental model is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;State changes
     ↓
Component renders
     ↓
Next UI is calculated
     ↓
React reconciles the result
     ↓
Necessary changes are committed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important distinction is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Re-render ≠ Complete DOM rebuild
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A re-render means React performs rendering work again to determine the component's next output.&lt;/p&gt;

&lt;p&gt;It does &lt;strong&gt;not&lt;/strong&gt; mean React destroys the entire browser DOM and recreates it from scratch.&lt;/p&gt;

&lt;p&gt;In fact, React can render a component and discover that little—or even nothing—needs to change in the actual DOM.&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A re-render is normal. Unnecessary expensive work is what you should care about.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  What Causes a Component to Render?
&lt;/h2&gt;

&lt;p&gt;Before optimizing a component, you need to understand &lt;strong&gt;why it rendered in the first place&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are several common causes.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Its State Changes
&lt;/h2&gt;

&lt;p&gt;This is the most obvious case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React receives a state update and schedules work to update the component.&lt;/p&gt;

&lt;p&gt;The component needs to calculate its UI using the new state.&lt;/p&gt;

&lt;p&gt;That's completely normal.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Its Parent Renders
&lt;/h2&gt;

&lt;p&gt;This is where many developers get confused.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Header&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Header rendered&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;My Website&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;count&lt;/code&gt; changes, &lt;code&gt;App&lt;/code&gt; renders again.&lt;/p&gt;

&lt;p&gt;As part of processing the updated tree, React may also render &lt;code&gt;Header&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So you might see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Header rendered
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;even though &lt;code&gt;Header&lt;/code&gt; doesn't use &lt;code&gt;count&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This often leads developers to think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"React is unnecessarily rendering my entire application!"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But that's too simplistic.&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;Header&lt;/code&gt; is tiny and its render is cheap, another render may have practically no meaningful performance impact.&lt;/p&gt;

&lt;p&gt;The important question is not whether &lt;code&gt;Header&lt;/code&gt; rendered.&lt;/p&gt;

&lt;p&gt;It's:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;How much work did that render actually do?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  3. Its Props Change
&lt;/h2&gt;

&lt;p&gt;Props are another source of updates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Profile&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Used like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nayan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Profile&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;name&lt;/code&gt; changes, &lt;code&gt;Profile&lt;/code&gt; receives different input and needs to calculate its new UI.&lt;/p&gt;

&lt;p&gt;That's exactly what we expect.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;New props
   ↓
New component input
   ↓
New render
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, this isn't automatically a performance problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. A Context Value Changes
&lt;/h2&gt;

&lt;p&gt;Context can also cause components that consume that context to update.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ThemeContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ThemeProvider&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTheme&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A component can consume it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Navbar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ThemeContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Navbar&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the context value changes, components consuming that context can update.&lt;/p&gt;

&lt;p&gt;This becomes especially important when one context contains many unrelated pieces of frequently changing state.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
Theme
Notifications
Cart
Language
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything lives inside one large context, a change to one part can cause more consumers to be affected than necessary.&lt;/p&gt;

&lt;p&gt;Sometimes separating unrelated concerns into smaller contexts is a better design.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. State Used by a Custom Hook Changes
&lt;/h2&gt;

&lt;p&gt;Custom hooks don't render independently.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useWindowWidth&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setWidth&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerWidth&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useWindowWidth&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;px&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the hook's state changes, the component using that hook is updated.&lt;/p&gt;

&lt;p&gt;A useful rule is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Hooks don't render independently. The component using them renders.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This distinction becomes helpful when debugging applications with many custom hooks.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Biggest Source of Unnecessary Work: State in the Wrong Place
&lt;/h2&gt;

&lt;p&gt;Now we get to one of the most useful React performance ideas.&lt;/p&gt;

&lt;p&gt;Consider this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSearch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;SearchBox&lt;/span&gt;
        &lt;span class="na"&gt;search&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;setSearch&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setSearch&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ExpensiveDashboard&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Sidebar&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Footer&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Imagine the user types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;N
Na
Nay
Naya
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every keystroke changes &lt;code&gt;search&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;search&lt;/code&gt; lives in &lt;code&gt;App&lt;/code&gt;, &lt;code&gt;App&lt;/code&gt; updates on every keystroke.&lt;/p&gt;

&lt;p&gt;That means React has to process the tree below that component repeatedly.&lt;/p&gt;

&lt;p&gt;Now ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Does &lt;code&gt;search&lt;/code&gt; actually need to live in &lt;code&gt;App&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If only &lt;code&gt;SearchBox&lt;/code&gt; needs that state, placing it closer to &lt;code&gt;SearchBox&lt;/code&gt; can reduce the scope of the updates.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;App
├── SearchBox
│     └── Search state
├── ExpensiveDashboard
├── Sidebar
└── Footer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;App
├── Search state
├── SearchBox
├── ExpensiveDashboard
├── Sidebar
└── Footer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This idea is called &lt;strong&gt;state colocation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And it is one of the most useful optimization techniques in React because you're improving the structure of the application rather than adding optimization APIs everywhere.&lt;/p&gt;

&lt;p&gt;Before reaching for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Can this state live closer to where it is actually needed?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Often, that's the better solution.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why "Re-render" Doesn't Mean "DOM Update"
&lt;/h2&gt;

&lt;p&gt;This distinction is worth making extremely clear.&lt;/p&gt;

&lt;p&gt;Suppose React renders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the next render produces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The component rendered.&lt;/p&gt;

&lt;p&gt;But the resulting UI is effectively the same.&lt;/p&gt;

&lt;p&gt;So React may have no meaningful DOM change to commit.&lt;/p&gt;

&lt;p&gt;That's why these concepts should stay separate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Render
   ↓
Reconciliation
   ↓
Commit
   ↓
DOM changes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A render can happen without a meaningful DOM update.&lt;/p&gt;

&lt;p&gt;This is why seeing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Component rendered
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in the console is &lt;strong&gt;not enough evidence&lt;/strong&gt; that something is wrong.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Reconciliation?
&lt;/h2&gt;

&lt;p&gt;During an update, React needs to determine how the new element tree relates to the previous one.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Previous UI

Count: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Next UI

Count: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The surrounding structure may remain the same while the displayed value changes.&lt;/p&gt;

&lt;p&gt;React's reconciliation process determines how the current tree should transition toward the next one.&lt;/p&gt;

&lt;p&gt;You may have heard this explained as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"React compares the old Virtual DOM with the new Virtual DOM."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's a useful beginner-friendly explanation, but it's an oversimplification.&lt;/p&gt;

&lt;p&gt;React has its own internal representations and reconciliation machinery. It isn't simply taking two snapshots of the browser DOM and comparing them.&lt;/p&gt;

&lt;p&gt;For practical React development, the more useful mental model is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;React calculates the next UI and determines what work is required to transition the current tree toward it.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  What Happens During the Commit?
&lt;/h2&gt;

&lt;p&gt;After React finishes the necessary rendering and reconciliation work, it reaches the &lt;strong&gt;commit phase&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is where React applies the required changes to the host environment.&lt;/p&gt;

&lt;p&gt;In a browser, that means updating the DOM.&lt;/p&gt;

&lt;p&gt;For our counter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Count: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;eventually becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Count: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key point is that React doesn't need to rebuild the entire DOM simply because a component rendered again.&lt;/p&gt;

&lt;p&gt;It commits the changes that are actually required.&lt;/p&gt;




&lt;h2&gt;
  
  
  What About the Browser?
&lt;/h2&gt;

&lt;p&gt;React isn't the final step.&lt;/p&gt;

&lt;p&gt;After the DOM has been updated, the browser still needs to turn that state into pixels.&lt;/p&gt;

&lt;p&gt;A simplified browser pipeline looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DOM + CSS
   ↓
Style
   ↓
Layout
   ↓
Paint
   ↓
Composite
   ↓
Pixels
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is important because React performance and browser rendering performance aren't exactly the same thing.&lt;/p&gt;

&lt;p&gt;You can have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;expensive React rendering&lt;/li&gt;
&lt;li&gt;expensive DOM work&lt;/li&gt;
&lt;li&gt;expensive layout&lt;/li&gt;
&lt;li&gt;expensive painting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding where the actual cost occurs is much more useful than simply counting renders.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Can Re-renders Be Expensive?
&lt;/h2&gt;

&lt;p&gt;A re-render itself isn't necessarily expensive.&lt;/p&gt;

&lt;p&gt;The work performed during that render might be.&lt;/p&gt;

&lt;p&gt;Imagine a component that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;filters thousands of records&lt;/li&gt;
&lt;li&gt;renders a huge list&lt;/li&gt;
&lt;li&gt;performs complex calculations&lt;/li&gt;
&lt;li&gt;renders a complicated chart&lt;/li&gt;
&lt;li&gt;causes many expensive children to update&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now frequent renders may matter.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Render
  ↓
Small component
  ↓
Cheap work
  ↓
Probably fine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Render
  ↓
Heavy calculations
  ↓
Large list
  ↓
Expensive children
  ↓
Noticeable lag
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the difference.&lt;/p&gt;

&lt;p&gt;So don't optimize because you see the word:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rendered
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in your console.&lt;/p&gt;

&lt;p&gt;Optimize when there is &lt;strong&gt;unnecessary work that actually matters&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  What About &lt;code&gt;React.memo&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;React.memo&lt;/code&gt; can prevent a child component from rendering when its props haven't changed, assuming the relevant comparison indicates that the props are equal.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Profile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Profile&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Profile rendered&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Profile&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Nayan"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;count&lt;/code&gt; changes, &lt;code&gt;App&lt;/code&gt; renders again.&lt;/p&gt;

&lt;p&gt;But &lt;code&gt;Profile&lt;/code&gt; still receives the same &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Because it's memoized, React can potentially skip rendering &lt;code&gt;Profile&lt;/code&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Parent renders
      ↓
Child props unchanged
      ↓
Memoization can skip child render
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But &lt;code&gt;React.memo&lt;/code&gt; isn't something you should automatically add to every component.&lt;/p&gt;

&lt;p&gt;It is most useful when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the parent renders frequently&lt;/li&gt;
&lt;li&gt;the child is relatively expensive&lt;/li&gt;
&lt;li&gt;the child's props often remain unchanged&lt;/li&gt;
&lt;li&gt;avoiding the render actually provides a measurable benefit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a tiny component like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Label&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;memoization may provide little practical benefit.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Don't use &lt;code&gt;React.memo&lt;/code&gt; because a component can re-render. Use it when skipping that render is actually useful.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The Hidden Problem: New Object References
&lt;/h2&gt;

&lt;p&gt;There is another reason memoization can sometimes fail to provide the expected benefit.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Profile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Profile&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Profile&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nayan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time &lt;code&gt;App&lt;/code&gt; renders, this creates a new object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nayan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though the contents are the same, the object itself is a new reference.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Previous render → Object A
Next render     → Object B

Object A !== Object B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This matters because memoization compares props using shallow equality.&lt;/p&gt;

&lt;p&gt;The same idea can apply to arrays and functions.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Profile&lt;/span&gt; &lt;span class="na"&gt;items&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;React&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Next.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Child&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A new function is created during each render.&lt;/p&gt;

&lt;p&gt;But don't take this to mean:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"New objects and functions are bad."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They're not.&lt;/p&gt;

&lt;p&gt;Creating a new object or function is perfectly normal JavaScript.&lt;/p&gt;

&lt;p&gt;It only becomes relevant when &lt;strong&gt;reference stability is important for a specific optimization&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  So What Is &lt;code&gt;useCallback&lt;/code&gt; For?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useCallback&lt;/code&gt; can preserve a function reference between renders when its dependencies haven't changed.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be useful when the callback is passed to a memoized child:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Child&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click me&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without stable references, a newly created callback can cause the child's props to appear changed.&lt;/p&gt;

&lt;p&gt;But that doesn't mean every function needs &lt;code&gt;useCallback&lt;/code&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is perfectly fine in many situations.&lt;/p&gt;

&lt;p&gt;The better rule is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;useCallback&lt;/code&gt; when a stable function reference enables a meaningful optimization.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not simply because a function is created during rendering.&lt;/p&gt;




&lt;h2&gt;
  
  
  And What About &lt;code&gt;useMemo&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useMemo&lt;/code&gt; can help avoid repeating an expensive calculation when its dependencies haven't changed.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filteredProducts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inStock&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can make sense when the calculation is genuinely expensive.&lt;/p&gt;

&lt;p&gt;It can also be useful when you need a stable value reference for another optimization.&lt;/p&gt;

&lt;p&gt;But this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nayan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;usually adds complexity without providing a meaningful benefit.&lt;/p&gt;

&lt;p&gt;So again:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;useMemo&lt;/code&gt; is an optimization tool, not a default requirement.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The Most Common React Performance Mistake
&lt;/h2&gt;

&lt;p&gt;A developer opens the console and sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dashboard rendered
Dashboard rendered
Dashboard rendered
Dashboard rendered
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They immediately add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React.memo
useMemo
useCallback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;everywhere.&lt;/p&gt;

&lt;p&gt;But they haven't answered the most important question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Is the render actually causing a performance problem?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A small component can render many times and still be completely fine.&lt;/p&gt;

&lt;p&gt;Meanwhile, a large component doing expensive work may be worth optimizing even if it renders less frequently.&lt;/p&gt;

&lt;p&gt;So don't optimize based on the number of renders alone.&lt;/p&gt;

&lt;p&gt;Optimize based on the &lt;strong&gt;cost of the work&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Investigate a Re-render
&lt;/h2&gt;

&lt;p&gt;When you notice frequent rendering, work through these questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What caused it?
&lt;/h2&gt;

&lt;p&gt;Was it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;state?&lt;/li&gt;
&lt;li&gt;props?&lt;/li&gt;
&lt;li&gt;context?&lt;/li&gt;
&lt;li&gt;a parent update?&lt;/li&gt;
&lt;li&gt;state used by a custom hook?&lt;/li&gt;
&lt;li&gt;another update source?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First understand the trigger.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Does the component actually need that state?
&lt;/h2&gt;

&lt;p&gt;If frequently changing state is stored high in the tree, ask whether it can move closer to the components that actually use it.&lt;/p&gt;

&lt;p&gt;State colocation is often a cleaner solution than memoization.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Is the render expensive?
&lt;/h2&gt;

&lt;p&gt;Look for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;large lists&lt;/li&gt;
&lt;li&gt;heavy calculations&lt;/li&gt;
&lt;li&gt;complex charts&lt;/li&gt;
&lt;li&gt;expensive child components&lt;/li&gt;
&lt;li&gt;slow interactions&lt;/li&gt;
&lt;li&gt;visible UI lag&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the render is cheap, there may be nothing to fix.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Can the component structure be improved?
&lt;/h2&gt;

&lt;p&gt;Sometimes the best optimization isn't an optimization API.&lt;/p&gt;

&lt;p&gt;It might simply be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Better component boundaries
Better state placement
Smaller update scope
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good architecture can prevent unnecessary work naturally.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Would memoization actually help?
&lt;/h2&gt;

&lt;p&gt;Only after understanding the problem should you consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React.memo
useMemo
useCallback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And ideally, measure whether the change actually improved the situation.&lt;/p&gt;




&lt;h2&gt;
  
  
  The React Performance Mindset
&lt;/h2&gt;

&lt;p&gt;This is the mindset I want you to remember.&lt;/p&gt;

&lt;p&gt;When you see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Component rendered
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;don't immediately think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Something is wrong."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Why did it render?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What work did the render perform?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Is that work expensive enough to matter?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And finally:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What's the simplest way to reduce that work?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That might be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Move state
     ↓
Split components
     ↓
Reduce unnecessary work
     ↓
Memoize when useful
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Something rendered
     ↓
useMemo
     ↓
useCallback
     ↓
React.memo
     ↓
Everything is memoized
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  The Mental Model to Remember
&lt;/h2&gt;

&lt;p&gt;React performance becomes much easier to understand when you separate these concepts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;State / Props / Context change
            ↓
      React renders
            ↓
   Next UI is calculated
            ↓
      Reconciliation
            ↓
          Commit
            ↓
      DOM is updated
            ↓
    Browser renders pixels
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But don't confuse:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Re-render
    ≠
DOM update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Parent re-render
    ≠
Performance problem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Memoization
    ≠
Always necessary
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Final Takeaway
&lt;/h2&gt;

&lt;p&gt;React components are supposed to render.&lt;/p&gt;

&lt;p&gt;A state change, a parent update, changed props, or a context update can cause rendering work.&lt;/p&gt;

&lt;p&gt;That alone isn't a problem.&lt;/p&gt;

&lt;p&gt;The real problem is &lt;strong&gt;unnecessary expensive work&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So when you see a component rendering more often than expected, don't immediately reach for &lt;code&gt;React.memo&lt;/code&gt;, &lt;code&gt;useMemo&lt;/code&gt;, or &lt;code&gt;useCallback&lt;/code&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Notice the render
      ↓
Understand what triggered it
      ↓
Measure the actual cost
      ↓
Improve component boundaries
      ↓
Keep state close to where it's needed
      ↓
Reduce unnecessary work
      ↓
Use memoization when it provides real value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The goal isn't to build an application where nothing re-renders.&lt;/p&gt;

&lt;p&gt;The goal is to build an application where &lt;strong&gt;the work that happens during updates is appropriate for what actually changed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So don't ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"How do I stop React from re-rendering?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Is this render doing unnecessary work, and what's the simplest way to fix it?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That small change in mindset is one of the most important steps toward understanding React performance.&lt;/p&gt;

&lt;p&gt;And once you understand &lt;strong&gt;why a component rendered, what work it performed, and what actually reached the DOM&lt;/strong&gt;, React optimization stops being a collection of tricks and starts becoming a reasoning problem.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>performance</category>
      <category>react</category>
    </item>
    <item>
      <title>What Really Happens When React State Changes?</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Sat, 29 Aug 2026 08:59:12 +0000</pubDate>
      <link>https://dev.to/tanu_priya/what-really-happens-when-react-state-changes-11ah</link>
      <guid>https://dev.to/tanu_priya/what-really-happens-when-react-state-changes-11ah</guid>
      <description>&lt;p&gt;You write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the user clicks a button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And suddenly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Count: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Count: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks simple.&lt;/p&gt;

&lt;p&gt;But what actually happens between calling &lt;code&gt;setCount()&lt;/code&gt; and seeing &lt;code&gt;1&lt;/code&gt; on the screen?&lt;/p&gt;

&lt;p&gt;React doesn't immediately find the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; element and change its text.&lt;/p&gt;

&lt;p&gt;Instead, the state update starts a series of steps that eventually lead to the browser displaying the new UI.&lt;/p&gt;

&lt;p&gt;The simplified mental model is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;State Update → Render → Reconciliation → Commit → Browser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's follow that process from beginning to end.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Example
&lt;/h2&gt;

&lt;p&gt;We'll use a simple counter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Increment&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initially, &lt;code&gt;count&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The user sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Count: 0

[ Increment ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the button is clicked, this runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the state needs to change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 → 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens next?&lt;/p&gt;




&lt;h1&gt;
  
  
  1. The State Update Is Scheduled
&lt;/h1&gt;

&lt;p&gt;The first thing to understand is that &lt;code&gt;setCount()&lt;/code&gt; is &lt;strong&gt;not a DOM manipulation command&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you're not telling the browser:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Find the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; and change its text to 1."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You're telling React that the component's state has changed and that its rendered output may need to change.&lt;/p&gt;

&lt;p&gt;That's a fundamentally different approach from imperative DOM manipulation.&lt;/p&gt;

&lt;p&gt;For example, this is imperative:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Count: 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, you're directly telling the browser what to change.&lt;/p&gt;

&lt;p&gt;With React:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you describe a &lt;strong&gt;state change&lt;/strong&gt;, and React takes responsibility for determining the UI that should result from it.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. React Renders the Component
&lt;/h1&gt;

&lt;p&gt;After processing the state update, React needs to determine what the UI should look like with the new state.&lt;/p&gt;

&lt;p&gt;Conceptually, our component now renders with:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;So this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1&amp;gt;Count: 1&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what we mean when we say the component &lt;strong&gt;re-renders&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And here's one of the most important React concepts:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A re-render does not mean the entire DOM is recreated.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;React is calculating the component's next UI output. It isn't throwing away the existing browser DOM and rebuilding everything from scratch.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Re-render ≠ DOM rebuild
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  3. React Determines What Changed
&lt;/h1&gt;

&lt;p&gt;Now React has a previous UI representation and a new one.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Count: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Count: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The surrounding structure hasn't changed.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; is still there.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; is still there.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; is still there.&lt;/p&gt;

&lt;p&gt;Only the displayed text has changed.&lt;/p&gt;

&lt;p&gt;React's reconciliation process determines how the new element tree relates to the previous one and what work needs to be performed.&lt;/p&gt;

&lt;p&gt;This is why reconciliation is often described as the process of figuring out &lt;strong&gt;what needs to change&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It's important not to think of this as React taking two snapshots of the browser DOM and comparing them.&lt;/p&gt;

&lt;p&gt;React works with its own internal representations and reconciliation machinery to determine how the current tree should transition toward the next one.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. React Commits the Changes
&lt;/h1&gt;

&lt;p&gt;Once React has determined the work that needs to happen, it enters the &lt;strong&gt;commit phase&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is where the required changes are applied to the host environment.&lt;/p&gt;

&lt;p&gt;In a browser, that means updating the DOM.&lt;/p&gt;

&lt;p&gt;For our counter, the result is effectively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Count: 1&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important thing is that React doesn't need to recreate the entire DOM structure just because the state changed.&lt;/p&gt;

&lt;p&gt;The existing DOM can be updated where necessary.&lt;/p&gt;

&lt;p&gt;So we can think of the process as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;State Update
     ↓
Render
     ↓
Reconciliation
     ↓
Commit
     ↓
DOM Update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the core React mental model.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. Then the Browser Has to Render It
&lt;/h1&gt;

&lt;p&gt;React updating the DOM isn't quite the same thing as the user seeing pixels change.&lt;/p&gt;

&lt;p&gt;The browser still has its own rendering work to perform.&lt;/p&gt;

&lt;p&gt;A simplified version looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DOM + CSS
   ↓
Style
   ↓
Layout
   ↓
Paint
   ↓
Composite
   ↓
Pixels
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So there are actually two different systems involved.&lt;/p&gt;

&lt;p&gt;React determines and applies the UI changes.&lt;/p&gt;

&lt;p&gt;The browser takes the resulting DOM, styles, and other rendering information and turns them into pixels.&lt;/p&gt;

&lt;p&gt;This distinction becomes especially useful when you start learning about frontend performance.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. Render Doesn't Always Mean DOM Changes
&lt;/h1&gt;

&lt;p&gt;Here's another important misconception.&lt;/p&gt;

&lt;p&gt;Suppose a component renders again.&lt;/p&gt;

&lt;p&gt;That doesn't necessarily mean the browser DOM changes.&lt;/p&gt;

&lt;p&gt;Imagine React renders the same output as before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Previous:

&amp;lt;h1&amp;gt;Hello&amp;lt;/h1&amp;gt;

Next:

&amp;lt;h1&amp;gt;Hello&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There may be no meaningful DOM mutation required.&lt;/p&gt;

&lt;p&gt;So these are three different things:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Render
Reconciliation
DOM Update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A render can happen without producing a DOM change.&lt;/p&gt;

&lt;p&gt;That's why saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The component rendered, so React changed the DOM."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;isn't necessarily correct.&lt;/p&gt;




&lt;h1&gt;
  
  
  7. Why Does a Component Re-render?
&lt;/h1&gt;

&lt;p&gt;State updates are one reason.&lt;/p&gt;

&lt;p&gt;But they're not the only reason a component can render.&lt;/p&gt;

&lt;p&gt;For example, a component may render because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;its state changed&lt;/li&gt;
&lt;li&gt;its parent rendered&lt;/li&gt;
&lt;li&gt;a context value it uses changed&lt;/li&gt;
&lt;li&gt;an external store triggered an update&lt;/li&gt;
&lt;li&gt;another React-driven update occurred&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why the statement:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"React only re-renders when state changes."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;is too simplistic.&lt;/p&gt;

&lt;p&gt;The more useful question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What caused this component to render, and what work did that render actually perform?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That question becomes very important when debugging performance.&lt;/p&gt;




&lt;h1&gt;
  
  
  8. What About the Virtual DOM?
&lt;/h1&gt;

&lt;p&gt;You've probably heard:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"React uses a Virtual DOM."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And you've probably also heard:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The Virtual DOM is a copy of the real DOM."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's a useful beginner explanation, but it's an oversimplification.&lt;/p&gt;

&lt;p&gt;A better mental model is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;React maintains in-memory representations of the UI and uses them as part of its rendering and reconciliation process.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When state changes, React calculates the next UI and determines how it should transition from the current tree.&lt;/p&gt;

&lt;p&gt;The important idea isn't:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Virtual DOM = faster DOM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The more useful idea is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You describe the UI.
React manages the transition.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a major part of what makes React &lt;strong&gt;declarative&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  9. Declarative vs Imperative
&lt;/h1&gt;

&lt;p&gt;Consider an imperative approach.&lt;/p&gt;

&lt;p&gt;You might manually update the DOM:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isOnline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Online&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Offline&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your code needs to keep track of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;which element changed&lt;/li&gt;
&lt;li&gt;what value it should have&lt;/li&gt;
&lt;li&gt;which classes need to be added or removed&lt;/li&gt;
&lt;li&gt;what happens when multiple pieces of state change&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React lets you describe the desired UI instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isOnline&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Online&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Offline&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You describe the UI for the current state.&lt;/p&gt;

&lt;p&gt;React handles the transition from the previous result to the next one.&lt;/p&gt;

&lt;p&gt;That's the declarative model.&lt;/p&gt;




&lt;h1&gt;
  
  
  10. What Happens With Multiple State Updates?
&lt;/h1&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nayan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It would be misleading to assume that React necessarily does this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setName()
  ↓
render

setAge()
  ↓
render

setLoading()
  ↓
render
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React can &lt;strong&gt;batch state updates&lt;/strong&gt; so that multiple updates are processed together when appropriate.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Multiple Updates
      ↓
React processes them
      ↓
Render
      ↓
Reconciliation
      ↓
Commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can reduce unnecessary work.&lt;/p&gt;

&lt;p&gt;The exact behavior depends on the React version and execution context, but the important mental model is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;State setters are requests for React to update its rendered output, not direct DOM commands.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  11. Does React Rebuild Everything?
&lt;/h1&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;This is probably the most important misconception to eliminate.&lt;/p&gt;

&lt;p&gt;Suppose a component contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isOnline&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Online&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Offline&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Change&lt;/span&gt; &lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;isOnline&lt;/code&gt; changes, the component may render again.&lt;/p&gt;

&lt;p&gt;But that doesn't mean React throws away:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
&amp;lt;h1&amp;gt;
&amp;lt;button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and creates completely new DOM elements for everything.&lt;/p&gt;

&lt;p&gt;React's reconciliation process determines what changed and the commit phase applies the necessary updates.&lt;/p&gt;

&lt;p&gt;So keep these distinctions in mind:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Component re-render
        ≠
Entire DOM recreated

Render
        ≠
DOM update

State update
        ≠
Direct DOM manipulation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These distinctions will save you from a lot of confusion later.&lt;/p&gt;




&lt;h1&gt;
  
  
  12. Why Can Re-renders Still Be Expensive?
&lt;/h1&gt;

&lt;p&gt;If React doesn't rebuild the entire DOM, does that mean re-renders are free?&lt;/p&gt;

&lt;p&gt;Definitely not.&lt;/p&gt;

&lt;p&gt;A render can still perform a lot of JavaScript work.&lt;/p&gt;

&lt;p&gt;Imagine a component that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;renders thousands of elements&lt;/li&gt;
&lt;li&gt;performs expensive calculations&lt;/li&gt;
&lt;li&gt;processes a large dataset&lt;/li&gt;
&lt;li&gt;creates many objects&lt;/li&gt;
&lt;li&gt;triggers many child renders&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even if the final DOM change is tiny, the work required to calculate that result might be expensive.&lt;/p&gt;

&lt;p&gt;That's why React performance optimization focuses on questions such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Where should state live?&lt;/li&gt;
&lt;li&gt;Which components actually need to render?&lt;/li&gt;
&lt;li&gt;Are expensive calculations being repeated?&lt;/li&gt;
&lt;li&gt;Are unnecessary child renders happening?&lt;/li&gt;
&lt;li&gt;Would &lt;code&gt;React.memo&lt;/code&gt; help?&lt;/li&gt;
&lt;li&gt;Would &lt;code&gt;useMemo&lt;/code&gt; or &lt;code&gt;useCallback&lt;/code&gt; actually solve a real problem?&lt;/li&gt;
&lt;li&gt;Should a large list be virtualized?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal isn't:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Prevent every re-render."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The goal is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Avoid unnecessary expensive work.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  13. The Complete Journey
&lt;/h1&gt;

&lt;p&gt;Let's return to our original example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The user doesn't directly cause this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setCount()
   ↓
DOM changes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A better mental model is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User interaction
      ↓
State update
      ↓
React renders
      ↓
Reconciliation
      ↓
Commit
      ↓
Browser rendering
      ↓
Pixels on screen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For our counter, the visible result is simply:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Count: 0
     ↓
Count: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But underneath that tiny change, React and the browser perform several steps to produce the final result.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Mental Model You Should Remember
&lt;/h1&gt;

&lt;p&gt;When you see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;don't think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"React changes the DOM."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The state changed, so React needs to determine what the UI should look like now."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That leads to this mental model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;State changes
     ↓
React calculates the next UI
     ↓
React reconciles it
     ↓
React commits required changes
     ↓
Browser renders the result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And remember these three rules:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. State updates are not DOM commands
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setState ≠ DOM manipulation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Re-rendering does not mean rebuilding the entire DOM
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Re-render ≠ DOM rebuild
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Rendering does not necessarily mean the DOM changes
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Render ≠ DOM update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Final Takeaway
&lt;/h1&gt;

&lt;p&gt;React's core idea is surprisingly simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;You describe what the UI should look like for a given state, and React manages the transition from the previous UI to the next one.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So the next time you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;remember that you're not telling the browser:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Change this text to 1."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You're changing the state that drives your UI.&lt;/p&gt;

&lt;p&gt;React then determines the next rendered result, reconciles it with the previous one, commits the necessary changes, and the browser eventually turns that result into pixels.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>JavaScript Event Loop: How Does JavaScript Handle Multiple Tasks?</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Fri, 28 Aug 2026 08:53:33 +0000</pubDate>
      <link>https://dev.to/tanu_priya/javascript-event-loop-how-does-javascript-handle-multiple-tasks-bc0</link>
      <guid>https://dev.to/tanu_priya/javascript-event-loop-how-does-javascript-handle-multiple-tasks-bc0</guid>
      <description>&lt;p&gt;JavaScript is often described as a &lt;strong&gt;single-threaded language&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At a high level, this means JavaScript executes JavaScript code on a single main thread, handling one piece of JavaScript execution at a time.&lt;/p&gt;

&lt;p&gt;But modern applications constantly perform operations that take time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API requests&lt;/li&gt;
&lt;li&gt;Timers&lt;/li&gt;
&lt;li&gt;Button clicks&lt;/li&gt;
&lt;li&gt;User input&lt;/li&gt;
&lt;li&gt;File operations&lt;/li&gt;
&lt;li&gt;Animations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So an important question comes up:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;If JavaScript executes one piece of code at a time, how can it handle asynchronous operations without freezing the application?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer is the &lt;strong&gt;Event Loop&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To understand it, we need to look at the different pieces involved:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             JavaScript Runtime

                  Call Stack
                      │
                      ▼
              JavaScript executes
                      │
          ┌───────────┴───────────┐
          │                       │
     Synchronous              Async work
       code                      │
                                  ▼
                              Host APIs
                                  │
                       ┌──────────┴──────────┐
                       ▼                     ▼
                Microtask Queue        Task Queue
                       │                     │
                       └──────────┬──────────┘
                                  ▼
                             Event Loop
                                  │
                                  ▼
                             Call Stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand how this works.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The Call Stack
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Call Stack&lt;/strong&gt; is where JavaScript keeps track of the functions it is currently executing.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;second&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;second&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;first()&lt;/code&gt; is called, it goes onto the Call Stack.&lt;/p&gt;

&lt;p&gt;Then &lt;code&gt;first()&lt;/code&gt; calls &lt;code&gt;second()&lt;/code&gt;, so &lt;code&gt;second()&lt;/code&gt; is added on top.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Call Stack

┌─────────────────┐
│    second()     │
├─────────────────┤
│     first()     │
└─────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;second()&lt;/code&gt; finishes first, so it is removed.&lt;/p&gt;

&lt;p&gt;Then &lt;code&gt;first()&lt;/code&gt; finishes.&lt;/p&gt;

&lt;p&gt;This follows the &lt;strong&gt;LIFO&lt;/strong&gt; principle:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Last In, First Out.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The important thing is that JavaScript cannot execute two pieces of JavaScript code simultaneously on the same main execution thread.&lt;/p&gt;

&lt;p&gt;So if one task keeps the Call Stack busy for a long time, other JavaScript work has to wait.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. What Happens With Asynchronous Work?
&lt;/h1&gt;

&lt;p&gt;Now consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timer finished&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might initially expect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
Timer finished
End
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the actual output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
Timer finished
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why doesn't JavaScript wait for two seconds?&lt;/p&gt;

&lt;p&gt;Because the timer does not remain on the Call Stack.&lt;/p&gt;

&lt;p&gt;The surrounding environment, such as the browser, provides APIs that can handle operations like timers and network requests.&lt;/p&gt;

&lt;p&gt;A simplified flow looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Call Stack
    │
    │ setTimeout(...)
    ▼
Host / Browser APIs
    │
    │ wait for timer
    ▼
Timer becomes ready
    │
    ▼
Task Queue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Meanwhile, JavaScript is free to continue executing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's why &lt;code&gt;"End"&lt;/code&gt; appears before &lt;code&gt;"Timer finished"&lt;/code&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. Host APIs Handle Asynchronous Operations
&lt;/h1&gt;

&lt;p&gt;The browser provides many capabilities outside the JavaScript Call Stack.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Timers&lt;/li&gt;
&lt;li&gt;DOM events&lt;/li&gt;
&lt;li&gt;Network requests&lt;/li&gt;
&lt;li&gt;Geolocation&lt;/li&gt;
&lt;li&gt;Other browser functionality&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Done&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The timer is registered with the host environment.&lt;/p&gt;

&lt;p&gt;JavaScript doesn't sit there doing nothing for two seconds.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript
     │
     │ Register timer
     ▼
Browser / Host
     │
     │ Timer runs
     ▼
Callback becomes ready
     │
     ▼
Task Queue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of the key ideas behind asynchronous JavaScript:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;JavaScript can delegate certain operations to its surrounding environment and continue executing other JavaScript code.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  4. The Task Queue
&lt;/h1&gt;

&lt;p&gt;When an asynchronous operation becomes ready, its callback may be scheduled as a &lt;strong&gt;task&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timeout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the timer is eligible to fire, its callback is placed into the appropriate task queue.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Task Queue

┌──────────────────────────┐
│ console.log("Timeout")   │
└──────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But being in the queue doesn't mean the callback executes immediately.&lt;/p&gt;

&lt;p&gt;The Call Stack must be available, and the runtime's scheduling rules determine when the task gets a chance to run.&lt;/p&gt;

&lt;p&gt;That's where the Event Loop comes in.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. The Event Loop
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;Event Loop&lt;/strong&gt; coordinates when queued work gets an opportunity to execute.&lt;/p&gt;

&lt;p&gt;A simplified mental model is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        Call Stack
            │
            ▼
       Is it empty?
            │
       ┌────┴────┐
       │         │
      No        Yes
       │         │
       ▼         ▼
    Continue   Process
    execution  queued work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When JavaScript is busy executing code, queued callbacks have to wait.&lt;/p&gt;

&lt;p&gt;When the current execution finishes, the runtime can process pending asynchronous work according to its scheduling rules.&lt;/p&gt;

&lt;p&gt;This continuous coordination is what we call the &lt;strong&gt;Event Loop&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. Microtasks: The Important Second Queue
&lt;/h1&gt;

&lt;p&gt;Things become more interesting when &lt;strong&gt;Promises&lt;/strong&gt; are involved.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timeout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
Promise
Timeout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because Promise reactions are scheduled as &lt;strong&gt;microtasks&lt;/strong&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Promise.then()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Promise.catch()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Promise.finally()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;queueMicrotask()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we can simplify the model into two categories:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Microtask Queue
    │
    ├── Promise callbacks
    └── queueMicrotask()

Task Queue
    │
    ├── Timers
    ├── User interaction tasks
    └── Other scheduled tasks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  7. Why Does the Promise Run Before &lt;code&gt;setTimeout&lt;/code&gt;?
&lt;/h1&gt;

&lt;p&gt;Let's break down the previous example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timeout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 1: Synchronous code executes
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the Promise reaction is scheduled as a microtask.&lt;/p&gt;

&lt;p&gt;The timer is also registered.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;runs.&lt;/p&gt;

&lt;p&gt;Output so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the current JavaScript execution has finished.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 2: Microtasks are processed
&lt;/h3&gt;

&lt;p&gt;The Promise callback is waiting in the Microtask Queue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Microtask Queue

[ Promise callback ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The runtime processes pending microtasks before moving on to the next task.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is printed.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 3: The timer task gets its turn
&lt;/h3&gt;

&lt;p&gt;The timer callback is waiting as a task.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Timeout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is printed.&lt;/p&gt;

&lt;p&gt;Final output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
Promise
Timeout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key idea is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;After the current JavaScript execution finishes, pending microtasks are processed before the next task gets a chance to run.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  8. A Classic Event Loop Question
&lt;/h1&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What will be printed?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
4
3
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see why.&lt;/p&gt;

&lt;h3&gt;
  
  
  Synchronous code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;Then the current execution finishes.&lt;/p&gt;

&lt;p&gt;The Promise callback is a microtask:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Microtask Queue
    ↓
    3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The timer callback is a task:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Task Queue
    ↓
    2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Microtasks are processed first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the timer task runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
4
3
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  9. Does &lt;code&gt;setTimeout(..., 0)&lt;/code&gt; Mean "Run Immediately"?
&lt;/h1&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;This is one of the most common misconceptions about JavaScript timers.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;0&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; mean:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Run this callback immediately.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It means the timer has a minimum delay of approximately zero milliseconds before the callback becomes eligible to be scheduled.&lt;/p&gt;

&lt;p&gt;It still has to wait for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Current JavaScript execution
        ↓
Microtasks
        ↓
Scheduling opportunity
        ↓
Timer callback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timeout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
Timeout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even with a zero-millisecond delay.&lt;/p&gt;




&lt;h1&gt;
  
  
  10. What If JavaScript Is Busy?
&lt;/h1&gt;

&lt;p&gt;Here's where the Event Loop becomes especially important.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timeout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10000000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Heavy computation&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The timer may become ready while the loop is running.&lt;/p&gt;

&lt;p&gt;But its callback cannot interrupt the currently executing JavaScript.&lt;/p&gt;

&lt;p&gt;The Call Stack is still busy.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Call Stack
    │
    ▼
Heavy computation
    │
    │
    │  Timer becomes ready
    │
    ▼
Task Queue
    │
    │ WAIT
    │
    ▼
Call Stack becomes available
    │
    ▼
Timer callback executes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why a long-running JavaScript task can make a web page feel frozen.&lt;/p&gt;

&lt;p&gt;Buttons may stop responding.&lt;/p&gt;

&lt;p&gt;Animations can appear stuck.&lt;/p&gt;

&lt;p&gt;Input can feel delayed.&lt;/p&gt;

&lt;p&gt;Timers may execute later than expected.&lt;/p&gt;




&lt;h1&gt;
  
  
  11. JavaScript Isn't Doing Everything by Itself
&lt;/h1&gt;

&lt;p&gt;A useful way to understand asynchronous JavaScript is to think of JavaScript as one worker coordinating with its environment.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             JavaScript
                  │
       ┌──────────┼──────────┐
       │          │          │
       ▼          ▼          ▼
     Timer      Network     Events
       │          │          │
       └──────────┼──────────┘
                  ▼
                Queues
                  │
                  ▼
              Event Loop
                  │
                  ▼
             Call Stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript doesn't need to sit on the Call Stack waiting for every external operation to finish.&lt;/p&gt;

&lt;p&gt;The host environment can handle the operation and later schedule the appropriate work for JavaScript.&lt;/p&gt;

&lt;p&gt;This is what allows applications to remain responsive while waiting for things such as network responses or user interactions.&lt;/p&gt;




&lt;h1&gt;
  
  
  12. Why the Event Loop Matters
&lt;/h1&gt;

&lt;p&gt;The Event Loop isn't just an interview question.&lt;/p&gt;

&lt;p&gt;It directly affects real applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  UI responsiveness
&lt;/h3&gt;

&lt;p&gt;If the main thread is blocked by expensive JavaScript, the browser has less opportunity to process user interactions and update the page.&lt;/p&gt;

&lt;h3&gt;
  
  
  API calls
&lt;/h3&gt;

&lt;p&gt;Network operations can take unpredictable amounts of time. JavaScript can continue executing other work instead of blocking the entire application while waiting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Timers
&lt;/h3&gt;

&lt;p&gt;A timer specifies when its callback becomes eligible, not an exact guarantee that it will execute at that exact moment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Promises
&lt;/h3&gt;

&lt;p&gt;Understanding microtasks explains why Promise callbacks often run before timer callbacks.&lt;/p&gt;

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

&lt;p&gt;Large synchronous tasks can delay everything waiting for the main thread.&lt;/p&gt;

&lt;p&gt;Understanding the Event Loop helps developers reason about all of these behaviors.&lt;/p&gt;




&lt;h1&gt;
  
  
  13. The Simplified Mental Model
&lt;/h1&gt;

&lt;p&gt;You don't need to memorize every implementation detail.&lt;/p&gt;

&lt;p&gt;Start with this model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             JavaScript
                  │
                  ▼
             Call Stack
                  │
        ┌─────────┴─────────┐
        │                   │
   Sync execution       Async operation
                            │
                            ▼
                       Host APIs
                            │
                   ┌────────┴────────┐
                   ▼                 ▼
             Microtask Queue     Task Queue
                   │                 │
                   └────────┬────────┘
                            ▼
                       Event Loop
                            │
                            ▼
                       Call Stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And remember the basic ordering:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Current JavaScript
        ↓
Microtasks
        ↓
Next task
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the core idea.&lt;/p&gt;




&lt;h1&gt;
  
  
  14. The Restaurant Analogy
&lt;/h1&gt;

&lt;p&gt;Here's another simple way to remember it.&lt;/p&gt;

&lt;p&gt;Imagine a restaurant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Call Stack
= Chef

Host APIs
= Kitchen staff handling things in the background

Microtask Queue
= High-priority orders

Task Queue
= Regular orders

Event Loop
= Manager deciding what the chef should handle next
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The chef can prepare only one order at a time.&lt;/p&gt;

&lt;p&gt;While something else is being prepared by the kitchen staff, the chef can work on another order.&lt;/p&gt;

&lt;p&gt;When the chef becomes available, the manager coordinates which waiting work gets processed next.&lt;/p&gt;

&lt;p&gt;The analogy isn't perfect, but it gives you the right intuition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;One execution thread doesn't mean the entire application has to stop whenever something takes time.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Final Mental Model
&lt;/h1&gt;

&lt;p&gt;When you see asynchronous JavaScript, think:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. JavaScript starts executing
             ↓
2. Synchronous code runs on the Call Stack
             ↓
3. Some operations are handled by the host environment
             ↓
4. Completed work is scheduled
             ↓
5. Current JavaScript execution finishes
             ↓
6. Microtasks are processed
             ↓
7. A task gets an opportunity to execute
             ↓
8. The cycle continues
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, in one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Call Stack
    ↓
Host APIs
    ↓
Microtasks / Tasks
    ↓
Event Loop
    ↓
Call Stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The most important thing to remember is that &lt;strong&gt;JavaScript's single-threaded execution model doesn't mean the application can perform only one kind of work at a time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The JavaScript engine, host environment, queues, and Event Loop work together to coordinate asynchronous operations.&lt;/p&gt;

&lt;p&gt;Once you understand that model, concepts like &lt;strong&gt;Promises, timers, API requests, event handlers, and UI responsiveness&lt;/strong&gt; become much easier to reason about.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Event Loop is essentially the mechanism that keeps asynchronous JavaScript moving without blocking every piece of work behind it.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How Does the Browser Turn Code Into Pixels?</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Thu, 27 Aug 2026 02:56:47 +0000</pubDate>
      <link>https://dev.to/tanu_priya/how-does-the-browser-turn-code-into-pixels-2fdp</link>
      <guid>https://dev.to/tanu_priya/how-does-the-browser-turn-code-into-pixels-2fdp</guid>
      <description>&lt;p&gt;When you build a website, you write code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Welcome&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;This is my website.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Get Started&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;48px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Clicked!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But your screen doesn't understand HTML.&lt;/p&gt;

&lt;p&gt;It doesn't understand CSS.&lt;/p&gt;

&lt;p&gt;And it doesn't understand JavaScript.&lt;/p&gt;

&lt;p&gt;Your screen understands one thing:&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Pixels.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;So how does this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTML + CSS + JavaScript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;become this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A complete website on your screen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser has to process your code through a rendering pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTML → DOM
CSS → CSSOM
       ↓
   Render Tree
       ↓
     Layout
       ↓
     Paint
       ↓
   Composite
       ↓
Pixels on Screen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see what actually happens behind the scenes.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;1. HTML Creates the Structure&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Everything starts with HTML.&lt;/p&gt;

&lt;p&gt;Suppose the browser receives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Welcome to my website.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser doesn't simply display this code directly.&lt;/p&gt;

&lt;p&gt;Instead, it parses the HTML and creates a tree-like structure called the &lt;strong&gt;DOM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DOM = Document Object Model&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        BODY
       /    \
     H1      P
     |       |
  Hello    Welcome
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The DOM represents the structure of the webpage.&lt;/p&gt;

&lt;p&gt;It tells the browser:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What elements exist&lt;/li&gt;
&lt;li&gt;How those elements are connected&lt;/li&gt;
&lt;li&gt;What content they contain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this point, the browser knows &lt;strong&gt;what the page contains&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But it still doesn't know how anything should look.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;2. CSS Defines How Everything Should Look&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now the browser processes CSS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;48px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser converts these styling rules into another structure called the &lt;strong&gt;CSSOM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSSOM = CSS Object Model&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can think of it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DOM
"What exists on the page?"

CSSOM
"How should it look?"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The CSSOM contains styling information such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Colors&lt;/li&gt;
&lt;li&gt;Fonts&lt;/li&gt;
&lt;li&gt;Sizes&lt;/li&gt;
&lt;li&gt;Margins&lt;/li&gt;
&lt;li&gt;Padding&lt;/li&gt;
&lt;li&gt;Borders&lt;/li&gt;
&lt;li&gt;Positioning&lt;/li&gt;
&lt;li&gt;Visibility&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now the browser has two important pieces of information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DOM  → Structure
CSSOM → Styles
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step is combining them.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;3. DOM + CSSOM Create the Render Tree&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The browser combines the DOM and CSSOM to create the &lt;strong&gt;Render Tree&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       DOM
        +
      CSSOM
        ↓
   Render Tree
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Render Tree represents the elements that actually need to be rendered on the screen.&lt;/p&gt;

&lt;p&gt;Here's an interesting example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Welcome&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The paragraph still exists in the DOM.&lt;/p&gt;

&lt;p&gt;But because it isn't visible, it doesn't need to be rendered.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DOM
= All elements in the document

Render Tree
= Elements that need to appear visually
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the browser knows:&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What should appear and how it should look.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;But one major question still remains:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where should everything go?&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;4. Layout Calculates the Exact Position of Everything&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This stage is called &lt;strong&gt;Layout&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The browser calculates the exact geometry of every visible element.&lt;/p&gt;

&lt;p&gt;For every element, it needs to determine things like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Width
Height
Position
Spacing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Imagine a simple webpage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+----------------------------------+
|              Header              |
+----------------------------------+
|                                  |
|           Main Content           |
|                                  |
+----------------------------------+
|              Footer              |
+----------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser needs to calculate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Where does the header start?
How wide is it?
How tall is it?

Where does the main content begin?
How much space does it take?

Where exactly should every piece of text appear?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In other words:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Render Tree
     ↓
Layout
     ↓
Exact size and position of every element
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this stage, the browser knows the complete geometry of the page.&lt;/p&gt;

&lt;p&gt;Now it is finally ready to draw it.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;5. Paint Turns the Page Into Visual Elements&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Paint&lt;/strong&gt; phase is where the browser draws the visual parts of the page.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Text&lt;/li&gt;
&lt;li&gt;Colors&lt;/li&gt;
&lt;li&gt;Backgrounds&lt;/li&gt;
&lt;li&gt;Borders&lt;/li&gt;
&lt;li&gt;Images&lt;/li&gt;
&lt;li&gt;Shadows&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.card&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#ddd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;4px&lt;/span&gt; &lt;span class="m"&gt;12px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Layout determines:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The card goes here and has these dimensions."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Paint determines:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Now draw its background, border and shadow."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can think of it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Layout
= Where does it go?

Paint
= What does it look like?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser now creates the visual output needed to display the page.&lt;/p&gt;

&lt;p&gt;But modern browsers have one more important step.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;6. Composite Combines Everything Together&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A webpage is often split into multiple layers.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Background
    +
Content
    +
Images
    +
Fixed Header
    +
Animated Elements
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During &lt;strong&gt;Compositing&lt;/strong&gt;, the browser combines these layers to produce the final image on your screen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Painted Layers
      ↓
   Composite
      ↓
Final Page
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is especially important for animations.&lt;/p&gt;

&lt;p&gt;Some changes can be handled by moving or transforming existing layers instead of recalculating the entire page.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;transform&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;translateX&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's why certain animations can be much smoother than others.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Where Does JavaScript Fit Into This Process?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript can change the webpage at any time.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome Back!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the browser has to update what appears on the screen.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;500px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Changing the width can affect the layout of other elements.&lt;/p&gt;

&lt;p&gt;The browser may need to perform work again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript changes something
        ↓
Style recalculation
        ↓
Layout
        ↓
Paint
        ↓
Composite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why JavaScript can have a major impact on frontend performance.&lt;/p&gt;

&lt;p&gt;If your application repeatedly changes many elements, the browser may repeatedly perform expensive rendering work.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;A Small Change Can Trigger Different Amounts of Work&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This is one of the most important things to understand.&lt;/p&gt;

&lt;p&gt;Imagine changing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;red&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser may only need to update the visual appearance.&lt;/p&gt;

&lt;p&gt;But changing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;width&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;500&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can affect the layout.&lt;/p&gt;

&lt;p&gt;And that may affect other elements around it.&lt;/p&gt;

&lt;p&gt;So the browser may need to do more work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Style
  ↓
Layout
  ↓
Paint
  ↓
Composite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why not every visual change has the same performance cost.&lt;/p&gt;

&lt;p&gt;Understanding the rendering pipeline helps you understand &lt;strong&gt;why some updates are cheap and others are expensive&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Complete Journey&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let's put everything together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTML
  ↓
Browser creates the DOM
  ↓
CSS
  ↓
Browser creates the CSSOM
  ↓
DOM + CSSOM
  ↓
Render Tree
  ↓
Layout
  ↓
Paint
  ↓
Composite
  ↓
Pixels on Your Screen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript can interact with this pipeline at different points by changing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Content&lt;/li&gt;
&lt;li&gt;Styles&lt;/li&gt;
&lt;li&gt;Elements&lt;/li&gt;
&lt;li&gt;Dimensions&lt;/li&gt;
&lt;li&gt;Positions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And depending on what changes, the browser may need to update part of the rendering process again.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;A Real-World Example&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Suppose you create a simple card:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Welcome to my website.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.card&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser roughly goes through this journey:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Read HTML
        ↓
2. Create DOM
        ↓
3. Read CSS
        ↓
4. Create CSSOM
        ↓
5. Create Render Tree
        ↓
6. Calculate Layout
   - Width: 300px
   - Padding: 20px
   - Text positions
   - Final height
        ↓
7. Paint
   - Background
   - Text
   - Border radius
        ↓
8. Composite
        ↓
9. Display Pixels on Screen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this entire process happens incredibly fast.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why Should Frontend Developers Understand This?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Because many performance problems are directly connected to this pipeline.&lt;/p&gt;

&lt;p&gt;A slow website isn't always caused by "slow code."&lt;/p&gt;

&lt;p&gt;Sometimes the browser is simply being forced to do too much work.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Too many style changes
        ↓
More rendering calculations

Frequent size or position changes
        ↓
More layout work

Complex visual effects
        ↓
More painting work

Heavy animations
        ↓
More compositing work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you understand this pipeline, you can start debugging performance more intelligently.&lt;/p&gt;

&lt;p&gt;Instead of saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The page feels slow."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is JavaScript causing too many updates?&lt;/li&gt;
&lt;li&gt;Is the browser recalculating layout too often?&lt;/li&gt;
&lt;li&gt;Are too many elements being repainted?&lt;/li&gt;
&lt;li&gt;Is this animation forcing unnecessary work?&lt;/li&gt;
&lt;li&gt;Can I reduce what the browser needs to render?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is where frontend development starts becoming much more interesting.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Most Important Way to Remember It&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Think of the browser as a factory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTML
↓
Creates the structure

CSS
↓
Adds the design

Render Tree
↓
Decides what should appear

Layout
↓
Calculates where everything goes

Paint
↓
Draws everything

Composite
↓
Combines everything together

Pixels
↓
What you finally see
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every button, image, card and animation on a website goes through some version of this journey before it reaches your screen.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Final Thought&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When you look at a website, you see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Text
Images
Buttons
Cards
Animations
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the browser sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code
  ↓
Structure
  ↓
Styles
  ↓
Render Tree
  ↓
Layout
  ↓
Paint
  ↓
Composite
  ↓
Pixels
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the next time you open a website, remember:&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Your browser isn't showing HTML and CSS.&lt;/strong&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;It is constantly converting your code into millions of carefully calculated pixels.&lt;/strong&gt;
&lt;/h2&gt;




</description>
      <category>css</category>
      <category>frontend</category>
      <category>html</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What Actually Happens When You Open a Website?</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Wed, 26 Aug 2026 05:08:19 +0000</pubDate>
      <link>https://dev.to/tanu_priya/what-actually-happens-when-you-open-a-website-4oc9</link>
      <guid>https://dev.to/tanu_priya/what-actually-happens-when-you-open-a-website-4oc9</guid>
      <description>&lt;p&gt;You type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Press &lt;strong&gt;Enter&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And within seconds, the website appears on your screen.&lt;/p&gt;

&lt;p&gt;But have you ever wondered what actually happens between &lt;strong&gt;pressing Enter&lt;/strong&gt; and &lt;strong&gt;seeing the webpage&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;Behind this simple action, your browser performs an incredible chain of operations.&lt;/p&gt;

&lt;p&gt;Let's follow the journey. &lt;/p&gt;




&lt;h2&gt;
  
  
  The Big Picture
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You Enter a URL
        ↓
       DNS
        ↓
    IP Address
        ↓
Server Connection
        ↓
   HTTP Request
        ↓
      Server
        ↓
  HTTP Response
        ↓
 HTML + CSS + JS
        ↓
 Browser Rendering
        ↓
   Website on Screen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break it down step by step.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. You Enter a URL
&lt;/h2&gt;

&lt;p&gt;Suppose you type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This URL tells the browser &lt;strong&gt;what resource you want to access&lt;/strong&gt; and &lt;strong&gt;where to look for it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But there is one problem.&lt;/p&gt;

&lt;p&gt;Computers don't really communicate using domain names like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They communicate using &lt;strong&gt;IP addresses&lt;/strong&gt;, such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;93.184.216.34
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the browser first needs to answer one important question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Where is example.com located?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's where &lt;strong&gt;DNS&lt;/strong&gt; comes in.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. DNS Finds the Website
&lt;/h2&gt;

&lt;p&gt;DNS stands for &lt;strong&gt;Domain Name System&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of it as the &lt;strong&gt;phonebook of the internet&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;example.com
      ↓
     DNS
      ↓
93.184.216.34
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser asks DNS:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What is the IP address for &lt;code&gt;example.com&lt;/code&gt;?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;DNS returns the address associated with that domain.&lt;/p&gt;

&lt;p&gt;Now the browser knows &lt;strong&gt;where it needs to go&lt;/strong&gt;. DNS lookups may also benefit from caching, so the browser does not necessarily need to repeat the full lookup every time.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. The Browser Connects to the Server
&lt;/h2&gt;

&lt;p&gt;Now that the browser knows the server's IP address, it establishes communication with the destination.&lt;/p&gt;

&lt;p&gt;For an HTTPS website, the browser also needs to establish a &lt;strong&gt;secure connection&lt;/strong&gt; before exchanging sensitive data.&lt;/p&gt;

&lt;p&gt;Then the browser is ready to ask for the webpage.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. The Browser Sends an HTTP Request
&lt;/h2&gt;

&lt;p&gt;Your browser sends a request similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nf"&gt;GET&lt;/span&gt; &lt;span class="nn"&gt;/&lt;/span&gt; &lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;
&lt;span class="na"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;example.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In simple terms, the browser is saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Hey server, I want the homepage of this website."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This communication happens using &lt;strong&gt;HTTP or HTTPS&lt;/strong&gt;, which defines how browsers and servers exchange requests and responses.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. The Server Sends Back a Response
&lt;/h2&gt;

&lt;p&gt;The server receives the request and sends a response.&lt;/p&gt;

&lt;p&gt;That response may contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; HTML&lt;/li&gt;
&lt;li&gt; CSS&lt;/li&gt;
&lt;li&gt; JavaScript&lt;/li&gt;
&lt;li&gt; Images&lt;/li&gt;
&lt;li&gt; Fonts&lt;/li&gt;
&lt;li&gt; Other assets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first response commonly gives the browser the initial HTML document.&lt;/p&gt;

&lt;p&gt;And this is where the browser's real work begins.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. HTML Becomes the DOM
&lt;/h2&gt;

&lt;p&gt;The browser starts reading the HTML.&lt;/p&gt;

&lt;p&gt;Imagine this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Welcome to my website.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser converts this HTML into a tree-like structure called the &lt;strong&gt;DOM (Document Object Model)&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        HTML
          │
        BODY
       /    \
     H1      P
     │       │
 Hello    Welcome
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The DOM becomes the browser's internal representation of the page structure.&lt;/p&gt;

&lt;p&gt;But right now, the page still doesn't know how it should look.&lt;/p&gt;

&lt;p&gt;That's CSS's job.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. CSS Tells the Browser How Everything Should Look
&lt;/h2&gt;

&lt;p&gt;The browser processes CSS rules such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;40px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CSS answers questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What color should this element be?&lt;/li&gt;
&lt;li&gt;How big should it be?&lt;/li&gt;
&lt;li&gt;Where should it appear?&lt;/li&gt;
&lt;li&gt;How much space should it take?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The browser creates a structure based on these styling rules, often described as the &lt;strong&gt;CSSOM&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTML → DOM
CSS  → CSSOM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then it combines the relevant information to prepare the page for rendering.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. DOM + CSS = Render Tree
&lt;/h2&gt;

&lt;p&gt;Now the browser combines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DOM + CSSOM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to create the &lt;strong&gt;Render Tree&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Render Tree focuses on the elements that need to be displayed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       DOM
        +
      CSSOM
        ↓
   Render Tree
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the browser knows:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What should appear on the screen and how it should look.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But it still needs to calculate &lt;strong&gt;where everything should go&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. Layout: Calculating Positions
&lt;/h2&gt;

&lt;p&gt;The browser calculates the:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Width&lt;/li&gt;
&lt;li&gt;Height&lt;/li&gt;
&lt;li&gt;Position&lt;/li&gt;
&lt;li&gt;Spacing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;of every visible element.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────────────────────┐
│           Header             │
├──────────────────────────────┤
│                              │
│         Main Content         │
│                              │
├──────────────────────────────┤
│           Footer             │
└──────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This process is called &lt;strong&gt;Layout&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The browser now knows the exact geometry of the page.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. Paint: Turning Everything Into Pixels
&lt;/h2&gt;

&lt;p&gt;Now comes the step where the browser actually draws:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Text&lt;/li&gt;
&lt;li&gt;Colors&lt;/li&gt;
&lt;li&gt;Borders&lt;/li&gt;
&lt;li&gt;Images&lt;/li&gt;
&lt;li&gt;Shadows&lt;/li&gt;
&lt;li&gt;Buttons&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;onto the screen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Render Tree
     ↓
   Layout
     ↓
   Paint
     ↓
  Pixels 🎉
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally...&lt;/p&gt;

&lt;h2&gt;
  
  
  You see the website!
&lt;/h2&gt;

&lt;p&gt;The browser's rendering process broadly involves building the relevant document and style structures, calculating layout, and painting pixels to the screen.&lt;/p&gt;




&lt;h2&gt;
  
  
  But What About JavaScript?
&lt;/h2&gt;

&lt;p&gt;JavaScript makes the website &lt;strong&gt;interactive&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It handles things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Button clicks&lt;/li&gt;
&lt;li&gt;Form submissions&lt;/li&gt;
&lt;li&gt;Dropdowns&lt;/li&gt;
&lt;li&gt;Animations&lt;/li&gt;
&lt;li&gt;API calls&lt;/li&gt;
&lt;li&gt;Dynamic content&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript can also modify the DOM and change what you see on the page. As the browser discovers scripts, stylesheets, images, and other resources, it may make additional requests and process them as part of loading the complete experience.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Complete Journey
&lt;/h2&gt;

&lt;p&gt;So, the next time you open a website, remember this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You type a URL
        ↓
Browser finds the IP using DNS
        ↓
Secure connection is established
        ↓
Browser sends an HTTP request
        ↓
Server sends a response
        ↓
Browser receives HTML
        ↓
HTML → DOM
        ↓
CSS → CSSOM
        ↓
DOM + CSSOM → Render Tree
        ↓
Layout
        ↓
Paint
        ↓
JavaScript adds interactivity
        ↓
🎉 Website appears on your screen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Why Should Frontend Developers Understand This?
&lt;/h2&gt;

&lt;p&gt;Because every frontend performance problem connects to this journey.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Slow DNS?&lt;/strong&gt; → Navigation is delayed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Slow server response?&lt;/strong&gt; → HTML arrives late.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Large CSS files?&lt;/strong&gt; → Rendering can be delayed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Heavy JavaScript?&lt;/strong&gt; → The page may become slow or unresponsive.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Large images?&lt;/strong&gt; → More data needs to be downloaded and rendered.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding this flow helps you understand &lt;strong&gt;why websites become slow&lt;/strong&gt; — not just how to write frontend code.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thought
&lt;/h2&gt;

&lt;p&gt;Opening a website feels almost instant.&lt;/p&gt;

&lt;p&gt;But in those few seconds, your browser has:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Found a server → communicated across the internet → requested resources → downloaded files → built the page structure → applied styles → calculated layouts → painted pixels → and executed JavaScript.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;All from one simple action:&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Pressing Enter.&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;The web might look simple from the outside.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;But underneath every page is a fascinating chain of engineering. *&lt;/em&gt;&lt;/p&gt;




</description>
      <category>networking</category>
      <category>web</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Design a Scalable Chat Application Combine everything into one architecture</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Tue, 25 Aug 2026 07:51:09 +0000</pubDate>
      <link>https://dev.to/tanu_priya/design-a-scalable-chat-application-combine-everything-into-one-architecture-6m2</link>
      <guid>https://dev.to/tanu_priya/design-a-scalable-chat-application-combine-everything-into-one-architecture-6m2</guid>
      <description>&lt;p&gt;Building a chat application may seem simple at first.&lt;/p&gt;

&lt;p&gt;A user sends a message, another user receives it, and the conversation continues.&lt;/p&gt;

&lt;p&gt;But once your application grows from a few users to millions, the architecture becomes much more interesting.&lt;/p&gt;

&lt;p&gt;You need to think about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How do users connect in real time?&lt;/li&gt;
&lt;li&gt;How are messages delivered?&lt;/li&gt;
&lt;li&gt;Where are messages stored?&lt;/li&gt;
&lt;li&gt;What happens when a server crashes?&lt;/li&gt;
&lt;li&gt;How do you support millions of concurrent connections?&lt;/li&gt;
&lt;li&gt;How do users receive messages when they're offline?&lt;/li&gt;
&lt;li&gt;How do you monitor the entire system?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where all the concepts from system design come together.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;In this article, we'll design a scalable chat application from requirements to a production-ready architecture.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 1: Understand the Requirements
&lt;/h2&gt;

&lt;p&gt;Before choosing technologies, we need to understand what we're building.&lt;/p&gt;

&lt;p&gt;Let's assume we're designing a WhatsApp- or Discord-like chat system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functional Requirements
&lt;/h2&gt;

&lt;p&gt;Our application should support:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One-to-one messaging&lt;/li&gt;
&lt;li&gt;Group chats&lt;/li&gt;
&lt;li&gt;Real-time message delivery&lt;/li&gt;
&lt;li&gt;Message history&lt;/li&gt;
&lt;li&gt;Online/offline status&lt;/li&gt;
&lt;li&gt;Read receipts&lt;/li&gt;
&lt;li&gt;Typing indicators&lt;/li&gt;
&lt;li&gt;Media messages&lt;/li&gt;
&lt;li&gt;Push notifications&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Non-Functional Requirements
&lt;/h2&gt;

&lt;p&gt;The system should also provide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Low latency&lt;/li&gt;
&lt;li&gt;High availability&lt;/li&gt;
&lt;li&gt;Scalability&lt;/li&gt;
&lt;li&gt;Message durability&lt;/li&gt;
&lt;li&gt;Fault tolerance&lt;/li&gt;
&lt;li&gt;Observability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These requirements will guide our architecture.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Estimate the Traffic
&lt;/h2&gt;

&lt;p&gt;Let's assume:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 Million Daily Active Users
1 Million Concurrent Users
100 Messages per User per Day
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That gives us:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10M × 100 = 1 Billion Messages / Day
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Average messages per second:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1,000,000,000 / 86,400

≈ 11,500 messages/second
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But traffic is not evenly distributed.&lt;/p&gt;

&lt;p&gt;Peak traffic could be several times higher.&lt;/p&gt;

&lt;p&gt;Let's design for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;50,000+ messages/second
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells us that a single server or database will eventually become a bottleneck.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: High-Level Architecture
&lt;/h2&gt;

&lt;p&gt;A scalable chat system might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                         ┌──────────────┐
                         │    Users     │
                         └──────┬───────┘
                                │
                         WebSocket / HTTPS
                                │
                                ▼
                      ┌──────────────────┐
                      │  Load Balancer   │
                      └────────┬─────────┘
                               │
              ┌────────────────┼────────────────┐
              ▼                ▼                ▼
        ┌───────────┐    ┌───────────┐    ┌───────────┐
        │ Chat Node │    │ Chat Node │    │ Chat Node │
        └─────┬─────┘    └─────┬─────┘    └─────┬─────┘
              │                │                │
              └────────────────┼────────────────┘
                               ▼
                        ┌─────────────┐
                        │ Message Bus │
                        │ Queue/PubSub│
                        └──────┬──────┘
                               │
              ┌────────────────┼────────────────┐
              ▼                ▼                ▼
        Message Store      Presence         Notifications
              │             Service            Service
              ▼
         Chat Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each component has a specific responsibility.&lt;/p&gt;

&lt;p&gt;This separation makes the system easier to scale independently.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4: Real-Time Communication
&lt;/h2&gt;

&lt;p&gt;Chat applications need a persistent connection between the client and server.&lt;/p&gt;

&lt;p&gt;Using traditional HTTP polling would create unnecessary requests.&lt;/p&gt;

&lt;p&gt;A better approach is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
   │
   │ WebSocket Connection
   │
   ▼
Chat Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once connected, either side can send data instantly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User A ─── Message ───→ Chat Server
                              │
                              ▼
                       User B receives it
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;WebSockets are useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New messages&lt;/li&gt;
&lt;li&gt;Typing indicators&lt;/li&gt;
&lt;li&gt;Read receipts&lt;/li&gt;
&lt;li&gt;Online presence&lt;/li&gt;
&lt;li&gt;Live updates&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 5: Scaling WebSocket Connections
&lt;/h2&gt;

&lt;p&gt;A single chat server cannot maintain millions of connections.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                      Load Balancer
                            │
              ┌─────────────┼─────────────┐
              ▼             ▼             ▼
          Chat Node 1    Chat Node 2    Chat Node 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each node manages a subset of active connections.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Node 1 → 100,000 Connections
Node 2 → 100,000 Connections
Node 3 → 100,000 Connections
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As users grow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;More Users
     │
     ▼
Add More Chat Nodes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This provides &lt;strong&gt;horizontal scalability&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The load balancer can route new WebSocket connections to healthy nodes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 6: Sending a Message
&lt;/h2&gt;

&lt;p&gt;Let's follow a message from User A to User B.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User A
  │
  │ "Hello!"
  ▼
Chat Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server first validates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is the user authenticated?&lt;/li&gt;
&lt;li&gt;Is the sender allowed in this conversation?&lt;/li&gt;
&lt;li&gt;Is the message valid?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then the message enters the processing flow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
   │
   ▼
Chat Gateway
   │
   ▼
Authentication
   │
   ▼
Message Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A message might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"messageId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"msg_123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"conversationId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"conv_456"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"senderId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user_A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Hello!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"timestamp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2026-08-23T12:00:00Z"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;messageId&lt;/code&gt; is important for deduplication and idempotency.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 7: Store the Message First
&lt;/h2&gt;

&lt;p&gt;A message should not be considered successfully sent until the system can reliably persist it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User A
   │
   ▼
Message Service
   │
   ▼
Message Database
   │
   ▼
Message Stored ✅
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After successful storage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message Service
       │
       ├──→ Deliver to recipient
       │
       ├──→ Update conversation
       │
       └──→ Trigger notifications
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This improves durability.&lt;/p&gt;

&lt;p&gt;If a chat server crashes after the message is stored, the message is not lost.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 8: Delivering Messages in Real Time
&lt;/h2&gt;

&lt;p&gt;Now we need to find User B.&lt;/p&gt;

&lt;p&gt;What if User B is connected to a different server?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User A
   │
   ▼
Chat Node 1
   │
   │ Message for User B
   ▼
        ?
   │
   ▼
Chat Node 3
   │
   ▼
User B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The system needs a way for chat servers to communicate.&lt;/p&gt;

&lt;p&gt;This is where a &lt;strong&gt;message broker or Pub/Sub system&lt;/strong&gt; can help.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                  Message Broker
                  /      |      \
                 ▼       ▼       ▼
              Node 1   Node 2   Node 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The message can be published to the appropriate channel.&lt;/p&gt;

&lt;p&gt;The node holding User B's connection delivers it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User A
   │
   ▼
Node 1
   │
   ▼
Message Broker
   │
   ▼
Node 3
   │
   ▼
User B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 9: Managing User Presence
&lt;/h2&gt;

&lt;p&gt;Presence tells us whether a user is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Online&lt;/li&gt;
&lt;li&gt;Offline&lt;/li&gt;
&lt;li&gt;Away&lt;/li&gt;
&lt;li&gt;Last seen recently&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A fast in-memory store can manage this information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_123 → Chat Node 2
user_456 → Chat Node 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a user connects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_123 → ONLINE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the connection closes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_123 → OFFLINE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A distributed presence store allows any chat server to find the user's active connection.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 10: What If the User Is Offline?
&lt;/h2&gt;

&lt;p&gt;If User B is offline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User A
   │
   ▼
Message Service
   │
   ├── Message Database ✅
   │
   └── User B Offline
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The message remains stored.&lt;/p&gt;

&lt;p&gt;The system can then send a push notification.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Notification Service
        │
        ▼
   Push Provider
        │
        ▼
     User B 📱
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When User B reconnects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User B
   │
   ▼
Chat Service
   │
   ▼
Fetch Unread Messages
   │
   ▼
Display Conversation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures offline users don't lose messages.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 11: Supporting Group Chats
&lt;/h2&gt;

&lt;p&gt;Group chats introduce another challenge.&lt;/p&gt;

&lt;p&gt;Imagine a group with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10,000 Members
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If one user sends a message, how should it be delivered?&lt;/p&gt;

&lt;p&gt;There are two common approaches.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fan-Out on Write
&lt;/h2&gt;

&lt;p&gt;When a message is sent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message
   │
   ├──→ User 1 Inbox
   ├──→ User 2 Inbox
   ├──→ User 3 Inbox
   └──→ Thousands More
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Fast reads&lt;/li&gt;
&lt;li&gt;Easy message retrieval&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Disadvantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Expensive writes for large groups&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Fan-Out on Read
&lt;/h2&gt;

&lt;p&gt;Store the message once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Group Message Store
        │
        ▼
Members read when needed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Efficient writes&lt;/li&gt;
&lt;li&gt;Better for very large groups&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Disadvantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;More expensive reads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A real system may use a hybrid approach.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 12: Database Design
&lt;/h2&gt;

&lt;p&gt;Chat applications generate massive amounts of data.&lt;/p&gt;

&lt;p&gt;A simple message table might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Messages
------------------------------------
message_id
conversation_id
sender_id
content
created_at
status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A useful query is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;conversation_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This suggests that messages should be efficiently organized around:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;conversation_id + timestamp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This supports fast retrieval of recent messages.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 13: Database Partitioning and Sharding
&lt;/h2&gt;

&lt;p&gt;As the number of messages grows, a single database may become too large.&lt;/p&gt;

&lt;p&gt;We can partition messages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Messages
   │
   ├── Partition 1
   ├── Partition 2
   └── Partition 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Eventually, we may shard data across multiple database servers.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hash(conversation_id) % 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Conversation A → Shard 1
Conversation B → Shard 2
Conversation C → Shard 3
Conversation D → Shard 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;conversation_id&lt;/code&gt; as a shard key keeps most messages from the same conversation together.&lt;/p&gt;

&lt;p&gt;This makes message retrieval easier.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 14: Adding Caching
&lt;/h2&gt;

&lt;p&gt;Some data is accessed frequently.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;User profiles&lt;/li&gt;
&lt;li&gt;Conversation metadata&lt;/li&gt;
&lt;li&gt;Active conversations&lt;/li&gt;
&lt;li&gt;Presence information&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of querying the database every time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application
     │
     ▼
    Cache
     │
     ├── Hit → Return Data ⚡
     │
     └── Miss → Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Caching reduces:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database load&lt;/li&gt;
&lt;li&gt;Latency&lt;/li&gt;
&lt;li&gt;Infrastructure cost&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But the database remains the source of truth.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 15: Reliability and Fault Tolerance
&lt;/h2&gt;

&lt;p&gt;Failures are inevitable.&lt;/p&gt;

&lt;p&gt;Suppose one chat node crashes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Chat Node 2 ❌
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The load balancer should stop routing new connections to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Load Balancer
     │
     ├── Node 1 ✅
     ├── Node 2 ❌
     └── Node 3 ✅
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Users connected to the failed node reconnect to healthy servers.&lt;/p&gt;

&lt;p&gt;For critical components, we need redundancy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                  Load Balancer
                         │
            ┌────────────┼────────────┐
            ▼            ▼            ▼
         Node 1        Node 2        Node 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Health checks&lt;/li&gt;
&lt;li&gt;Timeouts&lt;/li&gt;
&lt;li&gt;Retries&lt;/li&gt;
&lt;li&gt;Circuit breakers&lt;/li&gt;
&lt;li&gt;Database replicas&lt;/li&gt;
&lt;li&gt;Automatic failover&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is to minimize the impact of individual failures.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 16: Handling Duplicate Messages
&lt;/h2&gt;

&lt;p&gt;Distributed systems can deliver the same message more than once.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message Sent
    │
Network Timeout
    │
Client Doesn't Know if Server Received It
    │
Client Retries
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without protection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello!
Hello!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To prevent this, clients can generate a unique &lt;code&gt;messageId&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;messageId = msg_123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the server receives the same message again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;msg_123 already processed

→ Ignore duplicate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called &lt;strong&gt;idempotency&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is essential for reliable messaging.&lt;/p&gt;




&lt;h2&gt;
  
  
  Message Ordering
&lt;/h2&gt;

&lt;p&gt;Users expect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello
How are you?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How are you?
Hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ordering becomes difficult when multiple servers process messages.&lt;/p&gt;

&lt;p&gt;A practical approach is to assign a sequence number within each conversation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Conversation A

Message 1 → Sequence 101
Message 2 → Sequence 102
Message 3 → Sequence 103
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clients can then display messages in the correct order.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 17: Security and Authentication
&lt;/h2&gt;

&lt;p&gt;Every connection must be authenticated.&lt;/p&gt;

&lt;p&gt;A common flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Login
    │
    ▼
Authentication Service
    │
    ▼
Access Token
    │
    ▼
WebSocket Connection
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server validates the token before allowing the user to connect.&lt;/p&gt;

&lt;p&gt;The system should also verify:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is the sender part of the conversation?&lt;/li&gt;
&lt;li&gt;Does the user have permission to access this group?&lt;/li&gt;
&lt;li&gt;Is the message within allowed limits?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Security should be enforced on the server—not trusted to the client.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 18: Observability
&lt;/h2&gt;

&lt;p&gt;With multiple services and servers, debugging becomes difficult without visibility.&lt;/p&gt;

&lt;p&gt;We need:&lt;/p&gt;

&lt;h2&gt;
  
  
  Logs
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message received
Message stored
Message delivered
Delivery failed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Metrics
&lt;/h2&gt;

&lt;p&gt;Track:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Messages per second&lt;/li&gt;
&lt;li&gt;Delivery latency&lt;/li&gt;
&lt;li&gt;Active WebSocket connections&lt;/li&gt;
&lt;li&gt;Error rate&lt;/li&gt;
&lt;li&gt;Database latency&lt;/li&gt;
&lt;li&gt;Queue size&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Traces
&lt;/h2&gt;

&lt;p&gt;Follow a message across the system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
   │
   ▼
Chat Gateway
   │
   ▼
Message Service
   │
   ▼
Database
   │
   ▼
Message Broker
   │
   ▼
Recipient Chat Node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helps identify bottlenecks quickly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Putting Everything Together
&lt;/h2&gt;

&lt;p&gt;Now we can combine everything into one architecture.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                              USERS
                                │
                    WebSocket / HTTPS
                                │
                                ▼
                        LOAD BALANCER
                                │
             ┌──────────────────┼──────────────────┐
             ▼                  ▼                  ▼
         CHAT NODE 1        CHAT NODE 2        CHAT NODE 3
             │                  │                  │
             └──────────────────┼──────────────────┘
                                │
                     ┌──────────┴──────────┐
                     ▼                     ▼
              PRESENCE SERVICE       MESSAGE SERVICE
                     │                     │
                     ▼                     ▼
               IN-MEMORY STORE      MESSAGE BROKER
                                           │
                          ┌────────────────┼───────────────┐
                          ▼                ▼               ▼
                     MESSAGE DB      NOTIFICATIONS      DELIVERY
                          │
                    ┌─────┴─────┐
                    ▼           ▼
                 REPLICA     REPLICA
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Across the entire architecture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    OBSERVABILITY LAYER

        Logs ──────┐
        Metrics ───┼──→ Dashboards + Alerts
        Traces ────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And reliability mechanisms protect the system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Redundancy
Retries
Timeouts
Circuit Breakers
Failover
Health Checks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Complete Message Flow
&lt;/h2&gt;

&lt;p&gt;Let's follow one message end to end.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. User A sends a message
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User A → WebSocket → Chat Node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. The server authenticates and validates it
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Token Valid? ✅
Conversation Access? ✅
Message Valid? ✅
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. The message is stored
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message Service → Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. The message event is published
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Database
   │
   ▼
Message Broker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Find User B
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Presence Store
   │
   ▼
User B → Chat Node 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Deliver in real time
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message Broker
   │
   ▼
Chat Node 3
   │
   ▼
User B 📱
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. If User B is offline
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message Stored ✅
        │
        ▼
Push Notification
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. Track the entire process
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Logs + Metrics + Traces
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the complete journey of a message.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Does This System Scale?
&lt;/h2&gt;

&lt;p&gt;As usage grows, individual components can scale independently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;More Connections?
→ Add Chat Nodes

More Messages?
→ Add Message Processing Workers

More Database Load?
→ Add Replicas / Partition / Shard

More Cache Load?
→ Expand Cache Cluster

More Notifications?
→ Add Notification Workers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of the biggest advantages of separating responsibilities.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Scale the bottleneck, not the entire system.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Common Trade-Offs
&lt;/h2&gt;

&lt;p&gt;There is no perfect architecture.&lt;/p&gt;

&lt;p&gt;Every decision involves trade-offs.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Decision&lt;/th&gt;
&lt;th&gt;Benefit&lt;/th&gt;
&lt;th&gt;Trade-Off&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;WebSockets&lt;/td&gt;
&lt;td&gt;Real-time communication&lt;/td&gt;
&lt;td&gt;Connection management complexity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Message Broker&lt;/td&gt;
&lt;td&gt;Decouples services&lt;/td&gt;
&lt;td&gt;Additional infrastructure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Caching&lt;/td&gt;
&lt;td&gt;Lower latency&lt;/td&gt;
&lt;td&gt;Cache consistency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sharding&lt;/td&gt;
&lt;td&gt;Horizontal database scale&lt;/td&gt;
&lt;td&gt;Operational complexity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Replication&lt;/td&gt;
&lt;td&gt;Higher availability&lt;/td&gt;
&lt;td&gt;Replication lag&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retries&lt;/td&gt;
&lt;td&gt;Handles temporary failures&lt;/td&gt;
&lt;td&gt;Can cause retry storms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fan-out on write&lt;/td&gt;
&lt;td&gt;Fast reads&lt;/td&gt;
&lt;td&gt;Expensive for large groups&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fan-out on read&lt;/td&gt;
&lt;td&gt;Efficient writes&lt;/td&gt;
&lt;td&gt;Slower reads&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Good system design is about understanding these trade-offs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Takeaways
&lt;/h2&gt;

&lt;p&gt;Designing a scalable chat application brings together nearly every major system design concept.&lt;/p&gt;

&lt;p&gt;You start with &lt;strong&gt;requirements and traffic estimation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Then you design for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;WebSockets&lt;/strong&gt; for real-time communication&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Load balancing&lt;/strong&gt; for scalable connections&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Message brokers&lt;/strong&gt; for cross-server delivery&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Databases&lt;/strong&gt; for durable message storage&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Caching&lt;/strong&gt; for low-latency access&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Partitioning and sharding&lt;/strong&gt; for massive datasets&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Redundancy and failover&lt;/strong&gt; for reliability&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Retries and idempotency&lt;/strong&gt; for safe message delivery&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Logs, metrics, and traces&lt;/strong&gt; for observability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The final architecture is not about using every technology possible.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;It's about combining the right components to build a system that is real-time, scalable, reliable, and easy to operate.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The complete system design mindset:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Requirements
     ↓
Estimate Scale
     ↓
Choose Architecture
     ↓
Identify Bottlenecks
     ↓
Add Caching
     ↓
Scale Components
     ↓
Design for Failure
     ↓
Add Observability
     ↓
Continuously Improve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;And that's how individual system design concepts come together to build a scalable real-world application. *&lt;/em&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>scalability</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Observability: Understanding Your System Through Logs, Metrics, Traces &amp; Monitoring</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Mon, 24 Aug 2026 07:30:19 +0000</pubDate>
      <link>https://dev.to/tanu_priya/observability-understanding-your-system-through-logs-metrics-traces-monitoring-390h</link>
      <guid>https://dev.to/tanu_priya/observability-understanding-your-system-through-logs-metrics-traces-monitoring-390h</guid>
      <description>&lt;p&gt;Modern distributed systems are complex.&lt;/p&gt;

&lt;p&gt;A single user request might travel through multiple services, databases, caches, queues, and external APIs before a response is returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
  │
  ▼
API Gateway
  │
  ▼
User Service ───→ Redis
  │
  ▼
Order Service
  │
  ▼
Payment Service
  │
  ▼
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine a user reports:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"The application is very slow."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Where is the problem?&lt;/p&gt;

&lt;p&gt;Is it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The API server?&lt;/li&gt;
&lt;li&gt;The database?&lt;/li&gt;
&lt;li&gt;Redis?&lt;/li&gt;
&lt;li&gt;A slow microservice?&lt;/li&gt;
&lt;li&gt;An external API?&lt;/li&gt;
&lt;li&gt;A network issue?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without visibility into the system, finding the answer can feel like searching in the dark.&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;Observability&lt;/strong&gt; becomes essential.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Observability?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Observability is the ability to understand what is happening inside a system by analyzing the signals it produces.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In simple terms:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Observability helps you answer: "What is happening inside my system, and why?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A system is observable when engineers can investigate unexpected behavior without needing to manually reproduce every problem.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Problem Detected
      │
      ▼
Check Metrics
      │
      ▼
Find Affected Service
      │
      ▼
Check Logs
      │
      ▼
Follow Request Trace
      │
      ▼
Find Root Cause
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The foundation of observability is often built around three major signals:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Logs&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Metrics&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Traces&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are commonly called the &lt;strong&gt;three pillars of observability&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Logs — What Happened?
&lt;/h2&gt;

&lt;p&gt;Logs are detailed records of events that occur inside a system.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2026-08-23 10:30:12 INFO User logged in
2026-08-23 10:30:15 INFO Payment request started
2026-08-23 10:30:16 ERROR Payment service timeout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Logs help answer questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happened?&lt;/li&gt;
&lt;li&gt;When did it happen?&lt;/li&gt;
&lt;li&gt;Which user or request was affected?&lt;/li&gt;
&lt;li&gt;What error occurred?&lt;/li&gt;
&lt;li&gt;Which service generated the error?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A useful log usually contains context.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"timestamp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2026-08-23T10:30:16Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"level"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ERROR"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"service"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"payment-service"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"request_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"abc123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Payment provider timeout"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of just seeing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error occurred
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You now know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When it happened&lt;/li&gt;
&lt;li&gt;Which service failed&lt;/li&gt;
&lt;li&gt;Which request was affected&lt;/li&gt;
&lt;li&gt;What the failure was&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This makes debugging significantly easier.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Metrics — How Is the System Performing?
&lt;/h2&gt;

&lt;p&gt;Metrics are numerical measurements collected over time.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;CPU usage&lt;/li&gt;
&lt;li&gt;Memory usage&lt;/li&gt;
&lt;li&gt;Request rate&lt;/li&gt;
&lt;li&gt;Error rate&lt;/li&gt;
&lt;li&gt;Response time&lt;/li&gt;
&lt;li&gt;Database connections&lt;/li&gt;
&lt;li&gt;Queue size&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Requests per second: 12,000
Average latency: 120ms
Error rate: 0.4%
CPU usage: 68%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Metrics are useful because they help identify &lt;strong&gt;trends and anomalies&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Imagine this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10:00 → 0.2% errors
10:05 → 0.3% errors
10:10 → 0.4% errors
10:15 → 8.7% errors 🚨
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Something clearly changed.&lt;/p&gt;

&lt;p&gt;Metrics help teams answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is traffic increasing?&lt;/li&gt;
&lt;li&gt;Is latency getting worse?&lt;/li&gt;
&lt;li&gt;Is the error rate rising?&lt;/li&gt;
&lt;li&gt;Is a server overloaded?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike logs, metrics are especially useful for viewing the &lt;strong&gt;overall health of a system&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Traces — Where Did the Request Go?
&lt;/h2&gt;

&lt;p&gt;Logs tell you what happened.&lt;/p&gt;

&lt;p&gt;Metrics tell you how the system is performing.&lt;/p&gt;

&lt;p&gt;But in a distributed system, you also need to know:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Where did this specific request travel?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is where &lt;strong&gt;distributed tracing&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;Imagine a request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Request
     │
     ▼
API Gateway
     │ 20ms
     ▼
User Service
     │ 50ms
     ▼
Order Service
     │ 120ms
     ▼
Payment Service
     │ 2,500ms ⚠️
     ▼
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The total request is slow.&lt;/p&gt;

&lt;p&gt;A trace quickly reveals that the payment service is responsible for most of the latency.&lt;/p&gt;

&lt;p&gt;A distributed trace typically uses a &lt;strong&gt;Trace ID&lt;/strong&gt; that follows the request across services.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Trace ID: xyz-789

API Gateway
     │
     ▼
User Service
     │
     ▼
Order Service
     │
     ▼
Payment Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each operation within the trace is called a &lt;strong&gt;span&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Trace
 ├── API Gateway Span
 ├── User Service Span
 ├── Order Service Span
 └── Payment Service Span
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes it possible to follow one request across an entire distributed architecture.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Three Pillars Together
&lt;/h2&gt;

&lt;p&gt;Logs, metrics, and traces become far more powerful when used together.&lt;/p&gt;

&lt;p&gt;Imagine an alert:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🚨 Payment Error Rate Increased
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 1: Metrics
&lt;/h3&gt;

&lt;p&gt;Metrics show:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error Rate: 0.2% → 12%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You know there is a major issue.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Traces
&lt;/h3&gt;

&lt;p&gt;Traces show:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Most failed requests → Payment Provider
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You now know where the failure is happening.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Logs
&lt;/h3&gt;

&lt;p&gt;Logs reveal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Connection timeout after 5 seconds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you understand the root cause.&lt;/p&gt;

&lt;p&gt;The debugging flow becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Metrics
   ↓
Detect the problem
   ↓
Traces
   ↓
Locate the problem
   ↓
Logs
   ↓
Understand the problem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This combination dramatically reduces debugging time.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Monitoring?
&lt;/h2&gt;

&lt;p&gt;Observability and monitoring are related, but they are not exactly the same.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Monitoring&lt;/strong&gt; focuses on tracking known signals and alerting you when something crosses a threshold.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CPU &amp;gt; 90% → Alert
Error Rate &amp;gt; 5% → Alert
Latency &amp;gt; 2 seconds → Alert
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Monitoring answers:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Is something wrong?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Observability goes further and helps answer:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Why is it wrong?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A simple comparison:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Monitoring&lt;/th&gt;
&lt;th&gt;Observability&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Detects known problems&lt;/td&gt;
&lt;td&gt;Helps investigate unknown problems&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Uses dashboards and alerts&lt;/td&gt;
&lt;td&gt;Uses multiple system signals&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Answers "Is it failing?"&lt;/td&gt;
&lt;td&gt;Answers "Why is it failing?"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Focuses on symptoms&lt;/td&gt;
&lt;td&gt;Helps find root causes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You need both.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Importance of Good Alerts
&lt;/h2&gt;

&lt;p&gt;Not every metric should wake up an engineer at 3 AM.&lt;/p&gt;

&lt;p&gt;Poor alerting can create:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100 Alerts
   ↓
Most Are Not Important
   ↓
Alert Fatigue
   ↓
Important Alert Gets Ignored
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good alerts should be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Actionable&lt;/li&gt;
&lt;li&gt;Meaningful&lt;/li&gt;
&lt;li&gt;Relevant&lt;/li&gt;
&lt;li&gt;Prioritized&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of alerting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CPU usage is 80%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Consider alerting based on user impact:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Checkout error rate increased to 15%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second alert provides a clearer reason to investigate.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Four Golden Signals
&lt;/h2&gt;

&lt;p&gt;A useful framework for monitoring production systems focuses on four important signals:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Latency
&lt;/h2&gt;

&lt;p&gt;How long does a request take?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Average: 120ms
P95: 450ms
P99: 2.1s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Percentiles are important because averages can hide slow requests.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Traffic
&lt;/h2&gt;

&lt;p&gt;How much demand is the system handling?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Requests per second: 15,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A sudden spike may indicate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Viral traffic&lt;/li&gt;
&lt;li&gt;A marketing campaign&lt;/li&gt;
&lt;li&gt;Bots&lt;/li&gt;
&lt;li&gt;Abuse&lt;/li&gt;
&lt;li&gt;A potential attack&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. Errors
&lt;/h2&gt;

&lt;p&gt;How many requests are failing?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error Rate: 0.3%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A sudden increase needs investigation.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Saturation
&lt;/h2&gt;

&lt;p&gt;How close is the system to its capacity?&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CPU: 92%
Memory: 88%
Database Connections: 95%
Queue Capacity: 90%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;High saturation means the system may soon become unavailable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Correlation IDs: Connecting Everything
&lt;/h2&gt;

&lt;p&gt;One of the most useful practices in observability is using a &lt;strong&gt;correlation ID&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When a request enters the system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request ID = req-12345
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That ID travels with the request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;API Gateway → req-12345
User Service → req-12345
Order Service → req-12345
Payment Service → req-12345
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can search logs across multiple services and connect all events related to the same request.&lt;/p&gt;

&lt;p&gt;This is especially valuable in microservice architectures.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dashboards: Seeing the Health of Your System
&lt;/h2&gt;

&lt;p&gt;A dashboard gives teams a quick view of important signals.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────────────────────────┐
│ System Health                    │
├──────────────────────────────────┤
│ Requests/sec        12,400       │
│ Error Rate          0.4%         │
│ P95 Latency         320ms        │
│ CPU Usage           65%          │
│ Database Health     Healthy ✅    │
└──────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A good dashboard should answer:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Is my system healthy right now?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Different dashboards may exist for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Infrastructure&lt;/li&gt;
&lt;li&gt;APIs&lt;/li&gt;
&lt;li&gt;Databases&lt;/li&gt;
&lt;li&gt;Business metrics&lt;/li&gt;
&lt;li&gt;Individual services&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Observability in Microservices
&lt;/h2&gt;

&lt;p&gt;Observability becomes even more important when an application uses many services.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                API Gateway
                     │
        ┌────────────┼────────────┐
        ▼            ▼            ▼
   User Service   Order Service  Search Service
        │            │
        ▼            ▼
   Database      Payment Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A single request may involve multiple services.&lt;/p&gt;

&lt;p&gt;Without distributed tracing and structured logs, debugging becomes extremely difficult.&lt;/p&gt;

&lt;p&gt;With observability:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   │
   ▼
Trace ID
   │
   ├── API Gateway
   ├── User Service
   ├── Order Service
   ├── Payment Service
   └── Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can understand the complete request journey.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Makes a System Observable?
&lt;/h2&gt;

&lt;p&gt;A strong observability strategy usually includes:&lt;/p&gt;

&lt;h3&gt;
  
  
  Structured Logging
&lt;/h3&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Something failed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"level"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ERROR"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"service"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"order-service"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"request_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"abc123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"database_timeout"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Meaningful Metrics
&lt;/h3&gt;

&lt;p&gt;Track metrics that matter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Request latency&lt;/li&gt;
&lt;li&gt;Error rate&lt;/li&gt;
&lt;li&gt;Throughput&lt;/li&gt;
&lt;li&gt;Resource utilization&lt;/li&gt;
&lt;li&gt;Queue depth&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Avoid collecting thousands of metrics that nobody uses.&lt;/p&gt;




&lt;h3&gt;
  
  
  Distributed Tracing
&lt;/h3&gt;

&lt;p&gt;Propagate trace context across:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Services&lt;/li&gt;
&lt;li&gt;Queues&lt;/li&gt;
&lt;li&gt;Background jobs&lt;/li&gt;
&lt;li&gt;External calls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This makes end-to-end debugging possible.&lt;/p&gt;




&lt;h3&gt;
  
  
  Centralized Visibility
&lt;/h3&gt;

&lt;p&gt;Instead of logging into multiple servers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Server 1 Logs
Server 2 Logs
Server 3 Logs
Server 4 Logs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Centralize your signals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Logs ────┐
Metrics ─┼──→ Observability Platform
Traces ──┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now teams have one place to investigate incidents.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Observability Mistakes
&lt;/h2&gt;

&lt;h2&gt;
  
  
  1. Logging Too Much
&lt;/h2&gt;

&lt;p&gt;More logs don't automatically mean better observability.&lt;/p&gt;

&lt;p&gt;Excessive logs can create:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Higher storage costs&lt;/li&gt;
&lt;li&gt;Noise&lt;/li&gt;
&lt;li&gt;Slower investigations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Log useful information, not everything.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Logging Too Little
&lt;/h2&gt;

&lt;p&gt;This is equally dangerous.&lt;/p&gt;

&lt;p&gt;If an incident occurs and the logs only say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finding the root cause becomes difficult.&lt;/p&gt;

&lt;p&gt;Include meaningful context.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Monitoring Only Infrastructure
&lt;/h2&gt;

&lt;p&gt;Your servers may look healthy while users are experiencing errors.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CPU: 30% ✅
Memory: 45% ✅

Checkout Success Rate: 40% ❌
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Always monitor &lt;strong&gt;user-facing and business-critical metrics&lt;/strong&gt; too.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Too Many Alerts
&lt;/h2&gt;

&lt;p&gt;If everything creates an alert, nothing feels important.&lt;/p&gt;

&lt;p&gt;Prioritize alerts based on real impact.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Missing Trace Context
&lt;/h2&gt;

&lt;p&gt;If every service creates a completely separate trace, following a request becomes impossible.&lt;/p&gt;

&lt;p&gt;Always propagate the trace and correlation context.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Simple Observability Architecture
&lt;/h2&gt;

&lt;p&gt;A typical architecture may look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    Application
                         │
        ┌────────────────┼────────────────┐
        ▼                ▼                ▼
      Logs            Metrics           Traces
        │                │                │
        └────────────────┼────────────────┘
                         ▼
              Observability Platform
                         │
        ┌────────────────┼────────────────┐
        ▼                ▼                ▼
    Dashboards         Alerts        Investigation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives teams visibility into both:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Current system health&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The root cause of problems&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;You cannot reliably operate what you cannot understand.&lt;/p&gt;

&lt;p&gt;As systems become larger and more distributed, observability becomes a core part of system design—not something added only after production issues begin.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Logs&lt;/strong&gt; tell you what happened.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Metrics&lt;/strong&gt; tell you how the system is performing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Traces&lt;/strong&gt; tell you where a request traveled.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Monitoring&lt;/strong&gt; tells you when something needs attention.&lt;/p&gt;

&lt;p&gt;Together, they help transform production incidents from confusing mysteries into problems that can be detected, investigated, and resolved faster.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Monitoring tells you that something is wrong. Observability helps you understand why.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Observability&lt;/strong&gt; helps you understand what is happening inside a system.&lt;/li&gt;
&lt;li&gt;The three core signals are &lt;strong&gt;logs, metrics, and traces&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logs&lt;/strong&gt; provide detailed event information.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Metrics&lt;/strong&gt; reveal trends and overall system health.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Traces&lt;/strong&gt; follow requests across distributed services.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitoring&lt;/strong&gt; detects known problems and triggers alerts.&lt;/li&gt;
&lt;li&gt;Good alerts should focus on meaningful user impact.&lt;/li&gt;
&lt;li&gt;Use structured logs and correlation IDs for easier debugging.&lt;/li&gt;
&lt;li&gt;Track latency, traffic, errors, and saturation.&lt;/li&gt;
&lt;li&gt;Build observability into your system from the beginning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;If reliability keeps your system running, observability helps you understand what happens when it doesn't. *&lt;/em&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>microservices</category>
      <category>monitoring</category>
    </item>
    <item>
      <title>Reliability &amp; Fault Tolerance: Designing Systems That Survive Failures</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Sun, 23 Aug 2026 11:00:30 +0000</pubDate>
      <link>https://dev.to/tanu_priya/reliability-fault-tolerance-designing-systems-that-survive-failures-4jam</link>
      <guid>https://dev.to/tanu_priya/reliability-fault-tolerance-designing-systems-that-survive-failures-4jam</guid>
      <description>&lt;p&gt;Modern applications are expected to be available almost all the time.&lt;/p&gt;

&lt;p&gt;Users don't care if a server crashes, a database becomes unavailable, or a network connection fails—they simply expect the application to work. But in distributed systems, failures are not rare exceptions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failures are inevitable.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Servers crash. Networks fail. Databases become unavailable. Deployments go wrong. External APIs time out.&lt;/p&gt;

&lt;p&gt;The goal of good system design is not to build a system where failures never happen.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The goal is to build a system that continues working even when failures happen.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is where &lt;strong&gt;Reliability&lt;/strong&gt; and &lt;strong&gt;Fault Tolerance&lt;/strong&gt; become essential.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Reliability?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Reliability&lt;/strong&gt; is the ability of a system to perform its intended function correctly and consistently over time.&lt;/p&gt;

&lt;p&gt;A reliable system should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Return correct results&lt;/li&gt;
&lt;li&gt;Handle expected traffic&lt;/li&gt;
&lt;li&gt;Recover from temporary failures&lt;/li&gt;
&lt;li&gt;Minimize downtime&lt;/li&gt;
&lt;li&gt;Avoid data loss&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if an application promises 99.9% availability:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Total Time: 30 Days

Allowed Downtime ≈ 43 Minutes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Higher availability means less acceptable downtime.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Availability&lt;/th&gt;
&lt;th&gt;Approx. Downtime per Month&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;99%&lt;/td&gt;
&lt;td&gt;7 hours 18 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;99.9%&lt;/td&gt;
&lt;td&gt;43 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;99.99%&lt;/td&gt;
&lt;td&gt;4 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;99.999%&lt;/td&gt;
&lt;td&gt;26 seconds&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;But achieving higher reliability also increases infrastructure complexity and cost.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Fault Tolerance?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Fault tolerance&lt;/strong&gt; is the ability of a system to continue operating even when one or more components fail.&lt;/p&gt;

&lt;p&gt;Imagine a simple architecture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users
   │
   ▼
Application Server
   │
   ▼
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens if the application server crashes?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application Server 

→ Entire application becomes unavailable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This architecture has a &lt;strong&gt;single point of failure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A fault-tolerant system introduces redundancy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    Load Balancer
                         │
              ┌──────────┴──────────┐
              ▼                     ▼
         Server 1               Server 2
             │                     │
             └──────────┬──────────┘
                        ▼
                     Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If Server 1 fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Server 1 

Traffic → Server 2 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The system continues serving users.&lt;/p&gt;

&lt;p&gt;That is fault tolerance.&lt;/p&gt;




&lt;h2&gt;
  
  
  Types of Failures in Distributed Systems
&lt;/h2&gt;

&lt;p&gt;Before designing for reliability, we need to understand what can fail.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Server Failures
&lt;/h2&gt;

&lt;p&gt;A server can crash because of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hardware issues&lt;/li&gt;
&lt;li&gt;Memory exhaustion&lt;/li&gt;
&lt;li&gt;Application bugs&lt;/li&gt;
&lt;li&gt;CPU overload&lt;/li&gt;
&lt;li&gt;Operating system failures&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Server 1 
Server 2 
Server 3 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The system should detect the failure and redirect traffic.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Network Failures
&lt;/h2&gt;

&lt;p&gt;Networks can experience:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Packet loss&lt;/li&gt;
&lt;li&gt;High latency&lt;/li&gt;
&lt;li&gt;Connection failures&lt;/li&gt;
&lt;li&gt;DNS issues&lt;/li&gt;
&lt;li&gt;Network partitions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A service might appear unavailable even though it is still running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Service A ──── ❌ ──── Service B
        Network Failure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why distributed systems must carefully handle timeouts and retries.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Database Failures
&lt;/h2&gt;

&lt;p&gt;Databases can fail due to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hardware crashes&lt;/li&gt;
&lt;li&gt;Storage issues&lt;/li&gt;
&lt;li&gt;Connection limits&lt;/li&gt;
&lt;li&gt;Overloaded queries&lt;/li&gt;
&lt;li&gt;Software bugs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A single database creates a major risk:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application
     │
     ▼
Database 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if every application server is running, the application may still be unavailable.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Dependency Failures
&lt;/h2&gt;

&lt;p&gt;Modern applications often depend on external services.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application
     │
     ├── Payment Service
     ├── Email Service
     ├── Authentication Service
     └── Analytics Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If one dependency fails, your application should not always fail completely.&lt;/p&gt;

&lt;p&gt;For example, an analytics failure should not prevent a user from completing a payment.&lt;/p&gt;




&lt;h2&gt;
  
  
  Redundancy: Don't Depend on One Component
&lt;/h2&gt;

&lt;p&gt;One of the most important principles of fault tolerance is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Avoid single points of failure.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Redundancy means having multiple components capable of performing the same function.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                  Load Balancer
                       │
          ┌────────────┼────────────┐
          ▼            ▼            ▼
       Server 1      Server 2      Server 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If one server fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Server 1 
Server 2 
Server 3 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The remaining servers continue handling requests.&lt;/p&gt;

&lt;p&gt;Redundancy can exist at multiple levels:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application servers&lt;/li&gt;
&lt;li&gt;Databases&lt;/li&gt;
&lt;li&gt;Load balancers&lt;/li&gt;
&lt;li&gt;Data centers&lt;/li&gt;
&lt;li&gt;Network connections&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Database Replication
&lt;/h2&gt;

&lt;p&gt;Databases are critical, so they often use replication.&lt;/p&gt;

&lt;p&gt;A common architecture looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Application
                      │
              ┌───────┴───────┐
              ▼               ▼
           Primary         Replica
          Database        Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The primary database handles writes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Write Request
     │
     ▼
Primary Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The data is then replicated to other databases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Primary
   │
   ├────→ Replica 1
   │
   └────→ Replica 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the primary database fails, one replica may become the new primary.&lt;/p&gt;

&lt;p&gt;This process is called &lt;strong&gt;failover&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Failover: Switching When Something Fails
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Failover&lt;/strong&gt; is the process of automatically switching traffic or operations from a failed component to a healthy backup.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        Before Failure

Application
     │
     ▼
Primary Server ✅
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After failure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Primary Server ❌
        │
        ▼
Backup Server Becomes Active ✅
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same concept can be used for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application servers&lt;/li&gt;
&lt;li&gt;Databases&lt;/li&gt;
&lt;li&gt;Network infrastructure&lt;/li&gt;
&lt;li&gt;Data centers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A good failover mechanism should be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fast&lt;/li&gt;
&lt;li&gt;Automatic&lt;/li&gt;
&lt;li&gt;Reliable&lt;/li&gt;
&lt;li&gt;Regularly tested&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because a backup system that has never been tested may fail exactly when you need it most.&lt;/p&gt;




&lt;h2&gt;
  
  
  Retries: Handling Temporary Failures
&lt;/h2&gt;

&lt;p&gt;Not every failure is permanent.&lt;/p&gt;

&lt;p&gt;Sometimes a request fails because of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Temporary network issues&lt;/li&gt;
&lt;li&gt;Short service overload&lt;/li&gt;
&lt;li&gt;Brief database unavailability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In these cases, retrying the request can help.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request ❌
   │
Retry 1 ❌
   │
Retry 2 ❌
   │
Retry 3 ✅
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, retries must be implemented carefully.&lt;/p&gt;

&lt;p&gt;A naive retry strategy can make an outage worse.&lt;/p&gt;

&lt;p&gt;Imagine 1 million failed requests immediately retrying:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1M Requests
    │
    ▼
Service Overloaded ❌
    │
    ▼
1M Immediate Retries
    │
    ▼
Even More Overload 💥
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is known as a &lt;strong&gt;retry storm&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Exponential Backoff
&lt;/h2&gt;

&lt;p&gt;Instead of retrying immediately, systems often use &lt;strong&gt;exponential backoff&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Retry 1 → Wait 1 second
Retry 2 → Wait 2 seconds
Retry 3 → Wait 4 seconds
Retry 4 → Wait 8 seconds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives the failing service time to recover.&lt;/p&gt;

&lt;p&gt;A more realistic approach also adds &lt;strong&gt;jitter&lt;/strong&gt;, which introduces a small random delay.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Retry Delay = Exponential Backoff + Random Jitter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prevents millions of clients from retrying at exactly the same time.&lt;/p&gt;




&lt;h2&gt;
  
  
  Timeouts: Don't Wait Forever
&lt;/h2&gt;

&lt;p&gt;Every external call should have a reasonable timeout.&lt;/p&gt;

&lt;p&gt;Bad design:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application → Waiting forever...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Better design:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application
     │
     ▼
Call External Service
     │
     ├── Response within 3 seconds → Continue
     │
     └── No response → Timeout ❌
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without timeouts, requests can remain stuck and consume valuable resources.&lt;/p&gt;

&lt;p&gt;Timeouts help the system fail fast and recover gracefully.&lt;/p&gt;




&lt;h2&gt;
  
  
  Circuit Breakers
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;circuit breaker&lt;/strong&gt; prevents an application from repeatedly calling a service that is already failing.&lt;/p&gt;

&lt;p&gt;Imagine this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application → Payment Service ❌
Application → Payment Service ❌
Application → Payment Service ❌
Application → Payment Service ❌
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, after repeated failures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Circuit Breaker → OPEN 🔴
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application temporarily stops sending requests to the failing service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application
     │
     ▼
Circuit Breaker OPEN
     │
     └── Return fallback response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After some time, the system can test whether the dependency has recovered.&lt;/p&gt;

&lt;p&gt;Typical circuit breaker states:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CLOSED → OPEN → HALF-OPEN → CLOSED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prevents a failing dependency from causing a cascading failure across the entire system.&lt;/p&gt;




&lt;h2&gt;
  
  
  Graceful Degradation
&lt;/h2&gt;

&lt;p&gt;A reliable system doesn't always need to provide every feature during a failure.&lt;/p&gt;

&lt;p&gt;Instead, it can continue providing the most important functionality.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Normal Mode
├── Recommendations
├── Search
├── Payments
├── Notifications
└── Analytics
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the recommendation service fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Degraded Mode
├── Recommendations ❌ Temporarily Disabled
├── Search ✅
├── Payments ✅
├── Notifications ✅
└── Analytics ✅
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The system is not perfect, but users can still perform critical actions.&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;graceful degradation&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Health Checks
&lt;/h2&gt;

&lt;p&gt;How does a load balancer know whether a server is healthy?&lt;/p&gt;

&lt;p&gt;Using &lt;strong&gt;health checks&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET /health
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A healthy server might return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"healthy"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The load balancer periodically checks each server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Load Balancer
     │
     ├── Server 1 → Healthy ✅
     ├── Server 2 → Healthy ✅
     └── Server 3 → Unhealthy ❌
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Traffic is automatically removed from unhealthy servers.&lt;/p&gt;

&lt;p&gt;Health checks are especially important in distributed environments with multiple instances.&lt;/p&gt;




&lt;h2&gt;
  
  
  Monitoring: You Can't Fix What You Can't See
&lt;/h2&gt;

&lt;p&gt;Reliability also depends on detecting problems quickly.&lt;/p&gt;

&lt;p&gt;Important metrics include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Error rate&lt;/li&gt;
&lt;li&gt;Response time&lt;/li&gt;
&lt;li&gt;CPU usage&lt;/li&gt;
&lt;li&gt;Memory usage&lt;/li&gt;
&lt;li&gt;Database latency&lt;/li&gt;
&lt;li&gt;Request throughput&lt;/li&gt;
&lt;li&gt;Failed requests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A monitoring system might detect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Normal Error Rate: 0.2%

Current Error Rate: 15% 🚨
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An alert can then notify engineers before the issue becomes a major outage.&lt;/p&gt;

&lt;p&gt;Monitoring turns failures from:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Users are complaining that the app is down."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;into:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"We detected increasing errors and are investigating the problem."&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Avoiding Cascading Failures
&lt;/h2&gt;

&lt;p&gt;A cascading failure happens when one failure causes failures in other parts of the system.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Database Slows Down
       │
       ▼
Application Requests Wait
       │
       ▼
Threads Become Exhausted
       │
       ▼
Application Becomes Unresponsive
       │
       ▼
Entire System Fails 💥
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To prevent this, systems use techniques such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Timeouts&lt;/li&gt;
&lt;li&gt;Circuit breakers&lt;/li&gt;
&lt;li&gt;Rate limiting&lt;/li&gt;
&lt;li&gt;Queues&lt;/li&gt;
&lt;li&gt;Bulkheads&lt;/li&gt;
&lt;li&gt;Load shedding&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is to &lt;strong&gt;isolate failures instead of allowing them to spread&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Bulkhead Pattern
&lt;/h2&gt;

&lt;p&gt;A ship uses bulkheads to isolate flooded sections so the entire ship doesn't sink.&lt;/p&gt;

&lt;p&gt;The same idea can be used in software.&lt;/p&gt;

&lt;p&gt;Instead of sharing all resources:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;One Shared Resource Pool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You isolate resources:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Payment Resources
Recommendation Resources
Search Resources
Notification Resources
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the recommendation service consumes all its resources, it doesn't necessarily affect payments or search.&lt;/p&gt;

&lt;p&gt;This improves fault isolation.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Reliable System Architecture
&lt;/h2&gt;

&lt;p&gt;A highly reliable architecture might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                     Users
                       │
                       ▼
                  Load Balancer
                       │
          ┌────────────┼────────────┐
          ▼            ▼            ▼
       App 1         App 2         App 3
          │            │            │
          └────────────┼────────────┘
                       ▼
                Primary Database
                       │
              ┌────────┴────────┐
              ▼                 ▼
          Replica 1         Replica 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additional systems may include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cache
Queue
Monitoring
Alerting
Backup Storage
Disaster Recovery
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each component contributes to the overall reliability of the system.&lt;/p&gt;




&lt;h1&gt;
  
  
  Reliability vs Fault Tolerance
&lt;/h1&gt;

&lt;p&gt;These concepts are related but not exactly the same.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Reliability&lt;/th&gt;
&lt;th&gt;Fault Tolerance&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Focuses on consistent operation&lt;/td&gt;
&lt;td&gt;Focuses on surviving failures&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reduces the chance of failure&lt;/td&gt;
&lt;td&gt;Reduces the impact of failure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Includes monitoring and testing&lt;/td&gt;
&lt;td&gt;Includes redundancy and failover&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Measures how dependable a system is&lt;/td&gt;
&lt;td&gt;Measures how well a system handles faults&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A highly reliable system should ideally also be fault tolerant.&lt;/p&gt;




&lt;h1&gt;
  
  
  When Designing for Reliability, Ask These Questions
&lt;/h1&gt;

&lt;p&gt;Whenever you design a system, ask:&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens if this server crashes?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  What happens if the database becomes unavailable?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  What happens if a request times out?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  What happens if an external API fails?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Can the system retry safely?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Is there a backup?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  How does the system detect failures?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Can traffic automatically move to a healthy server?
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Can one failing service bring down the entire system?
&lt;/h3&gt;

&lt;p&gt;These questions often reveal weaknesses in a system before they become real outages.&lt;/p&gt;




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

&lt;p&gt;Failures are a normal part of distributed systems.&lt;/p&gt;

&lt;p&gt;You cannot completely prevent servers, networks, databases, or external services from failing. But you can design systems that &lt;strong&gt;expect failure and recover from it gracefully&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The core building blocks of reliable systems include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Retries&lt;/strong&gt; for temporary failures&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Timeouts&lt;/strong&gt; to prevent requests from hanging&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Redundancy&lt;/strong&gt; to remove single points of failure&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Failover&lt;/strong&gt; to switch to healthy components&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Circuit breakers&lt;/strong&gt; to prevent cascading failures&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Monitoring&lt;/strong&gt; to detect problems early&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Health checks&lt;/strong&gt; to identify unhealthy services&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Graceful degradation&lt;/strong&gt; to keep critical features running&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The biggest mindset shift in system design is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Don't ask, "Will this component fail?" Ask, "When it fails, what happens next?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is the foundation of &lt;strong&gt;Reliability and Fault Tolerance&lt;/strong&gt;.&lt;/p&gt;




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

&lt;ul&gt;
&lt;li&gt;Failures are inevitable in distributed systems.&lt;/li&gt;
&lt;li&gt;Reliability focuses on consistent, dependable operation.&lt;/li&gt;
&lt;li&gt;Fault tolerance helps systems continue working during failures.&lt;/li&gt;
&lt;li&gt;Redundancy removes single points of failure.&lt;/li&gt;
&lt;li&gt;Retries should use exponential backoff and jitter.&lt;/li&gt;
&lt;li&gt;Timeouts prevent requests from waiting indefinitely.&lt;/li&gt;
&lt;li&gt;Failover redirects traffic to healthy components.&lt;/li&gt;
&lt;li&gt;Circuit breakers prevent failing dependencies from causing larger outages.&lt;/li&gt;
&lt;li&gt;Monitoring and health checks help detect failures quickly.&lt;/li&gt;
&lt;li&gt;Design systems assuming that &lt;strong&gt;something will eventually fail&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;A scalable system handles growth. A reliable system survives failure. The best systems do both. *&lt;/em&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>softwareengineering</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Database Partitioning &amp; Sharding: How to Distribute Massive Datasets</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Sat, 22 Aug 2026 07:18:47 +0000</pubDate>
      <link>https://dev.to/tanu_priya/database-partitioning-sharding-how-to-distribute-massive-datasets-4313</link>
      <guid>https://dev.to/tanu_priya/database-partitioning-sharding-how-to-distribute-massive-datasets-4313</guid>
      <description>&lt;p&gt;As an application grows, its database grows with it.&lt;/p&gt;

&lt;p&gt;A small application might start with a few thousand users and a single database server. But what happens when that application reaches millions of users, processes thousands of requests per second, and stores terabytes or even petabytes of data?&lt;/p&gt;

&lt;p&gt;At that point, simply buying a more powerful database server is not always enough.&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;database partitioning&lt;/strong&gt; and &lt;strong&gt;sharding&lt;/strong&gt; come in.&lt;/p&gt;

&lt;p&gt;In this article, we'll understand what they are, why they matter, how they work, and when you should use them.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem: One Database Can Only Scale So Far
&lt;/h2&gt;

&lt;p&gt;Imagine a social media application with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;100 million users&lt;/li&gt;
&lt;li&gt;Billions of posts&lt;/li&gt;
&lt;li&gt;Millions of requests per second&lt;/li&gt;
&lt;li&gt;Terabytes of new data every day&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Initially, you might have a single database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              Application
                   │
                   ▼
            ┌─────────────┐
            │  Database   │
            │   Server    │
            └─────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As traffic grows, this server can become a bottleneck.&lt;/p&gt;

&lt;p&gt;You may face:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slow queries&lt;/li&gt;
&lt;li&gt;High CPU usage&lt;/li&gt;
&lt;li&gt;Memory limitations&lt;/li&gt;
&lt;li&gt;Storage limitations&lt;/li&gt;
&lt;li&gt;Increased latency&lt;/li&gt;
&lt;li&gt;Database outages affecting the entire application&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The obvious solution might seem simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Buy a bigger server.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This approach is called &lt;strong&gt;vertical scaling&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Small Server
     ↓
Bigger Server
     ↓
Even Bigger Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But vertical scaling has limits.&lt;/p&gt;

&lt;p&gt;You cannot infinitely keep upgrading a single machine.&lt;/p&gt;

&lt;p&gt;Eventually, you need to distribute the data across multiple machines.&lt;/p&gt;

&lt;p&gt;This is where partitioning and sharding become important.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Database Partitioning?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Database partitioning&lt;/strong&gt; means splitting a large dataset into smaller pieces called &lt;strong&gt;partitions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of storing everything as one huge table, the data is divided based on a specific rule.&lt;/p&gt;

&lt;p&gt;For example, imagine a &lt;code&gt;users&lt;/code&gt; table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;users
--------------------------------
id | name  | country
--------------------------------
1  | John  | USA
2  | Ravi  | India
3  | Emma  | UK
4  | Amit  | India
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could divide this table into smaller partitions.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users Table
     │
     ├── Partition 1
     ├── Partition 2
     ├── Partition 3
     └── Partition 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each partition contains only a portion of the total data.&lt;/p&gt;




&lt;h1&gt;
  
  
  Types of Database Partitioning
&lt;/h1&gt;

&lt;p&gt;There are several common ways to partition data.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Range Partitioning
&lt;/h2&gt;

&lt;p&gt;In range partitioning, data is divided based on a range of values.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User IDs

1 – 1,000,000       → Partition 1
1,000,001 – 2,000,000 → Partition 2
2,000,001 – 3,000,000 → Partition 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another common example is date-based partitioning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Orders

January 2026 → Partition 1
February 2026 → Partition 2
March 2026 → Partition 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works especially well for time-series data.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Logs&lt;/li&gt;
&lt;li&gt;Transactions&lt;/li&gt;
&lt;li&gt;Orders&lt;/li&gt;
&lt;li&gt;Analytics events&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2026-08-01'&lt;/span&gt;
&lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'2026-09-01'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The database may only need to search the August partition instead of scanning the entire orders table.&lt;/p&gt;

&lt;p&gt;This is known as &lt;strong&gt;partition pruning&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Hash Partitioning
&lt;/h2&gt;

&lt;p&gt;In hash partitioning, a hash function decides where the data goes.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hash(user_id) % 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This could distribute users like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User 101 → Partition 1
User 202 → Partition 3
User 303 → Partition 2
User 404 → Partition 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                Users
                  │
                  ▼
            Hash Function
                  │
        ┌─────────┼─────────┐
        ▼         ▼         ▼
   Partition 1 Partition 2 Partition 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main advantage is that data can be distributed more evenly.&lt;/p&gt;

&lt;p&gt;This helps prevent one partition from becoming significantly larger than others.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. List Partitioning
&lt;/h2&gt;

&lt;p&gt;In list partitioning, data is divided based on specific values.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;India       → Partition 1
USA         → Partition 2
UK          → Partition 3
Other       → Partition 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be useful when data naturally belongs to different categories or regions.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Database Sharding?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Sharding is a form of horizontal partitioning where data is distributed across multiple independent database servers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;           One Large Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                  Application
                       │
          ┌────────────┼────────────┐
          ▼            ▼            ▼
      Database      Database      Database
      Shard 1       Shard 2       Shard 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each shard stores only part of the total dataset.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users 1 – 10M      → Shard 1
Users 10M – 20M    → Shard 2
Users 20M – 30M    → Shard 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, instead of one database handling everything, the workload is distributed.&lt;/p&gt;




&lt;h1&gt;
  
  
  Partitioning vs Sharding
&lt;/h1&gt;

&lt;p&gt;The terms are sometimes used interchangeably, but there is an important difference.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Partitioning&lt;/th&gt;
&lt;th&gt;Sharding&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Data split&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple servers&lt;/td&gt;
&lt;td&gt;Not always&lt;/td&gt;
&lt;td&gt;Usually&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database manages split&lt;/td&gt;
&lt;td&gt;Often&lt;/td&gt;
&lt;td&gt;Often handled by application/infrastructure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Main goal&lt;/td&gt;
&lt;td&gt;Improve performance and manageability&lt;/td&gt;
&lt;td&gt;Scale horizontally&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Complexity&lt;/td&gt;
&lt;td&gt;Lower&lt;/td&gt;
&lt;td&gt;Higher&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A simple way to remember it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Partitioning splits data. Sharding distributes those splits across multiple database servers.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  How Does Sharding Work?
&lt;/h1&gt;

&lt;p&gt;A sharded system needs a &lt;strong&gt;shard key&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A shard key determines where a piece of data should be stored.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shard = user_id % 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User ID 101 → Shard 1
User ID 102 → Shard 2
User ID 103 → Shard 3
User ID 104 → Shard 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                   Application
                        │
                        ▼
                 Shard Router
                        │
        ┌───────────────┼───────────────┐
        ▼               ▼               ▼
     Shard 1          Shard 2          Shard 3
   Users A-F        Users G-M        Users N-Z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a request arrives, the system determines which shard contains the required data.&lt;/p&gt;




&lt;h1&gt;
  
  
  Choosing a Good Shard Key
&lt;/h1&gt;

&lt;p&gt;Choosing the right shard key is one of the most important decisions in a sharded architecture.&lt;/p&gt;

&lt;p&gt;A good shard key should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Distribute data evenly&lt;/li&gt;
&lt;li&gt;Distribute traffic evenly&lt;/li&gt;
&lt;li&gt;Be frequently available in queries&lt;/li&gt;
&lt;li&gt;Avoid creating hotspots&lt;/li&gt;
&lt;li&gt;Support future growth&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, using &lt;code&gt;user_id&lt;/code&gt; is often a good choice because many operations are user-specific.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Get user profile
Get user posts
Get user settings
Get user notifications
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All of these requests can potentially be routed using the user's ID.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Hot Shard Problem
&lt;/h1&gt;

&lt;p&gt;Imagine a social media application where users are sharded by celebrity accounts.&lt;/p&gt;

&lt;p&gt;A few celebrity accounts may generate millions of requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Shard 1 → 100K requests/sec
Shard 2 → 5K requests/sec
Shard 3 → 6K requests/sec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Shard 1 becomes overloaded while the other servers remain mostly idle.&lt;/p&gt;

&lt;p&gt;This is called a &lt;strong&gt;hot shard&lt;/strong&gt; or &lt;strong&gt;hotspot&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A poor shard key can cause:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uneven storage&lt;/li&gt;
&lt;li&gt;Uneven traffic&lt;/li&gt;
&lt;li&gt;Slow requests&lt;/li&gt;
&lt;li&gt;Server overload&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why shard key selection is critical.&lt;/p&gt;




&lt;h1&gt;
  
  
  Example: Sharding a Social Media Application
&lt;/h1&gt;

&lt;p&gt;Imagine we have 100 million users.&lt;/p&gt;

&lt;p&gt;Instead of storing everyone in one database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                  Users DB
               100 Million Users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We divide them into four shards.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 User Service
                      │
          ┌───────────┼───────────┐
          ▼           ▼           ▼
       Shard 1     Shard 2     Shard 3
       25M Users    25M Users   25M Users
                      │
                   Shard 4
                   25M Users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A routing layer decides where each user belongs.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_id = 12345

12345 % 4 = 1

→ Send request to Shard 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application can now handle more data by adding more shards.&lt;/p&gt;




&lt;h1&gt;
  
  
  Challenges of Database Sharding
&lt;/h1&gt;

&lt;p&gt;Sharding solves scaling problems, but it also introduces complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Cross-Shard Queries
&lt;/h2&gt;

&lt;p&gt;Imagine you want to run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If users are distributed across multiple shards, the system may need to query every shard.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Shard 1 ─┐
Shard 2 ─┼──→ Combine Results
Shard 3 ─┤
Shard 4 ─┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be expensive and slow.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Joins Become Difficult
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users → Shard 1
Orders → Shard 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A query like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;becomes much harder when the data exists on different servers.&lt;/p&gt;

&lt;p&gt;Distributed joins are usually more expensive than joins within a single database.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Rebalancing Data
&lt;/h2&gt;

&lt;p&gt;Suppose you initially have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 Shards
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later, your application grows and you need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8 Shards
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, some data may need to move.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Shard 1 → Split
Shard 2 → Split
Shard 3 → Split
Shard 4 → Split
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Moving large amounts of data can be complex and risky.&lt;/p&gt;

&lt;p&gt;This process is called &lt;strong&gt;resharding&lt;/strong&gt; or &lt;strong&gt;rebalancing&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Distributed Transactions
&lt;/h2&gt;

&lt;p&gt;Transactions are simple when all data exists in one database.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Transfer Money

Account A
   ↓
Account B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if Account A and Account B are stored on different shards, maintaining consistency becomes more complicated.&lt;/p&gt;

&lt;p&gt;You may need distributed transaction strategies such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two-phase commit&lt;/li&gt;
&lt;li&gt;Saga pattern&lt;/li&gt;
&lt;li&gt;Event-driven workflows&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  When Should You Use Partitioning?
&lt;/h1&gt;

&lt;p&gt;Partitioning is useful when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A table has grown very large&lt;/li&gt;
&lt;li&gt;Queries frequently target specific ranges&lt;/li&gt;
&lt;li&gt;You have time-based data&lt;/li&gt;
&lt;li&gt;You want faster query performance&lt;/li&gt;
&lt;li&gt;You need easier data cleanup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, log data can be partitioned by month:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Logs_2026_January
Logs_2026_February
Logs_2026_March
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When old data is no longer needed, you can remove an entire partition instead of deleting millions of individual rows.&lt;/p&gt;




&lt;h1&gt;
  
  
  When Should You Use Sharding?
&lt;/h1&gt;

&lt;p&gt;Sharding is useful when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A single database server cannot handle the load&lt;/li&gt;
&lt;li&gt;Storage requirements exceed one machine&lt;/li&gt;
&lt;li&gt;Write traffic is extremely high&lt;/li&gt;
&lt;li&gt;You need horizontal scalability&lt;/li&gt;
&lt;li&gt;Your application has millions of active users&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, sharding should &lt;strong&gt;not&lt;/strong&gt; be the first solution.&lt;/p&gt;

&lt;p&gt;Before sharding, consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better indexing&lt;/li&gt;
&lt;li&gt;Query optimization&lt;/li&gt;
&lt;li&gt;Caching&lt;/li&gt;
&lt;li&gt;Read replicas&lt;/li&gt;
&lt;li&gt;Database partitioning&lt;/li&gt;
&lt;li&gt;Vertical scaling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sharding adds significant operational complexity.&lt;/p&gt;




&lt;h1&gt;
  
  
  A Realistic Scaling Journey
&lt;/h1&gt;

&lt;p&gt;Most applications don't start with sharding.&lt;/p&gt;

&lt;p&gt;A typical evolution looks like this:&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 1: Single Database
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application
     │
     ▼
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Stage 2: Add Indexes and Optimize Queries
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application
     │
     ▼
Optimized Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Stage 3: Add Caching
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application
     │
     ├── Cache
     │
     ▼
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Stage 4: Add Read Replicas
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                Application
                     │
          ┌──────────┼──────────┐
          ▼          ▼          ▼
       Primary    Replica     Replica
       Database   Database    Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Stage 5: Partition Large Tables
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Database
   │
   ├── Partition 1
   ├── Partition 2
   └── Partition 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Stage 6: Shard the Database
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Application
                      │
               Shard Router
                      │
        ┌─────────────┼─────────────┐
        ▼             ▼             ▼
      Shard 1       Shard 2       Shard 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key lesson is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Don't introduce sharding before you actually need it.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




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

&lt;p&gt;Database partitioning and sharding are powerful techniques for scaling systems that handle massive amounts of data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Partitioning&lt;/strong&gt; helps organize and optimize large datasets by splitting them into smaller logical pieces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sharding&lt;/strong&gt; takes this further by distributing data across multiple database servers, allowing an application to scale horizontally.&lt;/p&gt;

&lt;p&gt;But with great scalability comes greater complexity.&lt;/p&gt;

&lt;p&gt;You need to carefully think about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Partition strategy&lt;/li&gt;
&lt;li&gt;Shard key selection&lt;/li&gt;
&lt;li&gt;Data distribution&lt;/li&gt;
&lt;li&gt;Hot shards&lt;/li&gt;
&lt;li&gt;Cross-shard queries&lt;/li&gt;
&lt;li&gt;Rebalancing&lt;/li&gt;
&lt;li&gt;Distributed transactions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The best approach is to start simple.&lt;/p&gt;

&lt;p&gt;Optimize your queries, use indexes, add caching and replicas, partition when necessary—and move to sharding only when a single database can no longer handle your application's scale.&lt;/p&gt;

&lt;p&gt;Because in system design, the goal isn't to use the most complex architecture.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The goal is to use the simplest architecture that can handle your scale.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Partitioning&lt;/strong&gt; splits large datasets into smaller pieces.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sharding&lt;/strong&gt; distributes data across multiple database servers.&lt;/li&gt;
&lt;li&gt;Common partitioning strategies include &lt;strong&gt;range, hash, and list partitioning&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Choosing the right &lt;strong&gt;shard key&lt;/strong&gt; is critical.&lt;/li&gt;
&lt;li&gt;Poor distribution can create &lt;strong&gt;hot shards&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Sharding improves scalability but increases system complexity.&lt;/li&gt;
&lt;li&gt;Optimize your database before deciding to shard.&lt;/li&gt;
&lt;li&gt;Start simple and scale your architecture when the actual requirements demand it.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>database</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Rate Limiting: Protecting APIs From Abuse and Traffic Spikes</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Fri, 21 Aug 2026 08:14:32 +0000</pubDate>
      <link>https://dev.to/tanu_priya/rate-limiting-protecting-apis-from-abuse-and-traffic-spikes-10pi</link>
      <guid>https://dev.to/tanu_priya/rate-limiting-protecting-apis-from-abuse-and-traffic-spikes-10pi</guid>
      <description>&lt;p&gt;Imagine your API suddenly receives &lt;strong&gt;1 million requests in a few seconds&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Maybe it's a viral product launch.&lt;/p&gt;

&lt;p&gt;Maybe a client accidentally created an infinite loop.&lt;/p&gt;

&lt;p&gt;Or maybe someone is deliberately trying to overwhelm your system.&lt;/p&gt;

&lt;p&gt;Without protection, your servers could become overloaded, response times could increase, and legitimate users could start receiving errors.&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;rate limiting&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;Rate limiting controls &lt;strong&gt;how many requests a client can make within a specific period of time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we'll understand how rate limiting works, the most common algorithms, where to implement it, how distributed systems handle it, and how to design rate limiting for large-scale APIs.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Rate Limiting?
&lt;/h2&gt;

&lt;p&gt;Rate limiting is a mechanism that restricts the number of requests a client can make during a given time window.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100 requests / minute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A client can make up to 100 requests during that period.&lt;/p&gt;

&lt;p&gt;After reaching the limit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request 101
   ↓
Rate Limiter
   ↓
❌ Too Many Requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The API typically responds with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt; &lt;span class="m"&gt;429&lt;/span&gt; &lt;span class="ne"&gt;Too Many Requests&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rate limiting protects your infrastructure while ensuring that one client cannot consume an unreasonable amount of resources.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Do We Need Rate Limiting?
&lt;/h2&gt;

&lt;p&gt;Without rate limiting, an API might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client 1 ───────┐
Client 2 ───────┤
Client 3 ───────┤
Client 4 ───────┼──→ API Servers
Client 5 ───────┤
Client 6 ───────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine one client starts sending thousands of requests per second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Attacker
   │
   ├── Request
   ├── Request
   ├── Request
   ├── Request
   ├── Request
   └── ...
          ↓
      API Servers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can lead to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increased CPU usage&lt;/li&gt;
&lt;li&gt;Higher memory consumption&lt;/li&gt;
&lt;li&gt;Database overload&lt;/li&gt;
&lt;li&gt;Increased network traffic&lt;/li&gt;
&lt;li&gt;Higher infrastructure costs&lt;/li&gt;
&lt;li&gt;Slow responses&lt;/li&gt;
&lt;li&gt;Service outages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rate limiting adds a protective layer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Clients
   ↓
Rate Limiter
   ↓
Allowed Requests
   ↓
API Servers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Rate Limiting vs Throttling
&lt;/h2&gt;

&lt;p&gt;These terms are often used together, but they can represent different behaviors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rate Limiting
&lt;/h3&gt;

&lt;p&gt;Restricts how many requests can be made.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100 requests / minute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Requests beyond the limit may be rejected.&lt;/p&gt;

&lt;h3&gt;
  
  
  Throttling
&lt;/h3&gt;

&lt;p&gt;Controls the rate at which requests are processed.&lt;/p&gt;

&lt;p&gt;For example, instead of rejecting everything immediately, the system may slow down processing or queue requests.&lt;/p&gt;

&lt;p&gt;A simple distinction:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Rate limiting controls how much traffic is allowed.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Throttling controls how quickly traffic is processed.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  A Simple Rate Limiting Example
&lt;/h2&gt;

&lt;p&gt;Suppose an API has this rule:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100 requests / minute / user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A user sends:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request 1
Request 2
Request 3
...
Request 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All requests are allowed.&lt;/p&gt;

&lt;p&gt;The next request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request 101
     ↓
❌ 429 Too Many Requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the limit resets, requests can be accepted again.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Should We Rate Limit?
&lt;/h2&gt;

&lt;p&gt;Not every endpoint needs the same limit.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET /products
→ 1000 requests/minute

POST /login
→ 10 requests/minute

POST /payments
→ 30 requests/minute

POST /password-reset
→ 5 requests/hour
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expensive or security-sensitive endpoints generally need stricter limits.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rate Limiting Strategies
&lt;/h2&gt;

&lt;p&gt;There are several common algorithms used to implement rate limiting.&lt;/p&gt;

&lt;p&gt;The most important ones are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fixed Window&lt;/li&gt;
&lt;li&gt;Sliding Window&lt;/li&gt;
&lt;li&gt;Token Bucket&lt;/li&gt;
&lt;li&gt;Leaky Bucket&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's understand each.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Fixed Window
&lt;/h2&gt;

&lt;p&gt;The fixed-window algorithm divides time into fixed intervals.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Limit: 100 requests / minute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The system creates windows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12:00:00 ───────── 12:01:00
12:01:00 ───────── 12:02:00
12:02:00 ───────── 12:03:00
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within each window:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Counter = 0

Request → Counter = 1
Request → Counter = 2
Request → Counter = 3
...
Request → Counter = 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the counter reaches the limit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request 101
    ↓
Rejected
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the next window:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Counter → 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Simple to implement&lt;/li&gt;
&lt;li&gt;Easy to understand&lt;/li&gt;
&lt;li&gt;Low memory usage&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Fixed windows can create &lt;strong&gt;boundary spikes&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12:00:59 → 100 requests
12:01:00 → 100 requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A client could potentially send 200 requests in a very short period while staying within two different windows.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Sliding Window
&lt;/h2&gt;

&lt;p&gt;A sliding-window algorithm evaluates requests over a continuously moving time period.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Last 60 seconds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of resetting everything at exactly 12:01:00, the system continuously checks the previous 60 seconds.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;         Current Time
              ↓
───────●────●────●────●────●──────
       ←── Last 60 seconds ──→
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The system counts requests within that moving window.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;More accurate&lt;/li&gt;
&lt;li&gt;Reduces boundary spikes&lt;/li&gt;
&lt;li&gt;Better traffic control&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Trade-off
&lt;/h3&gt;

&lt;p&gt;It can require more memory or more sophisticated data structures depending on the implementation.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Token Bucket
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;token bucket&lt;/strong&gt; algorithm is one of the most widely used approaches.&lt;/p&gt;

&lt;p&gt;Imagine a bucket that holds tokens.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        Token Generator
              ↓
       ┌─────────────┐
       │ ● ● ● ● ●   │
       │   Token     │
       │   Bucket    │
       └─────────────┘
              ↓
          API Request
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each request consumes a token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   ↓
Take 1 Token
   ↓
Token Available?
  / \
Yes  No
 ↓    ↓
Allow Reject
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tokens are added to the bucket at a fixed rate.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bucket capacity: 100 tokens
Refill rate: 10 tokens/second
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A client can temporarily make a burst of requests if tokens have accumulated.&lt;/p&gt;

&lt;p&gt;But once the bucket is empty, additional requests are rejected or delayed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Token Bucket Is Useful
&lt;/h3&gt;

&lt;p&gt;It supports both:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sustained traffic&lt;/li&gt;
&lt;li&gt;Controlled bursts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This makes it a popular choice for APIs.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Leaky Bucket
&lt;/h2&gt;

&lt;p&gt;The leaky-bucket algorithm behaves more like a queue.&lt;/p&gt;

&lt;p&gt;Imagine requests entering a bucket:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Requests
   ↓
┌─────────────┐
│ Request     │
│ Request     │
│ Request     │
│ Request     │
└──────┬──────┘
       ↓
   Fixed Rate
       ↓
      API
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Requests are processed at a relatively consistent rate.&lt;/p&gt;

&lt;p&gt;If the queue becomes full, new requests may be rejected.&lt;/p&gt;

&lt;h3&gt;
  
  
  Token Bucket vs Leaky Bucket
&lt;/h3&gt;

&lt;p&gt;A useful distinction is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Token Bucket&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Allows controlled bursts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Leaky Bucket&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Produces a smoother processing rate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The right choice depends on your traffic pattern.&lt;/p&gt;




&lt;h2&gt;
  
  
  Comparing Rate Limiting Algorithms
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Algorithm&lt;/th&gt;
&lt;th&gt;Main Idea&lt;/th&gt;
&lt;th&gt;Burst Support&lt;/th&gt;
&lt;th&gt;Complexity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Fixed Window&lt;/td&gt;
&lt;td&gt;Fixed time counters&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sliding Window&lt;/td&gt;
&lt;td&gt;Moving time window&lt;/td&gt;
&lt;td&gt;Limited&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Token Bucket&lt;/td&gt;
&lt;td&gt;Tokens refill over time&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Leaky Bucket&lt;/td&gt;
&lt;td&gt;Process requests at fixed rate&lt;/td&gt;
&lt;td&gt;Limited&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There is no universally best algorithm.&lt;/p&gt;

&lt;p&gt;The choice depends on whether your system needs simplicity, burst handling, accuracy, or smooth traffic.&lt;/p&gt;




&lt;h2&gt;
  
  
  Where Should Rate Limiting Be Implemented?
&lt;/h2&gt;

&lt;p&gt;Rate limiting can be implemented at different layers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Application Layer
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
 ↓
Application
 ↓
Rate Limiter
 ↓
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives the application detailed control over users and endpoints.&lt;/p&gt;

&lt;h3&gt;
  
  
  API Gateway
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
 ↓
API Gateway
 ↓
Rate Limiter
 ↓
Services
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is useful when multiple services need consistent protection.&lt;/p&gt;

&lt;h3&gt;
  
  
  CDN / Edge
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
 ↓
Edge
 ↓
Rate Limiter
 ↓
Origin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rate limiting at the edge can block unwanted traffic before it reaches your infrastructure.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multiple Layers
&lt;/h3&gt;

&lt;p&gt;Large systems may use several layers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
 ↓
CDN
 ↓
API Gateway
 ↓
Service
 ↓
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each layer can have different limits.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Should the Rate Limit Key Be?
&lt;/h2&gt;

&lt;p&gt;A rate limiter needs to determine &lt;strong&gt;who or what is being limited&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Possible keys include:&lt;/p&gt;

&lt;h3&gt;
  
  
  IP Address
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rate limit per IP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Useful for unauthenticated endpoints.&lt;/p&gt;

&lt;p&gt;But many users can share the same public IP.&lt;/p&gt;

&lt;h3&gt;
  
  
  User ID
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rate limit per user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Useful for authenticated APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  API Key
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rate limit per API key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Common for developer-facing APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Endpoint
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rate limit per endpoint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Useful when different APIs have different costs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Combined Key
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_id + endpoint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This provides more granular control.&lt;/p&gt;




&lt;h2&gt;
  
  
  Distributed Rate Limiting
&lt;/h2&gt;

&lt;p&gt;Here's where system design becomes interesting.&lt;/p&gt;

&lt;p&gt;Imagine your API has multiple servers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              Load Balancer
             /      |      \
            ↓       ↓       ↓
        Server 1 Server 2 Server 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If each server maintains its own counter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Server 1 → 40 requests
Server 2 → 40 requests
Server 3 → 40 requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A user could effectively make:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;120 requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;even if the intended limit was 100.&lt;/p&gt;

&lt;p&gt;The counters are not shared.&lt;/p&gt;




&lt;h2&gt;
  
  
  Shared Rate Limiter
&lt;/h2&gt;

&lt;p&gt;A common solution is to use a shared, fast data store.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Load Balancer
                /      |      \
               ▼       ▼       ▼
           Server 1 Server 2 Server 3
                \       |       /
                 \      |      /
                   ▼    ▼
                Redis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now all servers can use the same rate-limit state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   ↓
Any API Server
   ↓
Shared Counter
   ↓
Redis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows the system to enforce a global limit across multiple application instances.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rate Limiting With Redis
&lt;/h2&gt;

&lt;p&gt;A simplified concept might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Key:
rate:user:123

Value:
87

TTL:
60 seconds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every request increments the counter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   ↓
Increment counter
   ↓
Counter &amp;lt;= Limit?
   │
 ┌─┴───┐
Yes   No
 ↓     ↓
Allow Reject
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more sophisticated algorithms, Redis can store timestamps, token counts, or other state.&lt;/p&gt;

&lt;p&gt;Atomic operations are important because multiple requests can arrive simultaneously.&lt;/p&gt;




&lt;h2&gt;
  
  
  Race Conditions
&lt;/h2&gt;

&lt;p&gt;Imagine two requests arrive at exactly the same time.&lt;/p&gt;

&lt;p&gt;Both servers read:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Counter = 99
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both think:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;99 &amp;lt; 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both increment it.&lt;/p&gt;

&lt;p&gt;Now the actual count could become inconsistent.&lt;/p&gt;

&lt;p&gt;This is why distributed rate limiting needs &lt;strong&gt;atomic operations&lt;/strong&gt; or carefully designed server-side logic.&lt;/p&gt;

&lt;p&gt;The rate limiter itself must be safe under high concurrency.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Response Should the API Return?
&lt;/h2&gt;

&lt;p&gt;When a client exceeds the limit, the standard response is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;429 Too Many Requests
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The API can also communicate useful information about when the client can retry.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Retry-After: 30
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells the client to wait before trying again.&lt;/p&gt;

&lt;p&gt;Well-designed clients should respect these signals instead of immediately retrying.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rate Limiting and Retry Storms
&lt;/h2&gt;

&lt;p&gt;Imagine a service becomes overloaded.&lt;/p&gt;

&lt;p&gt;Clients receive errors and immediately retry.&lt;/p&gt;

&lt;p&gt;Those retries create even more traffic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Service Overloaded
       ↓
     Errors
       ↓
     Clients
       ↓
   Immediate Retry
       ↓
More Traffic
       ↓
More Errors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can become a &lt;strong&gt;retry storm&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Rate limiting works best alongside strategies such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exponential backoff&lt;/li&gt;
&lt;li&gt;Jitter&lt;/li&gt;
&lt;li&gt;Retry limits&lt;/li&gt;
&lt;li&gt;Circuit breakers&lt;/li&gt;
&lt;li&gt;Timeouts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is to prevent failures from amplifying themselves.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rate Limiting vs Authentication
&lt;/h2&gt;

&lt;p&gt;These systems often work together.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   ↓
Authentication
   ↓
Rate Limiting
   ↓
Authorization
   ↓
Application
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or at an API gateway:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
  ↓
API Gateway
  ├── Authentication
  ├── Rate Limiting
  └── Routing
        ↓
      Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Authentication identifies the client.&lt;/p&gt;

&lt;p&gt;Rate limiting controls how much traffic that client can generate.&lt;/p&gt;

&lt;p&gt;Authorization determines what the client is allowed to access.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rate Limiting Expensive Operations
&lt;/h2&gt;

&lt;p&gt;Not every request costs the same.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET /health
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /generate-report
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second request may consume significantly more CPU, memory, database resources, or external API calls.&lt;/p&gt;

&lt;p&gt;Using the same rate limit for both endpoints may not make sense.&lt;/p&gt;

&lt;p&gt;You can assign different limits based on cost:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Health Check
→ High limit

Read API
→ Medium limit

Heavy Computation
→ Low limit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is sometimes called &lt;strong&gt;cost-based rate limiting&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rate Limiting at Large Scale
&lt;/h2&gt;

&lt;p&gt;A large production architecture might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                       Users
                         │
                         ▼
                       CDN
                         │
                         ▼
                   API Gateway
                         │
                 ┌───────┴───────┐
                 │ Rate Limiter  │
                 └───────┬───────┘
                         │
                  Load Balancer
                         │
             ┌───────────┼───────────┐
             ▼           ▼           ▼
          Server 1    Server 2    Server 3
             │           │           │
             └───────────┼───────────┘
                         ▼
                      Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A shared store can maintain rate-limit state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             Servers
                │
                ▼
             Redis
                │
                ▼
        Rate Limit State
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows rate limiting to work across the entire application cluster.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Rate Limiting Mistakes
&lt;/h2&gt;

&lt;p&gt;Some common mistakes include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using only in-memory counters with multiple servers&lt;/li&gt;
&lt;li&gt;Applying the same limit to every endpoint&lt;/li&gt;
&lt;li&gt;Ignoring shared IP addresses&lt;/li&gt;
&lt;li&gt;Not handling bursts correctly&lt;/li&gt;
&lt;li&gt;Forgetting about concurrent requests&lt;/li&gt;
&lt;li&gt;Returning errors without retry information&lt;/li&gt;
&lt;li&gt;Allowing clients to retry immediately&lt;/li&gt;
&lt;li&gt;Using a single global limit for different user tiers&lt;/li&gt;
&lt;li&gt;Not monitoring rejected requests&lt;/li&gt;
&lt;li&gt;Making the rate limiter itself a single point of failure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rate limiting is infrastructure.&lt;/p&gt;

&lt;p&gt;It needs to scale alongside the system it protects.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rate Limiting for Different Users
&lt;/h2&gt;

&lt;p&gt;Not every user needs the same limits.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Free Plan
→ 100 requests/minute

Pro Plan
→ 1,000 requests/minute

Enterprise
→ Custom limits
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows APIs to support different usage levels.&lt;/p&gt;

&lt;p&gt;The rate-limit key could include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_id + plan + endpoint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives the system more granular control.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Big Picture
&lt;/h2&gt;

&lt;p&gt;Rate limiting is essentially a &lt;strong&gt;traffic-control mechanism for your APIs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Without it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Clients
   ↓
API
   ↓
Overload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Clients
   ↓
Rate Limiter
   ↓
Allowed Traffic
   ↓
API
   ↓
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rate limiter acts as a protective boundary between unpredictable client traffic and your infrastructure.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Takeaway
&lt;/h2&gt;

&lt;p&gt;Rate limiting isn't just about blocking excessive requests.&lt;/p&gt;

&lt;p&gt;It's about making your system &lt;strong&gt;more predictable, resilient, and fair&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A good rate-limiting strategy can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Protect APIs from abuse&lt;/li&gt;
&lt;li&gt;Handle traffic spikes&lt;/li&gt;
&lt;li&gt;Prevent resource exhaustion&lt;/li&gt;
&lt;li&gt;Protect databases&lt;/li&gt;
&lt;li&gt;Improve system stability&lt;/li&gt;
&lt;li&gt;Provide fair usage across clients&lt;/li&gt;
&lt;li&gt;Reduce infrastructure costs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For small systems, a simple fixed-window limiter may be enough.&lt;/p&gt;

&lt;p&gt;For larger distributed systems, you may need token buckets, shared Redis state, API gateways, edge protection, and carefully designed retry behavior.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The goal isn't to stop traffic. The goal is to control traffic so your system can keep serving legitimate users reliably.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>api</category>
      <category>backend</category>
      <category>security</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Authentication at Scale: Sessions, JWT, Tokens, OAuth &amp; Distributed Auth</title>
      <dc:creator>Tanu Priya</dc:creator>
      <pubDate>Thu, 20 Aug 2026 11:03:09 +0000</pubDate>
      <link>https://dev.to/tanu_priya/authentication-at-scale-sessions-jwt-tokens-oauth-distributed-auth-4aef</link>
      <guid>https://dev.to/tanu_priya/authentication-at-scale-sessions-jwt-tokens-oauth-distributed-auth-4aef</guid>
      <description>&lt;p&gt;Authentication looks simple when an application has a few users.&lt;/p&gt;

&lt;p&gt;A user enters an email and password, the server verifies the credentials, and the user is logged in.&lt;/p&gt;

&lt;p&gt;But what happens when your application has &lt;strong&gt;millions of users, thousands of requests per second, multiple servers, mobile apps, and services distributed across different regions?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Authentication becomes a system-design problem.&lt;/p&gt;

&lt;p&gt;You need to answer questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Where should authentication state be stored?&lt;/li&gt;
&lt;li&gt;How do multiple servers know that a user is logged in?&lt;/li&gt;
&lt;li&gt;Should you use sessions or JWTs?&lt;/li&gt;
&lt;li&gt;How should tokens expire?&lt;/li&gt;
&lt;li&gt;How do you revoke access?&lt;/li&gt;
&lt;li&gt;How does OAuth work?&lt;/li&gt;
&lt;li&gt;How do microservices verify users?&lt;/li&gt;
&lt;li&gt;How do you scale authentication across regions?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's break it down.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Authentication?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Authentication&lt;/strong&gt; answers one fundamental question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Who are you?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
  ↓
Email + Password
  ↓
Authentication Server
  ↓
Identity Verified
  ↓
Authenticated User
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Authentication is different from &lt;strong&gt;authorization&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Authentication
&lt;/h3&gt;

&lt;p&gt;Determines who the user is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Who are you?
→ User 123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Authorization
&lt;/h3&gt;

&lt;p&gt;Determines what the user is allowed to do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What can User 123 access?
→ Read orders
→ Create orders
→ Cannot access admin panel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A scalable system usually needs both.&lt;/p&gt;




&lt;h2&gt;
  
  
  Basic Authentication Flow
&lt;/h2&gt;

&lt;p&gt;A simple login system might work like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
  │
  │ Email + Password
  ▼
Auth Server
  │
  │ Verify Credentials
  ▼
User Database
  │
  │ Valid
  ▼
Session / Token
  │
  ▼
User
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After login, the client needs some way to prove its identity on future requests.&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;sessions and tokens&lt;/strong&gt; come in.&lt;/p&gt;




&lt;h2&gt;
  
  
  Session-Based Authentication
&lt;/h2&gt;

&lt;p&gt;With session-based authentication, the server creates a session after successful login.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
  ↓
Login
  ↓
Server
  ↓
Create Session
  ↓
Session ID
  ↓
Browser Cookie
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser might store:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;session_id=abc123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On future requests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
  ↓
Cookie: session_id=abc123
  ↓
Server
  ↓
Session Store
  ↓
User Identity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server uses the session ID to find the user's authentication state.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Sessions Work at Scale
&lt;/h2&gt;

&lt;p&gt;A single server can store sessions in memory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
 ↓
Server
 ↓
RAM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this creates a problem when multiple servers are introduced.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             Load Balancer
              /        \
             ↓          ↓
        Server 1     Server 2
           │             │
        Session A      Session B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A user logs in through Server 1.&lt;/p&gt;

&lt;p&gt;The session exists on Server 1.&lt;/p&gt;

&lt;p&gt;The next request might go to Server 2.&lt;/p&gt;

&lt;p&gt;Server 2 doesn't know about the session.&lt;/p&gt;

&lt;p&gt;This is a problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  Shared Session Store
&lt;/h2&gt;

&lt;p&gt;One solution is to move sessions into a shared data store.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                Load Balancer
                /           \
               ▼             ▼
          Server 1       Server 2
               \             /
                \           /
                  ▼       ▼
                Session Store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The session store could be a distributed in-memory database such as Redis.&lt;/p&gt;

&lt;p&gt;Now every application server can access the same session state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   ↓
Any Server
   ↓
Shared Session Store
   ↓
User Session
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes session-based authentication much easier to scale horizontally.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advantages of Sessions
&lt;/h2&gt;

&lt;p&gt;Sessions have several useful properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy to invalidate&lt;/li&gt;
&lt;li&gt;Server controls authentication state&lt;/li&gt;
&lt;li&gt;Sensitive authentication data doesn't need to be stored directly in the browser&lt;/li&gt;
&lt;li&gt;Logout can invalidate the session&lt;/li&gt;
&lt;li&gt;Good fit for traditional web applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But sessions also have a trade-off:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The server needs to maintain authentication state.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At very large scale, that shared state becomes an important infrastructure component.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is a Token?
&lt;/h2&gt;

&lt;p&gt;A token is a piece of data that represents an authenticated identity or authorization.&lt;/p&gt;

&lt;p&gt;Instead of storing all authentication state on the server, the client can present a token with its requests.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
 ↓
Login
 ↓
Auth Server
 ↓
Access Token
 ↓
Client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
 ↓
Authorization: Bearer &amp;lt;token&amp;gt;
 ↓
API Server
 ↓
Validate Token
 ↓
Request Allowed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tokens are commonly used in APIs, mobile applications, and distributed systems.&lt;/p&gt;




&lt;h2&gt;
  
  
  JWT Authentication
&lt;/h2&gt;

&lt;p&gt;One popular token format is &lt;strong&gt;JWT (JSON Web Token)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A JWT typically contains information such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Header
Payload
Signature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JWT
 ├── Header
 ├── Payload
 └── Signature
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The payload might contain claims such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"sub"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"exp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1780000000&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The token is signed so the server can verify that it was issued by a trusted authority and hasn't been modified.&lt;/p&gt;




&lt;h2&gt;
  
  
  JWT Authentication Flow
&lt;/h2&gt;

&lt;p&gt;A simplified JWT flow looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
  ↓
Login
  ↓
Authentication Server
  ↓
JWT
  ↓
Client
  ↓
API Request
  ↓
Authorization: Bearer JWT
  ↓
API Server
  ↓
Verify Signature
  ↓
Allow Request
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One major benefit is that the server doesn't necessarily need to look up a session for every request.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sessions vs JWT
&lt;/h2&gt;

&lt;p&gt;The difference can be summarized like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Sessions&lt;/th&gt;
&lt;th&gt;JWT&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Server maintains session state&lt;/td&gt;
&lt;td&gt;Token carries claims&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Session ID sent by client&lt;/td&gt;
&lt;td&gt;JWT sent by client&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Easy revocation&lt;/td&gt;
&lt;td&gt;Revocation requires additional strategy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Requires shared session storage at scale&lt;/td&gt;
&lt;td&gt;Can be validated independently&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Common for web applications&lt;/td&gt;
&lt;td&gt;Common for APIs and distributed systems&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server controls session state&lt;/td&gt;
&lt;td&gt;Client carries the token&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Neither is universally better.&lt;/p&gt;

&lt;p&gt;The right choice depends on the system.&lt;/p&gt;




&lt;h2&gt;
  
  
  Stateless Authentication
&lt;/h2&gt;

&lt;p&gt;JWTs are often used to create a more &lt;strong&gt;stateless authentication layer&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Load Balancer
                /      |      \
               ▼       ▼       ▼
           Server 1 Server 2 Server 3
              │        │        │
              └──── Verify JWT ─┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each server can independently validate the token.&lt;/p&gt;

&lt;p&gt;There doesn't necessarily need to be a centralized session lookup for every request.&lt;/p&gt;

&lt;p&gt;This can simplify horizontal scaling.&lt;/p&gt;

&lt;p&gt;But "stateless" doesn't mean there is no authentication infrastructure.&lt;/p&gt;

&lt;p&gt;You still need to manage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Token issuance&lt;/li&gt;
&lt;li&gt;Signing keys&lt;/li&gt;
&lt;li&gt;Expiration&lt;/li&gt;
&lt;li&gt;Refresh tokens&lt;/li&gt;
&lt;li&gt;Revocation&lt;/li&gt;
&lt;li&gt;Key rotation&lt;/li&gt;
&lt;li&gt;User sessions&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Access Tokens and Refresh Tokens
&lt;/h2&gt;

&lt;p&gt;A common authentication design uses two types of tokens.&lt;/p&gt;

&lt;h3&gt;
  
  
  Access Token
&lt;/h3&gt;

&lt;p&gt;Used to access APIs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
 ↓
Access Token
 ↓
API
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Access tokens are usually short-lived.&lt;/p&gt;

&lt;h3&gt;
  
  
  Refresh Token
&lt;/h3&gt;

&lt;p&gt;Used to obtain a new access token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Refresh Token
      ↓
Authentication Server
      ↓
New Access Token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The flow becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Login
  ↓
Access Token + Refresh Token
  ↓
Client
  │
  ├── Access Token → API
  │
  └── Refresh Token → Auth Server
                           ↓
                    New Access Token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Short-lived access tokens reduce the impact of a stolen token.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Not Make Access Tokens Last Forever?
&lt;/h2&gt;

&lt;p&gt;Suppose an access token remains valid for 30 days.&lt;/p&gt;

&lt;p&gt;If an attacker obtains it, they may be able to use it for the entire validity period.&lt;/p&gt;

&lt;p&gt;Shorter expiration reduces this window.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Access Token
   ↓
Short Lifetime
   ↓
Expires
   ↓
Refresh Token
   ↓
New Access Token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact expiration strategy depends on the application's security and usability requirements.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is OAuth?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;OAuth&lt;/strong&gt; is a framework for delegated authorization.&lt;/p&gt;

&lt;p&gt;A common example is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Continue with Google"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of giving your application your Google password, the user authenticates with Google and grants the application permission to access specific information.&lt;/p&gt;

&lt;p&gt;A simplified flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
  ↓
Your Application
  ↓
OAuth Provider
  ↓
User Authentication
  ↓
Authorization
  ↓
Authorization Code
  ↓
Your Backend
  ↓
Access Token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OAuth is especially useful when applications need access to resources controlled by another identity provider.&lt;/p&gt;




&lt;h2&gt;
  
  
  OAuth vs Authentication
&lt;/h2&gt;

&lt;p&gt;OAuth is primarily about &lt;strong&gt;authorization&lt;/strong&gt;, not simply proving identity.&lt;/p&gt;

&lt;p&gt;For user authentication, systems commonly use &lt;strong&gt;OpenID Connect (OIDC)&lt;/strong&gt; on top of OAuth 2.0.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OAuth
  ↓
Delegated Authorization

OIDC
  ↓
Authentication / Identity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This distinction is important when designing authentication systems.&lt;/p&gt;




&lt;h2&gt;
  
  
  Authentication in Microservices
&lt;/h2&gt;

&lt;p&gt;Now imagine a system with multiple services:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                API Gateway
                    │
        ┌───────────┼───────────┐
        ▼           ▼           ▼
   User Service Order Service Payment Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Should every service implement login and password verification?&lt;/p&gt;

&lt;p&gt;Usually, no.&lt;/p&gt;

&lt;p&gt;A better approach is to centralize identity management.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Identity Provider
                       │
                       ▼
                  Access Token
                       │
                       ▼
                  API Gateway
                       │
       ┌───────────────┼───────────────┐
       ▼               ▼               ▼
   User Service    Order Service   Payment Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Services can validate the authenticated identity and authorization information rather than each implementing its own login system.&lt;/p&gt;




&lt;h2&gt;
  
  
  Authentication at the API Gateway
&lt;/h2&gt;

&lt;p&gt;An API Gateway can perform authentication before forwarding requests.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
  ↓
API Gateway
  ↓
Validate Token
  ↓
Authenticated?
  │
 ┌┴─────────┐
 No         Yes
 ↓           ↓
401        Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This provides a centralized entry point.&lt;/p&gt;

&lt;p&gt;However, internal services should still be designed carefully rather than blindly trusting every request from the network.&lt;/p&gt;




&lt;h2&gt;
  
  
  Distributed Authentication
&lt;/h2&gt;

&lt;p&gt;At large scale, authentication infrastructure itself may be distributed.&lt;/p&gt;

&lt;p&gt;A simplified architecture could look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    Users
                      │
                      ▼
                Global Routing
                      │
          ┌───────────┴───────────┐
          ▼                       ▼
     Auth Region A           Auth Region B
          │                       │
          ▼                       ▼
      Auth Servers            Auth Servers
          │                       │
          └───────────┬───────────┘
                      ▼
               User / Identity Data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now additional challenges appear:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Regional availability&lt;/li&gt;
&lt;li&gt;Data replication&lt;/li&gt;
&lt;li&gt;Key management&lt;/li&gt;
&lt;li&gt;Token validation&lt;/li&gt;
&lt;li&gt;Session consistency&lt;/li&gt;
&lt;li&gt;Failover&lt;/li&gt;
&lt;li&gt;Clock synchronization&lt;/li&gt;
&lt;li&gt;Rate limiting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Authentication becomes part of the overall distributed-system architecture.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Happens If an Authentication Server Goes Down?
&lt;/h2&gt;

&lt;p&gt;Authentication is often a critical dependency.&lt;/p&gt;

&lt;p&gt;If users cannot authenticate, they may not be able to access the application.&lt;/p&gt;

&lt;p&gt;Therefore, authentication infrastructure should avoid having a single point of failure.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Load Balancer
                /      |      \
               ▼       ▼       ▼
            Auth 1   Auth 2   Auth 3
               │       │       │
               └───────┼───────┘
                       ▼
                 Shared Data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multiple authentication servers can provide redundancy.&lt;/p&gt;

&lt;p&gt;Health checks and failover mechanisms can route traffic away from unhealthy instances.&lt;/p&gt;




&lt;h2&gt;
  
  
  Token Validation at Scale
&lt;/h2&gt;

&lt;p&gt;Suppose your application receives millions of API requests.&lt;/p&gt;

&lt;p&gt;You don't want every request to perform an expensive remote authentication lookup if it can be avoided.&lt;/p&gt;

&lt;p&gt;A common approach is to use signed tokens that services can validate locally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
  ↓
Service
  ↓
Verify Token Signature
  ↓
Check Claims
  ↓
Allow / Reject
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This reduces dependency on a centralized authentication lookup for every request.&lt;/p&gt;

&lt;p&gt;However, services still need access to trusted signing keys.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Management
&lt;/h2&gt;

&lt;p&gt;If JWTs or other signed tokens are used, signing keys become critical infrastructure.&lt;/p&gt;

&lt;p&gt;A simplified flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Identity Provider
       │
       │ Signs Token
       ▼
    JWT Token
       │
       ▼
   Application
       │
       │ Verify Signature
       ▼
    Valid Token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keys should be protected carefully.&lt;/p&gt;

&lt;p&gt;Large systems may use mechanisms such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Key rotation&lt;/li&gt;
&lt;li&gt;Key versioning&lt;/li&gt;
&lt;li&gt;Secure key storage&lt;/li&gt;
&lt;li&gt;Public/private key pairs&lt;/li&gt;
&lt;li&gt;Key distribution endpoints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is to avoid making a single long-lived secret a permanent point of failure.&lt;/p&gt;




&lt;h2&gt;
  
  
  Logout and Token Revocation
&lt;/h2&gt;

&lt;p&gt;Logging out sounds simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User → Logout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But with stateless tokens, there's an important question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What happens to an access token that has already been issued?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the token remains valid until expiration, simply deleting it from the client doesn't necessarily invalidate it everywhere.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Short-lived access tokens&lt;/li&gt;
&lt;li&gt;Refresh-token revocation&lt;/li&gt;
&lt;li&gt;Session tracking&lt;/li&gt;
&lt;li&gt;Token deny lists&lt;/li&gt;
&lt;li&gt;Token versioning&lt;/li&gt;
&lt;li&gt;Centralized authorization checks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The right approach depends on how quickly the system needs to revoke access.&lt;/p&gt;




&lt;h2&gt;
  
  
  Authentication Rate Limiting
&lt;/h2&gt;

&lt;p&gt;Authentication endpoints are attractive targets for attackers.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /login
POST /signup
POST /forgot-password
POST /refresh-token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These endpoints should typically have appropriate protections such as rate limiting and abuse detection.&lt;/p&gt;

&lt;p&gt;A simplified flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Login Request
     ↓
Rate Limiter
     ↓
Authentication
     ↓
Success / Failure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helps prevent excessive login attempts and protects authentication infrastructure from abuse.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Scalable Authentication Architecture
&lt;/h2&gt;

&lt;p&gt;Putting the concepts together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                         Users
                           │
                           ▼
                     Load Balancer
                           │
                           ▼
                      API Gateway
                           │
                    Token Validation
                           │
          ┌────────────────┼────────────────┐
          ▼                ▼                ▼
      User Service     Order Service    Payment Service
          │                │                │
          └────────────────┼────────────────┘
                           │
                      Databases
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alongside the application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Identity Provider
                       │
              ┌────────┴────────┐
              ▼                 ▼
         Access Tokens     Refresh Tokens
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This separates identity management from application business logic.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Authentication Mistakes
&lt;/h2&gt;

&lt;p&gt;Some common architectural mistakes include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Storing passwords insecurely&lt;/li&gt;
&lt;li&gt;Making access tokens live too long&lt;/li&gt;
&lt;li&gt;Putting sensitive information into tokens&lt;/li&gt;
&lt;li&gt;Sharing authentication secrets carelessly&lt;/li&gt;
&lt;li&gt;Having a single authentication server&lt;/li&gt;
&lt;li&gt;Not planning for token revocation&lt;/li&gt;
&lt;li&gt;Ignoring key rotation&lt;/li&gt;
&lt;li&gt;Treating OAuth as the same thing as authentication&lt;/li&gt;
&lt;li&gt;Trusting internal services without proper controls&lt;/li&gt;
&lt;li&gt;Building authentication independently in every microservice&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Authentication is security-critical infrastructure.&lt;/p&gt;

&lt;p&gt;It should be designed accordingly.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Should You Choose?
&lt;/h2&gt;

&lt;p&gt;There isn't one authentication strategy that works for every system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Small Traditional Web Application
&lt;/h3&gt;

&lt;p&gt;A server-side session can be a simple and effective choice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Browser
  ↓
Session Cookie
  ↓
Server
  ↓
Session Store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Mobile or API-Heavy Application
&lt;/h3&gt;

&lt;p&gt;Token-based authentication may be more appropriate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
  ↓
Access Token
  ↓
API
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Distributed Microservices
&lt;/h3&gt;

&lt;p&gt;A centralized identity provider with signed access tokens can work well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Identity Provider
       ↓
Access Token
       ↓
API Gateway
       ↓
Microservices
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Social Login
&lt;/h3&gt;

&lt;p&gt;OAuth + OpenID Connect is commonly used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application
     ↓
Identity Provider
     ↓
User Authentication
     ↓
Application
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  The Big Picture
&lt;/h2&gt;

&lt;p&gt;Authentication at scale is not just about login forms.&lt;/p&gt;

&lt;p&gt;It is about creating a reliable identity system that works across:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple servers&lt;/li&gt;
&lt;li&gt;Multiple services&lt;/li&gt;
&lt;li&gt;Multiple regions&lt;/li&gt;
&lt;li&gt;Web applications&lt;/li&gt;
&lt;li&gt;Mobile applications&lt;/li&gt;
&lt;li&gt;APIs&lt;/li&gt;
&lt;li&gt;Third-party identity providers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A simplified evolution might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Simple App
    ↓
Sessions
    ↓
Shared Session Store
    ↓
Token-Based APIs
    ↓
Identity Provider
    ↓
Distributed Authentication
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The architecture should evolve as the system's requirements grow.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Takeaway
&lt;/h2&gt;

&lt;p&gt;There is no universal winner between &lt;strong&gt;sessions, JWTs, and tokens&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each approach solves a different problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sessions&lt;/strong&gt; provide centralized server-side control.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JWTs&lt;/strong&gt; can make token validation easier across distributed services.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access and refresh tokens&lt;/strong&gt; provide a flexible pattern for API authentication.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OAuth and OpenID Connect&lt;/strong&gt; make delegated access and external identity providers possible.&lt;/p&gt;

&lt;p&gt;And at large scale, authentication becomes distributed infrastructure that must be designed for &lt;strong&gt;security, availability, scalability, and failure&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Authentication isn't just about proving who the user is. At scale, it's about reliably proving identity across an entire distributed system.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>security</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
