KatInterview

Building a Fan-Out Service That Indexes a Huge Directory Tree

System DesignModel answer
MediumModel answerSystem Designsystem-designdistributed-systemsmessage-queue

Last updated

Design an asynchronous service where workers recursively split a directory scan into child tasks, with safe retries, deduplication, progress reporting and scaling to billions of files.

You are asked to design a backend service that inventories a very large file system, starting from a root path supplied by the caller.

A client calls an API to launch an indexing job. The call returns right away, and a fleet of background workers walks the directory tree asynchronously. When a worker handles a directory, it may break the work into smaller units and push new asynchronous tasks onto the same queue that fed it, so the system ends up triggering more of its own work recursively.

Walk through your design and cover the following:

  • The API for starting a job and for checking its status
  • The data model for jobs, tasks and the metadata of discovered files
  • How a worker schedules follow-up tasks for subdirectories and for large listings
  • How you deal with retries, idempotency, deduplication and partial failures
  • How the design scales to extremely large trees (think on the order of a billion files, and single directories with millions of entries)
  • What role the database, the message queue and the workers each play
  • How clients see progress and obtain the final results

State your assumptions about scale explicitly, and point out the trade-offs you are making.

Hints

Hint 1

Make the unit of work a single page of one directory listing rather than a whole directory. Think about what that does for huge directories and for the cost of a retry.

Hint 2

Assume the queue delivers at least once and workers die at arbitrary moments. Which writes need deterministic keys, and how can you make 'mark parent done' and 'publish children' succeed or fail together?

Hint 3

Think about how you know the job is finished when work keeps spawning more work. A counter of outstanding tasks that changes by (children - 1) per task is one approach; consider what happens when that counter becomes a hot row.

Hint 4

Decide which component is the source of truth. If the queue can lose or duplicate a message, what lets you recover, and what periodic process would detect stuck tasks?