KatInterview

Aggregate Node Counts Across an Unreliable Distributed Tree

System DesignModel answer
HardModel answerSystem Designdistributed-systemsfault-toleranceconvergecast

Last updated

Design a fault-tolerant protocol where each node in a distributed tree contributes its subtree size upward until the root knows the total node count.

Background

You are building a monitoring tool for a cluster of servers arranged in a tree topology. Each server knows only its own identity, its direct parent, and its immediate children — there is no global directory or shared memory. You need the root server to compute the total number of servers in the cluster.

The network is unreliable: any message can be dropped, delayed, or delivered more than once. A server may crash and restart at any time. Your design must still produce a correct answer eventually, without requiring every server to be up simultaneously.

Your Task

Design a distributed aggregation protocol in which each server maintains local state and responds to two kinds of incoming messages. The root server initiates the process and eventually learns the total count.

Address all four of the following questions in your answer.

  1. Message schema. What message types flow between a parent server and its children? List every field and the direction each message travels.

  2. Idempotency. The same request can arrive at a server more than once because the sender retransmits until it gets a reply. How does a server ensure it does not perform the aggregation work twice for the same logical request?

  3. Per-node state machine. What data does a server keep while it is waiting for its children to reply? Walk through the states a server transitions through, from receiving a request to sending its result upward.

  4. Concurrent requests. What should a server do if a second request arrives while it is still waiting for its children to respond to an earlier one? Consider both the case where the two requests share the same ID and the case where they have different IDs.

Constraints

  • A server has no knowledge of the tree beyond its own parent and children.
  • All message sends are fire-and-forget; there is no blocking RPC.
  • A server may receive the same request message several times due to retries, and it must behave correctly in all cases.

Example

Tree: A (root) → {B, C}, B → {D}

A sends REQUEST(id=42) to B and C.

B sends REQUEST(id=42) to D.
D (leaf) replies RESULT(id=42, count=1) to B.
B adds itself: count = 1 + 1 = 2. B replies RESULT(id=42, count=2) to A.

C (leaf) replies RESULT(id=42, count=1) to A.

A adds itself: count = 1 + 2 + 1 = 4. Done.

Deliverables

  • Define the fields in each message type.
  • Describe the data structure each server maintains per active request.
  • Sketch the logic for on_receive_request(req_id) and on_receive_result(req_id, child_id, count).
  • A working Python implementation is a strong bonus.

Hints

Hint 1

Think of this as a two-phase message flow: a wave of requests fans out from the root down to the leaves, then a wave of replies carries subtree sizes back up. Each node's job is simply to aggregate its children's replies and add 1 for itself.

Hint 2

A unique request ID is your main tool for idempotency. When a duplicate request arrives, the server should check whether it already started — or already finished — handling that ID, and act accordingly without restarting any work.

Hint 3

Track per-child results in a map keyed by child ID rather than a running integer sum. That way, a duplicated reply from the same child can be detected and ignored, because you can see it is already recorded.