Server Farms & Load Balancing Sockets: Server Design

 

Part 1: Server Farms & Load Balancing
Chapter 60 – Sockets: Server Design | TLPI Series

The Problem: Too Many Clients

A single server process can only handle so many clients before it becomes the bottleneck. When client load is very high, we need ways to spread the work across multiple machines. A group of server machines working together to serve clients is called a server farm.

There are two common approaches to building a server farm:

  • DNS Round-Robin Load Sharing – cheap and simple, uses DNS itself
  • Dedicated Load-Balancing Server – more complex but more powerful

Approach 1: DNS Round-Robin Load Sharing

How It Works

In normal DNS, one domain name maps to one IP address. In DNS round-robin, the authoritative DNS server for a zone maps the same domain name to several IP addresses. Each IP belongs to a different physical server.

When a client asks “what is the IP of api.example.com?”, the DNS server answers with all the IPs but rotates their order each time. Client 1 gets Server A first. Client 2 gets Server B first. Client 3 gets Server C first. And so on — round-robin style.

DNS Round-Robin Flow
Client
DNS Server
api.example.com
Server A: 10.0.0.1
Server B: 10.0.0.2
Server C: 10.0.0.3
Each DNS response returns IPs in a different order (rotating)

Advantages

  • Inexpensive – No extra hardware needed, just a DNS configuration change
  • Easy to set up – Supported by all major DNS server software

Problems with DNS Round-Robin

  • Remote DNS caching: When a remote DNS server resolves your domain, it caches the result for the TTL (time-to-live) duration. All clients using that DNS server will always hit the same backend server — completely bypassing the round-robin. The rotation only happens at your authoritative DNS, not at cached copies.
  • No true load balancing: Different clients place different loads on a server. One client may download 100MB while another just fetches 1KB. Round-robin distributes connections equally, not load equally.
  • No high availability: If one of the backend servers crashes, DNS round-robin has no mechanism to detect this and stop sending clients to the dead server.
  • Server affinity challenges: A sequence of requests from the same client may go to different servers (because DNS cache expires and a different IP is returned). If Server A stored session data for that client, Server B won’t have it.

Approach 2: Dedicated Load-Balancing Server

How It Works

A single load-balancing server sits in front of all the backend servers. To the outside world, the server farm looks like one machine — it has one IP address (the load balancer’s IP). All client connections go to the load balancer first, and it decides which backend server should handle each request.

Load Balancer Architecture
Client 1
Client 2
Client 3
Load Balancer
Single Public IP
(+ Backup LB)
Backend Server A
Backend Server B
Backend Server C

What a Load Balancer Provides

  • Eliminates DNS caching problem: Because the server farm presents only one IP to the world, clients always go through the load balancer — no DNS caching issues.
  • Intelligent load distribution: The load balancer uses algorithms that consider actual server load (CPU, memory, active connections) — not just connection count. Backend servers can report their metrics to the load balancer to help it decide.
  • Automatic failure detection: The load balancer health-checks backend servers regularly. If a backend server crashes, the load balancer stops sending traffic to it automatically.
  • Server affinity support: The load balancer can use techniques like sticky sessions or session cookies to ensure a client always goes back to the same backend server for the duration of a session.
  • High availability: To avoid the load balancer itself becoming a single point of failure, a backup load balancer is typically deployed. If the primary crashes, the backup takes over.

Key Concept: Server Affinity

Server affinity (also called session persistence or sticky sessions) means ensuring that all requests from the same client always go to the same backend server.

Why does this matter? Many server applications maintain state about a client — for example, a login session, a shopping cart, or a partially processed file upload. If Client X talks to Server A and logs in, Server A stores that session. If the next request from Client X goes to Server B, Server B has no idea who this client is — it sees an unauthenticated request and may reject it.

Real-world example: A user fills in a multi-step checkout form. Step 1 goes to Server A which stores the cart. Step 2 goes to Server B (no affinity) — the cart is gone. Server affinity ensures all steps go to Server A.

Quick Comparison: DNS Round-Robin vs Load Balancer
Feature DNS Round-Robin Load Balancer
Cost & Complexity Low – just DNS config Higher – needs dedicated server/software
True Load Balancing No – just connection rotation Yes – metrics-based distribution
Failure Detection None Yes – health checks
Server Affinity Difficult Yes – built-in support
DNS Caching Issue Yes – bypasses round-robin None – single IP presented

Interview Questions & Answers
Q1. What is a server farm and why is it needed?
A server farm is a group of multiple server machines that work together to handle client requests. It is needed when a single server cannot handle the total client load — either due to CPU, memory, bandwidth, or connection limits. The farm allows horizontal scaling.
Q2. What is DNS round-robin and what are its limitations?
DNS round-robin maps a single domain name to multiple IP addresses. The DNS server rotates through them for each request, spreading clients across multiple servers. Its main limitations are: remote DNS servers cache results bypassing the rotation, it doesn’t account for actual load, it has no failure detection, and it doesn’t easily support server affinity.
Q3. What is server affinity and why is it important?
Server affinity ensures that all requests from the same client always reach the same backend server. It is important when the server maintains per-client state such as login sessions, partially uploaded files, or in-progress transactions. Without affinity, a client could reach a different server that has no record of the client’s current state.
Q4. How does a load-balancing server improve on DNS round-robin?
A load balancer presents a single IP to the outside world (no DNS caching problem), uses server metrics to make intelligent routing decisions, automatically detects failed backend servers and stops sending traffic to them, and can support server affinity through sticky sessions. It can also have a backup instance for high availability.
Q5. What is the single point of failure risk in a load-balancer setup and how is it addressed?
The load balancer itself can become a single point of failure — if it crashes, all client connections are lost. This is addressed by deploying a backup (standby) load balancer that automatically takes over if the primary fails, typically using techniques like virtual IP failover (e.g., keepalived on Linux).

Leave a Reply

Your email address will not be published. Required fields are marked *