Relational Schema for Multi-Tenant Permission System
System DesignModel answerLast 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:
-
Users and Role Assignment: Users receive one or multiple roles. Each role assignment may apply everywhere or only to a particular resource instance.
-
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).
-
Granular Permissions: Permissions combine an action (like
read,update,remove) with a resource category (likepost,file,workspace). -
Scope Flexibility: A user might be an Admin globally, or an Editor only for Post #42.
What to Deliver
-
A complete schema including tables, keys, and relationships. Target third normal form.
-
An approach for handling role composition so that granting a higher role automatically provides all permissions from roles it extends.
-
A SQL query answering: "Does user 1001 have permission to
removethepostwith 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.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Authorization checks run constantly—potentially millions per second—while role definitions and assignments change rarely. The schema must make reads fast even if writes become more complex.
Core Design Decisions
Scope placement: Attach scope to the user-role assignment, not to permissions themselves. The permission remove:post means the same thing everywhere. What varies is where a user holds a role. This keeps the permission set small and moves variability into the assignment table.
Role composition: Maintain two tables: one for direct parent-child links (the source of truth admins edit) and one holding the full transitive closure (derived, optimized for queries). The closure eliminates recursion from the read path.
Global vs instance scope: Represent both with (scope_type, scope_instance) pairs. Use (0, 0) for global grants instead of NULL, so the values can participate in a composite primary key and support efficient indexed lookups.
Schema
CREATE TABLE users (
user_id BIGINT PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL
);
CREATE TABLE roles (
role_id INT PRIMARY KEY,
role_name VARCHAR(64) NOT NULL UNIQUE
);
-- Direct parent-child links (what admins configure)
CREATE TABLE role_parents (
child_id INT REFERENCES roles(role_id),
parent_id INT REFERENCES roles(role_id),
PRIMARY KEY (child_id, parent_id),
CHECK (child_id != parent_id)
);
-- Transitive closure (derived, includes identity rows)
CREATE TABLE role_hierarchy (
descendant_id INT REFERENCES roles(role_id),
ancestor_id INT REFERENCES roles(role_id),
distance SMALLINT NOT NULL,
PRIMARY KEY (descendant_id, ancestor_id)
);
CREATE TABLE resource_categories (
category_id SMALLINT PRIMARY KEY,
category_name VARCHAR(32) NOT NULL UNIQUE
);
CREATE TABLE permissions (
permission_id INT PRIMARY KEY,
action_name VARCHAR(16) NOT NULL,
category_id SMALLINT REFERENCES resource_categories(category_id),
UNIQUE (action_name, category_id)
);
CREATE TABLE role_permissions (
role_id INT REFERENCES roles(role_id),
permission_id INT REFERENCES permissions(permission_id),
PRIMARY KEY (role_id, permission_id)
);
CREATE TABLE user_role_grants (
user_id BIGINT REFERENCES users(user_id),
role_id INT REFERENCES roles(role_id),
scope_type SMALLINT NOT NULL DEFAULT 0,
scope_instance BIGINT NOT NULL DEFAULT 0,
granted_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP,
PRIMARY KEY (user_id, scope_type, scope_instance, role_id)
);
CREATE INDEX idx_user_grants ON user_role_grants(user_id, scope_type, scope_instance);
Key Points
-
3NF compliance: Each non-key attribute depends only on the whole key. Role names live in
roles, action-category pairs inpermissions, grants inuser_role_grants. -
role_hierarchy is derived: Rebuild it whenever
role_parentschanges. For a few hundred roles, recomputing the entire closure in a transaction is simple and correct. Include self-rows(role_id, role_id, 0)so every role trivially inherits from itself. -
Sentinel for global scope:
(0, 0)instead of NULL allows participation in the primary key and makes "global or this specific resource" an indexed range scan:(scope_type, scope_instance) IN ((0,0), (5, 42)).
Role Composition
The role_parents table holds direct edges. Admins add a row (Moderator, Member) to say Moderator extends Member.
To resolve inheritance at query time without recursion:
- Maintain
role_hierarchyas the transitive closure ofrole_parents. - When an edge is added, insert rows connecting all descendants of the child to all ancestors of the parent, plus identity rows.
- Reject any edge that would create a cycle by checking if the parent already reaches the child.
With the closure table, "what permissions does role X have" becomes a single join from role_hierarchy to role_permissions, regardless of hierarchy depth.
Authorization Query
Question: Can user 1001 remove post 42?
Step 1: Resolve the permission
In-memory or cache-backed lookup:
SELECT p.permission_id, rc.category_id
FROM permissions p
JOIN resource_categories rc ON rc.category_id = p.category_id
WHERE p.action_name = 'remove' AND rc.category_name = 'post';
Assume this returns permission_id = 78, category_id = 5.
Step 2: Check authorization
SELECT EXISTS (
SELECT 1
FROM user_role_grants urg
JOIN role_hierarchy rh ON rh.descendant_id = urg.role_id
JOIN role_permissions rp ON rp.role_id = rh.ancestor_id
WHERE urg.user_id = 1001
AND rp.permission_id = 78
AND (urg.scope_type, urg.scope_instance) IN ((0, 0), (5, 42))
AND (urg.expires_at IS NULL OR urg.expires_at > CURRENT_TIMESTAMP)
) AS has_permission;
How It Works
-
user_role_grants: Find all roles granted to user 1001, either globally
(0,0)or for post 42(5, 42). The index on(user_id, scope_type, scope_instance)makes this a narrow range scan yielding a handful of rows. -
role_hierarchy: For each granted role, find all roles it inherits from (including itself). Primary key lookup.
-
role_permissions: Check if any of those roles carry permission 78. Primary key lookup.
-
EXISTS: Short-circuits on the first match.
The query touches three tables and uses only indexed operations. No table scans, no recursion.
Scaling Considerations
-
Partition
user_role_grantsbyuser_id: Authorization checks are single-user operations. Partitioning keeps each query local to one shard. -
Replicate small reference tables:
roles,permissions,resource_categories,role_hierarchy, androle_permissionscollectively hold a few thousand rows. Replicate them to every database node or cache them in the application. -
Cache permission resolution: The mapping from
('remove', 'post')to IDs changes rarely. Load it once at startup. -
Precompute common checks: If 90% of queries check global access, maintain a denormalized view of global grants for even faster lookups.
-
Audit and expiration: The
expires_atcolumn supports temporary grants. Run a scheduled job to purge expired rows and keep the hot table lean.
Common Mistakes
-
Recursive CTEs in the hot path: Resolving role inheritance with
WITH RECURSIVEon every check is too slow. Precompute the closure. -
NULL for global scope: NULLs break composite keys and complicate indexing. Use a sentinel.
-
Attaching scope to permissions: This explodes the permission table and duplicates
(action, category)pairs for every resource instance. -
Missing expiration logic: Temporary grants accumulate. Either filter them in queries or clean them up asynchronously.
Complexity
-
Space: O(U + R² + G) where U is users, R is roles, G is grants. Role tables are O(R²) but R is small (~hundreds).
-
Authorization query: O(1) with proper indexes—constant number of index lookups regardless of hierarchy depth or number of permissions.
-
Hierarchy update: O(R²) to rebuild the closure. Rare operation, acceptable cost.