What if your website's images and videos could reach anyone instantly, no matter where they are?
Why Media storage and CDN in HLD? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you run a website with lots of images and videos. Every time someone visits, your main server sends these files directly to them, no matter where they are in the world.
This manual way makes your server slow and overloaded. Visitors far away get slow loading times, and if many people visit at once, your site can crash or become very slow.
Using media storage combined with a Content Delivery Network (CDN) stores your files in special places worldwide. When someone visits, the closest place sends the files quickly, reducing load on your main server and speeding up delivery.
Serve media files directly from main server for every request
Store media in cloud storage and deliver via CDN edge servers globally
Your website can serve media fast and reliably to users everywhere, even during traffic spikes.
Think of YouTube: videos are stored in data centers worldwide, so when you watch a video, it loads quickly from a nearby server, not from a faraway main server.
Directly serving media overloads your main server and slows down users.
Media storage with CDN spreads files globally for fast, reliable delivery.
This approach improves user experience and scales easily with traffic.
Practice
Solution
Step 1: Understand CDN role
A CDN stores copies of media files in servers near users to reduce latency.Step 2: Differentiate storage vs delivery
Media storage holds original files; CDN speeds up delivery by caching.Final Answer:
To cache media files closer to users for faster access -> Option BQuick Check:
CDN = cache near users [OK]
- Confusing CDN with permanent storage
- Thinking CDN compresses files
- Assuming CDN encrypts files
Solution
Step 1: Understand request flow
User first hits CDN cache to get media quickly.Step 2: Handle cache miss
If CDN cache misses, it fetches from origin storage and caches it.Final Answer:
User requests -> CDN cache -> Origin storage if cache miss -> Option AQuick Check:
Request flow = User -> CDN -> Storage [OK]
- Thinking origin storage serves user directly every time
- Reversing CDN and storage order
- Ignoring cache miss step
Solution
Step 1: Understand CDN TTL effect
TTL controls how long CDN keeps cached copy before checking origin.Step 2: Effect of update during TTL
Until TTL expires, CDN serves cached old file despite origin update.Final Answer:
User gets the old cached file from CDN until TTL expires -> Option CQuick Check:
Cache TTL = old file served until expiry [OK]
- Assuming CDN always fetches fresh file immediately
- Thinking cache invalidates automatically on update
- Believing user request fails on stale cache
Solution
Step 1: Analyze slow loading cause
Slow loading often happens if CDN cache misses and fetches from origin repeatedly.Step 2: Identify cache header role
Incorrect or missing cache headers prevent CDN from caching files properly.Final Answer:
CDN cache miss due to incorrect cache headers -> Option DQuick Check:
Cache headers control CDN caching [OK]
- Assuming small files don't benefit from CDN
- Thinking storage location alone causes slowness
- Believing long cache time causes slow loading
Solution
Step 1: Consider storage and CDN roles
Central storage simplifies management; CDN edge caches bring content close to users.Step 2: Evaluate options for scalability and latency
Multiple regions without CDN adds complexity; CDN without origin lacks source; local storage is impractical.Final Answer:
Store media in a single central region and use CDN with regional edge caches -> Option AQuick Check:
Central storage + CDN edges = scalable & fast [OK]
- Ignoring CDN benefits by using multi-region storage only
- Thinking CDN can replace origin storage
- Assuming local user storage is feasible
