> For the complete documentation index, see [llms.txt](https://notes.davidfeng.us/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.davidfeng.us/resources/load-balancer.md).

# Load Balancer

<https://opensource.com/article/18/10/internet-scale-load-balancing>

## Load Balancing - High Availability

### No load balancing

One server, one IP.

### DNS (Domain Name System) load balancing

Two servers, two IPs. DNS is cached, what if one server goes down?

### Network load balancing

One data center: one load balancer + many backend servers.

Layer 4 (TCP/IP) load balancer: can be hardware or software. DNS points one virtual IP, and it points to different backend servers (based on a hash of source/destination IP address/port and protocal (TCP/UDP)). Does not maintain state per connection.

L4 balancers can do health-checking, weighted balancing (for backends with different capacities).

### Going multi-site

More data center: The edge routers in both data centers advertise the same VIP. Request sent to that VIP can reach either site (Anycast). Most of the time it works. If one site fails, it stops advertising the VIP, and traffic goes to the other site.

Problems: can't control where traffic flows or limit how much traffic is sent to a given site. Can't explicitly route users to the nearest site (but usually users end up with nearest site).

### Controlling inbound requests in a multi-site system

Use different VIPs for each site and use geo-aware DNS to balance them with simple or weighted round-robin. New problems: DNS, cache records. Cannot redirect traffic quickly.

### Layer 7 load balancing

Aware of the structure and contents of requests. L7 load balancer can do: caching, rate limiting, fault injection, cost-aware load balancing (some requests require much more server time to process). They can also balance based on a request's attributes (e.g. HTTP cookies), terminate SSL connections, and help defend against application layger DOS attacks. Problems: cost. Usually L4 balancers are running in front of one or more pools of L7 balancers.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.davidfeng.us/resources/load-balancer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
