<?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: Harsh Mangalam</title>
    <description>The latest articles on DEV Community by Harsh Mangalam (@harshmangalam).</description>
    <link>https://dev.to/harshmangalam</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%2F366422%2Fa0bc76cf-cd38-4683-a643-885599b4b6e1.PNG</url>
      <title>DEV Community: Harsh Mangalam</title>
      <link>https://dev.to/harshmangalam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harshmangalam"/>
    <language>en</language>
    <item>
      <title>A Beginner's Guide to CCSDS Space Packets</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Tue, 17 Nov 2026 08:53:56 +0000</pubDate>
      <link>https://dev.to/harshmangalam/a-beginners-guide-to-ccsds-space-packets-4789</link>
      <guid>https://dev.to/harshmangalam/a-beginners-guide-to-ccsds-space-packets-4789</guid>
      <description>&lt;p&gt;Imagine you're on a satellite 500 km above Earth. You have temperature readings, battery levels, and a freshly captured photo. You need to get all of it to a ground station. You only get a few minutes per pass, the link is noisy, and nobody can walk over and plug in a cable.&lt;/p&gt;

&lt;p&gt;How do you package that data so the ground knows what each piece is?&lt;/p&gt;

&lt;p&gt;Most space agencies and many commercial missions use standards from &lt;strong&gt;CCSDS&lt;/strong&gt; (the Consultative Committee for Space Data Systems). Two of those standards handle the "packaging" step:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SPP&lt;/strong&gt;: Space Packet Protocol&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EPP&lt;/strong&gt;: Encapsulation Packet Protocol&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post explains both with plain analogies, a few diagrams, and a small bit of Python. No space background needed.&lt;/p&gt;




&lt;h2&gt;
  
  
  🗺️ The big picture: it's a postal system
&lt;/h2&gt;

&lt;p&gt;If you know networking, you already know layered protocols. CCSDS uses the same idea:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application Layer        →  "What I want to send"
       |
   SPP / EPP             →  Put it in a labelled envelope
       |
Data Link (TM/TC/AOS)    →  Load envelopes into a delivery truck
       |
Physical (RF/Optical)    →  The road the truck drives on
       |
   Ground Station
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The analogy we'll use throughout:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Real world 📮&lt;/th&gt;
&lt;th&gt;CCSDS 🛰️&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Letter contents&lt;/td&gt;
&lt;td&gt;Application data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Envelope with a label&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Space Packet&lt;/strong&gt; (SPP)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Label on the envelope&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;APID&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delivery truck&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Transfer Frame&lt;/strong&gt; (data link)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Separate lanes on the highway&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Virtual Channels&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The road itself&lt;/td&gt;
&lt;td&gt;RF / optical link&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;Remember:&lt;/strong&gt; A Space Packet is &lt;strong&gt;not&lt;/strong&gt; a Transfer Frame. Envelopes ride &lt;em&gt;inside&lt;/em&gt; trucks. Many people mix these up.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You'll also hear about CCSDS "books":&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📗 &lt;strong&gt;Green Books&lt;/strong&gt; explain the concepts and architecture (good for learning).&lt;/li&gt;
&lt;li&gt;📘 &lt;strong&gt;Blue Books&lt;/strong&gt; are the official rules you implement against.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✉️ Part 1: The Space Packet
&lt;/h2&gt;

&lt;p&gt;A Space Packet has two parts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+------------------+--------------------------------------+
|  Primary Header  |          Packet Data Field           |
|    (6 bytes)     |  [optional secondary header] + data  |
+------------------+--------------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;primary header&lt;/strong&gt; is always exactly &lt;strong&gt;6 bytes&lt;/strong&gt;. It's the label on the envelope.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;packet data field&lt;/strong&gt; holds your payload. It can also start with an optional &lt;strong&gt;secondary header&lt;/strong&gt;, which the mission defines (timestamps are a common choice).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What's on the label?
&lt;/h3&gt;

&lt;p&gt;Here are the 6 bytes (48 bits), broken down:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;| Version | Type | Sec Hdr | APID    | Seq Flags | Seq Count | Data Length |
| 3 bits  | 1    | 1       | 11 bits | 2 bits    | 14 bits   | 16 bits     |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;th&gt;Analogy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Version (PVN)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;000&lt;/code&gt; means "this is a Space Packet"&lt;/td&gt;
&lt;td&gt;Envelope format&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Type&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;0&lt;/code&gt; = telemetry, &lt;code&gt;1&lt;/code&gt; = telecommand&lt;/td&gt;
&lt;td&gt;Outgoing vs incoming mail&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Sec Hdr Flag&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Is there a secondary header?&lt;/td&gt;
&lt;td&gt;"See attached note" checkbox&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;APID&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Which data stream this belongs to&lt;/td&gt;
&lt;td&gt;Department name on the label&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Seq Flags&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Is this a whole message or part of one?&lt;/td&gt;
&lt;td&gt;"Part 2 of 5"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Seq Count&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Running number per APID&lt;/td&gt;
&lt;td&gt;Page number&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Data Length&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Size of the data field, &lt;strong&gt;minus 1&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;Weight on the label&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Packets are "self-delimiting"
&lt;/h3&gt;

&lt;p&gt;The receiver never has to guess where a packet ends. The length field tells it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Total Packet Length = 6 (header) + Packet Data Length + 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Packet Data Length = &lt;code&gt;99&lt;/code&gt; → &lt;code&gt;6 + 99 + 1 = 106 bytes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Why the &lt;code&gt;+1&lt;/code&gt;? A data field can never be empty, so the standard stores "length minus one". That lets 16 bits describe up to &lt;strong&gt;65,536&lt;/strong&gt; bytes of data instead of 65,535.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's decode one in Python 🐍
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;

&lt;span class="nf"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse_primary_header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;word1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;word2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&amp;gt;HHH"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# big-endian&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;      &lt;span class="n"&gt;word1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;         &lt;span class="s2"&gt;"TC"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"TM"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"has_sec_hdr"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"apid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;         &lt;span class="n"&gt;word1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0x7FF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;# lowest 11 bits&lt;/span&gt;
        &lt;span class="s2"&gt;"seq_flags"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="n"&gt;word2&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"seq_count"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="n"&gt;word2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0x3FFF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;         &lt;span class="c1"&gt;# lowest 14 bits&lt;/span&gt;
        &lt;span class="s2"&gt;"total_length"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;length&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;parse_primary_header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromhex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0864C02A0063"&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="c1"&gt;# {'version': 0, 'type': 'TM', 'has_sec_hdr': True, 'apid': 100,&lt;/span&gt;
&lt;span class="c1"&gt;#  'seq_flags': 3, 'seq_count': 42, 'total_length': 106}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a telemetry packet on APID 100, number 42 in its stream, 106 bytes long. Once you see it as bit-shifting, the header isn't scary.&lt;/p&gt;




&lt;h2&gt;
  
  
  🏷️ Part 2: APID, the label that matters most
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;APID&lt;/strong&gt; (Application Process Identifier) is an 11-bit number, so it ranges from &lt;strong&gt;0 to 2047&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of it like &lt;strong&gt;Slack channels&lt;/strong&gt; or &lt;strong&gt;Kafka topics&lt;/strong&gt;. It says &lt;em&gt;which stream&lt;/em&gt; a packet belongs to, so the ground knows where to send it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        Received Space Packet
                 |
          Read the APID
                 |
     Look up the mission's APID table
                 |
      +----------+----------+
      v          v          v
   APID 100   APID 101   APID 102
   Thermal    Battery    Camera
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;APID&lt;/th&gt;
&lt;th&gt;Example stream&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;Temperature telemetry&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;td&gt;Battery telemetry&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;td&gt;Camera data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;These numbers are made-up examples. Each mission picks its own.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Things to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ &lt;strong&gt;The mission decides&lt;/strong&gt; what each APID means. CCSDS doesn't assign them.&lt;/li&gt;
&lt;li&gt;✅ APIDs only need to be unique within a &lt;strong&gt;naming domain&lt;/strong&gt;, such as one spacecraft.&lt;/li&gt;
&lt;li&gt;🚫 &lt;strong&gt;APID 2047 (&lt;code&gt;0x7FF&lt;/code&gt;) is reserved for Idle Packets&lt;/strong&gt;, which are filler sent when there's nothing else to send.&lt;/li&gt;
&lt;li&gt;❌ An APID is &lt;strong&gt;not an IP address&lt;/strong&gt;. It labels a stream; it doesn't route anything across a network.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧱 Part 3: Two ways to use SPP
&lt;/h2&gt;

&lt;p&gt;SPP can sit in two different places in the stack. A given path uses one or the other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option A: SPP as the application layer.&lt;/strong&gt; Your flight software builds packets directly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application → SPP → Data Link → Physical
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Option B: SPP as a "shim".&lt;/strong&gt; Something else (IP, UDP, Bundle Protocol) already made a packet, and SPP wraps it so it can travel over CCSDS links.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TCP / UDP / IP / BP → SPP (shim) → Data Link → Physical
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;strong&gt;shim&lt;/strong&gt; is a thin adapter layer, like a &lt;strong&gt;travel plug adapter&lt;/strong&gt;. Your device is unchanged; the adapter just makes it fit a different socket.&lt;/p&gt;

&lt;p&gt;Two quick terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SDU&lt;/strong&gt; (Service Data Unit): what you hand &lt;em&gt;to&lt;/em&gt; a protocol.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PDU&lt;/strong&gt; (Protocol Data Unit): what the protocol produces, with its header added.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Wrapping one protocol's PDU inside another is called &lt;strong&gt;encapsulation&lt;/strong&gt;, which is the Russian-doll idea behind most networking.&lt;/p&gt;




&lt;h2&gt;
  
  
  📦 Part 4: What SPP promises (and what it doesn't)
&lt;/h2&gt;

&lt;p&gt;Applications talk to SPP through a &lt;strong&gt;Service Access Point (SAP)&lt;/strong&gt;, which works like a mail slot. SPP offers two services:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Service&lt;/th&gt;
&lt;th&gt;You hand over…&lt;/th&gt;
&lt;th&gt;SPP does…&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Packet Service&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;A fully built Space Packet&lt;/td&gt;
&lt;td&gt;Sends it unchanged&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Octet String Service&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Raw bytes&lt;/td&gt;
&lt;td&gt;Builds the Space Packet for you&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you've used &lt;strong&gt;UDP&lt;/strong&gt;, SPP's behaviour will feel familiar. It is:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Property&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Preconfigured&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Paths are set up in advance by mission management&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Unidirectional&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;One direction per path&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Asynchronous&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Send whenever you want&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Unconfirmed&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No "delivered!" receipt&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Incomplete&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Packets can be lost, and nothing retransmits them&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Not sequence-preserving&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Packets may arrive out of order end to end&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;No flow control&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Not defined between the user and SPP&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two-way communication means &lt;strong&gt;two separate one-way paths&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;Spacecraft                    Ground
    |---- Telemetry (TM) ------&amp;gt;|
    |&amp;lt;--- Telecommand (TC) -----|
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;Mental model:&lt;/strong&gt; SPP is the post office's basic service. It labels and sends your letter, but it doesn't promise delivery or send a receipt.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🔢 Part 5: Detecting lost packets
&lt;/h2&gt;

&lt;p&gt;So how do you know a packet went missing? Use the &lt;strong&gt;Packet Sequence Count&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;14-bit counter, range &lt;strong&gt;0 to 16383&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Counted separately &lt;strong&gt;per APID&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Wraps back to 0 after 16383&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of numbered pages in a book. If you go from page 101 to page 103, page 102 is missing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Satellite                 Ground
   |---- Seq 100 -----------&amp;gt;| ✓
   |---- Seq 101 -----------&amp;gt;| ✓
   |---- Seq 102 -----X      | ✗ lost
   |---- Seq 103 -----------&amp;gt;| ✓
                              |
                     "Gap: 101 → 103!"
                     ...but SPP won't resend it.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SPP lets you &lt;strong&gt;detect&lt;/strong&gt; a gap. It does not &lt;strong&gt;fix&lt;/strong&gt; it. For recovery you add other mechanisms:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mechanism&lt;/th&gt;
&lt;th&gt;What it gives you&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Sequence Count&lt;/td&gt;
&lt;td&gt;Spot possible gaps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;COP-1&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Reliable delivery of telecommand frames&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;CFDP&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;File delivery, with optional reliability&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Command ACK&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Mission-defined confirmation of command status&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  📸 Part 6: A real-world walkthrough
&lt;/h2&gt;

&lt;p&gt;Let's follow one command from start to finish: &lt;em&gt;"Take a picture."&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GROUND: "Capture image" command
            |
      SPP packet (APID 200)
            |
        TC Uplink  ⬆️
            |
SATELLITE: camera takes the picture
            |
     +------+------+
     v             v
 Command ACK    Image data
 (APID 201)     (APID 300)
     +------+------+
            |
       TM Downlink  ⬇️
            |
GROUND: check status + rebuild the image
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some points that trip people up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📨 &lt;strong&gt;SPP carries the command; it doesn't execute it.&lt;/strong&gt; Flight software does that.&lt;/li&gt;
&lt;li&gt;🚚 &lt;strong&gt;TC/TM&lt;/strong&gt; (data link) is what physically moves packets across the space link.&lt;/li&gt;
&lt;li&gt;⚠️ &lt;strong&gt;"The link delivered my packet" is not the same as "the satellite executed my command."&lt;/strong&gt; You need a mission-level ACK to know the second one.&lt;/li&gt;
&lt;li&gt;🧩 SPP won't reassemble the image for you. Your ground software does.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎁 Part 7: EPP, the gift-wrap for other protocols
&lt;/h2&gt;

&lt;p&gt;What if you already have a packet from &lt;strong&gt;another protocol&lt;/strong&gt;, like a CFDP file-transfer PDU or an IP packet, and you want to send it over CCSDS links?&lt;/p&gt;

&lt;p&gt;That's what &lt;strong&gt;EPP&lt;/strong&gt; (Encapsulation Packet Protocol) is for. Think of it as &lt;strong&gt;gift wrap&lt;/strong&gt;: it adds an outer layer with a label, and the gift inside stays exactly as it was.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   CFDP PDU (the gift 🎁)
           |
          EPP  →  +-------------+
                  | EPP Header  |  ← the wrapping + tag
                  +-------------+
                  | CFDP PDU    |  ← unchanged
                  +-------------+
           |
   Space Data Link (TM/TC/AOS)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;EPP rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One PDU per EPP packet.&lt;/strong&gt; No bundling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The inner PDU is never modified.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No automatic retransmission.&lt;/strong&gt; Same as SPP.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How does the receiver tell SPP from EPP?
&lt;/h3&gt;

&lt;p&gt;The first 3 bits, the &lt;strong&gt;Packet Version Number (PVN)&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;PVN&lt;/th&gt;
&lt;th&gt;Packet type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Space Packet (SPP)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;111&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Encapsulation Packet (EPP)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It works like a file's "magic number": read the first bits and you know how to parse the rest.&lt;/p&gt;

&lt;h3&gt;
  
  
  The EPP header is stretchy
&lt;/h3&gt;

&lt;p&gt;Unlike SPP's fixed 6 bytes, an EPP header can be &lt;strong&gt;1, 2, 4, or 8 bytes&lt;/strong&gt; long. The first byte says which:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+---------+-------------+--------+
|   PVN   | Protocol ID |  LID   |
| 3 bits  |   3 bits    | 2 bits |
+---------+-------------+--------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;LID&lt;/th&gt;
&lt;th&gt;Header size&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;00&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1 byte&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;01&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2 bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;10&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;4 bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;11&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;8 bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Protocol ID / EPI&lt;/strong&gt; tells the receiver which protocol is inside (for example, "this is CFDP").&lt;/li&gt;
&lt;li&gt;EPP &lt;strong&gt;doesn't use APIDs&lt;/strong&gt;. It identifies its user by combining &lt;strong&gt;PVN + EPI + data link channel&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The decoding steps on the ground:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Check PVN → Decode LID → Read header length → Read packet length
→ Identify protocol (EPI) → Extract original PDU → Hand to CFDP/IP handler
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  SPP vs EPP at a glance
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;SPP&lt;/th&gt;
&lt;th&gt;EPP&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Main job&lt;/td&gt;
&lt;td&gt;Package application data&lt;/td&gt;
&lt;td&gt;Wrap existing protocol PDUs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can act as a shim?&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes (its main role)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PVN&lt;/td&gt;
&lt;td&gt;&lt;code&gt;000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;111&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Header&lt;/td&gt;
&lt;td&gt;Fixed 6 bytes&lt;/td&gt;
&lt;td&gt;1, 2, 4, or 8 bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Uses APID?&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max size&lt;/td&gt;
&lt;td&gt;65,536-byte data field&lt;/td&gt;
&lt;td&gt;~4.29 GB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🤔 Part 8: Which one should I use?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        What are you sending?
                 |
       +---------+---------+
       |                   |
 Your own app data    An existing PDU
 (telemetry, etc.)    (CFDP, IP, ...)
       |                   |
      SPP             Consider EPP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You need…&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Housekeeping telemetry&lt;/td&gt;
&lt;td&gt;SPP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Your own application data&lt;/td&gt;
&lt;td&gt;SPP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Streams identified by APID&lt;/td&gt;
&lt;td&gt;SPP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CFDP PDUs&lt;/td&gt;
&lt;td&gt;EPP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IP packets&lt;/td&gt;
&lt;td&gt;EPP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Carry an existing protocol's PDUs&lt;/td&gt;
&lt;td&gt;EPP or SPP as a shim&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;You can use both in the same mission.&lt;/strong&gt; It's common, and neither one does routing or retransmission.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛡️ Part 9: Reliability, big files, and long delays
&lt;/h2&gt;

&lt;p&gt;SPP and EPP move packets. Other protocols handle the harder jobs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;Analogy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Regular telemetry&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;SPP&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Postcards&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reliable file transfer&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;CFDP&lt;/strong&gt; (acknowledged mode)&lt;/td&gt;
&lt;td&gt;Registered mail with tracking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Very long delays (deep space)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;LTP&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Mail that expects slow replies&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Keep traffic types apart&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Virtual Channels&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Separate highway lanes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Links that keep dropping&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;DTN / Bundle Protocol&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Store-and-forward relay stations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Urgent telemetry first&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;High-priority Virtual Channel&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Express lane&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;⏱️ SPP and EPP make &lt;strong&gt;no real-time promise&lt;/strong&gt;. They don't guarantee a maximum delivery time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Sending a big image
&lt;/h3&gt;

&lt;p&gt;A large image won't fit in one packet, so you split it into segments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Large Image
       |
 Split into segments
   +---+---+
   v   v   v
 Pkt1 Pkt2 Pkt3   →   Ground   →   Reassemble
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;SPP's &lt;strong&gt;Sequence Flags&lt;/strong&gt; mark the first, middle, and last segments.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;secondary header&lt;/strong&gt; can carry mission-defined segment info.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your application&lt;/strong&gt; does the reassembly, not SPP.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🌍 Part 10: The whole system, end to end
&lt;/h2&gt;

&lt;p&gt;Here's 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;               SPACECRAFT
       +---------+---------+
  Housekeeping         Image File
       |                   |
      SPP                 CFDP
       |                   |
       |                  EPP
       +---------+---------+
                 |
        CCSDS Data Link (TM / AOS / USLP)
                 |
          Transfer Frames
                 |
          Channel Coding
                 |
           RF Downlink  📡
                 |
             GROUND SYSTEM
                 |
          Frame Processing
         +-------+-------+
    SPP Decoder     EPP Decoder
         |               |
    Telemetry       CFDP Receiver
    Processor            |
                     Image File 🖼️
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Who does what
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Job&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Application&lt;/td&gt;
&lt;td&gt;Creates and interprets mission data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SPP&lt;/td&gt;
&lt;td&gt;Packages and labels application data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;EPP&lt;/td&gt;
&lt;td&gt;Wraps other protocols' PDUs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;APID&lt;/td&gt;
&lt;td&gt;Identifies a packet stream&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Virtual Channel&lt;/td&gt;
&lt;td&gt;Organizes traffic on the data link&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Transfer Frame&lt;/td&gt;
&lt;td&gt;Carries packets across the space link&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CFDP&lt;/td&gt;
&lt;td&gt;Delivers files, optionally with recovery&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Physical Layer&lt;/td&gt;
&lt;td&gt;Sends and receives the actual signal&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🧠 Cheat sheet: 7 rules to remember
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Space Packet ≠ Transfer Frame.&lt;/strong&gt; Envelopes ride inside trucks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;APID labels packet streams; VCID labels data link channels.&lt;/strong&gt; Different layers, different IDs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SPP/EPP don't route.&lt;/strong&gt; They label and package.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SPP/EPP don't guarantee delivery or retransmit.&lt;/strong&gt; Add CFDP, COP-1, etc. when you need that.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Packet delivered" ≠ "command executed."&lt;/strong&gt; Use mission-level ACKs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The mission defines&lt;/strong&gt; APIDs, command formats, and telemetry formats.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;For real implementation work, read the CCSDS Blue Books.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🚀 Wrapping up
&lt;/h2&gt;

&lt;p&gt;The whole flow fits in one line:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Application → Packet Protocol → Data Link → Physical Link → Ground Processing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each layer has one job. Most confusing satellite comms bugs come from expecting a layer to do something it was never designed to do, like assuming SPP retransmits, or treating an APID as a network address. Once those boundaries are clear, debugging gets much easier.&lt;/p&gt;

&lt;p&gt;If you write flight software or ground systems, or you're just curious how satellites talk, I hope this made CCSDS packets feel approachable. 🛰️&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Questions or corrections? Leave a comment below. 👇&lt;/em&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>PHP2Shell Explained: How Two Memory-Handling Bugs Can Lead to Remote Code Execution</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Thu, 22 Oct 2026 06:27:53 +0000</pubDate>
      <link>https://dev.to/harshmangalam/php2shell-explained-how-two-memory-handling-bugs-can-lead-to-remote-code-execution-3phd</link>
      <guid>https://dev.to/harshmangalam/php2shell-explained-how-two-memory-handling-bugs-can-lead-to-remote-code-execution-3phd</guid>
      <description>&lt;p&gt;On October 21, 2026, the PHP project disclosed two critical security vulnerabilities affecting PHP's core runtime.&lt;/p&gt;

&lt;p&gt;The vulnerabilities, tracked as &lt;strong&gt;CVE-2026-109618&lt;/strong&gt; and &lt;strong&gt;CVE-2026-109619&lt;/strong&gt;, received a CVSS v4.0 severity score of 9.2 out of 10.&lt;/p&gt;

&lt;p&gt;They are collectively known as &lt;strong&gt;PHP2Shell&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;What makes this incident particularly interesting for developers is that these are not ordinary application-level bugs. They involve PHP's internal memory management and error-handling paths, and successful exploitation can potentially lead to remote code execution without authentication.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore what the vulnerabilities are, how the underlying programming concepts work, and why a bug in a language runtime can affect applications across the web.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is PHP2Shell?
&lt;/h2&gt;

&lt;p&gt;PHP is a server-side programming language used by millions of websites and applications, including WordPress, Drupal and many custom web services.&lt;/p&gt;

&lt;p&gt;When a browser sends an HTTP request to a PHP application, the PHP runtime processes that request, executes the relevant application code and returns a response.&lt;/p&gt;

&lt;p&gt;A simplified request lifecycle 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;Incoming HTTP request
        |
        v
Web server / PHP runtime
        |
        v
Request initialization
        |
        v
Application code executes
        |
        v
Response returned
        |
        v
Request cleanup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PHP2Shell affects the internal request-processing lifecycle.&lt;/p&gt;

&lt;p&gt;The two vulnerabilities involve different failure scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CVE-2026-109618:&lt;/strong&gt; An out-of-memory condition can interrupt request processing while internal request data is only partially initialized.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CVE-2026-109619:&lt;/strong&gt; Internal request-related state can remain accessible after the memory backing it has been freed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Under the right conditions, these flaws can lead to memory corruption and remote code execution.&lt;/p&gt;

&lt;p&gt;An important detail is that the vulnerable processing can happen before the application's own PHP code runs. Consequently, an application-level login check cannot necessarily protect against these attack paths.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. CVE-2026-109618: Remote Code Execution Through Out-of-Memory Handling
&lt;/h2&gt;

&lt;p&gt;Let's start with the first vulnerability.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is an out-of-memory condition?
&lt;/h3&gt;

&lt;p&gt;Programs allocate memory to store data while they execute. PHP does this when processing request parameters, uploaded files and other input.&lt;/p&gt;

&lt;p&gt;PHP also enforces memory limits to prevent scripts from consuming unlimited resources.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;str_repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This attempts to create a string of approximately 100 MB.&lt;/p&gt;

&lt;p&gt;If the process cannot allocate the required memory, the operation may fail.&lt;/p&gt;

&lt;p&gt;A memory-allocation failure is not inherently a security vulnerability. Well-written software should handle such failures safely.&lt;/p&gt;

&lt;p&gt;The problem arises when the program enters an unexpected execution path and its internal state is no longer consistent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where does &lt;code&gt;longjmp()&lt;/code&gt; fit in?
&lt;/h3&gt;

&lt;p&gt;PHP is implemented primarily in C. C provides a mechanism called &lt;code&gt;setjmp()&lt;/code&gt; and &lt;code&gt;longjmp()&lt;/code&gt; that can save and restore execution contexts.&lt;/p&gt;

&lt;p&gt;In simplified terms, &lt;code&gt;longjmp()&lt;/code&gt; allows execution to jump back to an earlier saved point rather than returning normally through every intervening function.&lt;/p&gt;

&lt;p&gt;This can be useful for handling certain fatal errors, but it also makes control flow more complicated.&lt;/p&gt;

&lt;p&gt;Imagine a request being processed 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;Request initialization begins
        |
        v
PHP allocates internal request data
        |
        v
Memory allocation fails
        |
        v
Error handling interrupts normal execution
        |
        v
Control jumps to an earlier execution point
        |
        v
Request cleanup begins
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The critical question is: &lt;strong&gt;Was every piece of internal state initialized before cleanup began?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;According to the PHP advisory, the out-of-memory handling involved in CVE-2026-109618 can interrupt execution while internal request data is only partially initialized.&lt;/p&gt;

&lt;p&gt;Cleanup can then treat invalid or uninitialized data as if it were valid.&lt;/p&gt;

&lt;p&gt;This creates a memory-safety problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why can this become remote code execution?
&lt;/h3&gt;

&lt;p&gt;Memory corruption can have different consequences, depending on what memory is affected and how the program subsequently uses it.&lt;/p&gt;

&lt;p&gt;In some cases, it causes a crash. In others, carefully controlled corruption can influence program execution.&lt;/p&gt;

&lt;p&gt;Security researchers reported that this vulnerability can be exploited to achieve remote code execution under relevant conditions.&lt;/p&gt;

&lt;p&gt;The important distinction is that the issue is not simply that PHP runs out of memory. The security flaw is in &lt;strong&gt;how PHP handles the failure and subsequently processes its internal state&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. CVE-2026-109619: The Danger of Stale Memory
&lt;/h2&gt;

&lt;p&gt;The second vulnerability involves another classic memory-safety issue: using state after its backing memory has been freed.&lt;/p&gt;

&lt;p&gt;This is closely related to a concept known as &lt;em&gt;use-after-free&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is use-after-free?
&lt;/h3&gt;

&lt;p&gt;Imagine that a program allocates memory for an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program uses the buffer and eventually releases it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once memory has been freed, the program must not continue treating the old pointer as a valid reference to that allocation.&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 c"&gt;&lt;code&gt;&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/* Unsafe: buffer no longer points to a valid allocation */&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simplified illustration of unsafe memory access, not an exploit for PHP2Shell.&lt;/p&gt;

&lt;p&gt;The memory may be reused for something else. Reading or writing through the old pointer can therefore produce unpredictable behaviour, memory corruption or potentially exploitable conditions.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does this relate to PHP2Shell?
&lt;/h3&gt;

&lt;p&gt;PHP maintains internal state while processing requests.&lt;/p&gt;

&lt;p&gt;According to the PHP advisory, CVE-2026-109619 involves request-owned internal state surviving after the memory backing it has been freed.&lt;/p&gt;

&lt;p&gt;A later request handled by the same worker can encounter that stale state before normal initialization resets it.&lt;/p&gt;

&lt;p&gt;The simplified lifecycle is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request A begins
      |
      v
Internal state references allocated memory
      |
      v
Memory is released
      |
      v
A stale reference remains
      |
      v
Request B reaches the same worker
      |
      v
Stale state is used before initialization
      |
      v
Potential memory corruption
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actual execution paths are more complex than this diagram, but it illustrates the underlying problem.&lt;/p&gt;

&lt;p&gt;The key issue is not simply that memory was freed. It is that internal state remained capable of referring to memory whose lifetime had ended.&lt;/p&gt;

&lt;p&gt;Under the relevant conditions, researchers reported that this flaw can also lead to remote code execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Why Is This More Serious Than a Typical Website Bug?
&lt;/h2&gt;

&lt;p&gt;Most developers think about security at the application layer.&lt;/p&gt;

&lt;p&gt;For example, an application might check whether a user is authenticated before allowing access to a page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;isAuthenticated&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;http_response_code&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;exit&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 is a useful application-level control.&lt;/p&gt;

&lt;p&gt;However, application code cannot protect against every vulnerability in the software executing it.&lt;/p&gt;

&lt;p&gt;A vulnerable runtime may process an HTTP request before the request reaches the application's authentication logic.&lt;/p&gt;

&lt;p&gt;The distinction 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;Normal application attack

HTTP request
     |
     v
Application authentication
     |
     v
Application logic
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare that with a vulnerable runtime path:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Potential PHP2Shell attack path

HTTP request
     |
     v
PHP internal request processing
     |
     v
Memory-handling vulnerability
     |
     v
Potential code execution
     |
     v
Application code may never run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why fixing an application's login logic, validating its form fields or updating a WordPress plugin does not, by itself, fix a vulnerability in the underlying PHP runtime.&lt;/p&gt;

&lt;p&gt;PHP's security advisory identifies web-serving PHP installations as potentially affected, including PHP-FPM, Apache with mod_php, php-cgi, FrankenPHP and LiteSpeed. The exact exposure and exploitability depend on the deployment and vulnerability path.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. What Could an Attacker Achieve?
&lt;/h2&gt;

&lt;p&gt;Remote code execution means an attacker may be able to make a vulnerable process execute commands or code they control.&lt;/p&gt;

&lt;p&gt;The consequences depend on the permissions and environment in which PHP runs.&lt;/p&gt;

&lt;p&gt;Potential impacts include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access to sensitive files available to the PHP process.&lt;/li&gt;
&lt;li&gt;Exposure of application configuration and database credentials.&lt;/li&gt;
&lt;li&gt;Modification of website content or application files.&lt;/li&gt;
&lt;li&gt;Execution of additional malicious programs.&lt;/li&gt;
&lt;li&gt;Further compromise of systems accessible from the affected server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Successful exploitation does not automatically give an attacker unrestricted control of the entire machine. Operating-system permissions, container isolation and other controls still matter.&lt;/p&gt;

&lt;p&gt;Nevertheless, remote code execution in a web-facing runtime is a serious security issue because it can undermine the security boundaries of the application running on top of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Which PHP Versions Are Affected?
&lt;/h2&gt;

&lt;p&gt;The PHP project released security updates on October 21, 2026.&lt;/p&gt;

&lt;p&gt;The fixed releases are:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;PHP branch&lt;/th&gt;
&lt;th&gt;Fixed version&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PHP 8.5&lt;/td&gt;
&lt;td&gt;8.5.12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PHP 8.4&lt;/td&gt;
&lt;td&gt;8.4.27&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PHP 8.3&lt;/td&gt;
&lt;td&gt;8.3.36&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PHP 8.2&lt;/td&gt;
&lt;td&gt;8.2.35&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The vulnerabilities affect older PHP versions as well. PHP 8.1 and earlier are no longer supported by the upstream project, although some operating-system vendors may provide backported security fixes.&lt;/p&gt;

&lt;p&gt;If you manage PHP infrastructure, check your distribution's advisory as well as the version number. A vendor may backport a patch without changing the version string to match an upstream fixed release.&lt;/p&gt;

&lt;p&gt;Updating the PHP package is not enough if old worker processes continue running. The affected services must load the patched runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. What Should Developers and Security Teams Understand?
&lt;/h2&gt;

&lt;p&gt;There are three important lessons from PHP2Shell.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First, error-handling paths deserve the same security scrutiny as normal execution paths.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An allocation failure may seem like an availability problem, but incorrect cleanup or inconsistent state can turn it into a memory-safety vulnerability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second, memory lifetime is a security boundary.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A pointer that once referenced valid memory does not remain safe to use after that memory is released. Systems programming languages require developers to reason carefully about allocation, ownership and cleanup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Third, application security depends on the entire software stack.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Secure application code is important, but it runs on top of a runtime, operating system, web server and supporting libraries. Vulnerabilities at any of those layers can undermine protections implemented elsewhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Further Reading
&lt;/h2&gt;

&lt;p&gt;For the original technical details and remediation guidance, consult these sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.php.net/" rel="noopener noreferrer"&gt;Official PHP security advisory&lt;/a&gt; — the PHP project's announcement and fixed releases.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.wiz.io/blog/php2shell-php-rce" rel="noopener noreferrer"&gt;Wiz Research: PHP2Shell technical analysis&lt;/a&gt; — research on the vulnerabilities and their impact.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://securitylabs.datadoghq.com/articles/php2shell-critical-vulnerabilities-in-php/" rel="noopener noreferrer"&gt;Datadog Security Labs: PHP2Shell analysis&lt;/a&gt; — technical analysis, detection and remediation.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://cloud.google.com/blog/products/identity-security/responding-to-core-php-vulnerabilities-cve-2026-109618-cve-2026-109619" rel="noopener noreferrer"&gt;Google Cloud security analysis&lt;/a&gt; — vulnerability details and defensive guidance.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.ncsc.gov.uk/news/critical-security-issue-affecting-php" rel="noopener noreferrer"&gt;UK National Cyber Security Centre advisory&lt;/a&gt; — impact and mitigation guidance.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>security</category>
      <category>php</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>One Semicolon Changed My Rust Code: Expressions vs Statements</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Wed, 02 Sep 2026 05:27:05 +0000</pubDate>
      <link>https://dev.to/harshmangalam/one-semicolon-changed-my-rust-code-expressions-vs-statements-3iaf</link>
      <guid>https://dev.to/harshmangalam/one-semicolon-changed-my-rust-code-expressions-vs-statements-3iaf</guid>
      <description>&lt;p&gt;I added &lt;strong&gt;one semicolon&lt;/strong&gt; to my Rust code.&lt;/p&gt;

&lt;p&gt;It stopped compiling.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I wrote this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&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;Same calculation.&lt;/p&gt;

&lt;p&gt;Same numbers.&lt;/p&gt;

&lt;p&gt;Just one &lt;code&gt;;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And Rust basically said: &lt;strong&gt;Nope.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So what changed?&lt;/p&gt;

&lt;h2&gt;
  
  
  The missing piece: expressions
&lt;/h2&gt;

&lt;p&gt;In Rust, an &lt;strong&gt;expression produces a value&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 rust"&gt;&lt;code&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&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;15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So when Rust sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the last line is an expression.&lt;/p&gt;

&lt;p&gt;It produces an &lt;code&gt;i32&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And because it's the final expression in the function, that value becomes the function's result.&lt;/p&gt;

&lt;p&gt;No &lt;code&gt;return&lt;/code&gt; needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what did &lt;code&gt;;&lt;/code&gt; do?
&lt;/h2&gt;

&lt;p&gt;Adding the semicolon changes how that expression is used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&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 &lt;code&gt;a + b&lt;/code&gt; value isn't being used as the block's final value.&lt;/p&gt;

&lt;p&gt;But the function promised:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;blockquote&gt;
&lt;p&gt;"You promised me an &lt;code&gt;i32&lt;/code&gt;. Where is it?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And that's why this fails.&lt;/p&gt;

&lt;p&gt;The fix is surprisingly simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Remove the semicolon.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  And then Rust gets interesting
&lt;/h2&gt;

&lt;p&gt;Here's something I didn't expect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;discount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;discount&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The entire block produces a value.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;discount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;discount&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;evaluates to:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Which means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;block
  ↓
price - discount
  ↓
80
  ↓
result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the part that made the earlier function finally click for me.&lt;/p&gt;

&lt;p&gt;Rust isn't just about statements that execute one after another.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Expressions can produce values, and blocks can produce expressions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And suddenly things like &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;match&lt;/code&gt;, and function returns start making a lot more sense.&lt;/p&gt;

&lt;h3&gt;
  
  
  The takeaway
&lt;/h3&gt;

&lt;p&gt;When you see a Rust expression without a semicolon, don't automatically think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Someone forgot &lt;code&gt;;&lt;/code&gt;."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sometimes that missing semicolon is the whole point.&lt;/p&gt;

&lt;p&gt;🦀 &lt;strong&gt;In Rust, one tiny &lt;code&gt;;&lt;/code&gt; can change whether your code produces a value or simply performs an action.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>UPI at Scale: Handling Millions of Payments</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Thu, 13 Aug 2026 06:18:12 +0000</pubDate>
      <link>https://dev.to/harshmangalam/upi-at-scale-handling-millions-of-payments-47ah</link>
      <guid>https://dev.to/harshmangalam/upi-at-scale-handling-millions-of-payments-47ah</guid>
      <description>&lt;p&gt;Imagine this:&lt;/p&gt;

&lt;p&gt;It's salary day.&lt;/p&gt;

&lt;p&gt;It's 2 PM.&lt;/p&gt;

&lt;p&gt;Millions of people across India suddenly open their UPI apps and start paying rent, sending money to family, paying credit-card bills, and shopping online.&lt;/p&gt;

&lt;p&gt;Now here's the system-design interview question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;If millions of people make payments at almost exactly the same time, is every request hitting one central server? What prevents the entire payment system from freezing?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At first glance, it sounds like a scaling problem.&lt;/p&gt;

&lt;p&gt;It isn't &lt;em&gt;just&lt;/em&gt; a scaling problem.&lt;/p&gt;

&lt;p&gt;It's a combination of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;horizontal scaling&lt;/li&gt;
&lt;li&gt;concurrency&lt;/li&gt;
&lt;li&gt;distributed systems&lt;/li&gt;
&lt;li&gt;database consistency&lt;/li&gt;
&lt;li&gt;retries&lt;/li&gt;
&lt;li&gt;idempotency&lt;/li&gt;
&lt;li&gt;backpressure&lt;/li&gt;
&lt;li&gt;failure isolation&lt;/li&gt;
&lt;li&gt;downstream bottlenecks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's what makes payment systems such an interesting system-design problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  First: Don't Imagine One Giant UPI Server
&lt;/h2&gt;

&lt;p&gt;A common mental model 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;             Millions of users
                    |
                    v
             +-------------+
             | UPI Server  |
             +-------------+
                    |
                    v
                  Bank
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If that were literally true, we'd have a pretty serious problem.&lt;/p&gt;

&lt;p&gt;One machine cannot safely process the country's entire payment traffic.&lt;/p&gt;

&lt;p&gt;Instead, think about a distributed system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    Users
                      |
                      v
              +---------------+
              | API / Gateway |
              +---------------+
                 /    |    \
                /     |     \
               v      v      v
             [S1]   [S2]   [S3]
               |      |      |
               +------+------+
                      |
                Payment Services
                      |
             +--------+--------+
             |                 |
          Bank A             Bank B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact implementation of a real payment network is much more complicated than this diagram, but this is the right &lt;strong&gt;system-design mental model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The important idea is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The system is distributed across many machines and participating institutions.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 1: The First Problem — Traffic Spikes
&lt;/h2&gt;

&lt;p&gt;Let's take a concrete example.&lt;/p&gt;

&lt;p&gt;You want to pay your landlord:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;₹25,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the same moment, millions of other people are doing something similar.&lt;/p&gt;

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

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

100K requests/sec


Salary day:

████████████████████████
1M+ requests/sec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How do we handle the additional traffic?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Naive Solution: One Powerful Server
&lt;/h2&gt;

&lt;p&gt;We could buy a massive machine.&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/sec
                     |
                     v
             +---------------+
             | HUGE SERVER   |
             | 256 CPU cores |
             | 2 TB RAM      |
             +---------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called &lt;strong&gt;vertical scaling&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Make the machine bigger.&lt;/p&gt;

&lt;p&gt;But there are limits.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CPU        → limit
Memory     → limit
Network    → limit
Connections → limit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there's an even bigger problem.&lt;/p&gt;

&lt;p&gt;If the server dies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              💥
               |
               v
        +-------------+
        | One Server  |
        +-------------+

               |
               v

          Entire system
             DOWN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a payment system, that's unacceptable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Solution: Horizontal Scaling
&lt;/h2&gt;

&lt;p&gt;Instead of making one machine enormous, add more machines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    Requests
                       |
                       v
               +---------------+
               | Load Balancer |
               +---------------+
                 /     |     \
                /      |      \
               v       v       v
             [S1]    [S2]    [S3]
               |       |       |
             [S4]    [S5]    [S6]
               |       |       |
              ...     ...     ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If one server handles 20,000 requests/sec, and we need roughly 1 million:&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 / 20,000 ≈ 50 servers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can scale the application tier horizontally.&lt;/p&gt;

&lt;p&gt;Now 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;S1 💥

S2
S3
S4
S5
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Traffic can be routed to healthy servers.&lt;/p&gt;

&lt;p&gt;This gives us our first reusable system-design pattern:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Pattern #1: Horizontal Scaling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When request volume exceeds the capacity of one machine, distribute requests across many machines.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  But We've Created a New Problem
&lt;/h2&gt;

&lt;p&gt;Suppose two of your payments arrive simultaneously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Payment A:
Harsh → Landlord ₹25,000

Payment B:
Harsh → Amazon ₹20,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They might land on different 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
                   /          \
                  v            v
                [S1]          [S2]
                  |             |
                  +------?------+
                         |
                       Bank
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose Harsh has:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;₹30,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both servers read the balance at approximately the same time.&lt;/p&gt;

&lt;p&gt;Server 1 sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;₹30,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Server 2 sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;₹30,000
&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;S1:
₹30,000 - ₹25,000
= ₹5,000

S2:
₹30,000 - ₹20,000
= ₹10,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The system has effectively allowed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;₹45,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to be spent from an account containing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;₹30,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a &lt;strong&gt;race condition&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And this is where payment-system design gets interesting.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: We Need Atomic State Changes
&lt;/h2&gt;

&lt;p&gt;A payment isn't just:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Conceptually, we need something 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;Check balance
     ↓
Verify payment
     ↓
Debit sender
     ↓
Credit receiver
     ↓
Record transaction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The critical state transition must happen safely under concurrency.&lt;/p&gt;

&lt;p&gt;We need a guarantee that two competing operations cannot both incorrectly modify the same financial state.&lt;/p&gt;

&lt;p&gt;Depending on the architecture, this can involve:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;database transactions&lt;/li&gt;
&lt;li&gt;locking&lt;/li&gt;
&lt;li&gt;optimistic concurrency control&lt;/li&gt;
&lt;li&gt;serialization&lt;/li&gt;
&lt;li&gt;partition ownership&lt;/li&gt;
&lt;li&gt;carefully designed state machines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The important interview lesson isn't:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Use database locks."&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Identify the shared mutable state and protect the critical transition.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 3: Now the Database Becomes the Bottleneck
&lt;/h2&gt;

&lt;p&gt;Let's say we've successfully scaled our application servers.&lt;/p&gt;

&lt;p&gt;We now have:&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
                         |
          +--------------+--------------+
          |              |              |
         S1             S2             S3
          |              |              |
          +--------------+--------------+
                         |
                         v
                   +-----------+
                   | Database  |
                   +-----------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have 500 application servers.&lt;/p&gt;

&lt;p&gt;But one database.&lt;/p&gt;

&lt;p&gt;Now all those servers are fighting for the same resource.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;500 servers
     |
     |
     v
+-----------+
|    DB     |
|     💥    |
+-----------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application tier scales.&lt;/p&gt;

&lt;p&gt;The database doesn't.&lt;/p&gt;

&lt;p&gt;This is a classic distributed-systems bottleneck:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The fastest part of your system doesn't matter if a slower shared dependency limits the entire system.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 4: Partition the Work
&lt;/h2&gt;

&lt;p&gt;Instead of forcing everything through one database/resource, we can partition data and workload.&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;                 Payment Requests
                        |
                +-------+-------+
                |               |
                v               v
           Partition A     Partition B
                |               |
              DB-A            DB-B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The partitioning strategy could be based on something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;account ID
bank
customer ID
transaction domain
geographical region
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact choice depends on the system.&lt;/p&gt;

&lt;p&gt;This is generally called:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Sharding / Partitioning&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The reusable pattern is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Pattern #2: Partition the bottleneck&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When one resource can't handle the workload, divide the workload into independent partitions.&lt;/p&gt;




&lt;h2&gt;
  
  
  But There's Another Problem
&lt;/h2&gt;

&lt;p&gt;Imagine our application servers are perfectly healthy.&lt;/p&gt;

&lt;p&gt;Our databases are perfectly healthy.&lt;/p&gt;

&lt;p&gt;But a downstream bank suddenly becomes overloaded.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Payment Services
                /       |       \
               v        v        v
             Bank A   Bank B   Bank C
                                |
                                v
                              💥
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our system can process millions of incoming requests.&lt;/p&gt;

&lt;p&gt;But the downstream dependency might only safely process a smaller amount.&lt;/p&gt;

&lt;p&gt;This gives us another fundamental principle:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The capacity of a distributed system is constrained by its critical bottlenecks and dependencies.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can't solve a downstream bottleneck by simply adding more application servers.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5: Backpressure
&lt;/h2&gt;

&lt;p&gt;Suppose a component can safely process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100K operations/sec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but we're receiving:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;300K operations/sec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we blindly forward everything:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;300K
 |
 v
[Processor]
 |
 💥
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, for work that is safe to process asynchronously, we can introduce a buffer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;300K requests
      |
      v
  +-------+
  | Queue |
  +-------+
      |
      | 100K/sec
      v
  +-----------+
  | Processor |
  +-----------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The queue absorbs temporary bursts.&lt;/p&gt;

&lt;p&gt;This gives us:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Pattern #3: Backpressure&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When producers can generate work faster than consumers can process it, slow down producers or buffer the work.&lt;/p&gt;

&lt;p&gt;This is where technologies such as Kafka or other messaging systems become useful.&lt;/p&gt;

&lt;p&gt;But notice the reasoning.&lt;/p&gt;

&lt;p&gt;We didn't start with:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Let's use Kafka."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We started with:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Our producer is faster than our consumer. We need buffering/backpressure."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Then&lt;/strong&gt; we choose a technology.&lt;/p&gt;




&lt;h2&gt;
  
  
  Can We Queue the Entire Payment?
&lt;/h2&gt;

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

&lt;p&gt;This is an important interview trap.&lt;/p&gt;

&lt;p&gt;You shouldn't say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"We'll just put every payment into Kafka."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Financial transactions have correctness and latency requirements.&lt;/p&gt;

&lt;p&gt;There is a difference between:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Critical payment state transition
&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 plaintext"&gt;&lt;code&gt;Side effects
&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 plaintext"&gt;&lt;code&gt;Payment
  |
  +----&amp;gt; Update financial state
  |
  +----&amp;gt; Send notification
  |
  +----&amp;gt; Update analytics
  |
  +----&amp;gt; Generate receipt
  |
  +----&amp;gt; Update recommendation system
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The financial state transition may require strict correctness.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"₹25,000 paid successfully"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to a notification service doesn't necessarily need to block the core transaction.&lt;/p&gt;

&lt;p&gt;Those side effects can often be asynchronous.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 6: Now Imagine the Network Fails
&lt;/h2&gt;

&lt;p&gt;Here's where payment systems become really interesting.&lt;/p&gt;

&lt;p&gt;You press:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PAY ₹25,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The request reaches the payment system.&lt;/p&gt;

&lt;p&gt;The money gets debited.&lt;/p&gt;

&lt;p&gt;But before the response reaches your phone:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bank → 💥 network timeout 💥 → Phone
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your app says:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



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

&lt;blockquote&gt;
&lt;p&gt;"Okay, I'll try again."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So you press Pay again.&lt;/p&gt;

&lt;p&gt;Now the system receives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Payment #1
₹25,000

Payment #2
₹25,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the system treats both as new payments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Your account
   |
   +-- ₹25,000
   |
   +-- ₹25,000
   |
   v
₹50,000 debited
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's obviously unacceptable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 7: Idempotency
&lt;/h2&gt;

&lt;p&gt;We need the system to recognize:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"This retry is actually the same payment."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So the client/request can carry a unique identifier:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;First request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ABC123 → process payment
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ABC123 → already processed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The system returns the previous result rather than charging the user again.&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;Request
   |
   v
+----------------------+
| payment_id = ABC123  |
+----------------------+
          |
          v
    Already exists?
       /       \
     YES        NO
      |          |
      v          v
Return result  Process
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is &lt;strong&gt;idempotency&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And it is one of the most important concepts in distributed systems.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Pattern #4: Idempotency&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A retry of the same logical operation should not produce an additional side effect.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You will see this pattern everywhere:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;payments&lt;/li&gt;
&lt;li&gt;order creation&lt;/li&gt;
&lt;li&gt;payment webhooks&lt;/li&gt;
&lt;li&gt;message processing&lt;/li&gt;
&lt;li&gt;API retries&lt;/li&gt;
&lt;li&gt;distributed jobs&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 8: Why Retries Are Dangerous
&lt;/h2&gt;

&lt;p&gt;Retries sound harmless:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request failed
      ↓
Retry
      ↓
Retry
      ↓
Retry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But imagine 1 million clients doing this.&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
                   |
                failure
                   |
             +-----+-----+
             |           |
           retry       retry
             |           |
             +-----+-----+
                   |
                 retry
                   |
                   v
               💥💥💥
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A failing system can actually make itself &lt;strong&gt;more overloaded through retries&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is called a &lt;strong&gt;retry storm&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So production systems need things like:&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;timeouts&lt;/li&gt;
&lt;li&gt;circuit breakers&lt;/li&gt;
&lt;li&gt;idempotency&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Again, each mechanism exists because a specific failure mode exists.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Bigger Picture
&lt;/h2&gt;

&lt;p&gt;At this point our system is evolving.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
 |
 v
Server
 |
 v
Bank
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And progressively discovered problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Traffic spike
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                Load Balancer
               /     |      \
             S1      S2      S3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Shared state
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Multiple servers
      |
      v
Concurrency control
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Database bottleneck
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      |
  partition
   /      \
 DB-A    DB-B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Downstream overload
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Producer
   |
 Queue / Backpressure
   |
Consumer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Network failure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   |
 timeout
   |
 retry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Retry duplication
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;payment_id
     |
idempotency
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our architecture is now becoming:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                         Users
                           |
                           v
                    +-------------+
                    |   Gateway   |
                    +-------------+
                           |
                    Load balancing
                           |
             +-------------+-------------+
             |             |             |
            S1            S2            S3
             |             |             |
             +-------------+-------------+
                           |
                    Payment Service
                           |
                    +------+------+
                    |             |
               Partition A   Partition B
                    |             |
                   DB-A          DB-B
                    |
             Critical transaction
                    |
              +-----+------+
              |            |
          Async work    Notifications
              |
            Queue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And around the entire system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;timeouts
retries
idempotency
rate limits
backpressure
monitoring
failure isolation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  The Most Important Lesson
&lt;/h2&gt;

&lt;p&gt;When you're asked:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Design a UPI-scale payment system."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Don't start drawing 20 boxes.&lt;/p&gt;

&lt;p&gt;Start asking:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What's the workload?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Requests/sec?
Peak requests/sec?
Average transaction size?
Read/write ratio?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. What's the critical state?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Account balance
Transaction status
Payment ID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Where is concurrency dangerous?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Two payments touching the same account
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. What's the bottleneck?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application?
Database?
Network?
Downstream bank?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. What happens during failure?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Timeout
Crash
Duplicate request
Partial success
Network partition
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. What happens when traffic suddenly increases?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Scale
Buffer
Rate limit
Backpressure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. What happens when the client retries?
&lt;/h2&gt;



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

&lt;/div&gt;



&lt;p&gt;This is the real system-design thought process.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Reusable Pattern Library
&lt;/h2&gt;

&lt;p&gt;By solving this one problem, we've already learned several patterns.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Pattern&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;One server can't handle traffic&lt;/td&gt;
&lt;td&gt;Horizontal scaling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared state modified concurrently&lt;/td&gt;
&lt;td&gt;Atomicity / concurrency control&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;One DB becomes bottleneck&lt;/td&gt;
&lt;td&gt;Sharding / partitioning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Producer faster than consumer&lt;/td&gt;
&lt;td&gt;Queue / backpressure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Request repeated after timeout&lt;/td&gt;
&lt;td&gt;Idempotency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dependency becomes slow/unavailable&lt;/td&gt;
&lt;td&gt;Timeout / circuit breaker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Huge traffic spike&lt;/td&gt;
&lt;td&gt;Rate limiting / load shedding&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Temporary downstream failure&lt;/td&gt;
&lt;td&gt;Retry + backoff&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And this is exactly how you should prepare for SDE-2 system design.&lt;/p&gt;

&lt;p&gt;Don't memorize architectures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memorize the problem → failure → pattern relationship.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  One Final Mental Model
&lt;/h2&gt;

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

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Millions of requests arrive simultaneously."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&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;                 HUGE TRAFFIC
                      |
                      v
              Can one machine?
                   /     \
                 NO       YES
                 |
                 v
        Horizontal scaling
                 |
                 v
        What's shared?
                 |
                 v
        Concurrency problem?
                 |
                 v
        Protect state transition
                 |
                 v
        What's the bottleneck?
                 |
                 v
        Partition / scale
                 |
                 v
       What if dependency slows?
                 |
                 v
        Backpressure / queue
                 |
                 v
       What if request retries?
                 |
                 v
             Idempotency
                 |
                 v
          What if it fails?
                 |
                 v
       Timeout / retry / recovery
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the mental model I'd want an SDE-2 candidate to have.&lt;/p&gt;

&lt;p&gt;The interesting part of this problem isn't &lt;strong&gt;"How does UPI handle 1,000+ crore transactions?"&lt;/strong&gt;&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;How do you build a distributed system where massive concurrency, partial failures, retries, and bottlenecks don't turn a single ₹25,000 payment into a financial disaster?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And once you understand that, you can reuse the same reasoning for &lt;strong&gt;Cloudflare rate limiting, ticket booking, food delivery, Uber, payment gateways, distributed job systems, and almost every large-scale backend system.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>backend</category>
      <category>interview</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What Really Happens When 25 Lakh People Click “Book” at the Same Time?</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Wed, 12 Aug 2026 06:17:47 +0000</pubDate>
      <link>https://dev.to/harshmangalam/what-really-happens-when-25-lakh-people-click-book-at-the-same-time-4dl1</link>
      <guid>https://dev.to/harshmangalam/what-really-happens-when-25-lakh-people-click-book-at-the-same-time-4dl1</guid>
      <description>&lt;p&gt;Imagine this.&lt;/p&gt;

&lt;p&gt;Tatkal booking opens at &lt;strong&gt;10:00 AM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At exactly 10:00:00, around &lt;strong&gt;25 lakh people&lt;/strong&gt; click the &lt;strong&gt;Book Now&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;There are only &lt;strong&gt;4 seats left&lt;/strong&gt; in your coach.&lt;/p&gt;

&lt;p&gt;So the obvious question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How does the system decide which 4 people get the seats?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Is it literally first-come-first-served at the millisecond level?&lt;/p&gt;

&lt;p&gt;And what happens to the other 24,99,996 requests?&lt;/p&gt;

&lt;p&gt;This is a great system design problem because it teaches several important concepts at once:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;load balancing&lt;/li&gt;
&lt;li&gt;rate limiting&lt;/li&gt;
&lt;li&gt;stateless services&lt;/li&gt;
&lt;li&gt;race conditions&lt;/li&gt;
&lt;li&gt;atomic operations&lt;/li&gt;
&lt;li&gt;queues&lt;/li&gt;
&lt;li&gt;hot keys&lt;/li&gt;
&lt;li&gt;partitioning&lt;/li&gt;
&lt;li&gt;backpressure&lt;/li&gt;
&lt;li&gt;source of truth&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s build the system slowly, one problem at a time.&lt;/p&gt;




&lt;h2&gt;
  
  
  First, forget the database
&lt;/h2&gt;

&lt;p&gt;A beginner might imagine the system 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;25 lakh users
      |
      v
   Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That would be a disaster.&lt;/p&gt;

&lt;p&gt;A real system usually has 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
  |
  v
Internet
  |
  v
Edge / CDN / WAF
  |
  v
Load Balancer
  |
  v
API Servers
  |
  v
Booking Service
  |
  v
Inventory Service
  |
  v
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each layer exists because the simpler design eventually breaks.&lt;/p&gt;

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




&lt;h2&gt;
  
  
  1. Who actually clicked first?
&lt;/h2&gt;

&lt;p&gt;Suppose two users click:&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 clicks at 10:00:00.001
User B clicks at 10:00:00.009
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks like User A should win.&lt;/p&gt;

&lt;p&gt;But now include network latency.&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 network latency = 100 ms
User B network latency = 20 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Their requests reach the booking system at:&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 → 10:00:00.101
User B → 10:00:00.029
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;User B reaches the server first.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;The system usually cannot guarantee ordering based on the exact physical moment someone clicked.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The user's laptop is outside the system's control.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why not send the click timestamp?
&lt;/h2&gt;

&lt;p&gt;You might think the browser could send:&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;"clicked_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"10:00:00.001"&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;But clients cannot be trusted.&lt;/p&gt;

&lt;p&gt;A modified browser could simply send:&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;"clicked_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"09:59:59.000"&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;So authoritative decisions should happen on the server.&lt;/p&gt;

&lt;p&gt;This is a useful rule far beyond ticket booking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Never trust the client for critical state.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Price
Permissions
Wallet balance
Inventory
Seat availability
Booking status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. The first problem is not seats
&lt;/h2&gt;

&lt;p&gt;Before worrying about the 4 seats, there is a more immediate problem:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;25 lakh requests just arrived.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If all of them enter your backend, your servers may collapse before anyone gets a ticket.&lt;/p&gt;

&lt;p&gt;So the first layer protects 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;25 lakh users
      |
      v
+----------------------+
| Edge / Cloudflare    |
|----------------------|
| Rate limiting        |
| DDoS protection      |
| Bot detection        |
| Traffic filtering    |
+----------+-----------+
           |
           v
        Backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called &lt;strong&gt;admission control&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Admission control: don't accept unlimited work
&lt;/h2&gt;

&lt;p&gt;Imagine a nightclub with capacity for 500 people.&lt;/p&gt;

&lt;p&gt;There are 20,000 people waiting outside.&lt;/p&gt;

&lt;p&gt;You would not allow all 20,000 inside and then decide what to do.&lt;/p&gt;

&lt;p&gt;There is a bouncer.&lt;/p&gt;

&lt;p&gt;In distributed systems, the "bouncer" could be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rate limiting
Bot protection
Concurrency limits
Queues
Load shedding
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The principle is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Protect expensive downstream systems by limiting how much work enters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This pattern appears everywhere:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;flash sales&lt;/li&gt;
&lt;li&gt;online exams&lt;/li&gt;
&lt;li&gt;ticket launches&lt;/li&gt;
&lt;li&gt;gaming events&lt;/li&gt;
&lt;li&gt;IPO applications&lt;/li&gt;
&lt;li&gt;payment systems&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Then comes the load balancer
&lt;/h2&gt;

&lt;p&gt;One server cannot handle millions of requests.&lt;/p&gt;

&lt;p&gt;So we run many API 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
                     |
        ---------------------------
        |            |            |
        v            v            v
      API-1        API-2        API-100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The load balancer spreads requests across them.&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;horizontal scaling&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of buying one giant server, we add more servers.&lt;/p&gt;

&lt;p&gt;If one server handles roughly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5,000 requests/second
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then 100 servers could theoretically handle around:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;500,000 requests/second
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Actual capacity depends on the workload, but the pattern is what matters.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. API servers should be stateless
&lt;/h2&gt;

&lt;p&gt;Here is an important mistake.&lt;/p&gt;

&lt;p&gt;Suppose API Server 1 stores this 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;availableSeats = 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And API Server 2 also stores:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Now both servers might independently sell those four seats.&lt;/p&gt;

&lt;p&gt;That is obviously wrong.&lt;/p&gt;

&lt;p&gt;So critical shared state should not live independently inside each API server.&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;API-1 -----\
API-2 ------\
API-3 -------&amp;gt; Shared Inventory Service
API-4 ------/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The API servers are mostly &lt;strong&gt;stateless&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This makes scaling easier because any request can go to any server.&lt;/p&gt;

&lt;p&gt;If Server 27 crashes, the load balancer simply routes traffic elsewhere.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;Stateless compute is easy to scale. Shared mutable state is where distributed systems become difficult.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  6. Now we reach the real problem: race conditions
&lt;/h2&gt;

&lt;p&gt;Suppose only one seat remains.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Two servers receive booking requests almost simultaneously.&lt;/p&gt;

&lt;p&gt;Server A does:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;It sees:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Server B also does:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;It also sees:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Both then say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;seat is available
book it
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now one seat has been sold to two users.&lt;/p&gt;

&lt;p&gt;This is a &lt;strong&gt;race condition&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The timeline 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;Time →

Server A    READ seats = 1
Server B        READ seats = 1
Server A            WRITE seats = 0
Server B                WRITE seats = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both requests believed they succeeded.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. The fix: atomic operations
&lt;/h2&gt;

&lt;p&gt;The mistake above is that we did this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;READ
CHECK
WRITE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as separate steps.&lt;/p&gt;

&lt;p&gt;Instead, the check and update should behave like one indivisible operation.&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;IF seats &amp;gt; 0
THEN seats = seats - 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One database implementation might look 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;UPDATE&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;available_seats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;available_seats&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;train_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;AND&lt;/span&gt; &lt;span class="n"&gt;available_seats&lt;/span&gt; &lt;span class="o"&gt;&amp;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;Suppose one seat remains.&lt;/p&gt;

&lt;p&gt;Request A executes first:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Success.&lt;/p&gt;

&lt;p&gt;Request B executes next.&lt;/p&gt;

&lt;p&gt;The condition:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



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

&lt;p&gt;So Request B fails.&lt;/p&gt;

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

&lt;p&gt;This property is called &lt;strong&gt;atomicity&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Either the whole operation happens or none of it happens.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  8. Great, so let 25 lakh requests hit this SQL query?
&lt;/h2&gt;

&lt;p&gt;Not so fast.&lt;/p&gt;

&lt;p&gt;The query may be logically correct, but there is another problem.&lt;/p&gt;

&lt;p&gt;All 25 lakh users want the same resource.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;25 lakh requests
        |
        v
+----------------------+
| Train 12952          |
| Tatkal 3A            |
| available_seats = 4  |
+----------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This single inventory record becomes extremely hot.&lt;/p&gt;

&lt;p&gt;This is called a:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hot row&lt;/li&gt;
&lt;li&gt;hot key&lt;/li&gt;
&lt;li&gt;hotspot&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even if you have 10,000 application servers, they may all eventually fight over the same inventory record.&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 API servers
        |
        v
 same inventory row
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This teaches an important lesson:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Scaling your application servers does not automatically scale shared state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You see the same problem in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;concert tickets&lt;/li&gt;
&lt;li&gt;Amazon flash sales&lt;/li&gt;
&lt;li&gt;limited sneaker drops&lt;/li&gt;
&lt;li&gt;coupon redemption&lt;/li&gt;
&lt;li&gt;wallet balances&lt;/li&gt;
&lt;li&gt;stock trading&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  9. Use a queue to absorb the burst
&lt;/h2&gt;

&lt;p&gt;Instead of directly hammering the inventory system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;25 lakh requests
       |
       v
Inventory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can place a queue in front:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;25 lakh requests
       |
       v
+----------------+
| Booking Queue  |
+-------+--------+
        |
        v
     Workers
        |
        v
    Inventory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the huge incoming burst can be absorbed temporarily.&lt;/p&gt;

&lt;p&gt;Requests may enter the queue as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;R1
R2
R3
R4
R5
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose four seats exist.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;R1 → seat → 3 left
R2 → seat → 2 left
R3 → seat → 1 left
R4 → seat → 0 left
R5 → sold out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Huge uncontrolled concurrency
&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;Controlled processing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  10. Why queues are useful
&lt;/h2&gt;

&lt;p&gt;Queues decouple two different rates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rate at which requests arrive
&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 plaintext"&gt;&lt;code&gt;Rate at which the backend can process requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without a queue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Traffic spike
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; DATABASE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With a queue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Traffic spike
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;

        |
        v

+----------------------+
|        Queue         |
| |||||||||||||||||||  |
+----------+-----------+
           |
           v

       Workers

----&amp;gt; ----&amp;gt; ----&amp;gt; Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is also a form of &lt;strong&gt;backpressure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The downstream system says, in effect:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I will process work at the speed I can safely handle.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  11. Should there be one global queue?
&lt;/h2&gt;

&lt;p&gt;Probably not.&lt;/p&gt;

&lt;p&gt;Imagine one queue for every booking in India.&lt;/p&gt;

&lt;p&gt;Then a Delhi–Mumbai booking could block a completely unrelated Chennai–Bengaluru booking.&lt;/p&gt;

&lt;p&gt;That wastes parallelism.&lt;/p&gt;

&lt;p&gt;Instead, we partition the work.&lt;/p&gt;

&lt;p&gt;A possible inventory key could be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;train_id + journey_date + class + quota
&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 plaintext"&gt;&lt;code&gt;12952:2026-08-20:3A:TATKAL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All requests for the same inventory key should follow the same ordering path.&lt;/p&gt;

&lt;p&gt;Different trains can be processed independently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Train A → Partition 1 → Worker 1
Train B → Partition 2 → Worker 2
Train C → Partition 3 → Worker 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is &lt;strong&gt;partitioning&lt;/strong&gt; or &lt;strong&gt;sharding&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  12. The magic idea: serialize only conflicting work
&lt;/h2&gt;

&lt;p&gt;This is one of the most reusable ideas in system design.&lt;/p&gt;

&lt;p&gt;We do not need to serialize every booking in the country.&lt;/p&gt;

&lt;p&gt;We only need to serialize requests that compete for the same inventory.&lt;/p&gt;

&lt;p&gt;So 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;Every booking
    |
    v
one global worker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we do something 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;Same train/date/class/quota
          |
          v
   ordered processing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;while unrelated inventory runs in parallel.&lt;/p&gt;

&lt;p&gt;A good interview phrase is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Serialize operations per inventory key, not globally.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That gives us both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Correctness
+
Parallelism
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  13. So which four people actually win?
&lt;/h2&gt;

&lt;p&gt;Now we can finally answer the original question.&lt;/p&gt;

&lt;p&gt;The system may create an authoritative order somewhere inside its infrastructure.&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 B → sequence 91821
User X → sequence 91822
User A → sequence 91823
User Z → sequence 91824
User P → sequence 91825
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are four seats.&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;91821 → gets seat
91822 → gets seat
91823 → gets seat
91824 → gets seat
91825 → sold out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact real-world click ordering may be impossible to know perfectly.&lt;/p&gt;

&lt;p&gt;What matters is that the system defines one reliable ordering at a controlled point.&lt;/p&gt;

&lt;p&gt;This leads to a broader distributed systems idea:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Sometimes we create an authoritative ordering instead of trying to discover the absolute real-world ordering.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  14. What happens to the other 24,99,996 requests?
&lt;/h2&gt;

&lt;p&gt;We ideally do &lt;strong&gt;not&lt;/strong&gt; let every losing request perform an expensive database transaction.&lt;/p&gt;

&lt;p&gt;Once the system confidently knows:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;future work can often be rejected earlier.&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;User
 |
 v
API
 |
 v
Queue
 |
 v
Worker
 |
 v
Database
 |
 v
Sold out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we may eventually do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
 |
 v
Booking Service
 |
 v
Sold out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called &lt;strong&gt;fail fast&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If an operation clearly cannot succeed, reject it as early and cheaply as possible.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;ul&gt;
&lt;li&gt;CPU&lt;/li&gt;
&lt;li&gt;database connections&lt;/li&gt;
&lt;li&gt;queue capacity&lt;/li&gt;
&lt;li&gt;network calls&lt;/li&gt;
&lt;li&gt;locks&lt;/li&gt;
&lt;li&gt;memory&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  15. But be careful with caching
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Tatkal inventory = SOLD OUT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is useful for fast rejection.&lt;/p&gt;

&lt;p&gt;But what happens if someone fails payment?&lt;/p&gt;

&lt;p&gt;The seat might become available again.&lt;/p&gt;

&lt;p&gt;So we need to distinguish:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source of truth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A cache is fast, but it may be stale.&lt;/p&gt;

&lt;p&gt;The authoritative inventory system decides the truth.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;Use caches to make the system faster, not to accidentally create a second source of truth.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  16. Booking is not the same as confirmation
&lt;/h2&gt;

&lt;p&gt;There is another important complication.&lt;/p&gt;

&lt;p&gt;Imagine four users get seats, but payment takes two minutes.&lt;/p&gt;

&lt;p&gt;Should those seats be permanently gone immediately?&lt;/p&gt;

&lt;p&gt;Usually you need a temporary reservation.&lt;/p&gt;

&lt;p&gt;The booking may move through states like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AVAILABLE
    |
    v
HELD
    |
    v
PAYMENT_PENDING
    |
    +-----------+
    |           |
    v           v
CONFIRMED     FAILED
                |
                v
            AVAILABLE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;4 seats

A → held
B → held
C → held
D → held

available = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A payment succeeds → confirmed

B payment fails → seat released

C succeeds → confirmed

D times out → seat released
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This introduces several new system design topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TTLs&lt;/li&gt;
&lt;li&gt;reservation expiry&lt;/li&gt;
&lt;li&gt;state machines&lt;/li&gt;
&lt;li&gt;retries&lt;/li&gt;
&lt;li&gt;payment failures&lt;/li&gt;
&lt;li&gt;compensation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are natural follow-ups once the basic booking flow is correct.&lt;/p&gt;




&lt;h2&gt;
  
  
  17. Final high-level architecture
&lt;/h2&gt;

&lt;p&gt;Putting 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;                       USERS
                         |
                         v
               +-------------------+
               | Edge / Cloudflare |
               |-------------------|
               | DDoS protection   |
               | Rate limiting     |
               | Bot detection     |
               +---------+---------+
                         |
                         v
               +-------------------+
               |   Load Balancer   |
               +---------+---------+
                         |
          +--------------+--------------+
          |              |              |
          v              v              v
       API-1           API-2          API-N
          \              |              /
           \             |             /
            +------------+------------+
                         |
                         v
               +-------------------+
               | Booking Service   |
               +---------+---------+
                         |
                         v
               +-------------------+
               | Booking Queue     |
               | partitioned by    |
               | inventory key     |
               +---------+---------+
                         |
             +-----------+-----------+
             |           |           |
             v           v           v
          Worker 1    Worker 2    Worker N
             |           |           |
             +-----------+-----------+
                         |
                         v
               +-------------------+
               | Inventory Service |
               +---------+---------+
                         |
                         v
               +-------------------+
               |    Database       |
               | Source of Truth   |
               +-------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  18. What did we actually learn?
&lt;/h2&gt;

&lt;p&gt;The interesting part is not memorizing the architecture.&lt;/p&gt;

&lt;p&gt;The important part is understanding &lt;strong&gt;why each component appeared&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Pattern&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Millions of incoming requests&lt;/td&gt;
&lt;td&gt;Horizontal scaling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backend may collapse&lt;/td&gt;
&lt;td&gt;Admission control&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bots generate unfair traffic&lt;/td&gt;
&lt;td&gt;Rate limiting / bot protection&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Many API servers&lt;/td&gt;
&lt;td&gt;Stateless services&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Two users can get the same seat&lt;/td&gt;
&lt;td&gt;Atomicity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Millions hit the same inventory&lt;/td&gt;
&lt;td&gt;Hotspot recognition&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Huge burst at 10 AM&lt;/td&gt;
&lt;td&gt;Queue&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backend slower than incoming traffic&lt;/td&gt;
&lt;td&gt;Backpressure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Independent trains should run separately&lt;/td&gt;
&lt;td&gt;Partitioning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same inventory needs ordering&lt;/td&gt;
&lt;td&gt;Per-key serialization&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inventory already exhausted&lt;/td&gt;
&lt;td&gt;Fail fast&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repeated reads are expensive&lt;/td&gt;
&lt;td&gt;Caching&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;System needs one correct answer&lt;/td&gt;
&lt;td&gt;Source of truth&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  19. The most important system design habit
&lt;/h2&gt;

&lt;p&gt;Do not start with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;We need Kafka.
We need Redis.
We need Cassandra.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What problem do I have?
        |
        v
Why does it happen?
        |
        v
What is the simplest solution?
        |
        v
What breaks at scale?
        |
        v
What guarantee do I need?
        |
        v
Which system design pattern gives me that guarantee?
        |
        v
What new tradeoff did I introduce?
&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 plaintext"&gt;&lt;code&gt;25 lakh requests
    ↓
backend overload
    ↓
admission control
&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;traffic arrives faster than backend processes
    ↓
queue
&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;multiple servers update same seat
    ↓
race condition
    ↓
atomic operation
&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;everyone hits same inventory
    ↓
hotspot
    ↓
partition + serialize per key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That way, the architecture becomes a consequence of the problem instead of something you memorize.&lt;/p&gt;




&lt;h2&gt;
  
  
  20. The one sentence to remember
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The hard part is not receiving 25 lakh requests. The hard part is safely coordinating millions of concurrent requests around a tiny amount of shared mutable state.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once this idea makes sense, a lot of other systems start looking familiar:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;concert ticketing&lt;/li&gt;
&lt;li&gt;hotel bookings&lt;/li&gt;
&lt;li&gt;airline seats&lt;/li&gt;
&lt;li&gt;flash sales&lt;/li&gt;
&lt;li&gt;stock trades&lt;/li&gt;
&lt;li&gt;wallet balances&lt;/li&gt;
&lt;li&gt;coupon redemption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The domain changes.&lt;/p&gt;

&lt;p&gt;The underlying system design patterns repeat.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>distributedsystems</category>
      <category>backend</category>
      <category>beginners</category>
    </item>
    <item>
      <title>I Migrated a 61,000+ Line TypeScript Monorepo to Next.js 16.3 &amp; TypeScript 7 — The Results Surprised Me</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Thu, 06 Aug 2026 09:27:33 +0000</pubDate>
      <link>https://dev.to/harshmangalam/i-migrated-a-61000-line-typescript-monorepo-to-nextjs-163-typescript-7-the-results-2obl</link>
      <guid>https://dev.to/harshmangalam/i-migrated-a-61000-line-typescript-monorepo-to-nextjs-163-typescript-7-the-results-2obl</guid>
      <description>&lt;p&gt;Performance improvements in release notes are always nice to read, but I've learned to take them with a grain of salt.&lt;/p&gt;

&lt;p&gt;A benchmark on a small demo application rarely tells you how your production codebase will behave.&lt;/p&gt;

&lt;p&gt;So I decided to benchmark one of my own projects after upgrading from &lt;strong&gt;Next.js 16.1 + TypeScript 5.8&lt;/strong&gt; to &lt;strong&gt;Next.js 16.3 + TypeScript 7&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The repository isn't enormous by enterprise standards, but it's large enough to expose real-world bottlenecks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Overview
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;~61,000 lines of TypeScript&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Turborepo monorepo&lt;/li&gt;
&lt;li&gt;Multiple applications and shared packages&lt;/li&gt;
&lt;li&gt;Bun as the package manager/runtime&lt;/li&gt;
&lt;li&gt;Next.js frontend&lt;/li&gt;
&lt;li&gt;Shared internal packages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I measured:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cold type checking&lt;/li&gt;
&lt;li&gt;Cached type checking&lt;/li&gt;
&lt;li&gt;Development startup&lt;/li&gt;
&lt;li&gt;Production build&lt;/li&gt;
&lt;li&gt;Static page generation&lt;/li&gt;
&lt;li&gt;Build output size&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Upgrade Path
&lt;/h2&gt;

&lt;p&gt;One thing worth mentioning is that I couldn't jump directly to TypeScript 7.&lt;/p&gt;

&lt;p&gt;My starting point was:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Next.js 16.1&lt;/li&gt;
&lt;li&gt;TypeScript 5.8&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since &lt;strong&gt;Next.js 16.1 doesn't support TypeScript 7&lt;/strong&gt;, I first upgraded Next.js to &lt;strong&gt;16.3&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That migration was surprisingly smooth and required almost no code changes.&lt;/p&gt;

&lt;p&gt;Once that was complete, I upgraded the monorepo to &lt;strong&gt;TypeScript 7&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Current Ecosystem Caveat
&lt;/h2&gt;

&lt;p&gt;At the time of writing, &lt;strong&gt;typescript-eslint&lt;/strong&gt; still doesn't fully support TypeScript 7.&lt;/p&gt;

&lt;p&gt;That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;tsc&lt;/code&gt; uses TypeScript 7&lt;/li&gt;
&lt;li&gt;ESLint still performs its analysis using TypeScript 5.9&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's not a blocker, but it's something to be aware of if you upgrade today.&lt;/p&gt;




&lt;h2&gt;
  
  
  Benchmark 1 — Type Checking
&lt;/h2&gt;

&lt;p&gt;Cold run before upgrading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bun run typecheck

Tasks:    6 successful, 6 total
Cached:   0 cached
Time:     29.805s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After upgrading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bun run typecheck

Tasks:    6 successful, 6 total
Cached:   0 cached
Time:     9.296s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;TS 5.8&lt;/td&gt;
&lt;td&gt;29.8s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TS 7&lt;/td&gt;
&lt;td&gt;9.3s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That's roughly a &lt;strong&gt;3.2× improvement&lt;/strong&gt; for a cold type check.&lt;/p&gt;

&lt;p&gt;For a repository of this size, that's a significant reduction in feedback time.&lt;/p&gt;




&lt;h2&gt;
  
  
  Benchmark 2 — Turborepo Cache
&lt;/h2&gt;

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

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

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;Already fast, but still noticeably quicker.&lt;/p&gt;




&lt;h2&gt;
  
  
  Benchmark 3 — Development Startup
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bun run dev

Ready &lt;span class="k"&gt;in &lt;/span&gt;888ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bun run dev

Ready &lt;span class="k"&gt;in &lt;/span&gt;600ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not a massive difference, but enough to make restarting the dev server feel snappier.&lt;/p&gt;




&lt;h2&gt;
  
  
  Benchmark 4 — Production Build
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;time &lt;/span&gt;bun run build

real    0m52.044s
user    3m51.673s
sys     0m13.184s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;time &lt;/span&gt;bun run build

real    0m22.752s
user    0m58.419s
sys     0m8.791s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Build Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Before&lt;/td&gt;
&lt;td&gt;52.0s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After&lt;/td&gt;
&lt;td&gt;22.8s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That's approximately &lt;strong&gt;56% faster&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;One interesting observation is the CPU time.&lt;/p&gt;

&lt;p&gt;The previous build consumed almost four minutes of user CPU time.&lt;/p&gt;

&lt;p&gt;After upgrading, that dropped to under one minute, suggesting the build process itself became considerably more efficient.&lt;/p&gt;




&lt;h2&gt;
  
  
  Benchmark 5 — Static Generation
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Generating static pages (17/17) in 3.3s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Generating static pages (17/17) in 3.2s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Essentially unchanged.&lt;/p&gt;

&lt;p&gt;This suggests that most of the improvements came earlier in the build pipeline rather than during static page generation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Benchmark 6 — Output Size
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;du&lt;/span&gt; &lt;span class="nt"&gt;-sh&lt;/span&gt; .next

50M
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;du&lt;/span&gt; &lt;span class="nt"&gt;-sh&lt;/span&gt; .next

50M
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The build output remained identical.&lt;/p&gt;

&lt;p&gt;No regressions in generated artifacts.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Cold typecheck&lt;/td&gt;
&lt;td&gt;29.8s&lt;/td&gt;
&lt;td&gt;9.3s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cached typecheck&lt;/td&gt;
&lt;td&gt;259ms&lt;/td&gt;
&lt;td&gt;114ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dev startup&lt;/td&gt;
&lt;td&gt;888ms&lt;/td&gt;
&lt;td&gt;600ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Production build&lt;/td&gt;
&lt;td&gt;52.0s&lt;/td&gt;
&lt;td&gt;22.8s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Static generation&lt;/td&gt;
&lt;td&gt;3.3s&lt;/td&gt;
&lt;td&gt;3.2s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.next&lt;/code&gt; size&lt;/td&gt;
&lt;td&gt;50 MB&lt;/td&gt;
&lt;td&gt;50 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  My Observations
&lt;/h2&gt;

&lt;p&gt;A few things stood out during the migration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Next.js upgrade itself was uneventful.&lt;/li&gt;
&lt;li&gt;TypeScript 7 delivered the largest measurable improvement in day-to-day workflows.&lt;/li&gt;
&lt;li&gt;Production builds became dramatically faster.&lt;/li&gt;
&lt;li&gt;Output size remained unchanged.&lt;/li&gt;
&lt;li&gt;Turborepo caching continued to work flawlessly.&lt;/li&gt;
&lt;li&gt;The only missing piece today is full TypeScript 7 support in &lt;code&gt;typescript-eslint&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;I wasn't expecting a dramatic improvement.&lt;/p&gt;

&lt;p&gt;The biggest surprise was seeing a full monorepo type check drop from nearly &lt;strong&gt;30 seconds to just over 9 seconds&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Combined with a production build that finished in less than half the previous time, the upgrade noticeably improved the development experience without requiring significant application changes.&lt;/p&gt;

&lt;p&gt;If you're maintaining a medium or large TypeScript monorepo, it's worth running your own benchmarks after upgrading.&lt;/p&gt;

&lt;p&gt;Your numbers will differ, but the improvements may surprise you as much as they surprised me.&lt;/p&gt;




&lt;p&gt;Have you upgraded to TypeScript 7 yet? I'd be interested to hear how your build times compare.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>bunjs</category>
    </item>
    <item>
      <title>Your Go API Is Running—But Is It Healthy?</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Mon, 20 Jul 2026 07:08:11 +0000</pubDate>
      <link>https://dev.to/harshmangalam/your-go-api-is-running-but-is-it-healthy-2jgo</link>
      <guid>https://dev.to/harshmangalam/your-go-api-is-running-but-is-it-healthy-2jgo</guid>
      <description>&lt;p&gt;A Go server can start successfully and still be unable to handle real requests.&lt;/p&gt;

&lt;p&gt;The process may be running while PostgreSQL is down. The API is alive, but it cannot do its main job.&lt;/p&gt;

&lt;p&gt;That led me to a simple question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the server is running, is the application truly healthy?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is what I learned while building a useful health endpoint in Go.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a health endpoint?
&lt;/h2&gt;

&lt;p&gt;A health endpoint is a small HTTP endpoint, usually something like:&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;GET /health
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is mainly used by Docker, Kubernetes, load balancers, and monitoring tools. These systems call the endpoint and decide whether the application should receive traffic.&lt;/p&gt;

&lt;p&gt;Health checks are commonly split into three types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Liveness:&lt;/strong&gt; Is the application process running?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Readiness:&lt;/strong&gt; Can the application handle traffic right now?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Startup:&lt;/strong&gt; Has the application finished starting?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A liveness check should be simple. Restarting the application will not fix a database outage.&lt;/p&gt;

&lt;p&gt;The endpoint in this post is closer to a &lt;strong&gt;readiness check&lt;/strong&gt; because it checks PostgreSQL. If the database is unavailable, the application keeps running but reports that it is not ready for traffic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decide what the response should look like
&lt;/h2&gt;

&lt;p&gt;I wanted the response to answer three questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Is the whole application healthy?&lt;/li&gt;
&lt;li&gt;Which dependency failed?&lt;/li&gt;
&lt;li&gt;How long did each check take?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That gave me these two structs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Status&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;                 &lt;span class="s"&gt;`json:"status"`&lt;/span&gt;
    &lt;span class="n"&gt;Checks&lt;/span&gt;    &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="n"&gt;CheckResult&lt;/span&gt; &lt;span class="s"&gt;`json:"checks"`&lt;/span&gt;
    &lt;span class="n"&gt;Timestamp&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Time&lt;/span&gt;              &lt;span class="s"&gt;`json:"timestamp"`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;CheckResult&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Status&lt;/span&gt;     &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="s"&gt;`json:"status"`&lt;/span&gt;
    &lt;span class="n"&gt;Error&lt;/span&gt;      &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="s"&gt;`json:"error,omitempty"`&lt;/span&gt;
    &lt;span class="n"&gt;DurationMS&lt;/span&gt; &lt;span class="kt"&gt;int64&lt;/span&gt;  &lt;span class="s"&gt;`json:"duration_ms"`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Healthy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"healthy"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The JSON tags control the field names in the response. &lt;code&gt;omitempty&lt;/code&gt; hides the error field when there is no error. The small &lt;code&gt;Healthy&lt;/code&gt; method keeps the handler easy to read.&lt;/p&gt;

&lt;p&gt;When PostgreSQL is available, &lt;code&gt;/health&lt;/code&gt; returns &lt;code&gt;200 OK&lt;/code&gt;:&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="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"checks"&lt;/span&gt;&lt;span class="p"&gt;:&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;span class="nl"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;:&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;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;"up"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"duration_ms"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&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;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-07-20T10:30: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;When PostgreSQL is unavailable, it returns &lt;code&gt;503 Service Unavailable&lt;/code&gt;:&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;"unhealthy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"checks"&lt;/span&gt;&lt;span class="p"&gt;:&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;span class="nl"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;:&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;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;"down"&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;"ping postgres: connection refused"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"duration_ms"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;8&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;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-07-20T10:31: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 HTTP status is important. A tool should not need to read the JSON just to learn whether the application is ready.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give every dependency the same shape
&lt;/h2&gt;

&lt;p&gt;PostgreSQL may not be the only dependency forever. An API might later need Redis, Kafka, or another internal service.&lt;/p&gt;

&lt;p&gt;A small interface gives every health check the same shape:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Checker&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each checker provides a name for the response and returns an error when its dependency is unavailable.&lt;/p&gt;

&lt;p&gt;The PostgreSQL checker is small:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;PostgresChecker&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pgxpool&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;NewPostgresChecker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pgxpool&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;PostgresChecker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;PostgresChecker&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;PostgresChecker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"postgres"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;PostgresChecker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Ping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ping postgres: %w"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&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="no"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Ping&lt;/code&gt; asks the connection pool to contact PostgreSQL. We do not need a large SQL query. A health check should be fast, cheap, and safe to run often.&lt;/p&gt;

&lt;p&gt;Wrapping the error with &lt;code&gt;ping postgres&lt;/code&gt; adds useful context when something fails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run the checks safely
&lt;/h2&gt;

&lt;p&gt;The service has one job: run every checker and combine the results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Service&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;checkers&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;Checker&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;NewService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;checkers&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="n"&gt;Checker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Service&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;checkers&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="n"&gt;Checker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;checkers&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The constructor accepts any number of checkers and stores a copy of the list. The service begins each request with a healthy response and an empty map for the individual results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Status&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="s"&gt;"healthy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Checks&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="n"&gt;CheckResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;checkers&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="n"&gt;Timestamp&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTC&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;For each checker, it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creates a two-second timeout.&lt;/li&gt;
&lt;li&gt;Records the start time.&lt;/li&gt;
&lt;li&gt;Runs the check.&lt;/li&gt;
&lt;li&gt;Saves its status, error, and duration.&lt;/li&gt;
&lt;li&gt;Marks the whole response unhealthy if any check fails.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The important part looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;checkCtx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cancel&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WithTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;checker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;checkCtx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;CheckResult&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Status&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;     &lt;span class="s"&gt;"up"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;DurationMS&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Since&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;started&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Milliseconds&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"down"&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&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;Without a timeout, one stuck dependency could make &lt;code&gt;/health&lt;/code&gt; wait forever.&lt;/p&gt;

&lt;p&gt;The timeout is based on the incoming request context. If the caller disconnects, Go can cancel the remaining work instead of continuing a request nobody is waiting for.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why run checks concurrently?
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PostgreSQL: 100 ms
Redis:      200 ms
Kafka:      300 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running them one after another takes about &lt;code&gt;600 ms&lt;/code&gt;. Running them together takes about &lt;code&gt;300 ms&lt;/code&gt;, which is close to the slowest check.&lt;/p&gt;

&lt;p&gt;The service starts one goroutine for each checker and uses a &lt;code&gt;sync.WaitGroup&lt;/code&gt; to wait for all of them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;checker&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;checkers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;wg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;checker&lt;/span&gt; &lt;span class="n"&gt;Checker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;wg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Done&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="c"&gt;// Run the check and build its result.&lt;/span&gt;
    &lt;span class="p"&gt;}(&lt;/span&gt;&lt;span class="n"&gt;checker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;wg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Wait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The goroutines write to the same response map, so a &lt;code&gt;sync.Mutex&lt;/code&gt; protects those writes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Checks&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;checker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;()]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"unhealthy"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;mu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unlock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With only one dependency, concurrency does not make the endpoint faster. Its value appears when more checks are registered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turn the result into HTTP
&lt;/h2&gt;

&lt;p&gt;The handler runs the service, selects the status code, and writes JSON:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;ServeHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Healthy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusServiceUnavailable&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewEncoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&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;Finally, the checker, service, and handler are connected in &lt;code&gt;main.go&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;postgresChecker&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewPostgresChecker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;healthService&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;postgresChecker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;healthService&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One request now follows a clear path:&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
    -&amp;gt; handler
    -&amp;gt; health service
    -&amp;gt; dependency checkers
    -&amp;gt; JSON response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A few practical rules
&lt;/h2&gt;

&lt;p&gt;Health checks should stay lightweight. Avoid large queries, writes, or slow calls to external services.&lt;/p&gt;

&lt;p&gt;Health is also not the same as monitoring. A health endpoint answers, "Can this instance receive traffic?" Metrics, logs, and traces explain how the application performs and why failures happen.&lt;/p&gt;

&lt;p&gt;Be careful with error messages too. Detailed errors are helpful during development, but a public endpoint should not reveal passwords, connection strings, or other private information.&lt;/p&gt;

&lt;p&gt;For a small codebase, all of this can live in one &lt;code&gt;health.go&lt;/code&gt; file. Split it later when the file becomes harder to navigate, not simply because it might grow one day.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;p&gt;A useful health endpoint is more than a hard-coded &lt;code&gt;{"status":"ok"}&lt;/code&gt; response.&lt;/p&gt;

&lt;p&gt;It needs clear output, correct HTTP status codes, fast dependency checks, timeouts, and safe handling of multiple checks. It should also know its limit: health tells us whether the application is ready, while monitoring tells us what is happening over time.&lt;/p&gt;

&lt;p&gt;It is a small feature, but it makes an API much easier to operate and trust.&lt;/p&gt;

</description>
      <category>go</category>
      <category>webdev</category>
      <category>backend</category>
      <category>api</category>
    </item>
    <item>
      <title>From http.ServeMux to Dependency Injection in Go: A Beginner-Friendly Guide</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Fri, 17 Jul 2026 07:29:15 +0000</pubDate>
      <link>https://dev.to/harshmangalam/from-httpservemux-to-dependency-injection-in-go-a-beginner-friendly-guide-3kc3</link>
      <guid>https://dev.to/harshmangalam/from-httpservemux-to-dependency-injection-in-go-a-beginner-friendly-guide-3kc3</guid>
      <description>&lt;p&gt;When I first saw code like this, it felt as if too many things were happening in one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;healthService&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is a &lt;code&gt;mux&lt;/code&gt;? Why are we giving it a handler? How does the handler get the service? Who calls &lt;code&gt;ServeHTTP&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;This article answers those questions by starting with the smallest possible Go HTTP server and building toward a clean handler-and-service design one step at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The big idea
&lt;/h2&gt;

&lt;p&gt;An HTTP request moves through the application 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;GET /health
     │
     ▼
HTTP server
     │
     ▼
ServeMux
     │
     ▼
Health handler
     │
     ▼
Health service
     │
     ▼
Dependency checker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each part has one job:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The HTTP server receives requests.&lt;/li&gt;
&lt;li&gt;The mux decides which handler should receive each request.&lt;/li&gt;
&lt;li&gt;The handler translates between HTTP and our application.&lt;/li&gt;
&lt;li&gt;The service performs the application logic.&lt;/li&gt;
&lt;li&gt;A checker knows how to inspect one dependency, such as PostgreSQL.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let us build this structure gradually.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: The smallest useful mux and handler
&lt;/h2&gt;

&lt;p&gt;Go provides an HTTP router called &lt;code&gt;http.ServeMux&lt;/code&gt;. A router matches an incoming request with the code that should handle it.&lt;/p&gt;

&lt;p&gt;The method-and-path pattern used below, such as &lt;code&gt;"GET /health"&lt;/code&gt;, requires Go 1.22 or newer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fprintln&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"healthy"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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;There are three important lines here.&lt;/p&gt;

&lt;p&gt;First, we create a mux:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, we register a function for &lt;code&gt;GET /health&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fprintln&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"healthy"&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 tells the mux:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When a &lt;code&gt;GET /health&lt;/code&gt; request arrives, run this function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Finally, we start the server and give it the mux:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server receives requests, but the mux decides where they go.&lt;/p&gt;

&lt;p&gt;If we run the program and send this request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl http://localhost:8080/health
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we receive:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Give the handler function a name
&lt;/h2&gt;

&lt;p&gt;An anonymous function is convenient, but it becomes harder to read as it grows. We can move it into a named function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;healthHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fprintln&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"healthy"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;healthHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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;Notice that we pass the function itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;healthHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We do not call it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// Wrong: this tries to call the function immediately.&lt;/span&gt;
&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;healthHandler&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The mux saves the function and calls it later when a matching request arrives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Introduce a service
&lt;/h2&gt;

&lt;p&gt;Returning a fixed string does not require application logic. A real health endpoint may need to inspect PostgreSQL, Redis, or other dependencies.&lt;/p&gt;

&lt;p&gt;We can put that logic in a service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;HealthService&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;HealthService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"healthy"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For now, this service still returns a fixed value. That is okay—we are focusing on how the pieces connect.&lt;/p&gt;

&lt;p&gt;One easy way to let the handler use the service is with a closure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;HealthService&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fprintln&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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;The handler can use &lt;code&gt;service&lt;/code&gt; because the function was created in the same surrounding scope.&lt;/p&gt;

&lt;p&gt;The request now follows this path:&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 → mux → handler function → service.Status()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a good solution for a small program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Let a handler struct remember the service
&lt;/h2&gt;

&lt;p&gt;As an application grows, a handler may need several methods or dependencies. A struct gives us a clear place to store them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;HealthHandler&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;HealthService&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;service&lt;/code&gt; field means every &lt;code&gt;HealthHandler&lt;/code&gt; value remembers which health service it should use.&lt;/p&gt;

&lt;p&gt;We create the handler with a constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;NewHealthHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;HealthService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;HealthHandler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;HealthHandler&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;service&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;Passing a dependency into an object from the outside is called &lt;strong&gt;dependency injection&lt;/strong&gt;. Despite the intimidating name, the idea is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Create a value, then give it the things it needs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The handler does not create its own service. &lt;code&gt;main&lt;/code&gt; creates the service and passes it in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Understand the &lt;code&gt;http.Handler&lt;/code&gt; interface
&lt;/h2&gt;

&lt;p&gt;The standard library defines an interface that is approximately this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Handler&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ServeHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Request&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;Any type with a matching &lt;code&gt;ServeHTTP&lt;/code&gt; method can act as an HTTP handler.&lt;/p&gt;

&lt;p&gt;We can make our &lt;code&gt;HealthHandler&lt;/code&gt; satisfy that interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;HealthHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;ServeHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fprintln&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&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;We do not need to write &lt;code&gt;implements http.Handler&lt;/code&gt;. Go recognizes automatically that &lt;code&gt;*HealthHandler&lt;/code&gt; has the required method.&lt;/p&gt;

&lt;p&gt;Now we can register it with &lt;code&gt;mux.Handle&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;HealthService&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;NewHealthHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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;When a matching request arrives, the mux effectively calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ServeHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We do not call &lt;code&gt;ServeHTTP&lt;/code&gt; ourselves. The standard library does it for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;HandleFunc&lt;/code&gt; versus &lt;code&gt;Handle&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;These two methods are similar, so they are easy to confuse.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;HandleFunc&lt;/code&gt; when you have a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fprintln&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"healthy"&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;Use &lt;code&gt;Handle&lt;/code&gt; when you have a value that implements &lt;code&gt;http.Handler&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;NewHealthHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A useful shortcut is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function                         → HandleFunc
value with a ServeHTTP method    → Handle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Applying this to a real health-check package
&lt;/h2&gt;

&lt;p&gt;Now let us return to the original line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;healthService&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It becomes easier to understand if we expand it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;healthHandler&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;healthService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;healthHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The constructor stores the service inside the handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Handler&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Service&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;NewHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Handler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Handler&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;service&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;The handler uses that stored service when a request arrives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;ServeHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

    &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Healthy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusServiceUnavailable&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewEncoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&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 handler has a narrow responsibility:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ask the service for the current health result.&lt;/li&gt;
&lt;li&gt;Choose the correct HTTP status code.&lt;/li&gt;
&lt;li&gt;Encode the result as JSON.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It does not know how PostgreSQL is checked. That responsibility belongs elsewhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wiring the complete application
&lt;/h2&gt;

&lt;p&gt;The clearest way to read the application wiring is one layer at a time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewPostgres&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"initialize postgres: %w"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;postgresChecker&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewPostgresChecker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;healthService&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;postgresChecker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;healthHandler&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;healthService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;healthHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each line gives one object to the next:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PostgreSQL pool
      │
      ▼
PostgreSQL checker
      │
      ▼
Health service
      │
      ▼
Health handler
      │
      ▼
ServeMux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The shorter version is equivalent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;healthService&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The expanded version is often easier to learn and debug. The shorter version is only a convenience; it is not a different design.&lt;/p&gt;

&lt;h2&gt;
  
  
  Follow one request from beginning to end
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl http://localhost:8080/health
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The HTTP server receives &lt;code&gt;GET /health&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The server passes the request to the mux.&lt;/li&gt;
&lt;li&gt;The mux finds the handler registered for &lt;code&gt;GET /health&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The mux calls &lt;code&gt;healthHandler.ServeHTTP(w, r)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The handler calls &lt;code&gt;healthService.Check(r.Context())&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The service runs its registered dependency checkers.&lt;/li&gt;
&lt;li&gt;The PostgreSQL checker calls &lt;code&gt;pool.Ping(ctx)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The result returns through the service and handler.&lt;/li&gt;
&lt;li&gt;The handler sends JSON and either &lt;code&gt;200 OK&lt;/code&gt; or &lt;code&gt;503 Service Unavailable&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The dependencies are created from bottom to top during startup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pool → checker → service → handler → mux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The request travels from top to bottom at runtime:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mux → handler → service → checker → pool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Seeing those as two separate moments—application startup and request handling—usually removes much of the confusion.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why keep the service and handler separate?
&lt;/h2&gt;

&lt;p&gt;We could put everything inside one handler function. That might be fine for a tiny experiment, but separating them gives us useful boundaries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The handler deals with HTTP concepts such as request contexts, JSON, and status codes.&lt;/li&gt;
&lt;li&gt;The service coordinates health checks and decides whether the application is healthy.&lt;/li&gt;
&lt;li&gt;Each checker understands one external dependency.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;main&lt;/code&gt; creates and connects the components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This also makes future changes local. Adding Redis does not require changing the handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;healthService&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;postgresChecker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;redisChecker&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 handler still asks the same service the same question.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common beginner misunderstandings
&lt;/h2&gt;

&lt;h3&gt;
  
  
  “Does the mux execute the handler during startup?”
&lt;/h3&gt;

&lt;p&gt;No. Registering a handler only saves it for later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;healthHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It executes after a matching request arrives.&lt;/p&gt;

&lt;h3&gt;
  
  
  “Why do we not call &lt;code&gt;ServeHTTP&lt;/code&gt;?”
&lt;/h3&gt;

&lt;p&gt;The HTTP server and mux call it. Our responsibility is to provide a value with the correct method.&lt;/p&gt;

&lt;h3&gt;
  
  
  “Is &lt;code&gt;NewHandler&lt;/code&gt; special Go syntax?”
&lt;/h3&gt;

&lt;p&gt;No. It is an ordinary function following a common naming convention for constructors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;NewHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Handler&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  “Does the mux know about the service?”
&lt;/h3&gt;

&lt;p&gt;No. The mux knows only about the handler. The handler knows about the service because the service was injected into it.&lt;/p&gt;

&lt;h3&gt;
  
  
  “Why use a pointer such as &lt;code&gt;*Handler&lt;/code&gt;?”
&lt;/h3&gt;

&lt;p&gt;The pointer lets the handler methods work with the same handler value and its stored dependencies without copying the struct. This is the usual pattern for stateful handlers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final mental model
&lt;/h2&gt;

&lt;p&gt;If you remember only five ideas, remember these:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;http.Server&lt;/code&gt; receives HTTP requests.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;http.ServeMux&lt;/code&gt; chooses a handler based on the request path and method.&lt;/li&gt;
&lt;li&gt;A type becomes an &lt;code&gt;http.Handler&lt;/code&gt; by defining &lt;code&gt;ServeHTTP&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A handler can store a service in a struct field.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;main&lt;/code&gt; creates the dependencies and connects them together.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The line that once looked complicated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GET /health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;healthService&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now reads naturally:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Create a health handler that uses this health service, then register it for &lt;code&gt;GET /health&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is the core of mux, handlers, and dependency injection in Go.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>beginners</category>
      <category>go</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How Next.js Image Optimization Works Under the Hood</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Thu, 16 Jul 2026 06:39:54 +0000</pubDate>
      <link>https://dev.to/harshmangalam/how-nextjs-image-optimization-works-under-the-hood-55lf</link>
      <guid>https://dev.to/harshmangalam/how-nextjs-image-optimization-works-under-the-hood-55lf</guid>
      <description>&lt;p&gt;When I first started using Next.js, I replaced every &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; with &lt;code&gt;&amp;lt;Image /&amp;gt;&lt;/code&gt; because the documentation said it was "optimized."&lt;/p&gt;

&lt;p&gt;It worked.&lt;/p&gt;

&lt;p&gt;But I had one question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What is it actually doing behind the scenes?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After digging into it, I realized the idea is much simpler than I expected.&lt;/p&gt;

&lt;p&gt;Let's walk through it together.&lt;/p&gt;




&lt;h2&gt;
  
  
  The problem with a normal &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine you have a beautiful travel photo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;taj-mahal.png
4000 × 3000
6 MB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You display it on your website 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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/taj-mahal.png"&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;When someone opens your page, the browser asks the server for that image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Browser
   │
   ▼
GET /taj-mahal.png
   │
   ▼
Server sends 6 MB image
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser downloads the entire file.&lt;/p&gt;

&lt;p&gt;Now imagine your image is only shown inside a small card that's &lt;strong&gt;300 pixels wide&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The browser still downloads the full &lt;strong&gt;4000-pixel&lt;/strong&gt; image.&lt;/p&gt;

&lt;p&gt;That means you're downloading way more data than you actually need.&lt;/p&gt;




&lt;h2&gt;
  
  
  What would be the smarter approach?
&lt;/h2&gt;

&lt;p&gt;Instead of sending the same image to everyone, what if we could do this?&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;300 px image
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;600 px image
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1000 px image
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everyone sees the same picture, but only downloads the size they actually need.&lt;/p&gt;

&lt;p&gt;That is the main idea behind image optimization.&lt;/p&gt;




&lt;h2&gt;
  
  
  Enter Next.js &lt;code&gt;&amp;lt;Image /&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Instead of writing&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;img&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/taj-mahal.png"&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;we write&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;Image&lt;/span&gt;
  &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/taj-mahal.png"&lt;/span&gt;
  &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;300&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;Looks almost the same.&lt;br&gt;
But behind the scenes, something very different happens.&lt;/p&gt;


&lt;h2&gt;
  
  
  The first surprise
&lt;/h2&gt;

&lt;p&gt;You might think the browser requests this:&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;GET /taj-mahal.png
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Instead, it requests something like this:&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;GET /_next/image?url=/taj-mahal.png&amp;amp;w=640&amp;amp;q=75
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first, this looked strange to me.&lt;/p&gt;

&lt;p&gt;Why isn't it requesting the actual image?&lt;/p&gt;




&lt;h2&gt;
  
  
  Meet the Image Optimizer
&lt;/h2&gt;

&lt;p&gt;That &lt;code&gt;/_next/image&lt;/code&gt; URL is a special endpoint created by Next.js.&lt;/p&gt;

&lt;p&gt;Instead of sending the original image directly, the request goes through the optimizer first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Browser
    │
    ▼
Next.js Image Optimizer
    │
    ▼
Original Image
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can think of it as a smart middleman.&lt;/p&gt;

&lt;p&gt;Before giving the browser the image, it asks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What size do you need?&lt;/li&gt;
&lt;li&gt;What quality should I use?&lt;/li&gt;
&lt;li&gt;Which image format does your browser support?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then it prepares the best version.&lt;/p&gt;




&lt;h2&gt;
  
  
  What happens when the request arrives?
&lt;/h2&gt;

&lt;p&gt;Suppose the browser sends:&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;GET /_next/image?url=/taj-mahal.png&amp;amp;w=640&amp;amp;q=75
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next.js now knows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Original image location&lt;/li&gt;
&lt;li&gt;Required width (640px)&lt;/li&gt;
&lt;li&gt;Image quality (75)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now the real work begins.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1 — Load the original image
&lt;/h2&gt;

&lt;p&gt;Next.js finds the original image.&lt;/p&gt;

&lt;p&gt;It might come from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;your &lt;code&gt;public&lt;/code&gt; folder&lt;/li&gt;
&lt;li&gt;an external URL&lt;/li&gt;
&lt;li&gt;cloud storage like S3&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The optimizer first loads the original image into memory.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2 — Resize it
&lt;/h2&gt;

&lt;p&gt;Suppose the original image is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4000 px wide
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the browser only needs&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;There is no reason to send all 4000 pixels.&lt;/p&gt;

&lt;p&gt;Next.js resizes the image to the requested width.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4000 px

↓

640 px
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This alone saves a lot of bandwidth.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3 — Compress it
&lt;/h2&gt;

&lt;p&gt;Even after resizing, the image can often be made much smaller.&lt;/p&gt;

&lt;p&gt;If the quality is set to &lt;code&gt;75&lt;/code&gt;, Next.js compresses it while trying to keep it looking almost identical.&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;400 KB

↓

90 KB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The user usually can't notice the difference, but their internet connection definitely can.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4 — Choose a better format
&lt;/h2&gt;

&lt;p&gt;Modern browsers support formats like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;WebP&lt;/li&gt;
&lt;li&gt;AVIF&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These formats are usually much smaller than PNG or JPEG.&lt;/p&gt;

&lt;p&gt;When your browser makes a request, it also sends an &lt;code&gt;Accept&lt;/code&gt; header that tells the server which image formats it understands.&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;Accept: image/avif,image/webp,image/*,*/*
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the browser supports WebP or AVIF, Next.js may convert the original PNG or JPEG into one of those formats before sending it back.&lt;br&gt;
&lt;/p&gt;

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

PNG

↓

Optimized

WebP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The picture looks almost identical but downloads much faster.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5 — Cache the result
&lt;/h2&gt;

&lt;p&gt;Image optimization isn't free.&lt;/p&gt;

&lt;p&gt;Resizing and compressing images takes CPU time.&lt;/p&gt;

&lt;p&gt;So Next.js stores the optimized version.&lt;/p&gt;

&lt;p&gt;The first request 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;Browser

↓

Next.js

↓

Resize

↓

Compress

↓

Cache

↓

Send image
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second request becomes much simpler.&lt;br&gt;
&lt;/p&gt;

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

↓

Next.js

↓

Cache

↓

Send image
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;p&gt;Just return the cached image.&lt;/p&gt;




&lt;h2&gt;
  
  
  How does the browser know which size to download?
&lt;/h2&gt;

&lt;p&gt;This part surprised me the most.&lt;/p&gt;

&lt;p&gt;Next.js doesn't generate just one image.&lt;/p&gt;

&lt;p&gt;It generates multiple versions.&lt;/p&gt;

&lt;p&gt;Something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;320 px

640 px

750 px

1080 px

1920 px
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It then generates an HTML &lt;code&gt;srcset&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;Something similar to 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;img&lt;/span&gt;
  &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/_next/image?url=/taj-mahal.png&amp;amp;w=640&amp;amp;q=75"&lt;/span&gt;
  &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"
    /_next/image?url=/taj-mahal.png&amp;amp;w=320&amp;amp;q=75 320w,
    /_next/image?url=/taj-mahal.png&amp;amp;w=640&amp;amp;q=75 640w,
    /_next/image?url=/taj-mahal.png&amp;amp;w=1080&amp;amp;q=75 1080w
  "&lt;/span&gt;
  &lt;span class="na"&gt;sizes=&lt;/span&gt;&lt;span class="s"&gt;"100vw"&lt;/span&gt;
&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now here's the interesting part.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next.js doesn't decide which image gets downloaded.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;browser&lt;/strong&gt; does.&lt;/p&gt;

&lt;p&gt;If you're on a phone, it might download the 640px version.&lt;/p&gt;

&lt;p&gt;If you're on a desktop with a Retina display, it might choose the 1080px version.&lt;/p&gt;

&lt;p&gt;The browser looks at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Viewport size&lt;/li&gt;
&lt;li&gt;Device pixel ratio (Retina, 2x, 3x, etc.)&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;sizes&lt;/code&gt; attribute&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then it picks the most appropriate image.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why do we provide &lt;code&gt;width&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;One more nice feature.&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 jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Image&lt;/span&gt;
  &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;600&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;the browser already knows how much space the image will take.&lt;/p&gt;

&lt;p&gt;Even before the image finishes downloading.&lt;/p&gt;

&lt;p&gt;That means your page doesn't suddenly jump around while loading.&lt;/p&gt;

&lt;p&gt;Without dimensions:&lt;br&gt;
&lt;/p&gt;

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

Text

(Image loads)

Everything moves down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

[Reserved Image Space]

Text
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything stays stable.&lt;/p&gt;

&lt;p&gt;This improves a Core Web Vitals metric called &lt;strong&gt;Cumulative Layout Shift (CLS)&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Putting everything together
&lt;/h2&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;Image&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

      &lt;span class="err"&gt;│&lt;/span&gt;
      &lt;span class="err"&gt;▼&lt;/span&gt;

&lt;span class="nx"&gt;Browser&lt;/span&gt; &lt;span class="nx"&gt;requests&lt;/span&gt;

&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;_next&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;

      &lt;span class="err"&gt;│&lt;/span&gt;
      &lt;span class="err"&gt;▼&lt;/span&gt;

&lt;span class="nx"&gt;Next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt; &lt;span class="nx"&gt;loads&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;

      &lt;span class="err"&gt;│&lt;/span&gt;
      &lt;span class="err"&gt;▼&lt;/span&gt;

&lt;span class="nx"&gt;Resize&lt;/span&gt;

      &lt;span class="err"&gt;│&lt;/span&gt;
      &lt;span class="err"&gt;▼&lt;/span&gt;

&lt;span class="nx"&gt;Compress&lt;/span&gt;

      &lt;span class="err"&gt;│&lt;/span&gt;
      &lt;span class="err"&gt;▼&lt;/span&gt;

&lt;span class="nx"&gt;Convert&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;WebP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nc"&gt;AVIF &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;supported&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

      &lt;span class="err"&gt;│&lt;/span&gt;
      &lt;span class="err"&gt;▼&lt;/span&gt;

&lt;span class="nx"&gt;Store&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;

      &lt;span class="err"&gt;│&lt;/span&gt;
      &lt;span class="err"&gt;▼&lt;/span&gt;

&lt;span class="nx"&gt;Send&lt;/span&gt; &lt;span class="nx"&gt;optimized&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;browser&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Before learning this, I thought &lt;code&gt;&amp;lt;Image /&amp;gt;&lt;/code&gt; was just another React component.&lt;/p&gt;

&lt;p&gt;It turns out it's much more than that.&lt;/p&gt;

&lt;p&gt;It's an image optimization pipeline built into Next.js.&lt;/p&gt;

&lt;p&gt;Instead of sending every visitor the same large image, it creates a version that's appropriate for their device, browser, and screen size.&lt;/p&gt;

&lt;p&gt;As developers, we only write one component.&lt;/p&gt;

&lt;p&gt;Behind the scenes, Next.js handles resizing, compression, modern image formats, caching, and responsive images for us.&lt;/p&gt;

&lt;p&gt;That's a lot of work hidden behind a single line of code.&lt;/p&gt;




&lt;p&gt;If you enjoyed this article, the next logical step is understanding &lt;strong&gt;how &lt;code&gt;srcset&lt;/code&gt; and &lt;code&gt;sizes&lt;/code&gt; actually work&lt;/strong&gt;. That's where the browser starts making intelligent decisions about which image to download, and it's one of the coolest parts of responsive images.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>webdev</category>
      <category>webperf</category>
    </item>
    <item>
      <title>Stop Using react-error-boundary in Next.js? Meet next/error</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Fri, 03 Jul 2026 06:30:57 +0000</pubDate>
      <link>https://dev.to/harshmangalam/stop-using-react-error-boundary-in-nextjs-meet-nexterror-o53</link>
      <guid>https://dev.to/harshmangalam/stop-using-react-error-boundary-in-nextjs-meet-nexterror-o53</guid>
      <description>&lt;p&gt;For a long time, whenever I needed an Error Boundary in React, I reached for &lt;code&gt;react-error-boundary&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It has been the go-to solution because React itself only provides the low-level Error Boundary API.&lt;/p&gt;

&lt;p&gt;But if you're using &lt;strong&gt;Next.js App Router&lt;/strong&gt;, there's now a much cleaner option.&lt;/p&gt;

&lt;p&gt;Starting with &lt;strong&gt;Next.js 16&lt;/strong&gt;, you can use the built-in &lt;code&gt;next/error&lt;/code&gt; package.&lt;/p&gt;

&lt;p&gt;Let's see why it matters.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Old Way
&lt;/h2&gt;

&lt;p&gt;Most React applications use &lt;code&gt;react-error-boundary&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ErrorBoundary&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-error-boundary&lt;/span&gt;&lt;span class="dl"&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;ErrorFallback&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;resetErrorBoundary&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="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;Something went wrong.&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;&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;resetErrorBoundary&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Try Again
      &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;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&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="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;ErrorBoundary&lt;/span&gt; &lt;span class="na"&gt;FallbackComponent&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;ErrorFallback&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;Dashboard&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;ErrorBoundary&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;It works well, but it means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Installing another dependency&lt;/li&gt;
&lt;li&gt;Learning another API&lt;/li&gt;
&lt;li&gt;Slightly different patterns from what Next.js already provides&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're already using Next.js, that's no longer necessary in many cases.&lt;/p&gt;




&lt;h2&gt;
  
  
  Enter &lt;code&gt;next/error&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Next.js now ships with a helper called &lt;code&gt;catchError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Instead of wrapping your component with &lt;code&gt;&amp;lt;ErrorBoundary /&amp;gt;&lt;/code&gt;, you simply wrap your fallback component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;catchError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ErrorInfo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next/error&lt;/span&gt;&lt;span class="dl"&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;ErrorFallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;retry&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;ErrorInfo&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="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;Something went wrong.&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;&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;retry&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;
        Try Again
      &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;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;catchError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ErrorFallback&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 it.&lt;/p&gt;

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

&lt;p&gt;No separate Error Boundary component.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Real Example
&lt;/h2&gt;

&lt;p&gt;Here's an example with a nicer UI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AlertTriangle&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lucide-react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;catchError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ErrorInfo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next/error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@/components/ui/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;function&lt;/span&gt; &lt;span class="nf"&gt;ErrorFallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;compact&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;retry&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;ErrorInfo&lt;/span&gt;
&lt;span class="p"&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;compact&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="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"flex flex-col items-center gap-2 px-4 py-4 text-center"&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;AlertTriangle&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text-danger h-4 w-4"&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;p&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text-gray text-xs"&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Something went wrong&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;
          &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"sm"&lt;/span&gt;
          &lt;span class="na"&gt;variant&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"secondary"&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;retry&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;
          Try again
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&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;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="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"flex flex-col items-center gap-3 px-5 py-10 text-center"&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;AlertTriangle&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text-danger h-6 w-6"&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;p&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text-sm font-medium"&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Something went wrong&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;
        &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"sm"&lt;/span&gt;
        &lt;span class="na"&gt;variant&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"secondary"&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;retry&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;
        Try again
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&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;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;catchError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ErrorFallback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that your fallback component receives &lt;strong&gt;two things&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;props&lt;/strong&gt; → Any custom props you pass.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ErrorInfo&lt;/strong&gt; → Helpful methods like &lt;code&gt;retry()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Calling &lt;code&gt;retry()&lt;/code&gt; tells Next.js to attempt rendering the component again.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Feels Better
&lt;/h2&gt;

&lt;p&gt;The API is surprisingly simple.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt; &lt;span class="na"&gt;FallbackComponent&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;ErrorFallback&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;Dashboard&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;ErrorBoundary&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;You create an error boundary by exporting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;catchError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ErrorFallback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your fallback stays focused on displaying the UI.&lt;/p&gt;

&lt;p&gt;Next.js handles the rest.&lt;/p&gt;




&lt;h2&gt;
  
  
  When Should You Use It?
&lt;/h2&gt;

&lt;p&gt;If you're building a &lt;strong&gt;Next.js App Router&lt;/strong&gt; application and only need client-side Error Boundaries, &lt;code&gt;next/error&lt;/code&gt; is a great default.&lt;/p&gt;

&lt;p&gt;It gives you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ No additional dependency&lt;/li&gt;
&lt;li&gt;✅ Smaller bundle&lt;/li&gt;
&lt;li&gt;✅ Built into Next.js&lt;/li&gt;
&lt;li&gt;✅ Retry support out of the box&lt;/li&gt;
&lt;li&gt;✅ API that feels consistent with the framework&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Should You Remove &lt;code&gt;react-error-boundary&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;If you're starting a new Next.js project, I'd probably reach for &lt;code&gt;next/error&lt;/code&gt; first.&lt;/p&gt;

&lt;p&gt;If you already have &lt;code&gt;react-error-boundary&lt;/code&gt; everywhere, there's no urgent reason to rewrite everything overnight.&lt;/p&gt;

&lt;p&gt;But for new components, using the built-in solution keeps your dependency list a little smaller and your code a little more aligned with Next.js.&lt;/p&gt;




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

&lt;p&gt;One thing I like about recent Next.js releases is that they're gradually replacing common third-party utilities with built-in APIs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;next/error&lt;/code&gt; is another example of that.&lt;/p&gt;

&lt;p&gt;It's a small addition, but it removes a dependency, simplifies your code, and gives you an Error Boundary API that feels like it belongs in the framework.&lt;/p&gt;

&lt;p&gt;If you're using the App Router, it's definitely worth trying.&lt;/p&gt;




&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Next.js &lt;code&gt;catchError&lt;/code&gt; documentation: &lt;a href="https://nextjs.org/docs/app/api-reference/functions/catchError" rel="noopener noreferrer"&gt;https://nextjs.org/docs/app/api-reference/functions/catchError&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Next.js 16 release notes: &lt;a href="https://nextjs.org/blog" rel="noopener noreferrer"&gt;https://nextjs.org/blog&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>nextjs</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>TypeScript is quietly killing baseUrl (and most people haven’t noticed yet)</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Wed, 08 Apr 2026 05:13:34 +0000</pubDate>
      <link>https://dev.to/harshmangalam/typescript-is-quietly-killing-baseurl-and-most-people-havent-noticed-yet-1mbf</link>
      <guid>https://dev.to/harshmangalam/typescript-is-quietly-killing-baseurl-and-most-people-havent-noticed-yet-1mbf</guid>
      <description>&lt;p&gt;If you’re seeing this warning:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Option 'baseUrl' is deprecated and will stop functioning in TypeScript 7.0&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yeah… it’s real.&lt;/p&gt;

&lt;p&gt;For years, we’ve been doing this 👇&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;"extends"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"@workspace/typescript-config/bun.json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&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;span class="nl"&gt;"baseUrl"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"paths"&lt;/span&gt;&lt;span class="p"&gt;:&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;span class="nl"&gt;"@/*"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"src/*"&lt;/span&gt;&lt;span class="p"&gt;]&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;span class="p"&gt;}&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;But TypeScript is moving away from module resolution hacks.&lt;/p&gt;

&lt;p&gt;💡 The shift&lt;br&gt;
TypeScript no longer wants to resolve your imports.&lt;/p&gt;

&lt;p&gt;👉 Your runtime / bundler should do that (Bun, Vite, Next, etc.)&lt;br&gt;
👉 TypeScript should just type-check&lt;/p&gt;

&lt;p&gt;✅ The simple fix&lt;br&gt;
Just remove baseUrl.&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;"extends"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"@workspace/typescript-config/bun.json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&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;span class="nl"&gt;"paths"&lt;/span&gt;&lt;span class="p"&gt;:&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;span class="nl"&gt;"@/*"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"./src/*"&lt;/span&gt;&lt;span class="p"&gt;]&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;span class="p"&gt;}&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;⚡ Why this matters&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Future-proof (TS 7 won’t break you)&lt;/li&gt;
&lt;li&gt;Cleaner separation of concerns&lt;/li&gt;
&lt;li&gt;Matches modern tooling direction&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>🧩 Why Dialog Doesn’t Work When Wrapped with Tooltip in Radix / ShadCN UI (And How to Fix It)</title>
      <dc:creator>Harsh Mangalam</dc:creator>
      <pubDate>Thu, 07 Aug 2025 17:37:45 +0000</pubDate>
      <link>https://dev.to/harshmangalam/why-dialog-doesnt-work-when-wrapped-with-tooltip-in-radix-shadcn-ui-and-how-to-fix-it-4905</link>
      <guid>https://dev.to/harshmangalam/why-dialog-doesnt-work-when-wrapped-with-tooltip-in-radix-shadcn-ui-and-how-to-fix-it-4905</guid>
      <description>&lt;p&gt;When working with &lt;strong&gt;Radix UI&lt;/strong&gt; (used under the hood in &lt;a href="https://ui.shadcn.com" rel="noopener noreferrer"&gt;shadcn/ui&lt;/a&gt;), things are usually smooth — composable, accessible, and beautifully engineered.&lt;/p&gt;

&lt;p&gt;But one day, I wrapped a &lt;code&gt;DialogTrigger&lt;/code&gt; with a &lt;code&gt;Tooltip&lt;/code&gt; and…&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nothing happened.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No errors. No warnings. Just a button that didn’t open the dialog. 🤯&lt;/p&gt;

&lt;p&gt;Turns out, there's a &lt;strong&gt;common but sneaky mistake&lt;/strong&gt; that trips up many devs — and it’s all about the &lt;strong&gt;order in which you nest your components&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  ❌ The Broken Version
&lt;/h2&gt;

&lt;p&gt;This is what I initially wrote — trying to wrap my dialog trigger in a tooltip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Dialog&lt;/span&gt; &lt;span class="na"&gt;open&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onOpenChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setOpen&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;DialogTrigger&lt;/span&gt; &lt;span class="na"&gt;asChild&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;Tooltip&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;TooltipTrigger&lt;/span&gt; &lt;span class="na"&gt;asChild&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;Button&lt;/span&gt; &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"icon"&lt;/span&gt; &lt;span class="na"&gt;variant&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"outline"&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;EditIcon&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;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;TooltipTrigger&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;TooltipContent&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Edit podcast&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;TooltipContent&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;Tooltip&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;DialogTrigger&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;DialogContent&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"max-w-4xl max-h-[80vh] overflow-y-auto"&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;/* Form goes here */&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;DialogContent&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;Dialog&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;At first glance, this looks totally fine.&lt;/p&gt;

&lt;p&gt;But the dialog wouldn’t open when clicking the button.&lt;br&gt;
The tooltip shows, but the dialog never appears.&lt;/p&gt;

&lt;p&gt;🤔 What’s Going Wrong?&lt;br&gt;
Let’s break this down.&lt;/p&gt;

&lt;p&gt;Both DialogTrigger and TooltipTrigger from Radix use the asChild prop, which means:&lt;/p&gt;

&lt;p&gt;They don’t render any DOM element themselves.&lt;/p&gt;

&lt;p&gt;Instead, they pass event handlers like onClick to their child — which must be a real DOM element, like a .&lt;/p&gt;

&lt;p&gt;In the broken example, DialogTrigger wraps Tooltip, which is not a DOM element — it's just a React component.&lt;br&gt;
So the click event never reaches a usable target for Dialog.&lt;/p&gt;

&lt;p&gt;✅ The Working Version&lt;br&gt;
Here’s the corrected nesting:&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;Dialog&lt;/span&gt; &lt;span class="na"&gt;open&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onOpenChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setOpen&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;Tooltip&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;TooltipTrigger&lt;/span&gt; &lt;span class="na"&gt;asChild&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;DialogTrigger&lt;/span&gt; &lt;span class="na"&gt;asChild&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;Button&lt;/span&gt; &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"icon"&lt;/span&gt; &lt;span class="na"&gt;variant&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"outline"&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;EditIcon&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;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;DialogTrigger&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;TooltipTrigger&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;TooltipContent&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Edit podcast&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;TooltipContent&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;Tooltip&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;DialogContent&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"max-w-4xl max-h-[80vh] overflow-y-auto"&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;/* Form goes here */&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;DialogContent&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;Dialog&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;Why does this work?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TooltipTrigger passes props down to DialogTrigger&lt;/li&gt;
&lt;li&gt;DialogTrigger passes props down to the final Button&lt;/li&gt;
&lt;li&gt;The Button becomes the single, shared DOM element that receives both behaviors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🧠 Rule of Thumb&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Always place the outermost wrapper (like Tooltip) outside, and the final DOM element (like Button) inside both DialogTrigger and TooltipTrigger, using asChild.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>radix</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
