On Demand vs Precomputed Derivatives: Architecture Trade offs
On Demand Transformation
On demand (or just in time) transformation generates image variants when first requested. Client requests /image.jpg?w=400&format=webp. Edge or origin sees the request, fetches original, transforms, caches result, returns to client. Subsequent requests for same variant hit cache. Advantages: zero pre-compute cost, storage only for requested variants, instant availability (no processing pipeline to wait for). Disadvantages: first request latency (transformation takes 50-500ms), CPU cost on request path, cache miss thundering herd risk.
Precomputed Derivatives
Precomputed transformation generates all needed variants at upload time. Upload triggers pipeline that creates: thumbnail, small, medium, large, each in JPEG, WebP, AVIF. All variants stored alongside original. Serving is pure CDN cache lookup with no computation. Advantages: consistent low latency, predictable costs, no cache miss storms. Disadvantages: storage multiplier (10-20 variants per original), upload processing time, inflexibility (adding new sizes requires regenerating all images).
Hybrid Architecture
Production systems typically combine both approaches. Common variants precomputed: standard thumbnail (100px), card size (400px), detail view (1200px). These cover 90%+ of requests with zero compute latency. Uncommon variants on demand: custom crop dimensions, unusual sizes, new formats before rollout. The precomputed set should cover the pareto cases while on demand handles the long tail.
Video Derivative Strategy
Video is almost always precomputed due to encoding cost. Encoding a 1 hour video takes 30 minutes to hours of CPU time. On demand is impractical. Standard approach: upload triggers encoding pipeline producing full encoding ladder (360p, 480p, 720p, 1080p, 4K) plus HLS or DASH manifest files. Video becomes available only after encoding completes. Optimization: parallel encoding with distributed workers can reduce a 2 hour encode to 15 minutes by splitting into chunks.
Decision Framework
Use precomputed when: upload volume is manageable (< 10,000/day), latency is critical (e-commerce product images), variant set is predictable, storage cost is acceptable. Use on demand when: upload volume is high, many images are never viewed, variant requirements change frequently, storage cost is prohibitive. Use hybrid when: core use cases are predictable but edge cases exist, balancing latency for common cases with flexibility for uncommon ones. Monitor cache hit rates: if on demand cache hits exceed 95% for a variant, consider precomputing it.
Cost Comparison
Example: 1 million images, 10 variants each, average 200KB per variant. Precomputed storage: 10M variants × 200KB = 2TB. At $0.02/GB/month = $40/month. On demand with 30% image access rate: only 3M variants ever generated. Storage: 600GB = $12/month. But add compute cost for 3M transformations. If access is 90%+, precomputed is cheaper. If access is < 30%, on demand is cheaper despite compute overhead.