0%
Mobile System Design Interviews: Architecture, Offline Sync, Networking, Media, Push and Performance
Images, Video and Media Pipelines
Almost every mobile design prompt has images: avatars in a chat list, photos in a feed, product shots in a store, map tiles, thumbnails of documents. Images are usually the largest bytes the app downloads, the largest objects in its memory, and the most common cause of janky scrolling and out of memory crashes. In step five of the framework, deep dives, media is one of the four favourite topics, and an interviewer who asks how images get onto the screen expects a precise answer: a pipeline with stages, caches at two levels, and a clear story about memory.
You will rarely build this pipeline from scratch. On Android, Coil and Glide are the standard libraries, and Coil also runs in Compose Multiplatform. On iOS, Nuke, Kingfisher and SDWebImage are common, and SwiftUI's AsyncImage covers simple cases without a disk cache of its own. But the interview is not asking which library you would import. It is asking whether you know what the library does, so you can configure it, reason about its memory use and explain what goes wrong.
The stages of an image request
A request for an image goes through a series of stages, and each stage either answers the request or passes it on.
First, the memory cache. This holds decoded bitmaps ready to draw, keyed by the URL plus the target size and any transformations. A hit here is effectively free: the cell can show the image in the same frame. Second, the disk cache. This holds the encoded bytes, the JPEG, WebP, AVIF or HEIC file as downloaded, keyed by URL. A hit avoids the network but still needs decoding. Third, the network: a request to the CDN for the bytes, at the right size. When the bytes arrive they are written to the disk cache, decoded into a bitmap at the target size on a background thread, put in the memory cache and handed to the view.
Several details make this pipeline good rather than merely working. Requests for the same URL and size are coalesced, so ten cells showing the same avatar trigger one download and one decode. Decoding never happens on the main thread, because decoding a large JPEG can take tens of milliseconds, longer than a frame. And every stage is cancellable, so a cell that is reused for a different item cancels the old request instead of letting a stale image arrive and flash into the wrong row, a classic bug in hand written loaders.
Why decoded size is what matters
The single most important number in image memory is this: a decoded bitmap costs width times height times bytes per pixel, regardless of how small the file was. With the common four bytes per pixel format, a twelve megapixel photo from a phone camera, around four thousand by three thousand pixels, takes about forty eight megabytes of memory when decoded at full size, even if the HEIC file on disk is only two or three megabytes. Show ten of those in a grid and the app is using close to half a gigabyte for pictures that are each displayed in a cell a hundred points wide.
The fix is downsampling: decode the image directly to the pixel size it will occupy on screen, never to its original size. A cell one hundred points square on a three times density screen needs three hundred by three hundred pixels, about three hundred and sixty kilobytes decoded. Image libraries do this automatically when they know the target view size, which is one reason to use them. When you decode yourself, use the platform's thumbnail paths: on iOS, ImageIO's CGImageSource thumbnail functions with a maximum pixel size, or UIImage's preparingThumbnail; on Android, ImageDecoder with a target size, or BitmapFactory with inSampleSize on older code paths.
Downsampling also starts earlier in the pipeline. The API should let the client request the image at roughly the display size, as the networking lesson described, so the CDN sends a three hundred pixel WebP or AVIF instead of a four thousand pixel original. Then the download is small, the disk cache is small, and the decode is fast.
Sizing the memory cache
The memory cache is bounded by bytes, not by count, because one decoded hero image can cost as much as a hundred thumbnails. Image libraries default to a fraction of the memory the app is allowed to use, often somewhere around a fifth to a quarter, and use least recently used eviction. The right size holds a few screens of images, so scrolling back a little is instant, but not the whole feed.
The memory cache must also respond to memory pressure. When the operating system warns the app, through a memory warning on iOS or a trim memory callback on Android, the cache should shrink or clear. A cache that ignores these signals turns a warning into a termination, and on iOS a foreground termination for memory looks to the user exactly like a crash. Android adds one more option: hardware bitmaps, which keep pixel data in graphics memory rather than the app's heap, reducing garbage collection pressure for images that are only drawn and never read back. Libraries use them by default where supported.
Sizing the disk cache
The disk cache holds encoded bytes, which are far smaller than decoded bitmaps, so it can be much larger: tens to a few hundred megabytes is typical. It lives in a cache directory the operating system may purge, which is fine because everything in it can be downloaded again. It is keyed by URL, which is one more reason for image URLs to be immutable: if a user changes their avatar, the URL should change too, for example by including a content hash or version, rather than overwriting the same URL and leaving stale copies in every cache between the server and the screen. Immutable URLs also let the CDN and the HTTP layer cache with long lifetimes.
What the interviewer is checking
When images come up, the interviewer wants to hear the two cache levels and what each holds, the rule that decoded size is width times height times bytes per pixel, downsampling to display size both at the CDN and at decode, decoding off the main thread, cancellation tied to cell reuse, and a memory cache bounded in bytes that responds to memory warnings. If you add one number, such as a twelve megapixel photo costing about forty eight megabytes decoded, you have shown exactly the kind of grounded understanding the deep dive is meant to find.
Priorities inside the loader
An image loader is also a scheduler. Visible images should be fetched before prefetched ones, and a large hero image at the top of a screen should not wait behind fifty avatars further down. Good loaders let each request carry a priority, raise it when the cell becomes visible and lower it when the cell scrolls away, and limit how many downloads and decodes run at once so the CPU and the radio are not swamped. When the user opens a detail screen, the full size image for that screen should jump the queue, and its thumbnail, already in memory, can be shown scaled up while the larger version loads. Mentioning this shows that you see the pipeline as a set of shared resources with a queue in front, not as a function that is called once per image.
Formats in one paragraph
Format choice is mostly a server and CDN decision, but you should know the landscape. JPEG is universal and fine for photos. WebP is smaller than JPEG at similar quality and is supported everywhere that matters on mobile. AVIF is smaller again and supported on current versions of both platforms, though decoding is more expensive, which matters on older, slower phones. HEIC is what iPhone cameras produce by default and is efficient, but less convenient to serve to every client. PNG is for graphics with sharp edges or transparency, and vector formats suit icons. A practical answer is to let the CDN negotiate the format from the client's Accept header, serving AVIF or WebP where supported and JPEG as a fallback, and to keep the client agnostic.
A three megabyte photo can cost about forty eight megabytes once decoded at full size, because memory is width times height times four bytes. Downsampling to the display size at the CDN and at decode is the most effective image memory fix there is.