Thread-safe Organization Hierarchy: API, Concurrency, and Consistency
Context
You are building an in-memory organization directory that manages groups (hierarchical) and users. Multiple threads will concurrently read and mutate the hierarchy. The workload is read-heavy with periodic updates.
Assume:
-
Groups form a tree (or forest) identified by stable IDs; groups can have child groups.
-
Users have unique IDs and can be members of multiple groups.
-
The service runs in a single process but must be crash-safe via durable storage.
Task
Design thread-safe add/remove operations for users and groups.
-
Propose a minimal API for:
-
Creating/deleting users and groups
-
Adding/removing a user to/from a group
-
Moving a group under a new parent
-
Querying users, groups, and memberships
-
Propose a concurrency strategy (e.g., reader–writer locks, fine-grained locking with lock ordering, immutable snapshots with copy-on-write, or MVCC/RCU). Specify:
-
How writes occur atomically
-
How readers are isolated from in-progress writes
-
Deadlock avoidance strategy (if using locks)
-
Consistency guarantees:
-
How to guarantee linearizable writes (or at least read-consistent snapshots) while updates occur
-
How to serve long-running reads without blocking writers
-
Discuss:
-
Contention hot spots and performance trade-offs of your approach
-
Failure recovery (durability model, recovery steps)
-
How to test concurrency correctness (stress tests, race detection, linearizability/snapshot checks)