Skip to content

Cdn Caching

DodaTech 2 min read

title: "CDN Caching — Edge Caching for Global API Distribution" description: "CDN caching distributes cached API responses across global edge locations, reducing latency for users worldwide from hundreds to under 20 milliseconds." date: 2026-06-28 lastmod: 2026-06-28 weight: 19 tags: [apis, caching] }

CDN caching stores API responses at edge locations worldwide, serving users from the nearest point of presence with latency reductions from 200ms to under 20ms.

What You'll Learn

  • CDN edge caching for APIs
  • Origin pull vs push
  • CDN cache invalidation

Why It Matters

CDNs are essential for global APIs. Without edge caching, users on other continents face 200-400ms latency. With CDN caching, latency drops to 10-30ms.

CDN Architecture

flowchart TD
    U1[User - US East] --> E1[Edge - US East]
    U2[User - Europe] --> E2[Edge - Europe]
    U3[User - Asia] --> E3[Edge - Asia]
    E1 --> O[Origin Server]
    E2 --> O
    E3 --> O

Code Examples

# Origin response headers for CDN
@app.route('/api/global-data')
def global_data():
    # CDN will cache this at all edge locations
    response = jsonify(get_global_data())
    response.headers['Cache-Control'] = 'public, s-maxage=3600, max-age=300'
    response.headers['CDN-Cache-Control'] = 'max-age=3600'
    response.headers['CloudFront-Key'] = '/api/global-data'
    return response

# CDN invalidation via API
import boto3

def invalidate_cdn_path(path):
    client = boto3.client('cloudfront')
    client.create_invalidation(
        DistributionId='YOUR_DISTRIBUTION_ID',
        InvalidationBatch={
            'Paths': {'Quantity': 1, 'Items': [path]},
            'CallerReference': str(time.time())
        }
    )

# After updating content
invalidate_cdn_path('/api/global-data')
// Fastly CDN surrogate keys
app.get('/api/products', (req, res) => {
  const products = db.getProducts();
  res.set({
    'Cache-Control': 'public, s-maxage=3600',
    'Surrogate-Key': 'products all',
    'Surrogate-Control': 'max-age=3600'
  });
  res.json(products);
});

// Purge by surrogate key
// curl -X POST https://api.fastly.com/service/{sid}/purge/products

Common Mistakes

1. Not Setting s-maxage

CDN uses s-maxage as its TTL. Without it, CDN uses max-age which may be too short.

2. No CDN Cache Invalidation Strategy

Changes take hours to propagate without explicit invalidation.

3. Caching User-Specific Data in CDN

User data cached at edge locations leaks to other users.

4. No Cache Warmup

Cold start: first request from each edge location hits origin.

5. Ignoring CDN-Specific Headers

Many CDNs support custom headers for finer cache control.

Practice Questions

  1. How does a CDN reduce API latency?
  2. What is the difference between s-maxage and max-age?
  3. What is CDN cache invalidation?
  4. What is cache warmup?
  5. Why avoid caching user-specific data on CDN?

Answers:

  1. Caching responses at edge locations close to users.
  2. s-maxage is for shared caches (CDN); max-age for browsers.
  3. Removing cached content from CDN edge before TTL expires.
  4. Pre-populating cache to avoid cold starts for real users.
  5. User data could be served to other users via the shared cache.

Challenge: Configure a CDN for your API. Set appropriate cache TTLs, implement surrogate keys, and test cache invalidation.

FAQ

Which CDN providers support API caching?

: CloudFront, Fastly, Cloudflare, Akamai, and most major CDNs.

Can CDNs cache POST responses?

: Some CDNs support POST caching with custom configuration.

How do I troubleshoot CDN cache misses?

: Check CDN response headers like X-Cache, Age, and CF-Cache-Status.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro