Last updated on

Proxies and Load Balancing

🧍 Forward Proxies

A forward proxy is a middle server that sits between the client (user) and the destination server.

  • Function: It takes a client’s request, forwards it to the actual destination server on the client’s behalf, and then relays the response back to the client.
  • Key Action: It hides the client’s identity (e.g., their IP address) from the destination server.
  • Analogy (Simplified): It’s like having a friend do something for you, hiding your involvement.
  • Use Cases:
    • Identity Protection: Hides the client’s IP address.
    • Bypassing Restrictions: Allows a client to access a server that they are otherwise restricted from accessing (e.g., due to geographical or network blocks), as long as they can access the proxy.
    • Blocking Content: Can be used on corporate or school networks to block access to certain sites or resources (e.g., YouTube) by setting rules within the proxy.
  • Real-World Examples: Corporate network proxies, VPNs (Virtual Private Networks).

🔄 Reverse Proxies

A reverse proxy is a middle server that sits between the client (user) and the destination server(s), but in this case, it’s used to hide the actual server infrastructure.

  • Function: It accepts a request from the client and then forwards it to one of the origin servers that is behind it. It then sends the response from the origin server back to the client.
  • Key Action: It hides the destination server’s identity and abstracts the server infrastructure away from the client.
  • Client’s View: The client only knows about and interacts with the reverse proxy; they are unaware of the existence of the actual origin servers.
  • Real-World Examples:
    • CDNs (Content Delivery Networks): Clients interact with the CDN (the reverse proxy/cache), which may then make a request to the origin server if needed, without the client knowing.
    • Load Balancers: A special and very important type of reverse proxy.

⚖️ Load Balancers

A load balancer is a specific type of reverse proxy designed to distribute incoming network traffic across a group of backend servers. This is essential for handling high user traffic.

  • Purpose: To horizontally scale the application by distributing the load (traffic) evenly among multiple servers to prevent any single server from being overwhelmed.
  • Concept: A user’s request hits the load balancer first, and the load balancer decides which server among the replicated servers should handle the request.
  • Server Requirement: The backend servers are typically stateless (implemented with REST or similar principles) to allow for seamless scaling and distribution.

Load Balancing Algorithms

The load balancer uses algorithms to determine how to distribute traffic:

  1. Round Robin:
    • Mechanism: Requests are routed sequentially to the servers in a rotating cycle (Server 1, Server 2, Server 3, Server 1, etc.).
    • Result: Each server receives an equal amount of traffic.
  2. Weighted Round Robin:
    • Mechanism: Used when servers have different processing capabilities (power). Servers are assigned a “weight,” and traffic is routed proportionally to that weight.
    • Example: A server with a weight of 10 gets twice as much traffic as a server with a weight of 5.
  3. Least Connections:
    • Mechanism: A request is routed to the server that currently has the smallest number of active connections.
    • Benefit: This accounts for the fact that some requests take longer to process, ensuring that requests aren’t sent to a busy server just because it’s next in a simple rotation.
  4. User Location-Based Balancing:
    • Mechanism: Requests are routed to the server that is geographically closest to the client to minimize latency.
  5. Hashing:
    • Mechanism: A field from the request (e.g., IP address, user ID, or content of the request) is hashed, and the hash value is used to consistently route that user or request type to the same server.

Load Balancer Layers

Load balancers can operate at different layers of the network stack, offering trade-offs between speed and flexibility:

FeatureLayer 4 Load Balancer (Transport/TCP)Layer 7 Load Balancer (Application/HTTP)
LayerLower (TCP/Transport)Higher (HTTP/Application)
Data AccessLooks only at IP addresses and ports. Cannot access application data.Can look at application data (e.g., URL, headers).
SpeedFasterSlower
FlexibilityLess Flexible (Can only use simple methods like Round Robin or Location).More Powerful/Flexible (Can intelligently route based on resource, e.g., send “tweet” requests to the “tweets” server).
ImplementationForwards the request and replaces the destination IP.Decrypts the request, establishes a new connection, and forwards it (more expensive).

Single Point of Failure (SPOF)

  • Problem: If there is only a single load balancer, it becomes a Single Point of Failure—if it goes down, the entire application becomes inaccessible, regardless of how many servers are behind it.
  • Solution: Implement multiple replicas of the load balancer, either with requests going to all of them or by having a backup load balancer take over if the primary one fails.