CDN and WebSockets: Real-Time Content Delivery

· Alex Petrov · 4 min read

WebSockets and CDN

WebSockets provide persistent, bidirectional connections between client and server. Traditional CDNs are built for request-response patterns (HTTP), not persistent connections. However, modern CDNs have added WebSocket support to serve real-time applications.

CDN Network Overview — Understanding the delivery architectureCDN Network Overview — Understanding the delivery architecture CDN Network Topology — Content Delivery Flow U User A Tokyo U User B London U User C New York Edge Server Tokyo PoP Edge Server London PoP Edge Server New York PoP Mid-Tier Cache Origin Shield Origin Server Your Server Request Cache Miss Fetch Cache Hit → Instant Response
Figure 1: CDN Network Overview — Understanding the delivery architecture

CDN WebSocket Support

ProviderWebSocket SupportNotes
CloudflareYes (all plans)Free WebSocket proxying, no extra cost
CloudFrontYesSupported in distributions, origin must support
FastlyYesVia connection upgrades in VCL
AkamaiYesWebSocket acceleration available

How CDN Proxies WebSockets

  1. Client sends HTTP request with Upgrade: websocket header
  2. CDN edge server forwards the upgrade request to origin
  3. Origin accepts the upgrade (101 Switching Protocols)
  4. CDN maintains the persistent connection, proxying frames bidirectionally
  5. CDN applies: TLS termination, DDoS protection, rate limiting, and connection monitoring

Challenges

No Caching

WebSocket messages are dynamic and bidirectional — they cannot be cached. Every message must traverse: client → CDN edge → origin. The CDN provides connection optimization but not the primary CDN benefit (caching).

Performance Comparison: Edge vs Origin Processing
Metric Edge Processing Origin Processing Improvement
Latency (p50)8–15 ms120–300 ms15–20x faster
Cache Hit Ratio85–95%N/AReduces origin load
TLS Handshake1–5 ms (nearby PoP)50–150 ms10–30x faster
Bandwidth Cost$0.01–0.04/GB$0.05–0.12/GB3–5x cheaper
Availability99.99% (multi-PoP)99.9% (single DC)10x fewer outages
Request Lifecycle — How content moves through the CDNRequest Lifecycle — How content moves through the CDN CDN Request Flow — From Browser to Edge CDN Request Lifecycle Step 1 DNS Lookup Step 2 TCP Connect Step 3 TLS Handshake Step 4 Edge Server Step 5 Cache Check Step 6 Response Cache HIT → 5ms response Cache MISS → Fetch origin 0ms ~50ms (cache hit) ~200ms (cache miss)
Figure 2: Request Lifecycle — How content moves through the CDN

Connection Limits

WebSocket connections are persistent, consuming resources at every hop. At scale (millions of connections), consider:

Timeouts

CDNs may timeout idle WebSocket connections (typically 60-300 seconds). Applications should implement heartbeat/ping-pong to keep connections alive.

Real-Time Application Patterns

Server-Sent Events (SSE)

For server-to-client only updates, SSE is simpler than WebSockets and works better with CDN caching:

Long Polling

HTTP long polling works with all CDNs (standard HTTP requests). The CDN caches the polling response for a short TTL, reducing origin load when many clients poll simultaneously.

Edge-Based Pub/Sub

Some CDNs offer built-in pub/sub at the edge:

These edge compute solutions eliminate origin round-trips for real-time messaging.

When to Use CDN for WebSockets

Use CDN WebSocket proxying when: you need DDoS protection for WebSocket endpoints, you want TLS termination at the edge, your WebSocket server is behind the CDN anyway, or you want unified monitoring across HTTP and WebSocket traffic.

External References

References & Standards: OWASP CDN Security · HTTP/2 specification