- Level Up Coding
- Posts
- LUC #86: Load Balancing Algorithms You Should Know
LUC #86: Load Balancing Algorithms You Should Know
Plus, best practices to optimize API endpoints, understanding the OSI model, and the main components of Docker explained

This week’s issue brings you:
Load Balancing Algorithms You Should Know
Understanding The OSI Model (Recap)
READ TIME: 5 MINUTES
Thanks to our partners who keep this newsletter free to the reader.
Your New Favorite CLI Tool
Meet Amazon Q Developer CLI – the pair programmer that lives in your terminal. Write code, run scripts, automate tasks, and call APIs with a single prompt. No tab-switching. No context loss. Just you, your terminal, and a smarter way to build.

Load Balancing Algorithms You Should Know
Picture this: it’s launch day. Traffic is surging. Your team’s hard work is finally going live, but suddenly, servers start to crumble under uneven load. Chaos. Downtime. Frustrated users. The culprit? A poorly chosen load balancing algorithm.
Don't let this be your story.
Let’s explore six fundamental load balancing strategies, understand their trade-offs, and when to deploy them for maximum stability and efficiency.
Static Algorithms: Simple, Fast, and Sometimes Blind
Static load balancing distributes requests using predefined logic, without considering real-time server conditions. These are easy to implement and come with low overhead, but they often lack adaptability.
1. Round Robin
Each incoming request is passed to the next server in line, cycling through all options in sequence.
Pros: Easy to implement, works well when all servers are identical.
Cons: Doesn’t account for current server load, assumes all requests and servers are equal.
Round robin is great when you control server specs and request types are homogenous, but it can fall apart under asymmetric traffic patterns.

2. Random
Requests are distributed randomly across the available servers.
Pros: Super simple to implement.
Cons: Can lead to uneven loads; not ideal when consistency or performance matters.
Best used in low-stakes, low-traffic environments or as a fallback in failover configurations.

3. IP Hash
This algorithm hashes the client’s IP address and maps it to a specific server. The same client will consistently be routed to the same server.
Pros: Supports session persistence ("sticky sessions") without server-side state tracking.
Cons: Doesn’t adapt to traffic or server health changes. Also struggles if IP distribution is skewed.
IP hash works well when session stickiness is critical—like in e-commerce carts or user-specific dashboards—especially if you can’t afford shared session stores.

4. Weighted Round Robin
This builds on round robin by giving servers different weights based on capacity. For instance, a server with twice the resources might receive twice as many requests.
Pros: Accounts for server capacity, improving distribution balance.
Cons: Still doesn’t respond to real-time traffic or CPU spikes.
Weighted round robin is a solid upgrade over basic round robin, especially in mixed-hardware environments or cloud deployments with instance variety.

Dynamic Algorithms: Adaptive and Smarter
Dynamic algorithms take current server conditions into account, like active connections or response times, to make smarter routing decisions.
5. Least Connections
New requests are sent to the server with the fewest active connections. This assumes that active connections correlate with server load.
Pros: Dynamically balances requests based on real-time usage.
Cons: Not always accurate; some connections may be idle or lightweight.
This is a strong choice for long-lived connections (eg; WebSockets, HTTP/2) where connection count is a decent proxy for resource consumption.

6. Least Response Time
Requests are routed to the server with the fastest response time based on metrics like latency and processing duration.
Pros: Optimizes for end-user experience by favoring faster servers.
Cons: Requires active health checks and reliable metrics collection. Can oscillate under load if not smoothed.
This strategy is perfect for latency-sensitive apps, especially when performance varies across servers or workloads.

Choosing the Right Algorithm
There's no one-size-fits-all solution. Consider these key factors:
Workload uniformity: Are all requests roughly equal in complexity?
Session state: Do you need sticky sessions?
Server variability: Are all instances identical, or do some have significantly more horsepower?
Traffic patterns: Do you regularly experience bursts, or is traffic relatively predictable? Are requests short-lived or persistent?
In many production environments, load balancers like NGINX, HAProxy, AWS ELB, or Envoy enable combining algorithms or fine-tuning behaviors with custom logic and health checks.
Final Thoughts
Understanding load balancing algorithms isn't just academic; it's foundational to building resilient and performant systems.
A poorly matched algorithm can quickly lead to performance bottlenecks, unreliable systems, or excessive latency.
The right one ensures your application scales smoothly, gracefully absorbs traffic spikes, and stays robust under server or network issues.
Best Practices to Optimize API Endpoints (Recap)
Performance:
Optimize queries – Use execution plans and cache frequent ones.
Cache smartly – Client/server/CDN caching + proper invalidation.
Shrink payloads – Gzip, trim fields, and use efficient formats.
Paginate data – Use
limit/offset
or cursors to avoid overload.Go async – Offload heavy tasks to background jobs.
Security:
Rate limit – Throttle traffic to prevent abuse and spikes.
Validate inputs – Block SQL/XSS attacks with strict checks.
Monitor everything – Log errors, latency, and usage.
Secure access – Use OAuth2, API keys, or JWT.
Encrypt in transit – Always use HTTPS.

Understanding The OSI Model (Recap)
The OSI Model is a conceptual framework that standardizes how different systems communicate over a network. It divides network interactions into seven layers, each responsible for specific functions, ensuring data flows from one device to another in a structured way. Key concepts include:
Encapsulation: as data moves from the top (Application layer) to the bottom (Physical layer), each layer adds its own header, wrapping the data for transmission.
Decapsulation: on the receiving end, the process reverses as each layer removes its corresponding header, eventually delivering the original data to the application.
The OSI model promotes interoperability across different systems, simplifies troubleshooting, and ensures reliable, consistent communication across global networks.
Layers: Application, Presentation, Session, Transport, Network, Data Link, Physical.

The Main Components of Docker Explained (Recap)
Image: A read-only template for creating containers. Contains application code, libraries, and dependencies.
Container: An instance of an image. It is a lightweight and standalone executable package that includes everything needed to run an application.
Dockerfile: A script-like file that defines the steps to create a Docker image.
Docker engine: Responsible for running and managing containers. Consists of the daemon, a REST API, and a CLI.
Docker daemon: A background service responsible for managing Docker objects.
Docker registry: Repositories where Docker images are stored and can be distributed from; can be private or public.
Docker network: Provides the communication gateway between containers running on the same or different hosts; allowing them to communicate with each other and the outside world.
Volumes: Allow data to persist outside of containers and to be shared between container instances.

That wraps up this week’s issue of Level Up Coding’s newsletter!
Join us again next week, where we’ll explore and visually distil more important engineering concepts.