The whole course ▾
System Design · Lesson 9 of 39 · Week 2
CDNs and object storage
QuickBite's food photos are slowing everything down, because they live in the wrong place. Today: object storage and CDNs.
Got a doubt? Ask in the comments under the video.Subscribe
QuickBite's food photos.
QuickBite is the food delivery app we grow through the course, from one server to millions of orders.
Photos in the database
This week we scaled the app servers and put a cache in front of the database. But one kind of data has been quietly hurting both: the food photos.
Today, a restaurant app uploads a three megabyte photo of a biryani. It goes through the app servers, which save the bytes straight into the orders DB, inside the menu row. When customers scroll a menu, every photo is read back out of the database and through the app servers again. It works. It's also one of the most common scaling mistakes there is.
Here's what it costs. Rows full of images make the database huge, so backups and copies take hours, and the cache we built fills up with megabytes instead of menus. Meanwhile, the app servers spend their time carrying bytes instead of running QuickBite's logic. Ten thousand photo views a minute is about thirty gigabytes an hour, all of it through our servers.
Object storage
Files belong in a place built for files: object storage, like Amazon S3.
Object storage keeps files under a name, like photos slash restaurant forty-two slash biryani, very cheaply, and it's designed never to lose them. Here's the upload. The restaurant app asks the app server for permission to upload. The server answers with a presigned URL: a link that allows exactly one upload, to exactly one name, for a few minutes. The app sends the photo straight to object storage, and the server saves only the photo's name in the orders DB. A few bytes, not three megabytes.
Presigned URLs are worth knowing by name. The bytes never touch our servers, so uploading a thousand photos costs QuickBite almost nothing. The link expires in a few minutes, so it can't be reused. And when we sign it, we can cap the size and type, say images under five megabytes, so nobody uploads a two gigabyte video instead.
The CDN
Uploads are solved. Now the other ninety-nine percent of the traffic: people looking at the photos.
Our photos live in one storage region, say Mumbai. A customer in Guwahati is a long way from Mumbai, and every photo would cross the country. A CDN, a content delivery network, keeps copies on servers near every big city. Customers fetch photos from the nearest CDN edge. The first request in a city fetches the photo from storage once, and the next thousand people in that city get the copy from the edge.
Here's what that buys. Fetching a photo from storage across the country takes around two hundred milliseconds. From a nearby edge, around twenty. And if the edges answer ninety-five percent of requests, storage sees only one request in twenty. Faster for customers, and cheaper for QuickBite, because CDN traffic usually costs less than traffic straight out of storage.
Telling the CDN what to keep
A CDN is a cache, so it has the same question as Redis: how long can a copy be trusted?
Three rules. Send a Cache-Control header telling the CDN to keep photos for a year. Then never change a photo in place: a new photo gets a new file name, like biryani-v2, so the old copy simply stops being asked for. And purge, which asks every edge to delete a file, only as a last resort, because it's slow and edges don't all obey at once.
That's the trick worth remembering: long cache times, plus new names for new versions. Photos load fast because they're cached for a year, and they're never stale, because a changed photo is, as far as the CDN knows, a different file.
One photo, many sizes
There's one more waste left. The restaurant uploaded a three megabyte photo, and a phone showing a thumbnail doesn't need three megabytes.
So after each upload, a background worker makes a few sizes. A thumbnail, a hundred and fifty pixels wide, about fifteen kilobytes, for lists. A menu size, six hundred pixels, for the restaurant page. And the full image, only when someone tapped it. Workers and queues get their own lesson, lesson fifteen.
Why bother? A thumbnail is about fifteen kilobytes and the original is three megabytes: two hundred times less to download, on every scroll, on every phone, often on a slow mobile connection. It's the single cheapest speed-up in the whole app.
In the interview
Any design with images or video needs this answer: Instagram, YouTube, food delivery, and Sunday's mock interview.
Say this: files go to object storage through presigned URLs, only their metadata goes to the database, and users read them through a CDN, in a few sizes. It's one sentence, and it shows the interviewer you won't push gigabytes through your app servers.
Expect these. How do you stop someone uploading two gigabytes? The presigned URL caps size and type. How do you update a photo the CDN has cached? Give the new version a new name. And what if one region's storage is slow? The CDN hides most of it, and multi-region storage is lesson thirty-eight.
The common mistake is putting images in the database, or proxying every upload through your own servers. Both work in a demo, and both fall over at scale.
Quick check
Why is a presigned URL better than uploading through your own server?
Show the answer
Bytes never touch your servers, and the link expires and can cap size and type.
A restaurant replaces a photo, but customers keep seeing the old one. What's the fix?
Show the answer
Give the new photo a new file name: the CDN treats it as a new file.
Remember: files go to object storage, never the database. Uploads go through presigned URLs. And photos reach customers through a CDN, with long cache times and new names for new versions. That's the whole scaling week: servers, load balancers, caches and CDNs.
Lessons on weekdays, the week's revision on Saturday, a mock interview on Sunday.