DEV Community

Cover image for ๐–๐ก๐ฒ ๐“๐ข๐œ๐ค๐ž๐ญ๐ฆ๐š๐ฌ๐ญ๐ž๐ซ ๐œ๐ซ๐š๐ฌ๐ก๐ž๐ฌ ๐›๐ฎ๐ญ ๐€๐ฆ๐š๐ณ๐จ๐ง ๐ฌ๐ฎ๐ซ๐ฏ๐ข๐ฏ๐ž๐ฌ ๐๐ฅ๐š๐œ๐ค ๐…๐ซ๐ข๐๐š๐ฒ โ€“ ๐ญ๐ก๐ž ๐ญ๐ซ๐ฎ๐ญ๐ก ๐š๐›๐จ๐ฎ๐ญ ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐ .
Creative Coder
Creative Coder

Posted on • Originally published at dev.to

๐–๐ก๐ฒ ๐“๐ข๐œ๐ค๐ž๐ญ๐ฆ๐š๐ฌ๐ญ๐ž๐ซ ๐œ๐ซ๐š๐ฌ๐ก๐ž๐ฌ ๐›๐ฎ๐ญ ๐€๐ฆ๐š๐ณ๐จ๐ง ๐ฌ๐ฎ๐ซ๐ฏ๐ข๐ฏ๐ž๐ฌ ๐๐ฅ๐š๐œ๐ค ๐…๐ซ๐ข๐๐š๐ฒ โ€“ ๐ญ๐ก๐ž ๐ญ๐ซ๐ฎ๐ญ๐ก ๐š๐›๐จ๐ฎ๐ญ ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐ .

๐Ž๐จ๐ฉ๐ฌ! ๐๐จ๐ญ๐ข๐œ๐ž ๐ฌ๐จ๐ฆ๐ž ๐ฌ๐ž๐ซ๐ฏ๐ž๐ซ๐ฌ ๐๐ข๐ž ๐ฎ๐ง๐ž๐ฑ๐ฉ๐ž๐œ๐ญ๐ž๐๐ฅ๐ฒ?

๐˜๐˜ฎ๐˜ข๐˜จ๐˜ช๐˜ฏ๐˜ฆ ๐˜ข ๐˜ค๐˜ฆ๐˜ญ๐˜ฆ๐˜ฃ๐˜ณ๐˜ช๐˜ต๐˜บ ๐˜ฑ๐˜ฐ๐˜ด๐˜ต๐˜ด ๐˜บ๐˜ฐ๐˜ถ๐˜ณ ๐˜ธ๐˜ฆ๐˜ฃ๐˜ด๐˜ช๐˜ต๐˜ฆ ๐˜ญ๐˜ช๐˜ฏ๐˜ฌ. 50,000 ๐˜ฑ๐˜ฆ๐˜ฐ๐˜ฑ๐˜ญ๐˜ฆ ๐˜ฉ๐˜ช๐˜ต ๐˜บ๐˜ฐ๐˜ถ๐˜ณ ๐˜ด๐˜ฆ๐˜ณ๐˜ท๐˜ฆ๐˜ณ ๐˜ข๐˜ต ๐˜ต๐˜ฉ๐˜ฆ ๐˜ด๐˜ข๐˜ฎ๐˜ฆ ๐˜ต๐˜ช๐˜ฎ๐˜ฆ ๐˜ฎ๐˜ข๐˜ฌ๐˜ช๐˜ฏ๐˜จ ๐˜ณ๐˜ฆ๐˜ฒ๐˜ถ๐˜ฆ๐˜ด๐˜ต๐˜ด. ๐˜›๐˜ฉ๐˜ฆ๐˜ฏ ๐˜ช๐˜ฏ ๐˜ญ๐˜ฆ๐˜ด๐˜ด ๐˜ต๐˜ฉ๐˜ข๐˜ฏ 10 ๐˜ฎ๐˜ช๐˜ฏ๐˜ถ๐˜ต๐˜ฆ๐˜ด, ๐˜ฆ๐˜ท๐˜ฆ๐˜ณ๐˜บ๐˜ต๐˜ฉ๐˜ช๐˜ฏ๐˜จ ๐˜จ๐˜ฐ๐˜ฆ๐˜ด ๐˜ฃ๐˜ญ๐˜ข๐˜ฏ๐˜ฌ!!!! ๐˜๐˜ข! ๐˜š๐˜ฆ๐˜ณ๐˜ท๐˜ฆ๐˜ณ ๐˜ต๐˜ช๐˜ฎ๐˜ฆ๐˜ฅ ๐˜ฐ๐˜ถ๐˜ต. ๐˜›๐˜ฉ๐˜ข๐˜ต ๐˜ค๐˜ข๐˜ฏ ๐˜ฃ๐˜ฆ ๐˜ง๐˜ณ๐˜ถ๐˜ด๐˜ต๐˜ณ๐˜ข๐˜ต๐˜ช๐˜ฏ๐˜จ. ๐˜“๐˜ฆ๐˜ต'๐˜ด ๐˜ฅ๐˜ช๐˜ท๐˜ฆ ๐˜ช๐˜ฏ๐˜ต๐˜ฐ ๐˜ข ๐˜ฃ๐˜ฆ๐˜ต๐˜ต๐˜ฆ๐˜ณ ๐˜ด๐˜ฐ๐˜ญ๐˜ถ๐˜ต๐˜ช๐˜ฐ๐˜ฏ ๐˜ด๐˜ฐ ๐˜ต๐˜ฉ๐˜ช๐˜ด ๐˜ด๐˜ฉ๐˜ฐ๐˜ถ๐˜ญ๐˜ฅ๐˜ฏ'๐˜ต ๐˜ฃ๐˜ฆ ๐˜ฉ๐˜ข๐˜ฑ๐˜ฑ๐˜ฆ๐˜ฏ๐˜ช๐˜ฏ๐˜จ, ๐˜ฉ๐˜ข๐˜ฉ๐˜ข ๐Ÿ˜‚

๐“๐ก๐ž ๐๐จ๐จ๐ซ ๐š๐ง๐š๐ฅ๐จ๐ ๐ฒ ๐ญ๐ก๐š๐ญ ๐ž๐ฑ๐ฉ๐ฅ๐š๐ข๐ง๐ฌ ๐ž๐ฏ๐ž๐ซ๐ฒ๐ญ๐ก๐ข๐ง๐ 

Imagine ten thousand people trying to walk through a single door at the same time. That's what happens to a website when concert tickets go on sale, when iPhone pre-orders open, or when your favorite artist drops surprise merch. The door is your server, and it has a maximum capacity before it breaks.

Now imagine that same crowd, but instead of one door, there are fifty doors, all leading into the same venue. A smart security guard directs people evenly across all fifty doors. Nobody waits long, no door gets overwhelmed, everyone gets in smoothly. ๐“๐ก๐š๐ญ'๐ฌ ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐ .

๐“๐ก๐ž ๐ฉ๐ซ๐จ๐›๐ฅ๐ž๐ฆ: ๐ฌ๐ž๐ซ๐ฏ๐ž๐ซ๐ฌ ๐ก๐š๐ฏ๐ž ๐ฅ๐ข๐ฆ๐ข๐ญ๐ฌ

Every server can handle a limited number of requests per second. Maybe yours can handle one thousand requests before it starts slowing down. At two thousand requests, it starts timing out. At three thousand, it crashes completely.

For a small blog with a hundred visitors per day, one server is plenty. But what happens when you go viral? What happens when a celebrity tweets your link? What happens when you're selling limited edition sneakers and fifty thousand people hit your site simultaneously at release time?

๐Ž๐ง๐ž ๐ฌ๐ž๐ซ๐ฏ๐ž๐ซ ๐ฐ๐ข๐ฅ๐ฅ ๐œ๐จ๐ฅ๐ฅ๐š๐ฉ๐ฌ๐ž. It's not a question of if, it's when.

You can buy a bigger server (called vertical scaling), but even the biggest servers have limits, and they're expensive. A better solution is to use multiple smaller servers working together (called horizontal scaling with load balancing).

๐‡๐จ๐ฐ ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐  ๐š๐œ๐ญ๐ฎ๐š๐ฅ๐ฅ๐ฒ ๐ฐ๐จ๐ซ๐ค๐ฌ

Instead of having one server handling all traffic, you have multiple servers โ€“ five, ten, fifty, whatever you need โ€“ all capable of doing the same work. In front of these servers sits a ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ž๐ซ, which is like a traffic controller. Every incoming request hits the load balancer first, and the load balancer's job is to decide which server should handle that specific request.

Let's say you have five servers behind a load balancer. When a request comes in, the load balancer might:

  • Simply pick the server currently handling the fewest requests (called l๐ž๐š๐ฌ๐ญ ๐œ๐จ๐ง๐ง๐ž๐œ๐ญ๐ข๐จ๐ง๐ฌ method)
  • Rotate through servers in order (called ๐ซ๐จ๐ฎ๐ง๐-๐ซ๐จ๐›๐ข๐ง)
  • Check which server has the lowest CPU usage and send the request there (called l๐ž๐š๐ฌ๐ญ ๐ซ๐ž๐ฌ๐ฉ๐จ๐ง๐ฌ๐ž ๐ญ๐ข๐ฆ๐ž)

The beautiful part? From the user's perspective, they have no idea this is happening. They visit yoursite.com and get a response. They don't know whether Server 1, Server 3, or Server 5 actually handled their request. ๐—œ๐˜ ๐—ท๐˜‚๐˜€๐˜ ๐˜„๐—ผ๐—ฟ๐—ธ๐˜€.

๐‘๐ž๐š๐ฅ-๐ฐ๐จ๐ซ๐ฅ๐ ๐ž๐ฑ๐š๐ฆ๐ฉ๐ฅ๐ž: ๐‡๐จ๐ฐ ๐’๐ก๐จ๐ฉ๐ซ๐ข๐ญ๐ž ๐œ๐ก๐ž๐œ๐ค๐จ๐ฎ๐ญ ๐ฐ๐จ๐ซ๐ค๐ฌ

You've been to Shoprite during the weekend rush. There are fifteen checkout counters. Imagine if they only opened one counter and everyone had to queue there. The line would stretch to the back of the store, people would abandon their shopping, and the whole system would collapse.

Instead, they open multiple counters and put someone near the entrance directing people: "Counter five is free, counter eight has a short line." ๐“๐ก๐š๐ญ ๐ฉ๐ž๐ซ๐ฌ๐จ๐ง ๐ข๐ฌ ๐š ๐ก๐ฎ๐ฆ๐š๐ง ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ž๐ซ. They're distributing the workload (customers) across available resources (cashiers) to prevent any single resource from becoming overwhelmed.

When one cashier is faster than others or when one takes a break, the person directing customers adjusts their strategy. They send more people to the faster cashiers and fewer to the slower ones. This is exactly what digital load balancers do with server traffic.

๐–๐ก๐š๐ญ ๐ก๐š๐ฉ๐ฉ๐ž๐ง๐ฌ ๐ฐ๐ก๐ž๐ง ๐ญ๐ก๐ข๐ง๐ ๐ฌ ๐ ๐จ ๐ฐ๐ซ๐จ๐ง๐ 

Remember when everyone tried to register for COVID vaccines on the NCDC portal or when JAMB registration opens? The sites crashed immediately because they couldn't handle the load. ๐“๐ก๐š๐ญ'๐ฌ ๐š ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐  ๐Ÿ๐š๐ข๐ฅ๐ฎ๐ซ๐ž.

Compare that to Amazon during Black Friday sales. Millions of people hit their site simultaneously, and it just works. Amazon has thousands of servers behind sophisticated load balancers. When traffic spikes, their system automatically spins up more servers and the load balancer starts directing traffic to them. When traffic drops, they shut down the extra servers to save costs.

๐‘ป๐’‰๐’† ๐’…๐’Š๐’‡๐’‡๐’†๐’“๐’†๐’๐’„๐’† ๐’ƒ๐’†๐’•๐’˜๐’†๐’†๐’ ๐’”๐’Š๐’•๐’†๐’” ๐’•๐’‰๐’‚๐’• ๐’„๐’“๐’‚๐’”๐’‰ ๐’‚๐’๐’… ๐’”๐’Š๐’•๐’†๐’” ๐’•๐’‰๐’‚๐’• ๐’”๐’„๐’‚๐’๐’† ๐’”๐’Ž๐’๐’๐’•๐’‰๐’๐’š ๐’๐’‡๐’•๐’†๐’ ๐’„๐’๐’Ž๐’†๐’” ๐’…๐’๐’˜๐’ ๐’•๐’ ๐’˜๐’‰๐’†๐’•๐’‰๐’†๐’“ ๐’•๐’‰๐’†๐’š'๐’—๐’† ๐’Š๐’Ž๐’‘๐’๐’†๐’Ž๐’†๐’๐’•๐’†๐’… ๐’‘๐’“๐’๐’‘๐’†๐’“ ๐’๐’๐’‚๐’… ๐’ƒ๐’‚๐’๐’‚๐’๐’„๐’Š๐’๐’ˆ.

๐ƒ๐ข๐Ÿ๐Ÿ๐ž๐ซ๐ž๐ง๐ญ ๐ญ๐ฒ๐ฉ๐ž๐ฌ ๐จ๐Ÿ ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐ 

๐‡๐š๐ซ๐๐ฐ๐š๐ซ๐ž ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ž๐ซ๐ฌ are physical devices that sit in data centers and route traffic. They're expensive but extremely fast. Big companies with their own data centers use these.

๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ž๐ซ๐ฌ run as applications on regular servers. Tools like Nginx, HAProxy, and cloud services like AWS Elastic Load Balancer fall into this category. They're cheaper and more flexible than hardware options.

๐ƒ๐๐’ ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐  uses the domain name system itself to distribute traffic. When someone looks up yoursite.com, the DNS can return different IP addresses for different users, sending some to Server A and others to Server B.

๐†๐ฅ๐จ๐›๐š๐ฅ ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐  distributes traffic across different geographic regions. If you have servers in America, Europe, and Asia, a global load balancer can send American users to American servers and Nigerian users to the closest available server, reducing latency.

๐“๐ก๐ž ๐ก๐ž๐š๐ฅ๐ญ๐ก ๐œ๐ก๐ž๐œ๐ค ๐ฆ๐ž๐œ๐ก๐š๐ง๐ข๐ฌ๐ฆ โ€“ ๐ญ๐ก๐ž ๐ฌ๐ž๐œ๐ซ๐ž๐ญ ๐ญ๐จ ๐ซ๐ž๐ฅ๐ข๐š๐›๐ข๐ฅ๐ข๐ญ๐ฒ

Here's a critical feature: load balancers constantly check if your servers are actually healthy. Every few seconds, the load balancer sends a small test request to each server (called a ๐ก๐ž๐š๐ฅ๐ญ๐ก ๐œ๐ก๐ž๐œ๐ค). If a server doesn't respond or responds with an error, the load balancer marks it as unhealthy and stops sending traffic there.

This is why load-balanced systems are more reliable than single-server setups. If one server crashes, the load balancer notices within seconds and routes all traffic to the remaining healthy servers. ๐”๐ฌ๐ž๐ซ๐ฌ ๐ฆ๐ข๐ ๐ก๐ญ ๐ง๐จ๐ญ ๐ž๐ฏ๐ž๐ง ๐ง๐จ๐ญ๐ข๐œ๐ž ๐ญ๐ก๐ž ๐Ÿ๐š๐ข๐ฅ๐ฎ๐ซ๐ž because their requests just get handled by different servers. Meanwhile, engineers can fix or restart the failed server without taking the whole site offline.

๐’๐ž๐ฌ๐ฌ๐ข๐จ๐ง ๐ฉ๐ž๐ซ๐ฌ๐ข๐ฌ๐ญ๐ž๐ง๐œ๐ž โ€“ ๐ญ๐ก๐ž ๐ญ๐ซ๐ข๐œ๐ค๐ฒ ๐ฉ๐š๐ซ๐ญ

Load balancing gets complicated when your application needs to remember things about users. If you log into a shopping site and add items to your cart, that information is stored on whichever server handled your requests. But what if your next request goes to a different server? That server doesn't know about your cart.

This is called the ๐ฌ๐ž๐ฌ๐ฌ๐ข๐จ๐ง ๐ฉ๐ž๐ซ๐ฌ๐ข๐ฌ๐ญ๐ž๐ง๐œ๐ž ๐ฉ๐ซ๐จ๐›๐ฅ๐ž๐ฆ or sticky sessions. Solutions include:

๐’๐ญ๐ข๐œ๐ค๐ฒ ๐ฌ๐ž๐ฌ๐ฌ๐ข๐จ๐ง๐ฌ:The load balancer remembers which server you used and always sends your requests there. This works but reduces flexibility.

๐’๐ก๐š๐ซ๐ž๐ ๐ฌ๐ž๐ฌ๐ฌ๐ข๐จ๐ง ๐ฌ๐ญ๐จ๐ซ๐š๐ ๐ž: All servers store session data in a central database or cache (like Redis) that they all access. This way, any server can handle any user's request because the session data isn't tied to a specific server.

๐’๐ญ๐š๐ญ๐ž๐ฅ๐ž๐ฌ๐ฌ ๐๐ž๐ฌ๐ข๐ ๐ง: The best solution is making your servers stateless, meaning they don't store user information locally. Instead, authentication tokens or session data get sent with every request. This way, truly any server can handle any request.

๐‹๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐  ๐š๐ญ ๐๐ข๐Ÿ๐Ÿ๐ž๐ซ๐ž๐ง๐ญ ๐ฅ๐š๐ฒ๐ž๐ซ๐ฌ

You can load balance at different levels of your architecture:

๐€๐ฉ๐ฉ๐ฅ๐ข๐œ๐š๐ญ๐ข๐จ๐ง ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐ : Distributing web requests across multiple web servers. This is what most people mean when they say load balancing.

๐ƒ๐š๐ญ๐š๐›๐š๐ฌ๐ž ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐ : Distributing read queries across multiple database replicas. Your main database handles writes, but multiple read replicas handle read requests, preventing the main database from being overwhelmed.

๐Œ๐ข๐œ๐ซ๐จ๐ฌ๐ž๐ซ๐ฏ๐ข๐œ๐ž๐ฌ ๐ฅ๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐ : If your app is split into multiple services (user service, payment service, notification service), each service might have multiple instances behind its own load balancer.

๐‚๐จ๐ง๐ง๐ž๐œ๐ญ๐ข๐ง๐  ๐ž๐ฏ๐ž๐ซ๐ฒ๐ญ๐ก๐ข๐ง๐  ๐ฐ๐ž'๐ฏ๐ž ๐ฅ๐ž๐š๐ซ๐ง๐ž๐

Day 1 taught us about geographic distribution with Netflix's Open Connect. Day 2 showed us efficiency through proven technology with WhatsApp's Erlang. Day 3 explained caching. ๐‹๐จ๐š๐ ๐›๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐  ๐œ๐จ๐ง๐ง๐ž๐œ๐ญ๐ฌ ๐š๐ฅ๐ฅ ๐จ๐Ÿ ๐ญ๐ก๐ž๐ฌ๐ž ๐œ๐จ๐ง๐œ๐ž๐ฉ๐ญ๐ฌ.

Load balancers can use geographic information to route users to the nearest data center (like Netflix does). They can distribute work efficiently across servers (like WhatsApp's philosophy of doing more with less). They work hand-in-hand with caching because cached content can be served from any server without hitting the database.

๐’๐ฒ๐ฌ๐ญ๐ž๐ฆ ๐๐ž๐ฌ๐ข๐ ๐ง ๐ข๐ฌ๐ง'๐ญ ๐š๐›๐จ๐ฎ๐ญ ๐ฎ๐ฌ๐ข๐ง๐  ๐จ๐ง๐ž ๐ญ๐ž๐œ๐ก๐ง๐ข๐ช๐ฎ๐ž. ๐ˆ๐ญ'๐ฌ ๐š๐›๐จ๐ฎ๐ญ ๐œ๐จ๐ฆ๐›๐ข๐ง๐ข๐ง๐  ๐ฆ๐ฎ๐ฅ๐ญ๐ข๐ฉ๐ฅ๐ž ๐ฌ๐ญ๐ซ๐š๐ญ๐ž๐ ๐ข๐ž๐ฌ ๐ญ๐จ ๐›๐ฎ๐ข๐ฅ๐ ๐ฌ๐จ๐ฆ๐ž๐ญ๐ก๐ข๐ง๐  ๐ซ๐ž๐ฅ๐ข๐š๐›๐ฅ๐ž ๐š๐ง๐ ๐Ÿ๐š๐ฌ๐ญ.

What this means for your projects

If you're building something small, you probably don't need load balancing yet. But understanding it prepares you for scale. Many cloud providers make it easy to add load balancing when you need it. You can start with one server and add a load balancer plus additional servers when traffic grows.

The mental model matters more than the implementation details. When you're designing any system, ask yourself: ๐–๐ก๐š๐ญ ๐ก๐š๐ฉ๐ฉ๐ž๐ง๐ฌ ๐ข๐Ÿ ๐ญ๐ซ๐š๐Ÿ๐Ÿ๐ข๐œ ๐๐จ๐ฎ๐›๐ฅ๐ž๐ฌ ๐ญ๐จ๐ฆ๐จ๐ซ๐ซ๐จ๐ฐ? ๐–๐ก๐š๐ญ ๐ข๐Ÿ ๐ข๐ญ ๐ข๐ง๐œ๐ซ๐ž๐š๐ฌ๐ž๐ฌ ๐ญ๐ž๐ง ๐ญ๐ข๐ฆ๐ž๐ฌ? Load balancing is the answer to those questions.

Join the dev Community

๐“๐จ๐ฆ๐จ๐ซ๐ซ๐จ๐ฐ (๐ƒ๐š๐ฒ ๐Ÿ“): How does Google show you search results in 0.3 seconds when they're searching billions of web pages? The secret isn't speed, it's pre-computation. We're breaking down why fast apps don't work harder; they work smarter.

JOIN THE CLASS AND SEE HOW LOAD BALANCING COULD MAKE YOUR APP FASTER AND MORE RELIABLE {https://ssic.ng}

Drop a ๐Ÿ”ฅ if you finally understand why Amazon never crashes, but your favorite local site always does during sales.


SystemDesign #LoadBalancing #WebDevelopment #SoftwareEngineering #BackendDevelopment #Scalability #CloudComputing #DevOps #TechArchitecture #DistributedSystems #HighAvailability #SoftwareArchitecture #NigerianDevelopers #TechCommunity #CodingTips #tcm #creativethinkes #claymic

Top comments (1)

Collapse
 
innovated_dev profile image
Creative Coder

let's dive deep