Reverse Proxy vs CDN: Key Differences
A reverse proxy and a content delivery network (CDN) both stand in front of your servers and both can cache responses. The difference is where they run. A reverse proxy usually sits in your own data centre or cloud region. A CDN runs hundreds of servers around the world, close to your users.
This tutorial explains how they relate, how caching flows from the edge to the origin, and how to control it with headers. For background, see Reverse Proxy and What Is a CDN.
1. What Are a Reverse Proxy and a CDN?
A reverse proxy is a server in front of your application servers that receives client requests, forwards them, and can terminate TLS, cache, compress and route traffic. There is typically one logical proxy tier per region.
A CDN is a globally distributed network of edge servers, called points of presence (PoPs), operated by a provider. Users are routed to a nearby PoP, which serves cached content or fetches it from your origin. In practice, a CDN is a planet-wide fleet of caching reverse proxies.
2. Edge and Origin Architecture
The CDN absorbs most traffic at the edge. Only cache misses and dynamic requests travel to the origin, where your reverse proxy handles them.
3. How a Request Flows
- A user in Berlin requests
https://cdn.shop.example/img/mouse.jpg. DNS or anycast routing sends them to the Berlin PoP. - The edge checks its cache. On a hit it responds within a few milliseconds and the origin is never contacted.
- On a miss it requests the file from the origin, which is your reverse proxy.
- The reverse proxy serves it from its own cache or from an application server, with a
Cache-Controlheader such aspublic, s-maxage=600. - The edge stores the response for 600 seconds and returns it. The next Berlin user gets a cache hit.
Origin shield
With many PoPs, a popular file that expires can cause a miss at every edge at the same moment, and all of those requests hit your origin. Many CDNs offer an origin shield: one designated mid-tier cache that the edges ask first, so the origin receives one request instead of hundreds. Your own reverse proxy cache plays a similar role inside the data centre by protecting the application servers from repeated misses.
4. Key Concepts
Origin vs edge
The origin is the source of truth: your reverse proxy and app servers. The edge is the CDN copy near users.
Cache-Control directives
Freshness comes from the response's Cache-Control header. Every cache, the browser included, honours max-age; a CDN or proxy gives s-maxage precedence when it is present; private keeps a response out of CDNs and proxies; and no-store prevents storing it anywhere.
Invalidation and versioning
Purge a URL through the CDN API, or better, put a version or hash in file names (app.3f9a2c.js) so new releases use new URLs.
Dynamic acceleration
Even uncacheable API calls benefit from a CDN: TLS is terminated near the user, and the CDN keeps warm connections to the origin.
Cache key
The cache key decides which requests share a cached copy, usually the host, path and query string. Including unnecessary parts, such as tracking parameters or every cookie, splits the cache into many copies and lowers the hit ratio.
5. Practical Implementation
Origin reverse proxy (Nginx) with a cache
http {
proxy_cache_path /var/cache/nginx keys_zone=origin_cache:20m max_size=5g inactive=1h;
server {
listen 443 ssl;
server_name origin.shop.example;
ssl_certificate /etc/nginx/certs/origin.crt;
ssl_certificate_key /etc/nginx/certs/origin.key;
location /img/ {
root /var/www; # static files served directly
add_header Cache-Control "public, max-age=86400, immutable";
}
location /api/ {
proxy_pass http://10.0.1.11:8080;
proxy_cache origin_cache; # respects Cache-Control from the app
proxy_cache_use_stale error timeout updating;
}
}
}
Spring Boot: setting cache headers for the CDN
The application decides what may be cached. In this example the browser may reuse the product for 60 seconds, and the CDN may reuse it for 600.
package com.example.catalog;
import java.time.Duration;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
public record Product(long id, String name, long priceInCents) {
}
@GetMapping("/api/products/{id}")
public ResponseEntity<Product> product(@PathVariable long id) {
Product product = new Product(id, "Wireless Mouse", 2499);
return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(Duration.ofMinutes(1)) // browsers keep it 1 minute
.sMaxAge(Duration.ofMinutes(10)) // CDN and shared caches keep it 10 minutes
.cachePublic())
.body(product);
}
}
Output:
Cache-Control: max-age=60, public, s-maxage=600
That header value was generated by running Spring's CacheControl builder (Spring Framework 7) with the settings above. The controller compiles against Spring Web 7 on Java 17.
6. Advantages and Disadvantages
| Reverse proxy | CDN | |
|---|---|---|
| Main benefit | Full control over routing, security and caching | Low latency worldwide and huge capacity |
| Traffic protection | Limited by your own bandwidth | Absorbs large traffic spikes and many DDoS attacks |
| Dynamic content | Handles all application logic routing | Best for cacheable content; dynamic calls pass through |
| Drawbacks | Users far away still see high latency | Cost, vendor dependency, cache invalidation complexity |
7. Reverse Proxy vs CDN Comparison
| Aspect | Reverse proxy | CDN |
|---|---|---|
| Location | Your data centre or cloud region | Hundreds of edge PoPs worldwide |
| Operated by | Your team | A CDN provider |
| Main purpose | Front, route and protect your servers | Serve content close to users |
| Caching | Optional, one location | Core feature, distributed |
| Request routing | Paths and hosts to backends | Users to the nearest PoP (DNS or anycast) |
| Typical examples | Nginx, HAProxy, Envoy | Cloudflare, Akamai, Amazon CloudFront, Fastly |
8. Real-World Use Cases
- E-commerce: images, scripts and product listings come from the CDN; checkout APIs pass through to the origin reverse proxy.
- Video streaming: video segments are cached at the edge, keeping origin bandwidth low.
- Software downloads: large installers are distributed from PoPs near users.
- Internal apps: an intranet tool used in one office needs only a reverse proxy, not a CDN.
Deciding what to cache
- Static assets with hashed names: cache for a year at both layers with
immutable. - Public product and article pages: cache at the CDN for minutes and revalidate with
ETagorLast-Modified. - Personalised pages and account APIs: mark them
privateorno-store. - Search and listing APIs: short shared caching, such as 30 to 60 seconds, often removes most origin load during traffic spikes.
Review these rules whenever you add an endpoint. One missing private directive can expose one user's data to another.
9. Best Practices
- Set explicit cache headers: use
s-maxagefor the CDN and shortermax-agefor browsers. - Never cache personal data publicly: mark user-specific responses
privateorno-store. - Version static assets so you rarely need purges.
- Lock down the origin: accept traffic only from the CDN, for example with provider IP lists or a secret header, so attackers cannot bypass it.
- Serve stale on error: allow stale content when the origin is down to stay available.
- Monitor the cache hit ratio at both the edge and the origin.
Also plan for failure of the CDN itself. Keep DNS time-to-live values moderate and document how to send traffic straight to the origin, with the reverse proxy sized to absorb the extra load, if the CDN provider has an outage.
10. Summary
- A CDN is a global network of caching reverse proxies operated by a provider.
- Your reverse proxy sits at the origin and handles cache misses and dynamic requests.
- Cache-Control headers from the origin decide what the CDN may cache and for how long.
- CDNs reduce latency and absorb traffic spikes; reverse proxies give routing and control.
- Most public websites use both layers together.
Next, compare the reverse proxy with an API-focused entry point in API Gateway vs Reverse Proxy.
Reverse Proxy vs CDN: Top Interview Questions
These are the reverse proxy and CDN questions most often asked in system design interviews, with short answers you can explain in your own words.
Q1. Is a CDN a reverse proxy?
Yes, in function. Each CDN edge server receives requests for your site and serves cached content or fetches it from your origin. The difference is scale and location: a CDN runs many PoPs worldwide, operated by a provider.
Q2. Why must personalised responses never be cached by a CDN?
A CDN edge serves one stored copy to everyone who requests the same cache key. If an account page or a cart response were stored there, the next visitor could receive another person's data. Mark such responses private or no-store, and keep cookies and authorisation headers out of shared cache keys.
Q3. What happens on a CDN cache miss?
The edge forwards the request to the origin, usually your reverse proxy, stores the response according to its cache headers, and returns it to the user. Later requests at that edge are served from cache until the content expires.
Q4. How do you stop attackers from bypassing the CDN and hitting the origin directly?
Allow origin traffic only from the CDN, for example through a firewall with the provider's IP ranges, mutual TLS, or a secret header checked by the reverse proxy. Also avoid publishing the origin's address in DNS.