Load Balancer¶
A load balancer is a component that distributes incoming network traffic across multiple backend servers to improve availability, throughput, and fault tolerance.
In a PHP deployment, the load balancer sits in front of a pool of PHP application servers (typically PHP-FPM behind Nginx or Apache). When a request arrives, the load balancer forwards it to one of the available servers according to a strategy: round-robin, least connections, IP hash, or weighted distribution.
Because requests can land on any server, PHP applications behind a load balancer must be stateless or externalise their state: + Sessions must be stored in a shared backend server, not on the local filesystem + Uploaded files must be written to shared storage, like NFS, S3-compatible object store + Caches should be shared or invalidated across all nodes + Any resource that must persist between calls must be stored in a common server
A load balancer also provides health checks, in order to remove unhealthy nodes automatically, SSL termination, and sticky sessions, where it is routing a user’s requests to the same backend when true statelessness is not possible.
<?php
// Detect the real client IP behind a trusted load balancer
$clientIp = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
?>
See also Load Balancing PHP Applications and AWS Elastic Load Balancing with PHP.
Related : Horizontal Scaling, Stateless, Session, Distributed State, Server Architecture, Cache, Docker