๐ 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 โ
Vertical Scaling
Scale Up โ bigger machine
Horizontal Scaling
Scale Out โ more machines
- CostExpensive hardwareCommodity servers
Vertical scaling requires buying the next SKU up; horizontal scaling uses cheaper, easier-to-source hardware.
- LimitHardware ceilingNearly unlimited
Physics dictates hardware limits โ there is only so much you can cram into one chassis.
- Fault ToleranceSingle point of failureRedundant
A single beefy box has one power supply, one NIC, one OS to crash.
- ComplexitySimpleRequires coordination
More nodes means more moving parts to keep in sync โ load balancers, sticky sessions, data partitioning.
๐ป 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.
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 Type | Guarantees | Example Scenario |
|---|---|---|
| CP | Correct data, may be unavailable | Banking: Reject transaction if network split to avoid double spending. |
| AP | Always responds, may return stale data | Social Media: Show old feed rather than an error page. |
| CA | Consistent + Available | Local 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.
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.
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
