CDN vs Load Balancer

Both improve performance and availability, but they operate at different layers of your infrastructure.

A CDN (Content Delivery Network) caches and serves content from geographically distributed edge servers to reduce latency, while a load balancer distributes incoming traffic across multiple backend servers to prevent overload. CDNs optimize content delivery to end users, and load balancers optimize request distribution across your infrastructure. Most production architectures use both together.

CDN

A CDN is a globally distributed network of servers that caches static and sometimes dynamic content close to end users. When a user requests a resource, the CDN serves it from the nearest edge location rather than routing the request all the way to the origin server. This dramatically reduces latency, lowers bandwidth costs, and provides resilience against traffic spikes and DDoS attacks. Major CDN providers include Cloudflare, AWS CloudFront, Akamai, and Fastly. CDNs are essential for serving images, JavaScript bundles, CSS files, videos, and other assets efficiently. Modern CDNs also support edge computing, allowing you to run logic at edge nodes for tasks like A/B testing, geolocation-based routing, and request transformation.

Load Balancer

A load balancer sits between clients and a pool of backend servers, distributing incoming requests to prevent any single server from becoming overwhelmed. It monitors server health and routes traffic only to healthy instances, providing both performance and high availability. Load balancers operate at different layers: Layer 4 (TCP/UDP) balancers route based on IP and port, while Layer 7 (HTTP) balancers can inspect request content for smarter routing decisions. Popular options include NGINX, HAProxy, AWS ALB/NLB, and Google Cloud Load Balancing. Load balancers support various algorithms like round-robin, least connections, and weighted distribution. They are essential for horizontal scaling, enabling you to add or remove servers without downtime.

Key Differences

- **Primary purpose**: CDNs cache and serve content closer to users to reduce latency. Load balancers distribute traffic across backend servers to prevent overload. - **Location**: CDNs operate at the edge, geographically close to end users. Load balancers typically sit in front of your origin servers within your data center or cloud region. - **Content handling**: CDNs cache static assets and can serve them without hitting your origin. Load balancers forward every request to a backend server. - **Scope**: CDNs span multiple geographic regions globally. Load balancers typically operate within a single region or data center. - **Traffic type**: CDNs primarily optimize read-heavy, cacheable content. Load balancers handle all types of traffic including dynamic API requests and database connections.

When to Use Each

**Use a CDN** when you serve static assets (images, scripts, stylesheets, videos) to a geographically distributed audience, want to reduce origin server load for cacheable content, or need DDoS protection at the edge. **Use a Load Balancer** when you run multiple instances of an application and need to distribute requests evenly, require health checking and automatic failover, or need to route traffic based on request attributes like URL path or headers. **Use both together** in most production setups: the CDN handles cacheable content at the edge, and the load balancer distributes cache misses and dynamic requests across your backend fleet.

Analogy

**A CDN** is like a chain of local warehouses: instead of shipping every order from a central factory, you stock popular items at warehouses near customers so deliveries arrive faster. **A Load Balancer** is like a traffic officer at a busy intersection: they direct cars to different lanes and routes so no single road gets jammed, keeping traffic flowing smoothly.