<?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: Victor Ayoola</title>
    <description>The latest articles on DEV Community by Victor Ayoola (@lowlifehighway).</description>
    <link>https://dev.to/lowlifehighway</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3859268%2F2fb4a2b5-798f-46bd-8cea-4d68c94cef57.jpeg</url>
      <title>DEV Community: Victor Ayoola</title>
      <link>https://dev.to/lowlifehighway</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lowlifehighway"/>
    <language>en</language>
    <item>
      <title>How My Team from Risevest Academy and I Built an End-to-End Encrypted Messaging App in 3 Weeks</title>
      <dc:creator>Victor Ayoola</dc:creator>
      <pubDate>Thu, 28 May 2026 23:02:13 +0000</pubDate>
      <link>https://dev.to/lowlifehighway/how-my-team-from-risevest-academy-and-i-built-an-end-to-end-encrypted-messaging-app-in-3-weeks-2ib5</link>
      <guid>https://dev.to/lowlifehighway/how-my-team-from-risevest-academy-and-i-built-an-end-to-end-encrypted-messaging-app-in-3-weeks-2ib5</guid>
      <description>&lt;p&gt;We were each asked to come up with an Idea of a project we would like to build. I have always wondered what the tech behind messaging platforms is like, so for me it was easy. Build a secure messaging platform. After a bit of research, I thought I understood what that meant. After the past three weeks, having built something I'm really proud of, I can tell you that I had no idea what I was getting into. Not in a bad way, but in a way that meant I was bound to learn.&lt;/p&gt;

&lt;p&gt;This is the story of how a team of four backend developers built a real-time, end-to-end encrypted messaging platform with WebSocket messaging, media sharing, push notifications, and a functioning encryption layer.&lt;/p&gt;




&lt;h2&gt;
  
  
  Where We Started
&lt;/h2&gt;

&lt;p&gt;The idea was initially simple; build something like WhatsApp, but with privacy as the foundation. Messages had to be encrypted on the sender's device and decrypted only on the recipient's. So not even our servers could reach the messages.&lt;/p&gt;

&lt;p&gt;That last requirement changed everything about how my vision for the system looked. Building a simple CRUD app is relatively easy, REST APIs, maybe a real-time feature here and there. E2EE is a different ball game entirely. It forces you to think about different boundaries, what does the server know? What should it know? And what is off limit?&lt;/p&gt;

&lt;p&gt;Before we started actually coding, we spent time on three questions: what were we actually building, what did each piece of the stack need to do, and what were the dependencies between them?&lt;/p&gt;




&lt;h2&gt;
  
  
  The Tech Stack and Why We Chose It
&lt;/h2&gt;

&lt;p&gt;We landed on Node.js, TypeScript, Express, PostgreSQL, Redis, and Socket.io for the backend.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TypeScript with strict mode&lt;/strong&gt; was non-negotiable. With four people writing backend code simultaneously as well as working on the frontend, type safety is what keeps everyone honest. Every API response shape, every socket event payload, every database model is fully typed. When a response shape changes the frontend catches it at compile time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Express.js&lt;/strong&gt; was a deliberate choice. We considered other choices - briefly, but Express felt like the obvious choice: every developer on the team already knows it - for one. We built a clean middleware stack on top of it — validation, JWT auth, error handling and request logging.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PostgreSQL and Knex&lt;/strong&gt; because relational data fits our domain. Users, conversations, members, messages — these have clear relationships and constraints. Knex gave us type-safe migrations and query building without the magic of a full ORM. We wrote our migrations in dependency order and never had a schema conflict.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Socket.io with Redis adapter&lt;/strong&gt; for real-time messaging. Socket.io's room abstraction maps perfectly to chat conversations — a socket joins the conversation room, messages are emitted to the room, everyone in it receives them. The Redis adapter is what allows this to scale: when you have multiple server instances, a message arriving at server A needs to reach a user connected to server B. Redis pub/sub is the channel between them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BullMQ&lt;/strong&gt; for offline message delivery. When a recipient is offline, the message queues. A background worker processes the queue and triggers push notifications. Jobs retry automatically with exponential backoff. This is the kind of infrastructure that feels like overkill until the day it saves you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Firebase&lt;/strong&gt; for three separate things: Authentication through firebase phone verification. Cloud Messaging handles push notifications. We originally planned to use Firebase Storage for media but switched to Cloudinary midway through because of its built-in transformation pipeline.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Architecture Decision That Defined Everything
&lt;/h2&gt;

&lt;p&gt;Early in week one we made a decision that shaped everything that followed: the server would store ciphertext and nothing else.&lt;/p&gt;

&lt;p&gt;This sounds obvious for an E2EE app but the implications run deep. It means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The backend has no business logic that depends on message content&lt;/li&gt;
&lt;li&gt;Encryption and decryption happen entirely on the client&lt;/li&gt;
&lt;li&gt;If our database is breached, the attacker gets encrypted blobs they cannot read&lt;/li&gt;
&lt;li&gt;The server cannot comply with a request to hand over message contents because it genuinely does not have them&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We used the Web Crypto API on the frontend — ECDH for key agreement and AES-256-GCM for symmetric encryption. Private keys are stored in IndexedDB as non-extractable CryptoKey objects. So even JavaScript cannot read them back out. They can only be used to perform cryptographic operations.&lt;/p&gt;

&lt;p&gt;Here is what that looks like in practice. When a user sends a message:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The frontend fetches the recipient's public key from the backend&lt;/li&gt;
&lt;li&gt;An ephemeral key pair is generated for this specific message&lt;/li&gt;
&lt;li&gt;ECDH key agreement derives a shared secret using the ephemeral private key and the recipient's public key&lt;/li&gt;
&lt;li&gt;The message is encrypted with AES-256-GCM using that shared secret&lt;/li&gt;
&lt;li&gt;The encrypted blob is sent to the backend&lt;/li&gt;
&lt;li&gt;The backend stores it without reading it&lt;/li&gt;
&lt;li&gt;The recipient's frontend receives the blob, derives the same shared secret from the other side, and decrypts locally&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We originally planned to use the Signal Protocol library on both frontend and backend. We ran into a hard wall, which was that libsignal is a Node.js library with native bindings that does not run in the browser. The frontend had to use a different approach. We chose the Web Crypto API — it's built into every modern browser, has no dependencies, and the non-extractable key storage in IndexedDB was more secure than what most apps do.&lt;/p&gt;




&lt;h2&gt;
  
  
  The 3-Week Sprint
&lt;/h2&gt;

&lt;p&gt;We split the work across four backend developers with clear ownership each week.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Week one&lt;/strong&gt; was foundation — Express scaffold, TypeScript config, database migrations, Firebase Auth integration, JWT session management, and the encryption key management infrastructure. The primary thing in week one was to get the auth middleware right. Everything else depended on it. We used Firebase Auth for phone number verification and OTP, then exchanged the Firebase ID token for our own internal JWT pair. Fifteen-minute access tokens, thirty-day refresh tokens and silent renewal on 401 responses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Week two&lt;/strong&gt; was the core product — Socket.io server with Redis adapter, message routing, message persistence, delivery status tracking, group messaging, media pipeline, and push notifications. This was the hardest week. Getting real-time messaging right requires thinking about state in multiple places simultaneously: the socket connection registry in Redis, the message status in PostgreSQL, the optimistic UI on the frontend, the BullMQ queue for offline delivery. We had moments where all four of us were debugging the same flow from different angles.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Week three&lt;/strong&gt; was integration. The frontend and backend had been built largely side by side and week three was where we wired them. This is where the choice to use TypeScript paid off most visibly — the type definitions meant most things just worked, and where they didn't the errors were specific, easy to trace and fixable.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Actually Went Wrong
&lt;/h2&gt;

&lt;p&gt;I want to be honest about the things that did not go smoothly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The authentication flow&lt;/strong&gt; this wasn't much of an issue but we most definitely struggled with finalizing and understanding the authentication flow on registration, and how to properly integrate firebase auth into the mix.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The libsignal mismatch&lt;/strong&gt; cost us a lot of time. We had built the backend key validation assuming the frontend would use Signal Protocol. When we discovered libsignal doesn't run in browsers we had to strip the validation out of the keys routes and rethink the frontend encryption approach. The lesson: validate your stack choices before you build anything on top of them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Route ordering in Express&lt;/strong&gt; this one hurt a bit, and genuinely had me thinking I was losing it. In reality, it is a genuinely easy mistake to make and we made it. Parameterised routes like &lt;code&gt;/:id&lt;/code&gt; will swallow everything that comes after them if registered in the wrong order. &lt;code&gt;GET /users/level-up&lt;/code&gt; must come before &lt;code&gt;GET /users/:id&lt;/code&gt; or search is unreachable — Express matches top to bottom and has no way to know that &lt;code&gt;search&lt;/code&gt; is not a UUID. We caught this problem during integration and it was quite mind boggling, one of those mistakes I most definitely won't be making again.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Would Do Differently
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Start with a shared Type contract.&lt;/strong&gt; A shared type contract would have helped avoid some of the issues we faced while building and integrating the front and backend, as it ensures both sides are always on the same page about how data is sent and received.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do a dependency audit before starting.&lt;/strong&gt; Know which tasks are truly parallel and which are blocked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Proper research on stack choice.&lt;/strong&gt; This is definitely a hard check for me from now on, even when it feels like you're using the industry standard. It's always best to ensure what you go with aligns perfectly with your goals and works properly with all other tools being used in the project.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I'm Most Proud Of
&lt;/h2&gt;

&lt;p&gt;Building this in three weeks, with four developers on a project of this complexity, We pushed to see it through and it most definitely wasn't smooth sailing all through but we were able to pull it off.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Stack in One Place
&lt;/h2&gt;

&lt;p&gt;For anyone who wants the full picture of our full tech stack, this is what it looked like:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Backend:&lt;/strong&gt; Node.js, TypeScript, Express.js, PostgreSQL, Knex, Redis, Socket.io, BullMQ, Firebase Admin SDK, Cloudinary, Zod, jose, Multer&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Frontend:&lt;/strong&gt; React, TypeScript, Web Crypto API, IndexedDB, Socket.io client, Firebase Auth SDK, Axios&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Infrastructure:&lt;/strong&gt; Redis Cloud, Cloudinary, Firebase (Auth + FCM), PostgreSQL&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;The backend is built to support Android — the API doesn't care whether the client is a browser or a mobile app. React Native is next.&lt;/p&gt;

&lt;p&gt;Voice and video calls are Phase 2. WebRTC for peer-to-peer, our signalling server already handles the connection infrastructure.&lt;/p&gt;

&lt;p&gt;Full Signal Protocol on the frontend is something I want to revisit. The Web Crypto API approach works and is genuinely secure but Signal's Double Ratchet algorithm provides forward secrecy that our current implementation lacks — if a key is compromised, past messages should remain safe. That's the next cryptographic milestone.&lt;/p&gt;




&lt;p&gt;If you're thinking about building something similar, my advice is this: make the hard architectural decisions first. For us, the decision that the server would never read message content was not a negotiable. Everything else followed from it. Start with your constraints, not your components.&lt;/p&gt;

&lt;p&gt;The code is messy in places, some corners were cut, and the UI could definitely do with some improvement. But it works, and messages encrypted on one device are being decrypted on another without our server ever knowing what was said.&lt;/p&gt;

&lt;p&gt;That's the thing we set out to build. That's the thing we built.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I am a fullstack developer who built the backend architecture (alongside my team) and the frontend for Culver as part of a 3-week mentorship project. These are the repo links, &lt;a href="https://github.com/iota-org/Culver-backend" rel="noopener noreferrer"&gt;Backend&lt;/a&gt;, &lt;a href="https://github.com/iota-org/culver-frontend" rel="noopener noreferrer"&gt;Frontend&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>backend</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Staying Open for Business on the Busiest Internet Shopping Day</title>
      <dc:creator>Victor Ayoola</dc:creator>
      <pubDate>Fri, 03 Apr 2026 10:45:52 +0000</pubDate>
      <link>https://dev.to/lowlifehighway/staying-open-for-business-on-the-busiest-internet-shopping-day-2gdd</link>
      <guid>https://dev.to/lowlifehighway/staying-open-for-business-on-the-busiest-internet-shopping-day-2gdd</guid>
      <description>&lt;p&gt;Your e-commerce store has a sale scheduled for midnight. You've spent weeks preparing: discounted inventory loaded, email campaign fired, social media countdown ticking. At 11:59 PM, traffic is normal. At 12:00:01 AM, thirty thousand users simultaneously click "Shop Now."&lt;/p&gt;

&lt;p&gt;Your single-process Node.js server gets hit with a wall of concurrent requests—product lookups, cart operations, inventory checks, checkout flows. The event loop, which was humming along handling a few dozen requests per second, is now buried under thousands. Response times balloon from 80ms to 8 seconds. Then your server crashes. Thirty thousand customers see a blank screen. Your sale is over before it started.&lt;/p&gt;

&lt;p&gt;This is not a hypothetical. It happens every Black Friday, to real businesses, running exactly the kind of code most tutorials teach you to write.&lt;/p&gt;

&lt;p&gt;The fix is not simply "get a bigger server." It is a fundamental architectural upgrade: &lt;strong&gt;Node.js clustering with graceful shutdown&lt;/strong&gt;. By the end of this article, you will understand how to use every CPU core your machine has, how to deploy new code without a single dropped request, and how to survive a traffic spike that would kill a single-process app.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why one process is not enough
&lt;/h2&gt;

&lt;p&gt;Node.js runs on a single thread. That is usually its superpower: instead of spawning a new OS thread per request (expensive, slow), Node handles thousands of concurrent connections through non-blocking I/O and its event loop.&lt;/p&gt;

&lt;p&gt;But here is the catch: on a machine with 8 CPU cores, a single Node.js process uses exactly one. The other seven sit idle, doing nothing, while your one overwhelmed process burns through its queue.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The core problem:&lt;/strong&gt; CPU-bound operations—checkout calculation, inventory validation, price aggregation—block the event loop. When thirty thousand users hit these simultaneously, everyone waits behind everyone else. On a single process, you have one lane of traffic. Clustering gives you eight.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;cluster&lt;/code&gt; module, built into Node.js since v0.8, solves this. It lets you fork multiple worker processes from a single master, each running your app code independently, each handling its own slice of incoming traffic. The OS load-balances connections across them. You go from one cashier to eight, instantly.&lt;/p&gt;

&lt;p&gt;But clustering alone is not enough. The harder problem is: what happens when you need to deploy a bug fix &lt;em&gt;during&lt;/em&gt; the sale? Restarting all eight workers simultaneously drops every in-flight request. Customers lose their cart. Orders fail mid-checkout. You need &lt;strong&gt;zero-downtime deployment&lt;/strong&gt;—the ability to swap out running workers one at a time, without ever leaving the server unable to accept requests.&lt;/p&gt;

&lt;p&gt;That is the graceful shutdown problem, and it is the heart of what we are building.&lt;/p&gt;




&lt;h2&gt;
  
  
  Architecture first
&lt;/h2&gt;

&lt;p&gt;Before writing code, understand the shape of what we are building:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Process&lt;/th&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;Count&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Master&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Forks workers, watches for exits, handles signals, orchestrates rolling restarts&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Worker&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Runs your actual Express/HTTP app, handles requests, drains on shutdown&lt;/td&gt;
&lt;td&gt;N (one per CPU core)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The master never handles HTTP traffic. It is a process manager—pure lifecycle control. Workers never know how many siblings they have. They just accept connections and serve requests.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: The master process
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// cluster-master.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cluster&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;os&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;os&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;WORKER_COUNT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cpus&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Use all available CPU cores&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;             &lt;span class="c1"&gt;// Track worker pid -&amp;gt; worker object&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;spawnWorker&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;worker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;workers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;exit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;workers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Auto-respawn unless we sent SIGTERM intentionally&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;signal&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SIGTERM&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; crashed. Respawning...`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;spawnWorker&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; started`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;worker&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;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isPrimary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Master &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; starting &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;WORKER_COUNT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; workers`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;WORKER_COUNT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;spawnWorker&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// SIGTERM kicks off a rolling restart&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SIGTERM&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;rollingRestart&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 the &lt;code&gt;workers&lt;/code&gt; Map. We track every live worker by PID. When a worker exits because of a crash (not our intentional &lt;code&gt;SIGTERM&lt;/code&gt;), we automatically respawn it. This is your self-healing loop—Black Friday traffic crashes rarely take down all eight workers simultaneously.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: The rolling restart
&lt;/h2&gt;

&lt;p&gt;This is the mechanism that makes zero-downtime deployment possible. Instead of killing all workers at once, we cycle through them one at a time: tell a worker to stop accepting new requests, wait for it to finish its current work, kill it, spawn a replacement, wait for it to be ready, then move to the next worker.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Still in cluster-master.js&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;rollingRestart&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Starting zero-downtime rolling restart...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;workerList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;workers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;()];&lt;/span&gt;

  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;worker&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;workerList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

      &lt;span class="c1"&gt;// 1. Tell this worker to stop accepting new connections&lt;/span&gt;
      &lt;span class="c1"&gt;//    and drain its existing ones&lt;/span&gt;
      &lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SHUTDOWN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

      &lt;span class="c1"&gt;// 2. Spawn a replacement immediately so capacity is maintained&lt;/span&gt;
      &lt;span class="nf"&gt;spawnWorker&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

      &lt;span class="c1"&gt;// 3. Wait for the old worker to exit cleanly&lt;/span&gt;
      &lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;once&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;exit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; drained and exited`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;

      &lt;span class="c1"&gt;// 4. Safety valve: force kill after 30s if still hanging&lt;/span&gt;
      &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="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="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isDead&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; timed out. Force killing.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;kill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SIGKILL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;30000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rolling restart complete. All workers refreshed.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Why spawn the replacement before waiting for the old worker to exit?&lt;/strong&gt; If you kill a worker first, then spawn the replacement, there is a gap where you have one fewer worker handling traffic. During a Black Friday spike, that gap matters. By spawning first and waiting second, capacity is maintained throughout the entire restart cycle.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 3: The worker — graceful shutdown logic
&lt;/h2&gt;

&lt;p&gt;The worker is where your actual HTTP application runs. It receives the &lt;code&gt;SHUTDOWN&lt;/code&gt; message from the master and must: stop the server from accepting new connections, wait for all in-flight requests to finish, then exit cleanly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// worker.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isShuttingDown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;activeRequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Middleware to reject new requests during shutdown&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isShuttingDown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Tell load balancers/clients not to reuse this connection&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Connection&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;close&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;503&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Server is restarting. Please retry.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;activeRequests&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;finish&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;activeRequests&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Your actual routes go here&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/products/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Running Shoes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;79.99&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/checkout&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// In production: validate inventory, process payment, write order&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Simulated async work&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`ORD-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;confirmed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; listening on port 3000`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Listen for shutdown signal from master&lt;/span&gt;
&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SHUTDOWN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; beginning graceful shutdown...`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;isShuttingDown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Stop accepting new TCP connections&lt;/span&gt;
  &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; HTTP server closed`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// Poll for active requests to drain&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;drainCheck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;activeRequests&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;drainCheck&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; all requests drained. Exiting.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; waiting: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;activeRequests&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; active requests`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;500&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;Three things to notice here:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;isShuttingDown&lt;/code&gt; is checked in middleware&lt;/strong&gt;—before your routes run. Any request that arrives after shutdown is triggered gets a &lt;code&gt;503&lt;/code&gt; immediately, not a hanging connection that never resolves.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;activeRequests&lt;/code&gt; is tracked per-worker&lt;/strong&gt; using simple increment/decrement on &lt;code&gt;res.finish&lt;/code&gt;. This is lightweight and accurate for HTTP/1.1 request lifecycles.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are two paths to &lt;code&gt;process.exit(0)&lt;/code&gt;&lt;/strong&gt;: the &lt;code&gt;server.close()&lt;/code&gt; callback (fires when the server's keep-alive connections all close), and the active-request drain poll. Whichever fires first wins. The 30-second SIGKILL in the master is the nuclear option if neither fires.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4: Wire it all together
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// index.js — single entry point&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cluster&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isPrimary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./cluster-master&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./worker&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it with &lt;code&gt;node index.js&lt;/code&gt;. On an 8-core machine, you get 8 workers. To trigger a zero-downtime restart during a deploy:&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;kill&lt;/span&gt; &lt;span class="nt"&gt;-SIGTERM&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;app.pid&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  The Black Friday simulation: what this actually achieves
&lt;/h2&gt;

&lt;p&gt;At 12:00 AM, connections arrive. The OS distributes them across 8 workers. Each worker handles its slice of traffic independently—no shared memory, no lock contention. Worker 1 processing a checkout has no impact on Worker 4 processing a product lookup.&lt;/p&gt;

&lt;p&gt;At 12:07 AM, your team spots a bug in the discount calculation. They push a fix. Deployment triggers &lt;code&gt;SIGTERM&lt;/code&gt; on the master.&lt;/p&gt;

&lt;p&gt;The rolling restart begins. Worker 1 receives &lt;code&gt;SHUTDOWN&lt;/code&gt;. It stops accepting new requests. The 3 requests currently in flight finish—they take about 60ms combined. Worker 1 exits. A fresh Worker 9 starts, running the patched code, and begins accepting traffic. Then Worker 2. Then Worker 3. By 12:09 AM, all 8 workers are running the patched code. Not a single in-flight request was dropped. No customer saw an error.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Single Process&lt;/th&gt;
&lt;th&gt;Cluster + Graceful Shutdown&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;CPU utilization (8-core machine)&lt;/td&gt;
&lt;td&gt;❌ 12.5% (1 of 8 cores)&lt;/td&gt;
&lt;td&gt;✅ ~100% (all cores active)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Requests dropped on deploy&lt;/td&gt;
&lt;td&gt;❌ All in-flight requests&lt;/td&gt;
&lt;td&gt;✅ Zero&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Worker crash recovery&lt;/td&gt;
&lt;td&gt;❌ Full outage&lt;/td&gt;
&lt;td&gt;✅ Auto-respawn, 7 workers continue&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Throughput under spike&lt;/td&gt;
&lt;td&gt;❌ Single queue, degrades fast&lt;/td&gt;
&lt;td&gt;✅ Distributed queue, scales linearly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deployment strategy&lt;/td&gt;
&lt;td&gt;❌ Stop → deploy → restart&lt;/td&gt;
&lt;td&gt;✅ Rolling restart, no downtime window&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  What about load balancers and PM2?
&lt;/h2&gt;

&lt;p&gt;In a real production system with multiple machines, you would typically have a load balancer (NGINX, AWS ALB) in front, and a process manager like PM2 handling clustering and restarts. PM2's &lt;code&gt;pm2 reload&lt;/code&gt; command does almost exactly what we built above.&lt;/p&gt;

&lt;p&gt;But here is why understanding the raw &lt;code&gt;cluster&lt;/code&gt; module matters: PM2 and cloud load balancers are abstractions over exactly this machinery. When a rolling restart goes wrong in production—and eventually it will—you need to know what signal the master is sending, why a worker is not draining, and what the 30-second SIGKILL safety valve is protecting against. You cannot debug an abstraction you have never looked underneath.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production note:&lt;/strong&gt; For single-machine deployments, use the raw cluster module or PM2 in cluster mode (&lt;code&gt;pm2 start index.js -i max&lt;/code&gt;). For multi-machine deployments, let your orchestration layer (Kubernetes, ECS) handle node-level scaling, and use the &lt;code&gt;SIGTERM&lt;/code&gt; graceful shutdown logic in your worker for zero-downtime pod replacement. The worker shutdown code in Step 3 applies identically to both cases.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;01 — Clustering is basic resource utilization.&lt;/strong&gt; Node.js is single-threaded. On a multi-core machine, a single process wastes most of your hardware. Clustering is not premature optimization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;02 — Graceful shutdown is not just &lt;code&gt;server.close()&lt;/code&gt;.&lt;/strong&gt; It means tracking in-flight requests, rejecting new ones with 503, draining the queue, then exiting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;03 — Spawn before you kill.&lt;/strong&gt; In a rolling restart, create the replacement worker before waiting for the old one to exit. One fewer worker during a traffic spike is a real cost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;04 — Always set a SIGKILL timeout.&lt;/strong&gt; A worker that holds open a database connection or hangs in a third-party API call will never drain. The safety valve is not optional.&lt;/p&gt;




&lt;p&gt;E-commerce failures during peak traffic are almost always architectural, not hardware problems. The stores that stay up on Black Friday are not running bigger servers—they are running smarter code.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;cluster&lt;/code&gt; module and graceful shutdown logic are not advanced topics. They are table stakes for any Node.js application that real people depend on.&lt;/p&gt;

&lt;p&gt;Your store is ready. Let's get to selling.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
