KatInterview

Build a Parallel Image Transform Pipeline With Worker Processes

System DesignModel answer
MediumModel answerSystem Designconcurrencymultiprocessingpipeline-design

Last updated

Design a resize-then-watermark pipeline for a batch of photos, then scale it across a bounded pool of worker processes with safe output writes and per-photo failure isolation.

A photo-hosting service needs to convert every newly uploaded image into a feed-ready thumbnail: resized to specified dimensions and marked with a small corner watermark. Your job is to build this pipeline in two stages — first get the single-image operation right, then scale it across multiple worker processes.

Context

To keep the problem self-contained and easy to test without external dependencies, represent images as 2D lists of integers (pixel values). The design decisions you make here — transform ordering, output safety, process boundaries — carry over directly to a real implementation using a library like Pillow.

Constraints

  • Every image comes with an explicit job spec: source file path, destination file path, target width and height, and a watermark intensity value. None of these are hardcoded or defaulted anywhere.
  • Images are independent of each other, but memory and disk I/O are shared across the whole batch.
  • A corrupt or missing image must never abort the batch. It should produce an error record; every other image should continue unaffected.
  • Worker count is bounded — you should not spawn one process per image.

Part 1 — Single-image processing

Implement a function that reads one image, resizes it, stamps the watermark into the bottom-right corner, writes the output, and returns a result record. It must never raise an exception past its own boundary.

def process_image(spec: dict) -> dict:
    """
    spec keys: id, src_path, dst_path, target_size (w, h), watermark_value
    Returns {'id': ..., 'status': 'ok'}
         or {'id': ..., 'status': 'error', 'error': str}
    """

Consider what happens to the destination file if the process is killed mid-write. How do you ensure no reader ever mistakes a half-written file for a completed one?

Part 2 — Parallel batch processing

def run_pipeline(specs: list[dict], max_workers: int) -> list[dict]:
    """
    Process every spec using at most max_workers processes in flight.
    Return exactly one result or error record per spec.
    """

Think carefully about what you pass to each worker process and why it should be as small as possible.

What a strong answer covers

  • Transform order: resize first, watermark second — explain why the order is not arbitrary.
  • Atomic output: a destination file is either absent or complete, never partially written.
  • A bounded worker pool rather than unbounded process spawning.
  • Passing lightweight job specs across process boundaries, not decoded pixel buffers.
  • Per-image failure isolation: one bad input produces one error record, nothing more.

Hints

Hint 1

Apply the watermark after resizing, not before. If you stamp the corner first, the resize step will scale and distort the watermark pixels along with the rest of the image.

Hint 2

Write output to a temporary file next to the destination, then use os.replace() to move it into place. On POSIX systems this rename is atomic, so a reader never sees a partially written file at the final path.

Hint 3

Each worker process receives a job spec (a few strings and numbers) and reads the source image file itself. Shipping the already-decoded pixel grid through the IPC pipe would cost tens of megabytes per image and negate most of the parallelism benefit.