KatInterview

Designing a Thread-Safe Organization Hierarchy Service

System DesignModel answer
HardModel answerSystem Designsystem-designconcurrencylock-free

Last updated

Design a thread-safe, concurrent organization tree supporting high-volume reads and atomic modifications without deadlocks or partial updates.

Design an in-memory organization hierarchy service that supports concurrent access for both reads and writes across users and groups.

Core Structure & Operations

  • The hierarchy is structured as a tree composed of Users (leaves) and Groups (internal nodes).
  • Read Workloads: High-throughput queries such as finding the Lowest Common Ancestor (LCA) of multiple users, fetching member lists of subtrees, and navigating paths to the root.
  • Write Workloads: Modifications including adding/removing users or groups, re-parenting subtrees (move), and executing multi-step batch updates.

Requirements & Challenges

  1. Concurrency Strategy: Propose an architectural model balancing low-latency reads and safe, atomic writes (e.g., Reader-Writer locks, fine-grained node locks, or immutable Copy-on-Write snapshots).
  2. Consistency Guarantees: Ensure queries never observe transient invalid states (such as dangling references, detached nodes, or temporary cycles during re-parenting). Explain how to support snapshot isolation and read-your-own-writes semantics.
  3. Edge Cases & Deadlock Prevention: Detail cycle detection protocols during node moves, handling deletion of non-empty groups, and preventing deadlocks under concurrent write attempts.
  4. Performance & Memory: Analyze memory contention, garbage collection/epoch management for long-running readers, and trade-offs under high write contention.

Hints

Hint 1

Identify the read-to-write ratio first; in typical enterprise org structures, reads outnumber writes by several orders of magnitude.

Hint 2

Consider how immutable persistent data structures (like structural sharing) eliminate lock contention for readers.

Hint 3

To prevent cycles during a subtree move, trace the path from the target parent upwards to ensure the target is not a descendant of the node being moved.