DEV Community

Hrishikesh Dalal
Hrishikesh Dalal

Posted on

EP 9: The Legend of ShopStream: The Gatekeeper Chronicles

Akash, the founder of ShopStream, had successfully split his massive Monolith into Microservices. The app was flexible, and the code was clean.

But success brought a new problem: Traffic.

Chapter 1: The Crush (Why We Need a Load Balancer)

It was the day of the "Mega Summer Sale." Millions of users flooded ShopStream to buy discounted headphones.

Akash had prepared for this. He had spun up 10 identical servers for the Checkout Service. He thought he was safe.

But then, disaster struck.

All the users were trying to connect to Server #1 because that was the only IP address the app knew. Server #1 caught fire (metaphorically) and crashed. The other 9 servers sat idle, doing absolutely nothing, while users stared at "404 Errors."

Akash realized he didn't need just more servers; he needed a Traffic Cop.

He introduced a Load Balancer.

The Concept:
Imagine a popular nightclub. Inside, there are 5 bartenders (Servers). If everyone rushes to the first bartender, chaos ensues.
The Load Balancer is the Host at the door.

  • The Host stops you at the entrance.
  • He looks inside to see which bartender is free.
  • He points you to Bartender #3.
  • If Bartender #2 passes out (Server Crash), the Host stops sending people to him. (This is called a Health Check).

Suddenly, the traffic was spread evenly. No single server died. The system survived.

Chapter 2: The Decision Logic (Algorithms)

Now that Akash had a "Host" at the door, he had to teach it how to distribute the guests. This is where Load Balancing Algorithms came in.

1. The "Round Robin" (Taking Turns)
At first, Akash told the Host: "Just send them in order. One for Server A, one for Server B, one for Server C."

  • Pros: Simple and fair.
  • Cons: It didn't account for strength. Server A was a powerful beast, while Server C was an old laptop. Server C got overwhelmed quickly.

2. The "Least Connections" (The Smart Observer)
Akash changed the rule: "Look at who is busy. Send the new user to the server with the fewest active customers."

  • Pros: Perfect for when user sessions have different lengths (e.g., one user buys quickly, another browses for hours).
  • Cons: Slightly more complex to calculate.

3. The "IP Hash" (The Sticky Memory)
Some users complained: "I put an item in my cart on Server A, but on my next click, the Load Balancer sent me to Server B, and my cart was empty!"
Akash switched to IP Hashing. The Host now remembered the user's face (IP Address). "Ah, I remember you. You go back to Server A."

  • Pros: Essential for maintaining user sessions (Sticky Sessions).

Chapter 3: The Two Guardians (L4 vs. L7 Load Balancers)

As ShopStream grew into a global empire, Akash realized not all traffic was the same. He had to choose between two types of Load Balancers: Layer 4 (L4) and Layer 7 (L7).

The L4 Load Balancer: "The Bouncer"

Akash hired a tough, fast Bouncer for the main entrance.

  • How he works: He only looks at the envelope of the message (IP Address and Port). He doesn't open the letter.
  • The Logic: "Oh, you are coming from IP 192.168.1.5 trying to reach Port 80? Go to Server 3."
  • The Pros: He is incredibly fast. He doesn't waste time reading the data. He can handle millions of requests per second.
  • The Cons: He is "dumb." He doesn't know if you are asking for a Video or a JPG image. He just forwards traffic blindly.

The L7 Load Balancer: "The Concierge"

Inside the VIP area, Akash placed a sophisticated Concierge.

  • How she works: She actually opens the envelope and reads the request (HTTP Header, URL, Cookies).
  • The Logic:
  • "I see you are requesting /video/stream. I will send you to the high-performance Media Servers."
  • "I see you are requesting /billing/invoice. I will send you to the highly secure Finance Servers."

  • The Pros: She is smart. She allows for "Content-Based Routing." She can even block hackers if she sees a suspicious SQL code in the URL.

  • The Cons: She is slower than the Bouncer because she has to read the message (Decrypt HTTPS, read headers, re-encrypt).

The Moral of the Story

Akash learned that a robust system usually needs both.

  1. L4 (The Bouncer) sits at the very edge, taking the massive hit of incoming internet traffic and distributing it quickly.
  2. L7 (The Concierge) sits behind him, sorting that traffic into specific microservices (Checkout vs. Video vs. Reviews).

Top comments (0)