KatInterview

Choosing a Concurrency Model for a CPU-Heavy Image Resizer

System DesignModel answer
MediumModel answerSystem Designconcurrencythreads-vs-processesshared-memory

Last updated

Decide between threads, processes, or a hybrid for a CPU-bound image preprocessing service, weighing memory, GIL limits, IPC cost, crash isolation and how to benchmark.

Context

You are building the preprocessing stage in front of a multimodal model. Each job delivers a large decoded image, for example 4000x3000 RGBA at about 48 MB, and the stage must downsample it (bicubic), convert its color space, and normalize it into a tensor. The work is almost entirely CPU-bound. The stage runs on multi-core Linux machines with a fixed memory budget.

You must decide whether the workers should be threads, processes, or a hybrid, with the goal of maximizing throughput inside that memory budget.

What to cover

Give a reasoned comparison and a recommendation, addressing each of these areas:

  1. Memory model and footprint

    • How address spaces, page tables and allocators differ between threads and processes.
    • How copy-on-write after fork() behaves for large read-only pixel buffers, and the situations in which pages get copied anyway (reference counts, garbage collection, allocator bookkeeping).
  2. Parallelism in the runtime

    • How the Python GIL or a stop-the-world collector limits what threads can achieve on CPU-bound work.
    • The difference between native libraries that release the GIL (OpenCV, libvips, and similar) and pure interpreted code.
  3. Moving data between workers

    • Zero-copy sharing among threads compared with process-level options: POSIX shared memory, Unix domain sockets, pipes.
    • The cost of serialization (pickle, protobuf) compared with passing a descriptor to shared raw memory.
  4. Pool design and failure isolation

    • How to size the pool against physical cores, cache capacity and memory so you avoid cache thrashing and the OOM killer.
    • What happens when a malformed image triggers a segfault or a leak in a thread versus in a child process.
  5. Measurement

    • An experimental plan for choosing between the designs: which OS and process metrics to track (context switches, page faults, user vs. system CPU time, resident memory) and which tools to use (perf, eBPF, valgrind/massif, and so on).

End with a concrete recommendation and the conditions that would make you change it.

Hints

Hint 1

Work out how many copies a 48 MB frame goes through in each transport and compare that to the time the resize itself takes; memcpy runs at roughly 5-10 GB/s per core.

Hint 2

Ask which parts of the pipeline hold the GIL. If the heavy kernels release it, threads may scale well, so the real deciding factors become crash containment and memory accounting.

Hint 3

Remember that RSS double-counts shared pages. Use PSS to judge fork and shared-memory savings, and size the pool from the memory limit divided by peak per-task usage, not just from core count.

Hint 4

Beware of nested thread pools: each worker process running its own multi-threaded OpenCV or BLAS pool oversubscribes the CPUs.