Caching Strategy: Multi-Layer Performance Optimization

This article explains how we use caching at every layer to deliver fast page loads globally.

The Problem: Uncached Requests Are Slow

Without caching, every page load requires:

  • Database queries: Fetching product data, stock levels, pricing

  • API calls: External services for shipping rates, payments

  • Computation: Price calculations, availability checks

  • Image loading: Product images, thumbnails

Caching eliminates repeated work by storing results for reuse.

The Solution: Multi-Layer Caching

We cache at five layers:

  1. Browser cache: User's device stores static assets
  2. CDN cache: CloudFront edge locations cache content globally
  3. Application cache: Valkey stores frequently accessed data
  4. Database cache: Valkey sits in front of the database
  5. AI cache: DeepSeek caches system prompts

Caching Layers

Browser Caching (CloudFront)

CloudFront provides browser caching with appropriate TTLs:

  • Images: Long TTL (24-72 hours)

  • CSS/JS: Versioned with cache-busting

  • API responses: Short TTL or no cache

CDN Caching (CloudFront)

CloudFront caches content at edge locations:

Application Caching (Valkey)

Valkey provides in-memory caching:

  • Sessions: User sessions with sliding expiry

  • Popular queries: Most searched terms

  • Autocomplete: Search suggestions

  • Product counts: Stock availability

  • Phrase mappings: Filter extraction data

Database Caching

Valkey sits in front of the database:

  • Product data: Cached for performance

  • Part costs: Cached for price calculations

  • SKU lists: Cached for product listings

  • Image manifests: Cached for image loading

AI Caching (DeepSeek)

DeepSeek provides prompt caching:

  • System prompts: Cached for efficiency

  • Cache hits: Reduce token usage

  • Cache misses: Generate new content

Translation Caching

Translation caching uses multiple layers:

  • Valkey: Phrase tables cached in memory

  • Files: Phrase tables stored persistently in JSON files

  • Fallback: Legacy hash-based cache for backward compatibility

Cache Invalidation

We invalidate caches when:

  • Product updates: Invalidate product caches

  • Stock changes: Invalidate stock count caches

  • Content updates: Invalidate page caches

  • Translation updates: Invalidate translation caches

Cache Organization

Valkey caches are organized by function:

  • Sessions: User authentication state

  • Autocomplete: Search suggestion data

  • Popular queries: Frequently searched terms

  • Phrase mappings: Filter extraction data

  • Part costs: Pricing calculation data

Summary

Our caching strategy provides:

  • Multi-layer: Browser, CDN, application, database

  • Efficient: Valkey for fast in-memory access

  • Smart: AI prompt caching for cost savings

  • Reliable: Multiple fallback mechanisms