Skip to content

๐Ÿ“ˆ Level 2 โ€” Scalability โ€‹

IntermediateIntermediate1โ€“3 yrs experience Builds on the foundations. Expect trade-off discussions, light math, and short code snippets.Prerequisites: Solid grasp of networking, databases, and at least one web framework.

Learn how to design systems that grow from 100 users to 100 million.


2.1 Vertical vs Horizontal Scaling โ€‹

Visual Comparison โ€‹

Comparing

Vertical Scaling

Scale Up โ€” bigger machine

Horizontal Scaling

Scale Out โ€” more machines

  • Cost
    Expensive hardware
    Commodity servers
  • Limit
    Hardware ceiling
    Nearly unlimited
  • Fault Tolerance
    Single point of failure
    Redundant
  • Complexity
    Simple
    Requires coordination

๐Ÿ’ป JS Example: Horizontal Scaling with Node.js Cluster โ€‹

In Node.js, you can scale horizontally across multiple CPU cores on a single machine using the cluster module.

javascript
const cluster = require("cluster");
const http = require("http");
const numCPUs = require("os").cpus().length;

if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);

  // Fork workers for each CPU core
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on("exit", (worker) => {
    console.log(`worker ${worker.process.pid} died. Respawning...`);
    cluster.fork();
  });
} else {
  // Workers can share any TCP connection
  http
    .createServer((req, res) => {
      res.writeHead(200);
      res.end("Hello from scaled server!\n");
    })
    .listen(8000);
}

2.2 CAP Theorem โ€‹

A distributed system can only guarantee 2 out of 3 properties simultaneously.

System TypeGuaranteesExample Scenario
CPCorrect data, may be unavailableBanking: Reject transaction if network split to avoid double spending.
APAlways responds, may return stale dataSocial Media: Show old feed rather than an error page.
CAConsistent + AvailableLocal DB: High reliability on a single machine, but fails on network split.

2.3 Consistency Models โ€‹

Eventual vs Strong Consistency โ€‹

๐Ÿ’ป JS Example: Handling Eventual Consistency โ€‹

When dealing with eventual consistency, clients often need to retry or "poll" for the latest state.

javascript
async function getConsistentData(id, expectedValue, retries = 3) {
  for (let i = 0; i < retries; i++) {
    const data = await fetch(`/api/data/${id}`).then((r) => r.json());
    if (data.value === expectedValue) return data;

    console.log(`Stale data detected, retry ${i + 1}...`);
    await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait 1s
  }
  throw new Error("Failed to reach consistency");
}

2.4 How to Scale: Step by Step โ€‹

The 5-Stage Journey โ€‹


2.5 Rate Limiting โ€‹

Protects your system from being overwhelmed by too many requests.

๐Ÿ’ป JS Example: Basic Token Bucket Implementation โ€‹

A conceptual look at how a rate limiter might track requests in memory.

javascript
class RateLimiter {
  constructor(limit, windowMs) {
    this.limit = limit;
    this.requests = new Map(); // Store counts per IP
  }

  isAllowed(ip) {
    const now = Date.now();
    const count = this.requests.get(ip) || 0;

    if (count >= this.limit) {
      return false; // Rate limit exceeded
    }

    this.requests.set(ip, count + 1);
    // Reset limit after window expires
    setTimeout(() => this.requests.delete(ip), 60000);
    return true;
  }
}

โœ… Checklist Before Moving On โ€‹

  • [ ] I can explain CAP theorem with a real-world example
  • [ ] I know when to scale vertically vs horizontally
  • [ ] I understand eventual vs strong consistency
  • [ ] I can draw the 5-stage scaling journey

โžก๏ธ Next: Level 3 โ€” Databases

Released under the ISC License.