API Acceleration via CDN: Caching and Routing Strategies

· Alex Petrov · 11 min read

APIs are the backbone of modern applications. Every mobile app launch, every SPA page transition, and every microservice interaction generates API calls that traverse the network. Yet most organizations treat API traffic as inherently uncacheable and route it directly to origin servers, sacrificing latency and resilience. A CDN configured for API workloads reduces response times by 50–90%, absorbs traffic spikes that would overwhelm the origin, and adds a security layer against API abuse—all without modifying a line of application code. For a comprehensive overview, see our guide on CDN for E-Commerce: Speed That Converts — FlyFish NWA.

This guide covers the techniques for accelerating REST and GraphQL APIs through a CDN: response caching strategies, authentication-aware caching, rate limiting at the edge, WebSocket and gRPC proxying, and the architectural relationship between CDNs and API gateways.

How Do You Cache REST API Responses at the CDN Edge?

REST APIs follow HTTP semantics, which means CDN caching works naturally with proper Cache-Control headers. The challenge is not whether to cache but how long and with what cache key.

CDN Architecture for This Use Case — How content reaches end usersCDN Architecture for This Use Case — How content reaches end users 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 Architecture for This Use Case — How content reaches end users

Cacheable methods. GET and HEAD requests are cacheable by definition (RFC 7231). POST, PUT, PATCH, and DELETE are not cacheable and should always pass through to origin. Configure the CDN to cache only GET responses and bypass all other methods.

TTL by endpoint type. Different API endpoints have different freshness requirements:

Endpoint TypeExampleRecommended TTLInvalidation
Reference data/api/countries, /api/categories1–24 hoursDeploy-time purge
Content listings/api/products, /api/articles1–5 minutesstale-while-revalidate
Search results/api/search?q=shoes30–60 secondsTTL expiry
User-specific data/api/me/profile0 (no cache) or 10–30s with auth keyEvent-driven purge
Real-time data/api/notifications, /api/prices0 (no cache)N/A

Cache key design. The CDN cache key for API responses should include: the URL path, sorted query parameters (so ?page=1&limit=20 and ?limit=20&page=1 hit the same cache entry), and the Accept header (to distinguish JSON from XML responses). Exclude: tracking parameters (utm_source, referrer), timestamps, and random tokens. See Cache Key Design for detailed patterns.

The stale-while-revalidate advantage. Setting Cache-Control: max-age=30, stale-while-revalidate=300 on API responses means: for the first 30 seconds, the CDN serves the cached response instantly. After 30 seconds, the CDN serves the stale response instantly while fetching a fresh response from origin in the background. The client always gets a sub-10ms response, and the data is never more than 30 seconds stale plus one revalidation cycle. This pattern is covered in depth at Stale-While-Revalidate.

How Does a CDN Handle GraphQL Queries?

GraphQL presents unique CDN caching challenges because queries are typically sent as POST requests with variable shapes. Traditional CDN caching (keyed on URL) does not work out of the box.

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

Persisted queries (recommended). The client sends the query during development, receives a hash, and at runtime sends only the hash as a GET parameter: GET /graphql?id=abc123&variables={"userId":"42"}. The CDN caches by URL (including the hash and variables), achieving the same cache efficiency as REST. This also improves security by preventing arbitrary query injection.

Automatic Persisted Queries (APQ). Apollo's APQ protocol automates the registration step: the client sends a query hash, and if the server does not recognize it, the client resends the full query. Subsequent requests use the hash. CDN edge functions can implement APQ at the edge, caching the hash-to-query mapping in an edge KV store.

POST body caching. Some CDNs (Fastly, Cloudflare with Workers) can compute cache keys from POST request bodies. The edge function parses the GraphQL POST body, extracts the operation name and variables, normalizes them, and generates a deterministic cache key. This enables caching without modifying the client to use GET requests.

Query complexity and cost. GraphQL allows clients to request deeply nested data, potentially generating expensive backend queries. An edge function can analyze query complexity (depth, breadth, estimated cost) before forwarding to origin, rejecting or rate-limiting expensive queries at the edge. This protects the origin from query-based denial of service.

How Does an API Gateway Compare to a CDN for API Delivery?

API gateways (Kong, AWS API Gateway, Apigee) and CDNs serve overlapping but distinct functions. Understanding the overlap prevents both gaps and redundancy.

CapabilityAPI GatewayCDNBest Handled By
Response cachingBasicAdvanced (global edge cache)CDN
Geographic distributionLimited (regional)Global (200+ PoPs)CDN
DDoS protectionLimitedMulti-Tbps absorptionCDN
AuthenticationAdvanced (OAuth, JWT, API key)Basic (JWT validation at edge)Gateway
Request transformationAdvancedVia edge functionsGateway or CDN edge
Rate limitingGoodGood (edge-based)Both (CDN first)
API versioningAdvancedVia routing rulesGateway
AnalyticsAPI-specificTraffic-levelBoth

The recommended architecture. Place the CDN in front of the API gateway: Internet → CDN Edge → API Gateway → Backend Services. The CDN handles caching (reducing load on everything downstream), DDoS protection (absorbing attacks at the edge), geographic acceleration (TLS termination and connection optimization), and basic rate limiting. The API gateway handles authentication, authorization, request routing, transformation, and API-specific policies.

How Do You Cache Authenticated API Responses?

Caching authenticated responses is the most nuanced aspect of API CDN configuration. The goal is to cache where possible without ever serving User A's data to User B.

Performance Gains — Measurable improvements with CDNPerformance Gains — Measurable improvements with CDN CDN Performance Dashboard — Core Web Vitals Core Web Vitals with CDN LCP 1.8s Target: < 2.5s Good FID 45ms Target: < 100ms Good CLS 0.05 Target: < 0.1 Good TTFB 120ms Target: < 800ms Good
Figure 2: Performance Gains — Measurable improvements with CDN

Public data behind authentication. Many authenticated APIs return data that is identical for all users (product catalog, configuration, feature flags). These responses should set Cache-Control: public, max-age=300 and be cached at the CDN without any user-specific cache key. The authentication check happens at the edge (validate the JWT), but the response is shared across all authenticated users.

Role-based caching. If responses vary by role (admin sees more data than regular users), extract the role from the JWT at the edge and include it in the cache key: one cached version for admins, one for regular users. Use a Vary header on a custom role header that the edge function injects.

User-specific caching. For truly per-user data (profile, preferences, activity history), include a sanitized user identifier (not the full JWT) in the cache key. This creates one cache entry per user per endpoint. The cache hit ratio is lower (each user's data is cached independently), but it still eliminates redundant origin calls when the same user makes repeated requests.

Security critical rule. Never include the full Authorization header in the cache key. JWTs contain sensitive claims that should not be stored in cache key indexes. Instead, hash the user ID claim and use the hash as the key discriminator. See Cache Key Design for secure patterns.

How Do CDNs Rate-Limit API Traffic at the Edge?

API rate limiting at the CDN edge stops abusive traffic before it reaches origin, preserving capacity for legitimate users. Edge-based rate limiting is 10–100x more effective than origin-based because the edge has orders of magnitude more capacity.

Rate limit dimensions. Effective API rate limiting considers multiple dimensions: per IP address (prevents individual abuse), per API key (enforces plan limits), per authenticated user (prevents account-level abuse), per endpoint (tighter limits on sensitive endpoints like /login), and per geographic region (detects regional attack patterns).

Algorithm choice. Token bucket (allows burst traffic up to a limit, then throttles) is best for API use cases where occasional bursts are acceptable. Sliding window (fixed request count per time window) provides more predictable rate enforcement. Most CDN providers support both algorithms at the edge.

Response to rate-limited requests. Return HTTP 429 Too Many Requests with a Retry-After header indicating when the client can retry. Include rate limit headers in all responses: X-RateLimit-Limit, X-RateLimit-Remaining, and X-RateLimit-Reset. This allows well-behaved clients to self-throttle before hitting the limit. See CDN DDoS Protection for the security perspective on rate limiting.

How Do CDNs Handle WebSocket and gRPC Traffic?

Not all API traffic is HTTP request-response. WebSocket connections (real-time updates, chat, live data) and gRPC (high-performance microservice communication) require different CDN handling.

WebSocket proxying. CDNs proxy WebSocket connections by performing the HTTP upgrade handshake at the edge and maintaining a persistent connection between the client and a backend WebSocket server. The CDN provides: TLS termination (securing the WebSocket with WSS), connection routing to the nearest backend, and DDoS protection on the WebSocket endpoint. WebSocket traffic is not cacheable (it is bidirectional and real-time), but the CDN still adds value through security and connection optimization.

gRPC over CDN. gRPC uses HTTP/2 with Protocol Buffers for efficient binary serialization. CDNs that support HTTP/2 can proxy gRPC traffic, providing the same benefits as HTTP API proxying: TLS termination, DDoS protection, and geographic routing. Some CDNs (Cloudflare, Fastly) support gRPC-aware health checking and load balancing. gRPC response caching at the CDN edge is possible but requires custom edge functions to parse the Protocol Buffer payloads.

How Do You Measure API CDN Performance?

API performance measurement differs from page performance measurement. The key metrics and measurement approaches:

TTFB by endpoint. Track TTFB for each API endpoint, segmented by cache status (HIT vs MISS). Cache HITs should show sub-20ms TTFB. Cache MISSes should show origin response time plus CDN overhead (typically 5–15ms). If MISS TTFB is much higher than direct-to-origin, investigate CDN processing overhead.

Cache hit ratio by endpoint. Monitor cache hit ratio per endpoint. Reference data endpoints should hit 95%+. Dynamic endpoints with short TTLs should hit 60–80%. If an endpoint shows unexpectedly low hit ratio, check for cache key issues, TTL configuration, or Vary header problems. Use CDN log analysis to identify the root cause.

Error rate monitoring. Track 4xx and 5xx error rates through the CDN. A spike in 502/504 errors indicates origin health issues. A spike in 429 errors indicates rate limiting is engaging (either blocking abuse or misconfigured limits). Use CDN troubleshooting techniques to diagnose error patterns.

Client-side measurement. Implement Real User Monitoring in your API clients to measure actual request latency as experienced by end users. RUM data captures network variability, DNS resolution time, and TLS overhead that synthetic monitoring misses. This is the truest measure of API CDN effectiveness.

External References

References & Standards: W3C Web Performance · HTTP/2 specification