/Projects

Image loader with coalescing and two cache tiers

Memory, then disk, then network, and fifty identical requests become one download.

PracticeMachine codingTier 3 · Edge casesCreate45 minMobile systems

You will be able to: build image loader with coalescing and two cache tiers in 45 minutes, edge cases included.

Source: Tested in tests/mobile/MobileTests.kt

Mobile engineeringAll mobile pages

A smooth scroll depends on this: it coalesces duplicate requests, sizes images and cancels what scrolled away. Start from the basics in Day 12 →

Study with AI

45-minute round · HardKotlin · coroutines · tested Watch as reels
Part 1 of 5

Basics

The concepts this component is built on. With these in mind, every line of the solution has a reason.

1.1Key concepts

One idea per card, in the order the solution uses them.

01Cache tiers

Memory is fastest but lost when the app dies, disk survives restarts but is slower, and the network is slowest and can fail. Check them in that order and fill the faster tiers on the way back.

02Request coalescing

If fifty rows ask for the same URL at once, start one download and let all fifty wait for its result.

03Coroutines and Deferred

A coroutine is a lightweight task that can pause while it waits. async returns a Deferred: a handle to a future result that any number of callers can await.

04Mutex

A lock for coroutines. Only one coroutine at a time runs the code inside withLock; the others pause instead of blocking a thread.

05Scopes and SupervisorJob

Every coroutine belongs to a scope that can cancel it. With a SupervisorJob, one failed child does not cancel its siblings.

Part 2 of 5

Question

What exactly you are asked to build, what must be true when you are done, and what to ask before you start.

2.1Prompt

The question as the interviewer asks it, and the requirements to check your solution against.

Interviewer

Design and implement the image loader behind a scrolling feed. Many rows ask for the same URL at once; rows get recycled while images load; the app must work from disk when offline.

Requirements

  • Serve from memory, then disk, then network
  • Concurrent requests for one URL share a single download
  • Cancel work when a row is recycled
  • Safe to call from any coroutine

2.2Clarify first

Questions worth asking, and the answers this interviewer gives.

You askInterviewer answers
Decode in the loader or the view?The loader returns bytes here; decode and downsample off the main thread (Dispatchers.Default) before display.
How big is the disk cache?A few hundred MB with LRU eviction; out of scope for this round, injected as an interface.
Do we need priorities?Nice to have: visible rows first. Mention it, implement if time allows.
Part 3 of 5

Detailed answer

The design decisions, the complete Kotlin solution, and a walk through it one block at a time.

3.1Design decisions

Settle these before writing code; each one shows up in the solution.

A Mutex owns the state

The memory cache and the in-flight map are only touched while holding the lock: no data races.

In-flight map of Deferreds

The first caller starts an async download; later callers await the same Deferred. That is request coalescing.

Downloads live in the app scope

The Deferred runs in an injected scope with a SupervisorJob, so one caller leaving does not cancel it for the others, and one failure does not cancel the scope.

Interfaces for I/O

Fetcher and disk cache are interfaces, so tests inject fakes and count network calls.

3.2Solution

Complete, compiling Kotlin with coroutines, built with warnings as errors and covered by tests/mobile/MobileTests.kt. The Java and TypeScript tabs do the same job with each language's own tools: Java compiled and run in our tests, TypeScript run in our tests.

ImageLoader.ktKotlin
1import kotlinx.coroutines.CoroutineScope2import kotlinx.coroutines.Deferred3import kotlinx.coroutines.NonCancellable4import kotlinx.coroutines.async5import kotlinx.coroutines.sync.Mutex6import kotlinx.coroutines.sync.withLock7import kotlinx.coroutines.withContext8 9fun interface ImageFetcher {10  suspend fun fetch(url: String): ByteArray11}12 13interface DiskCache {14  suspend fun get(key: String): ByteArray?15  suspend fun put(key: String, bytes: ByteArray)16}17 18/**19 * Memory → disk → network image loading with request coalescing:20 * fifty rows asking for the same URL trigger exactly one download.21 */22class ImageLoader(23  private val fetcher: ImageFetcher,24  private val scope: CoroutineScope,   // app scope with a SupervisorJob: downloads outlive one caller25  private val disk: DiskCache? = null,26  memoryLimit: Int = 50 * 1024 * 1024,27) {28  private val lock = Mutex()29  private val memory = LruCache<String, ByteArray>(memoryLimit)30  private val inFlight = HashMap<String, Deferred<ByteArray>>()31 32  suspend fun load(url: String): ByteArray {33    val download = lock.withLock {34      memory[url]?.let { return it }                     // 1. memory35      inFlight[url] ?: scope.async {                     // 2. or join the download already running36        disk?.get(url)?.let { return@async it }          // 3. disk37        fetcher.fetch(url).also { disk?.put(url, it) }   // 4. network, written back to disk38      }.also { inFlight[url] = it }39    }40    try {41      val bytes = download.await()42      lock.withLock { memory.put(url, bytes, cost = bytes.size) }43      return bytes44    } finally {45      withContext(NonCancellable) {                      // clean up even if this caller was cancelled46        lock.withLock { if (inFlight[url] === download) inFlight.remove(url) }47      }48    }49  }50 51  /** Call when a row is recycled before its image arrives and nobody else needs it. */52  suspend fun cancel(url: String) = lock.withLock {53    inFlight.remove(url)?.cancel()54  }55 56  suspend fun clearMemory() = lock.withLock { memory.clear() }57}
57 linesUTF-8Kotlin

3.3Line-by-line walkthrough

The same code in the order you would explain it in the room. The line numbers follow the Kotlin file; the Java and TypeScript tabs above have the same steps in the same order.

1

State behind a Mutex

The memory cache and the in-flight map are only read or written inside lock.withLock.

ImageLoader.ktKotlin
28  private val lock = Mutex()29  private val memory = LruCache<String, ByteArray>(memoryLimit)30  private val inFlight = HashMap<String, Deferred<ByteArray>>()
lines 28–30UTF-8Kotlin
2

1. Memory hit

Cheapest path first: return the cached bytes and refresh their recency. return exits load() straight from the inline lambda.

ImageLoader.ktKotlin
34      memory[url]?.let { return it }                     // 1. memory
lines 34–34UTF-8Kotlin
3

2. Join a running download

If this URL is already downloading, reuse its Deferred. Fifty rows, one download.

ImageLoader.ktKotlin
35      inFlight[url] ?: scope.async {                     // 2. or join the download already running
lines 35–35UTF-8Kotlin
4

3–4. Disk, then network

The async block checks disk, fetches if needed and writes back to disk for offline use.

ImageLoader.ktKotlin
36        disk?.get(url)?.let { return@async it }          // 3. disk37        fetcher.fetch(url).also { disk?.put(url, it) }   // 4. network, written back to disk
lines 36–37UTF-8Kotlin
5

Register before awaiting

Store the Deferred while still holding the lock, so the next caller finds it.

ImageLoader.ktKotlin
38      }.also { inFlight[url] = it }
lines 38–38UTF-8Kotlin
6

Await outside the lock, then cache

Never suspend on the network while holding the lock. Cache with cost = byte count, so the LRU budget is in bytes.

ImageLoader.ktKotlin
41      val bytes = download.await()42      lock.withLock { memory.put(url, bytes, cost = bytes.size) }43      return bytes
lines 41–43UTF-8Kotlin
7

Always clean up

finally removes the entry even if this caller was cancelled. NonCancellable lets the cleanup take the lock; the identity check avoids removing a newer download.

ImageLoader.ktKotlin
45      withContext(NonCancellable) {                      // clean up even if this caller was cancelled46        lock.withLock { if (inFlight[url] === download) inFlight.remove(url) }47      }
lines 45–47UTF-8Kotlin
8

Cancel on recycle

When a row scrolls away and nobody else needs the URL, cancel the download.

ImageLoader.ktKotlin
52  suspend fun cancel(url: String) = lock.withLock {53    inFlight.remove(url)?.cancel()54  }
lines 52–54UTF-8Kotlin

Complexity: O(1) locked work per request; one network call per unique URL

Part 4 of 5

Q&A

The questions interviewers ask after the code works. Answer out loud, then open.

4.1Follow-up questions

How would you add priorities?
Keep a queue of pending URLs with priority; visible rows bump priority, off-screen ones lower it; limit concurrent downloads with a Semaphore.
Why a scope parameter instead of coroutineScope?
A download started by one row must survive that row being recycled while another row waits on it. Structured concurrency inside load() would tie it to the first caller.
How do you test it?
Inject a counting fake fetcher, fire 20 concurrent loads and assert one network call. That test ships with this code.
Part 5 of 5

Extra depth

The cases that break naive solutions, how the same design looks on iOS, and what to practise next.

5.1Edge cases

Say these before the interviewer does.

Show detailsHide details
  • A download fails: every joiner gets the same exception and the URL can be retried
  • Cancelling a URL another visible row still needs (keep a reference count before cancelling)
  • onTrimMemory: clearMemory(), keep disk
  • Very large images: downsample before caching

5.2On iOS

The same design in Swift and Apple's frameworks.

Show detailsHide details
ApproachSwift / iOS
In productionKingfisher or Nuke: memory and disk caches plus request coalescing out of the box.
From scratchAn actor holding [URL: Task<Data, Error>]; callers await the same Task's value.
CancellationCancel in prepareForReuse(), or tie the Task to the SwiftUI view with .task(id:).
AI Engineer · Image loader with coalescing and two cache tiers