Image loader with coalescing and two cache tiers

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

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.

KotlinJavaTypeScript
ImageLoader.ktKotlinCopy
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
ImageLoader.javaJavaCopy
1import java.util.HashMap;2import java.util.Map;3import java.util.concurrent.CompletableFuture;4import java.util.concurrent.CompletionException;5import java.util.concurrent.Executor;6 7/**8 * Memory → disk → network image loading with request coalescing:9 * fifty rows asking for the same URL trigger exactly one download.10 */11public final class ImageLoader {12  @FunctionalInterface13  public interface ImageFetcher {14    byte[] fetch(String url) throws Exception;15  }16 17  public interface DiskCache {18    byte[] get(String key);19    void put(String key, byte[] bytes);20  }21 22  private final ImageFetcher fetcher;23  private final Executor executor;          // app-wide pool: downloads outlive one caller24  private final DiskCache disk;             // may be null25  private final Object lock = new Object();26  private final LruCache<String, byte[]> memory;27  private final Map<String, CompletableFuture<byte[]>> inFlight = new HashMap<>();28 29  public ImageLoader(ImageFetcher fetcher, Executor executor, DiskCache disk, int memoryLimit) {30    this.fetcher = fetcher;31    this.executor = executor;32    this.disk = disk;33    this.memory = new LruCache<>(memoryLimit);34  }35 36  public CompletableFuture<byte[]> load(String url) {37    CompletableFuture<byte[]> download;38    synchronized (lock) {39      byte[] cached = memory.get(url);                                      // 1. memory40      if (cached != null) return CompletableFuture.completedFuture(cached);41      download = inFlight.get(url);                                         // 2. or join the download already running42      if (download == null) {43        download = CompletableFuture.supplyAsync(() -> download(url), executor);44        inFlight.put(url, download);45      }46    }47    CompletableFuture<byte[]> shared = download;48    return shared.whenComplete((bytes, error) -> {49      synchronized (lock) {50        if (error == null) memory.put(url, bytes, bytes.length);51        if (inFlight.get(url) == shared) inFlight.remove(url);              // clean up even if the download failed52      }53    });54  }55 56  private byte[] download(String url) {57    try {58      byte[] onDisk = disk == null ? null : disk.get(url);                  // 3. disk59      if (onDisk != null) return onDisk;60      byte[] bytes = fetcher.fetch(url);                                    // 4. network, written back to disk61      if (disk != null) disk.put(url, bytes);62      return bytes;63    } catch (Exception e) {64      throw new CompletionException(e);65    }66  }67 68  /** Call when a row is recycled before its image arrives and nobody else needs it. */69  public void cancel(String url) {70    CompletableFuture<byte[]> job;71    synchronized (lock) { job = inFlight.remove(url); }72    if (job != null) job.cancel(true);73  }74 75  public void clearMemory() {76    synchronized (lock) { memory.clear(); }77  }78}
78 linesUTF-8Java
ImageLoader.tsTypeScriptCopy
1import { LruCache } from "./LruCache.ts";2 3export interface ImageFetcher {4  fetch(url: string, signal: AbortSignal): Promise<Uint8Array>;5}6 7export interface DiskCache {8  get(key: string): Promise<Uint8Array | undefined>;9  put(key: string, bytes: Uint8Array): Promise<void>;10}11 12interface Download {13  promise: Promise<Uint8Array>;14  abort: AbortController;15}16 17/**18 * Memory → disk → network image loading with request coalescing:19 * fifty rows asking for the same URL trigger exactly one download.20 */21export class ImageLoader {22  private fetcher: ImageFetcher;23  private disk?: DiskCache;24  private memory: LruCache<string, Uint8Array>;25  private inFlight = new Map<string, Download>();26 27  constructor(fetcher: ImageFetcher, disk?: DiskCache, memoryLimit = 50 * 1024 * 1024) {28    this.fetcher = fetcher;29    this.disk = disk;30    this.memory = new LruCache(memoryLimit);31  }32 33  async load(url: string): Promise<Uint8Array> {34    const cached = this.memory.get(url);               // 1. memory35    if (cached) return cached;36    let job = this.inFlight.get(url);                  // 2. or join the download already running37    if (!job) {38      const abort = new AbortController();39      job = { promise: this.download(url, abort.signal), abort };40      this.inFlight.set(url, job);41    }42    try {43      const bytes = await job.promise;44      this.memory.put(url, bytes, bytes.length);45      return bytes;46    } finally {47      if (this.inFlight.get(url) === job) this.inFlight.delete(url);   // clean up even if the download failed48    }49  }50 51  private async download(url: string, signal: AbortSignal): Promise<Uint8Array> {52    const onDisk = await this.disk?.get(url);          // 3. disk53    if (onDisk) return onDisk;54    const bytes = await this.fetcher.fetch(url, signal);   // 4. network, written back to disk55    await this.disk?.put(url, bytes);56    return bytes;57  }58 59  /** Call when a row is recycled before its image arrives and nobody else needs it. */60  cancel(url: string): void {61    const job = this.inFlight.get(url);62    if (!job) return;63    this.inFlight.delete(url);64    job.abort.abort();65  }66 67  clearMemory(): void {68    this.memory.clear();69  }70}
70 linesUTF-8TypeScript

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.ktKotlinCopy
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.ktKotlinCopy
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.ktKotlinCopy
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.ktKotlinCopy
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.ktKotlinCopy
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.ktKotlinCopy
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.ktKotlinCopy
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.ktKotlinCopy
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:).