Unified Permission Service for Roles and Per-Item Sharing
System DesignModel answerLast updated
Design a multi-tenant authorization service that combines scoped role assignments with per-resource grants, covering the data model, evaluation rules, APIs, caching, auditing, and tricky edge cases.
You are building the permission layer for a multi-tenant collaboration product. Content is organized as a tree of resources, for example site → space → page and site → project → issue. The layer must answer one question quickly and safely: may this user perform this action on this resource?
It must support two ways of granting access at the same time:
- Role-based access: a user or group is assigned a named role (Admin, Editor, Viewer) that bundles a set of permissions, optionally limited to one part of the tree (for example, "Editor on Space X").
- Resource-level grants: a specific resource can give permissions straight to a user or group (for example, one page shared with one person).
Requirements
- Actions:
read,write,delete,share,admin. - Works across several resource types (spaces, pages, projects, issues).
- Principals are users and groups.
- Provide APIs to grant access, revoke access, and check access.
- Cover policy evaluation, the data model, caching, and audit logging.
What to deliver
- Core entities and a storage schema.
- The evaluation algorithm that turns grants into an allow/deny decision.
- A REST API surface.
- How you handle edge cases: whether deny beats allow, how inheritance down the tree works (and how it can be cut), and what happens when group membership changes.
State your assumptions about scale and latency up front and justify your design choices against them.
Hints
Hint 1
Try to treat a role assignment and a direct share as the same thing: a set of actions attached to a node in the resource tree for some principal. That makes evaluation a single walk.
Hint 2
Decide the conflict rule early (default deny, does explicit deny win, can inheritance be stopped at a node) and make sure the lockout case for administrators is handled.
Hint 3
Checks vastly outnumber changes, so think about caching the user's expanded group set and the resource's ancestor chain, and how a revocation can be made visible quickly while a grant is allowed to lag.
Hint 4
Consider the stale-read problem: someone is removed from a page, then sensitive content is added, and a cached decision still lets the removed person read it. What version or token could prevent that?
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Assumptions and targets
- Scale: about 100M users, 10B resources, 1M groups (some with 100k+ members), strictly isolated tenants.
- Traffic: roughly 1M checks/sec versus about 5k grant/revoke writes/sec. This is a read-dominated system.
- Latency: p99 under 10 ms per check, since checks run on every page render.
- Availability: 99.99%, and fail closed if the service cannot decide.
- Freshness: revocations take effect within seconds; grants may lag a little.
2. Model
Actions form an implication chain: admin ⊇ share ⊇ write ⊇ read, and delete is implied only by admin. Store action sets as bitmasks and expand them through this closure.
Key insight: a role binding is just an ACL entry whose actions come from a role definition. Both are "grants attached to a node of the resource tree", so one evaluator handles both.
principal(id, tenant_id, type, status)
group_member(group_id, member_id, member_type) -- nesting allowed
resource(id, tenant_id, type, parent_id, inherit, owner_id, version)
role(id, tenant_id, name, actions, applicable_types)
role_binding(id, tenant_id, principal_id, role_id, scope_resource_id)
acl_entry(id, tenant_id, resource_id, principal_id, actions,
effect, created_by, created_at)
audit_event(id, tenant_id, ts, actor_id, op, target, before, after, request_id)
Shard by tenant_id; index grants on (resource_id, principal_id).
3. Evaluation
Policy: default deny; an explicit deny overrides any allow; deny applies to its node and all descendants. Tenant site-admins bypass denies so nobody can lock the tenant out.
def check(tenant, user, action, resource_id):
if is_site_admin(tenant, user):
return ALLOW
principals = {user} | expanded_groups(tenant, user)
chain = ancestors(resource_id, stop_at_inherit_break=True)
allowed = False
for node in chain:
for g in grants_at(node, principals): # ACLs + role bindings
if not covers(g.actions, action):
continue
if g.effect == DENY:
return DENY
allowed = True
return ALLOW if allowed else DENY
- Inheritance: walking up the chain lets a space-level Editor edit its pages.
- Broken inheritance: a node with
inherit=falseends the walk, so a restricted page ignores space-level grants (site-admin bypass still applies). - Why deny-overrides: it is easy to reason about and audit. "Most specific wins" is the alternative, but it makes outcomes harder to predict.
- Cost: O(depth x |principals|) lookups; depth is 3-5, and with pre-expanded groups plus batched fetches this is a couple of cache reads.
Two companion operations:
listAccessible(user, type): never runcheckover billions of rows. Index each resource's effective allowed principals in the search layer, filter by the user's principal set, then post-verify the top results withcheck.whoCanAccess(resource): walk up the chain, union the grants, and expand groups lazily. This powers "shared with" screens.
4. REST API
POST /v1/authz/check {principal, action, resource} -> {decision, reason, consistency_token}
POST /v1/authz/check:batch [{...}] -> [{...}]
GET /v1/resources/{id}/permissions
POST /v1/resources/{id}/grants {principal, actions|role, effect}
DELETE /v1/resources/{id}/grants/{grant_id}
POST /v1/roles PUT /v1/roles/{id}
POST /v1/groups/{id}/members DELETE /v1/groups/{id}/members/{uid}
GET /v1/principals/{id}/accessible?type=page&cursor=...
GET /v1/audit?resource=&actor=&from=
- Granting and revoking are themselves authorized: the caller needs
shareoradminon the resource, and can only grant actions they hold (prevents privilege escalation). - Batch checks matter because a single page render needs many decisions.
- Return a
reason("allowed via Editor on space 7") to make support and debugging easy.
5. Architecture
- Stateless authz service, scaled horizontally, usable as a sidecar or library with an in-process cache.
- Policy store: sharded Postgres as the source of truth; resource moves and permission changes are transactional.
- Change stream (CDC to Kafka) feeding cache invalidation, the audit pipeline, and ACL fields in the search index.
- Caches (local LRU in front of Redis): user to transitive groups, resource to ancestor chain, node to grants, and optionally final decisions keyed by
(user, action, resource, policy_version). - Audit store: append-only and immutable (for example ClickHouse or object storage with a query engine), with retention per compliance rules. Log all policy changes, and optionally sample decisions on sensitive resources.
6. Deep dives and edge cases
Stale reads ("new enemy" problem). If Alice removes Bob from a page and then adds secret content, Bob must not read it through a stale cache. Every write returns a consistency token (a version). Content writes record the token, and checks on that content must be evaluated at or above it, falling back to the database if the cache is behind. Revocations publish invalidations synchronously before acknowledging; grants can propagate asynchronously.
Group membership changes. Large nested groups are costly to expand. Either keep a flattened user -> groups closure updated on membership events (more expensive writes, O(1) reads) or expand at check time with memoization. Prefer flattening for read-heavy traffic, cap nesting depth, and reject cycles on insert with a DFS. Removing a member must invalidate that user's cached closure immediately.
Moves and deletions. Re-parenting a resource changes its inherited grants, so bump versions on the whole subtree (or key caches by ancestor-chain version) and invalidate. Deleting a principal should tombstone its grants, with background cleanup.
Hot groups. A group with 100k+ members is cheap to check from the user's side (is the group in my closure?) but expensive to update. Batch and coalesce membership events.
7. Trade-offs and common mistakes
- Caching final decisions is fastest but hardest to invalidate; caching the building blocks (groups, ancestors, grants) is safer.
- Failing open on outages is a security bug; fail closed and degrade gracefully with stale-but-safe cached allows only for low-risk reads if the business accepts it.
- Mistakes to avoid: separate code paths for roles and ACLs, forgetting that revocation needs tighter freshness than grants, no escalation check on grant APIs, scanning all resources for "what can I see," and having no admin bypass or break-glass path.