KatInterview

Relational Schema for Multi-Tenant Permission System

System DesignModel answer
MediumModel answerSystem Designdatabase-designauthorizationnormalization

Last updated

Design a normalized database schema supporting users, roles, permissions, role inheritance, and both global and resource-scoped access control.

Context

You're building the authorization layer for a multi-tenant content management platform. The system needs to enforce fine-grained access control where users receive permissions through roles, roles can build on other roles, and access can be granted either platform-wide or limited to individual resources.

Your Task

Design a relational database schema that satisfies these needs:

  1. Users and Role Assignment: Users receive one or multiple roles. Each role assignment may apply everywhere or only to a particular resource instance.

  2. Role Composition: Roles bundle permissions together. One role can extend another (for example, a Moderator role might include everything a Member role has, plus additional capabilities).

  3. Granular Permissions: Permissions combine an action (like read, update, remove) with a resource category (like post, file, workspace).

  4. Scope Flexibility: A user might be an Admin globally, or an Editor only for Post #42.

What to Deliver

  1. A complete schema including tables, keys, and relationships. Target third normal form.

  2. An approach for handling role composition so that granting a higher role automatically provides all permissions from roles it extends.

  3. A SQL query answering: "Does user 1001 have permission to remove the post with ID 42?"

Constraints

  • Plan for 10 million users and 100 million resource instances.
  • Authorization checks happen on every API call. Optimize the read path.
  • Keep the schema normalized and avoid redundancy.

Hints

Hint 1

Store role relationships separately from role definitions to keep the schema flexible.

Hint 2

Precompute the transitive closure of role relationships to avoid recursive queries during authorization checks.

Hint 3

Use a composite key or sentinel value to represent both global and instance-specific grants in the same table.