Design Instagram: media upload, CDN, and feed ranking
Instagram's CDN: Serving 500M Users Globally
Instagram has users in 150+ countries. Serving photos from a single S3 region would mean 300ms+ latency for users in Asia or South America. Their CDN caches processed images at 200+ edge nodes globally — dropping image load time to under 50ms worldwide.
Request Flow
Viewer requests image from CDN
The client's image URL points to a CDN domain (e.g. cdn.instagram.com). The CDN routes the request to the nearest edge node.
<img src="https://cdn.example.com/photos/user123/photo456_md.jpg" />
Cache HIT — serve from edge
If the image is cached at the edge, the CDN responds in ~5ms without touching S3. For popular content, >90% of reads are cache hits.
Cache MISS — fetch from S3
On a miss, the CDN pulls from S3 / Blob Storage, caches the response, and returns it to the client. Subsequent requests for the same image hit the edge.
Cache TTL and invalidation
CDN cache TTL for photos is typically 1 year (immutable content). If a photo is deleted or replaced, the CDN edge cache is invalidated via API.
// On delete:
CF.purgeCache({ files: ["https://cdn.example.com/photos/user123/photo456*"] })Why We Need This
Without a CDN, every image view hits S3 in a single region. At 100M daily active users viewing 50 photos each, that's 5 billion S3 reads per day. A CDN offloads 95%+ of those to edge cache.
CDN egress cost (~$0.008/GB) is also cheaper than S3 transfer cost (~$0.09/GB) for high-volume delivery.
Key Insight
CDNs are the single highest-leverage read optimisation for media-heavy systems. Processed outputs are immutable — once an image is processed and cached at the edge, it never needs to be regenerated or re-fetched from origin for that user's region.
Step 5 of 7
