KatInterview

Building a Concurrent Rendering Pipeline for Placeholder Substitution

System DesignModel answer
MediumModel answerSystem Designworker-poolproducer-consumermessage-queue

Last updated

Design a horizontally scalable backend that renders millions of placeholder-substitution jobs per hour using queues and worker pools, with fault tolerance, backpressure, and sensible job routing.

Imagine you already have a routine that takes a template such as server={{db_host}}:{{db_port}} and a dictionary of values, and returns the text with every placeholder filled in. You now need to offer this as a production backend service that handles millions of rendering jobs per hour.

Each job contains:

  • a unique identifier,
  • one template string (or a small group of them),
  • a dictionary of values to substitute.

The service must sustain high throughput, scale out horizontally, keep slow or oversized jobs from holding up everything else, and use a worker pool to run jobs concurrently.

Walk through a design that covers the following.

  1. Overall architecture. Name the main components (entry API, queue, workers, storage) and explain how a job moves through them.
  2. Worker pool mechanics. Explain how jobs enter the system (producers) and how workers pick them up (consumers). Show how this is the producer–consumer pattern and how you bound concurrency so the system is not overloaded.
  3. Distributing work. Compare ways to assign jobs to workers: round-robin, random, a single shared queue, several queues, and hashing on a key. Explain when hash-based assignment (for example by customer or resource) is worth it, and the trade-offs among fairness, load balance, and ordering of related jobs.
  4. Scalability and reliability. Describe how the system scales as volume grows, how it recovers when a worker dies mid-job (retries, idempotency), and how it pushes back on producers that send too much.
  5. Implementation choices. Suggest queue technology and justify it, list the metrics you would monitor, and mention optimizations specific to template rendering (such as caching parsed templates or batching).

Give a step-by-step design and be explicit about the trade-offs behind each decision.

Hints

Hint 1

Start with numbers: convert jobs per hour to jobs per second, estimate payload size, and note that a single-pass substitution is cheap. The real risks are I/O, serialization, and a few huge jobs, which should shape the design.

Hint 2

Think about two levels of producer–consumer: the broker between API servers and workers, and a bounded in-memory buffer between a worker's fetcher and its executor threads or processes. Bounded buffers are where backpressure comes from.

Hint 3

For distribution, ask what property you need: ordering, cache locality, or isolation justifies hashing on a key; pure throughput favors a shared competing-consumer queue. Then consider hot keys and head-of-line blocking, and separate lanes for small and large jobs.

Hint 4

For failures, decide when to acknowledge or commit (after the result is durably written) and why deterministic rendering makes at-least-once delivery safe when paired with idempotent result writes.