Why CDNs Matter

A Content Delivery Network (CDN) distributes content across globally distributed servers (edge locations), serving users from the server geographically closest to them. This reduces latency, improves load times, and offloads traffic from your origin servers. At Nexis Limited, our websites and SaaS product assets are served through CDNs for optimal global performance.

How CDNs Work

When a user requests a resource:

  1. DNS resolves the CDN hostname to the nearest edge server.
  2. The edge server checks its cache for the requested resource.
  3. If cached (cache hit), the edge server returns the resource directly — fast, no origin contact needed.
  4. If not cached (cache miss), the edge server fetches the resource from the origin server, caches it, and returns it to the user.

Subsequent requests from nearby users are served from the edge cache, eliminating the round trip to the origin server.

Cache Strategy

Cache Headers

Control CDN caching with HTTP cache headers:

  • Cache-Control: public, max-age=31536000: Cache for one year. Use for immutable assets (hashed filenames).
  • Cache-Control: public, max-age=3600, stale-while-revalidate=86400: Cache for one hour, serve stale content for 24 hours while revalidating in the background.
  • Cache-Control: no-cache: Cache but revalidate with the origin before serving. Use for HTML pages that should reflect recent changes.
  • Cache-Control: no-store: Never cache. Use for personalized or sensitive content.

Cache Invalidation

When content changes, the CDN must serve the new version. Strategies:

  • Cache busting with hashes: Include a content hash in filenames (app.a1b2c3.js). When content changes, the filename changes, and the CDN treats it as a new resource. This is the most reliable approach.
  • Purge: Explicitly purge specific URLs from the CDN cache via API. Use for critical content updates that cannot wait for natural cache expiry.
  • Short TTL with revalidation: Use short cache durations with stale-while-revalidate for content that changes frequently.

Origin Shielding

Without origin shielding, every edge server independently fetches from the origin on cache misses. With many edge locations, the origin can receive excessive traffic during cache cold starts or invalidations. Origin shielding adds an intermediate cache layer between edge servers and the origin, reducing origin load significantly.

Edge Computing

Modern CDNs offer edge computing capabilities (Cloudflare Workers, Vercel Edge Functions, AWS CloudFront Functions) that execute code at edge locations. Use cases include:

  • URL rewriting and redirects: Handle redirects at the edge without origin round trips.
  • A/B testing: Route users to different content variants at the edge.
  • Authentication: Validate JWT tokens at the edge before forwarding to the origin.
  • Personalization: Customize cached content based on user location, device, or headers.
  • Geographic routing: Serve different content based on user location.

CDN for Different Content Types

  • Static assets (JS, CSS, images): Long cache TTL with content-hashed filenames. Maximum CDN benefit.
  • HTML pages: Short cache TTL or no-cache with revalidation. Balances freshness with performance.
  • API responses: Selective caching based on endpoint. Public, read-heavy endpoints benefit from CDN caching. Authenticated endpoints require careful cache key design (usually not cached at CDN level).
  • Video streaming: CDNs with HLS/DASH support for adaptive bitrate streaming.

Choosing a CDN

  • Cloudflare: Excellent free tier, edge computing (Workers), DDoS protection, and DNS.
  • AWS CloudFront: Deep AWS integration, Lambda@Edge for edge computing, pay-per-use pricing.
  • Vercel Edge Network: Built into Vercel's platform for Next.js deployments.
  • Fastly: Real-time purging, VCL configuration, and excellent performance for dynamic content.

Conclusion

CDNs are essential infrastructure for any web application serving global users. Configure cache headers thoughtfully, use content-hashed filenames for reliable cache busting, and consider edge computing for logic that benefits from proximity to users. The performance and cost benefits are significant.

Optimizing your content delivery? Our team configures and optimizes CDN infrastructure.