KatInterview

Background Job API for Hierarchical Data Replication

System DesignModel answer
MediumModel answerSystem Designbackground-jobsdatabasesapi-design

Last updated

Design an endpoint that replicates a multi-level data hierarchy with automatic identifier reassignment, progress tracking, and fault tolerance.

You're building a service that manages collections of evaluation data. The storage hierarchy has four levels: a collection holds items, each item has submissions, and each submission has assessments. All relationships use foreign keys in a relational database.

Design an API endpoint that replicates an entire collection along with all nested items, submissions, and assessments. The replica must be fully independent—modifications to either the original or the replica should never affect the other.

Requirements

Your design should address:

  1. Small collections: For collections that finish quickly, define the endpoint contract and explain how foreign key references are updated to point to newly created rows rather than the originals.

  2. Large collections: When replication takes too long for a single HTTP request, restructure the operation to run asynchronously with progress visibility and the ability to recover from interruptions without losing completed work.

  3. Reliability: Handle duplicate requests from clients and prevent multiple worker processes from executing the same replication simultaneously.

  4. Concurrent modifications: Decide what happens when the source collection is modified during replication, and define cleanup behavior when replication fails partway through.

Areas to Consider

  • How do you maintain referential integrity when every row receives a new primary key?
  • What data structures support resumable progress after a crash?
  • How do clients distinguish between a new replication request and a retry of an existing one?
  • What mechanisms prevent two workers from processing the same job?
  • Should the replica reflect the source at job start time, or can it include changes made during replication?
  • When should partial replicas be visible to users, and when should they be removed?

Hints

Hint 1

Consider how a mapping table from old IDs to new IDs can be persisted and used across restarts

Hint 2

Think about what database constraints or application-level checks can ensure a request creates only one job even if submitted multiple times

Hint 3

For worker coordination, explore how a timestamp field or version counter on the job record can serve as a lease mechanism