KatInterview

Resource Lifecycle and Isolation in Multi-Tenant Allocation

System DesignModel answer
MediumModel answerSystem Designconcurrencymulti-tenancystate-machine

Last updated

Design entities, state machine, and concurrency control for a resource allocator where tenants share infrastructure but remain strictly isolated.

You're designing the internal module for a multi-tenant platform where users request and release allocatable units—think seats, slots, or licenses. The module must guarantee two properties: no unit is ever assigned to multiple holders simultaneously, and one tenant's users can never interact with another tenant's resources, even by accident or malicious intent.

Your design should demonstrate clean domain modeling, an explicit lifecycle for each unit showing legal state transitions, safe handling of concurrent requests through optimistic locking, and structural enforcement of tenant boundaries within the code itself.

What You Need to Address

Entities and Ownership
Identify the core domain objects, their attributes and relationships, and where tenant identity lives. Show the service and repository boundaries.

Lifecycle State Machine
Define every state a resource passes through, the operations that trigger transitions, and how illegal transitions are rejected—for instance, releasing a unit that was never allocated or is already free.

Concurrency Control
Two users from the same tenant request the last available unit at the same moment on different service nodes. Walk through your optimistic locking mechanism: what each caller receives, how version conflicts are detected, and what happens under heavy contention when many compete for few resources.

Tenant Isolation
Demonstrate how your design structurally prevents any code path from reading or modifying another tenant's data, even if a caller supplies a cross-tenant identifier.

Constraints

  • Treat a resource as a generic allocatable entity with an ID and owning tenant.
  • Multiple service instances share one transactional store; in-process locks alone are insufficient.
  • Use optimistic concurrency control, but note when you would choose pessimistic locking instead.

Questions to Consider

  • Does each tenant own a separate pool, or do they draw from shared inventory with per-tenant quotas?
  • Can one user hold multiple resources? Are there per-user or per-tenant limits?
  • When no resources are available, should requests fail immediately or queue?
  • Do allocations expire if never released?
  • Should the module retry version conflicts internally or surface them to the caller?

Hints

Hint 1

Consider carrying a version counter on each resource record and using a compare-and-swap write to claim it

Hint 2

State transitions can be validated by maintaining an explicit allowed-transition map and checking before every write

Hint 3

Derive the tenant context once at the service boundary, then thread it through every query and write so repositories never see cross-tenant data