Edge Caching vs Origin Caching: Speed Up Site Response Times for SEO
Quick Summary
- What this covers: Compare edge caching (CDN) vs origin caching (Varnish, Redis) for improving TTFB. Choose the right caching strategy for Core Web Vitals optimization.
- Who it's for: site owners and SEO practitioners
- Key takeaway: Read the first section for the core framework, then use the specific tactics that match your situation.
Caching strategies reduce server processing time and improve Time to First Byte (TTFB), directly affecting Core Web Vitals and Googlebot crawl efficiency — edge caching serves content from geographically distributed CDN nodes (Cloudflare, Fastly, Akamai) with <50ms latency, while origin caching stores rendered pages or database query results at the application server layer (Varnish, Redis, Nginx FastCGI) with 100-300ms response times but no geographic distribution. Sites with global audiences benefit from edge caching's proximity advantages, but dynamic content (personalized product recommendations, user-specific data, real-time inventory) often bypasses CDN cache, requiring origin caching to avoid database hits on every request. Incorrect cache configurations — setting edge TTL too high and serving stale product prices, caching user-specific pages at edge (privacy violation), or missing cache-control headers that disable caching entirely — leave performance gains unrealized or create serious data exposure risks. This guide compares edge vs. origin caching architectures, implements hybrid strategies for dynamic e-commerce sites, and configures cache purging for content updates.
Edge Caching: How CDNs Cache Content
Content Delivery Networks (CDNs) cache content at edge locations worldwide.
How Edge Caching Works
1. Request flow:
User (London) → Cloudflare London Edge → Origin Server (US)
2. First request (cache miss):
- Edge node doesn't have content
- Fetches from origin server
- Stores copy at edge
- Serves to user
- TTFB: 200-400ms (includes origin fetch)
3. Subsequent requests (cache hit):
- Edge node serves cached copy
- No origin server request
- TTFB: 10-50ms (edge proximity)
Edge Caching Providers
Major CDN providers:
Cloudflare:
- Free tier available
- 300+ edge locations
- Automatic SSL
- DDoS protection
- Cache everything page rules (3 free)
Fastly:
- $50+/month
- Instant purge (2-5 seconds globally)
- Advanced VCL configuration
- Real-time analytics
Amazon CloudFront:
- Pay-as-you-go ($0.085/GB)
- 400+ edge locations
- Deep AWS integration
Akamai:
- Enterprise ($1000+/month)
- 4000+ edge locations
- Industry leader in scale
Edge Caching Benefits
1. Geographic latency reduction:
- User in Australia: 300ms to US origin → 30ms to Sydney edge
- 90% TTFB reduction
2. Origin server protection:
- Edge absorbs traffic spikes
- Origin sees 10-50x fewer requests
3. Bandwidth cost savings:
- Origin serves content once
- Edge serves thousands of times
Origin Caching: Server-Side Caching Layers
Origin caching stores content at the application server before it reaches CDN.
Origin Caching Types
1. Full-page caching (Varnish, Nginx FastCGI): Stores entire rendered HTML pages.
2. Object caching (Redis, Memcached): Stores database query results, API responses, fragments.
3. Opcode caching (OPcache for PHP): Stores compiled PHP bytecode (not content, but speeds execution).
Varnish Full-Page Cache
Varnish sits in front of web server, serves cached pages without hitting PHP/database.
Request flow:
User → CDN Edge → Varnish → Nginx/Apache → PHP/Database
↑ ↑
Cache hit (10ms) Cache miss (200ms)
Varnish configuration:
vcl 4.0;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
# Remove cookies for static assets
if (req.url ~ "\.(jpg|jpeg|gif|png|css|js|webp)$") {
unset req.http.Cookie;
}
# Don't cache logged-in users
if (req.http.Cookie ~ "wordpress_logged_in") {
return (pass);
}
}
sub vcl_backend_response {
# Cache successful responses for 1 hour
if (beresp.status == 200) {
set beresp.ttl = 1h;
}
}
Benefits:
- 100-500x faster than uncached (10ms vs. 500ms)
- Reduces origin server load 90%+
Redis Object Caching
Redis caches database query results and expensive computations.
WordPress example:
// Check Redis cache
$cache_key = 'product_data_' . $product_id;
$product = $redis->get($cache_key);
if (!$product) {
// Cache miss: query database
$product = $wpdb->get_row("SELECT * FROM products WHERE id = $product_id");
// Store in Redis (1 hour TTL)
$redis->setex($cache_key, 3600, serialize($product));
}
return unserialize($product);
Benefits:
- Reduces database queries 80%+
- Faster than querying database every time
- Scales horizontally (Redis cluster)
See WordPress database optimization.
Nginx FastCGI Cache
Nginx caches PHP responses without external caching layer.
Configuration:
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=MYAPP:100m inactive=60m;
server {
location ~ \.php$ {
fastcgi_cache MYAPP;
fastcgi_cache_valid 200 60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header X-Cache-Status $upstream_cache_status;
fastcgi_pass unix:/var/run/php-fpm.sock;
}
}
Benefits:
- No additional software (built into Nginx)
- 10-50ms response times for cached pages
- Automatic cache invalidation on disk
Edge vs. Origin: When to Use Each
Choose based on content type and audience.
Use Edge Caching When:
1. Static content:
- Images, CSS, JavaScript
- PDFs, fonts, videos
2. Globally distributed audience:
- International users benefit from geographic proximity
3. High traffic:
- Edge absorbs traffic spikes
- Protects origin from overload
4. Content updates infrequently:
- Blog posts (update weekly)
- Product pages (update daily or less)
Use Origin Caching When:
1. Dynamic content:
- User-specific dashboards
- Shopping carts
- Personalized recommendations
2. Frequently changing content:
- Real-time inventory
- Pricing changes every hour
- Live event data
3. Privacy-sensitive data:
- User account pages
- Order history
- Personal information
4. Regional audience:
- Single-country traffic (edge benefit minimal)
Hybrid Strategy: Edge + Origin Caching
Combine both for maximum performance.
Layer 1: Edge Caching (Static Assets)
Cache at Cloudflare edge:
- CSS, JavaScript (1 year TTL)
- Images, fonts (1 month TTL)
- Videos (1 week TTL)
Cloudflare Page Rule:
Cache Level: Cache Everything
Edge Cache TTL: 1 month
Browser Cache TTL: 1 day
Layer 2: Origin Full-Page Cache (Dynamic Pages)
Cache at Varnish/Nginx:
- Product pages (1 hour TTL)
- Category pages (30 minutes TTL)
- Homepage (15 minutes TTL)
Don't cache at origin:
- Cart pages
- Checkout
- User dashboards
Layer 3: Object Cache (Database Queries)
Cache at Redis:
- Product data (1 hour)
- Category lists (6 hours)
- Navigation menus (24 hours)
Implementation:
// Layer 3: Check Redis
$product = $redis->get('product_' . $id);
if (!$product) {
// Layer 2: Varnish serves if cached
// Layer 3 miss: Query database
$product = get_product_from_db($id);
$redis->setex('product_' . $id, 3600, serialize($product));
}
Request Flow with Hybrid Caching
Cache hit at each layer:
Request → Cloudflare Edge (hit: 20ms)
↓
Request → Varnish (miss, hit: 100ms)
↓
Request → Redis (miss, hit: 150ms)
↓
Request → Database (miss: 500ms)
Best case: Edge hit = 20ms TTFB Worst case: Database query = 500ms TTFB
Cache Control Headers
HTTP headers control caching behavior at edge and browser.
Cache-Control Directive
Public (cacheable by edge and browser):
Cache-Control: public, max-age=86400
Private (browser only, not edge):
Cache-Control: private, max-age=3600
No cache:
Cache-Control: no-store
Set Cache Headers (Apache)
# .htaccess
<FilesMatch "\.(jpg|jpeg|png|gif|webp|svg)$">
Header set Cache-Control "public, max-age=2592000"
</FilesMatch>
<FilesMatch "\.(css|js)$">
Header set Cache-Control "public, max-age=604800"
</FilesMatch>
Set Cache Headers (Nginx)
location ~* \.(jpg|jpeg|png|gif|webp)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
location ~* \.(css|js)$ {
expires 7d;
add_header Cache-Control "public";
}
Vary Header for Dynamic Content
Serve different cached versions based on headers:
Vary: Accept-Encoding, Cookie
Example: Serve compressed vs. uncompressed, logged-in vs. logged-out users.
Cache Purging and Invalidation
Update content without waiting for TTL expiration.
Cloudflare Cache Purge
API purge:
curl -X POST "https://api.cloudflare.com/client/v4/zones/ZONE_ID/purge_cache" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
--data '{"files":["https://example.com/updated-page.html"]}'
WordPress plugin: Cloudflare plugin auto-purges on post publish.
Varnish Cache Purge
PURGE request:
curl -X PURGE https://example.com/updated-page
Varnish VCL:
sub vcl_recv {
if (req.method == "PURGE") {
return (purge);
}
}
Programmatic Invalidation
Invalidate cache after content updates:
// After saving product
function purge_product_cache($product_id) {
$url = "https://example.com/products/" . $product_id;
// Purge Cloudflare
cloudflare_purge($url);
// Purge Varnish
shell_exec("curl -X PURGE $url");
// Clear Redis
$redis->del('product_' . $product_id);
}
Frequently Asked Questions
Does edge caching hurt dynamic e-commerce sites with personalized content?
Not if configured correctly. Use Vary: Cookie header to serve different cached versions for logged-in vs. logged-out users. Or bypass edge cache entirely for logged-in users, use origin caching only. Example: Cloudflare Page Rule excludes /cart and /my-account from edge cache. Static assets (images, CSS) always benefit from edge caching.
What's the difference between CDN and caching?
CDN (Content Delivery Network) is the infrastructure (global edge servers). Edge caching is the feature (storing content at edge servers). All major CDNs include caching, but CDNs also provide DDoS protection, load balancing, and SSL termination. Some CDNs (basic tiers) don't cache HTML by default — only static assets. See DNS configuration for CDN setup.
How long should cache TTL be for e-commerce product pages?
15-60 minutes for product pages with price/inventory changes. 1-6 hours for static product info (descriptions, images). Edge TTL shorter than origin TTL (edge: 15min, origin: 1hr) allows faster content updates at edge while reducing origin load. Use cache purge API for immediate updates after manual changes. See Core Web Vitals optimization.
Can I use edge caching with WordPress or Shopify?
Yes. WordPress: Use Cloudflare plugin or WP Rocket with CDN integration. Shopify: Shopify includes Fastly CDN by default (built-in edge caching). Custom Shopify Plus stores can use additional CDNs. Most CMSs work with edge caching with proper cache-control headers. Test logged-in user experience to ensure personalization still works.
Will caching fix slow database queries?
Origin caching (Redis) caches query results, avoiding slow queries after first execution. Edge caching doesn't help database speed (queries still run on cache miss). For slow databases, optimize queries first (see WordPress database optimization), then add Redis object caching, then add edge caching for maximum speed. Caching masks database issues but doesn't fix root causes.
When This Fix Isn't Your Priority
Skip this for now if:
- Your site has fundamental crawling/indexing issues. Fixing a meta description is pointless if Google can't reach the page. Resolve access, robots.txt, and crawl errors before optimizing on-page elements.
- You're mid-migration. During platform or domain migrations, freeze non-critical changes. The migration itself introduces enough variables — layer optimizations after the new environment stabilizes.
- The page gets zero impressions in Search Console. If Google shows no data for the page, the issue is likely discoverability or indexation, not on-page optimization. Investigate why the page isn't indexed first.