Access Control System With Caching and Revocation Guarantees
System DesignModel answerLast updated
Design an RBAC authorization service handling fast permission checks, role inheritance, scoped assignments, and bounded revocation delay.
You're designing an authorization service that many applications will query to enforce access control. The service manages roles, permissions, and assignments, then answers whether a given principal can perform an action on a resource.
Your design should address:
Core Model
- Permissions represent actions on resource types (e.g., "documents:read", "projects:delete")
- Roles bundle permissions and can inherit from other roles
- Principals (users, service accounts) receive role assignments
- Assignments may be scoped to tenants, projects, or specific resources
- The system denies access by default
Operations
- Administrative: create/modify roles, assign/revoke roles, manage role hierarchies
- Runtime:
checkPermission(principal, action, resource)returns allow/deny
Requirements
- Permission checks must be fast (single-digit milliseconds) since they're on every protected request
- Revocations must take effect within a bounded time window
- The system should remain available even when the central authority is unreachable
- All administrative changes and access denials need audit trails
Discussion Points
- How do you model the entities and relationships? What data structures support efficient evaluation?
- How do you achieve low-latency checks while ensuring revocations propagate within your time bound?
- What happens when the authorization service is down or slow?
- How do you prevent administrator mistakes from causing widespread outages?
- What observability and debugging tools does the system need?
Consider role inheritance cycles, the cost of scope matching, cache invalidation strategies, and the tension between availability and security.
Hints
Hint 1
Model inheritance as a directed acyclic graph and pre-compute the transitive closure of permissions when roles change, not during every check
Hint 2
Cache compiled permission sets per principal at the application layer with TTL-based expiration; your TTL becomes your revocation bound
Hint 3
Design for explicit fail-open or fail-closed behavior per resource type, and log every failure mode separately for alerting
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Model and Core Evaluation
Entities:
Principal: identified by ID (user, service account, or group)Role: named collection with a set ofPermissionobjects and optional parent rolesPermission: tuple of(action, resourceType), e.g.,("read", "document")RoleAssignment: links a principal to a role with an optional scope (global, tenant ID, project ID, or resource ID)
Data Structures:
Store roles and assignments in a relational database with indexes on principal ID and scope. Materialize the transitive closure of permissions when a role is created or modified:
class Role:
id: str
name: str
direct_permissions: Set[Permission]
parent_roles: List[str]
# Computed on write:
effective_permissions: Set[Permission]
When a role changes or its parents change, recompute effective_permissions by walking the parent graph. Detect cycles during role creation and reject them. This makes checks fast: you never traverse the hierarchy at read time.
Check Evaluation:
def check_permission(principal_id, action, resource):
# 1. Identify applicable assignments by matching scope
assignments = get_assignments(principal_id)
applicable = [a for a in assignments if scope_matches(a.scope, resource)]
# 2. Collect effective permissions from all applicable roles
permissions = set()
for assignment in applicable:
role = get_role(assignment.role_id)
permissions.update(role.effective_permissions)
# 3. Check if requested permission is present
requested = Permission(action, resource.type)
return requested in permissions
Scope Matching: A global assignment applies everywhere. A tenant-scoped assignment applies to all resources in that tenant. More specific scopes (project, resource) require exact matches or prefix matching depending on your hierarchy. Evaluate from most specific to least specific.
Cost: With pre-computed permissions and indexed assignments, a check involves one database lookup (or cache hit) for assignments and one set membership test per role. Target: O(number of assignments × 1) with typical assignment counts under 10 per principal.
Fast Checks With Bounded Revocation
Caching Strategy:
Applications cache a compiled view of each principal's permissions:
class CachedPermissionSet:
principal_id: str
permissions: Set[Permission] # All permissions from all applicable roles
scope: Optional[Scope]
expires_at: timestamp
When an application receives a request, it checks its local cache first. On miss, it queries the authorization service, compiles the permission set, and caches it with a TTL (e.g., 30 seconds for high-security contexts, 5 minutes for less sensitive).
Propagation:
Administrative changes write to the central database and publish an invalidation event (via Pub/Sub, Kafka, or a similar bus). Applications subscribe to these events and evict matching cache entries immediately. However, some applications may be partitioned, delayed, or miss events.
Revocation Bound:
The TTL is your worst-case revocation delay. If an admin revokes a role at time T, an application that cached the permission set at T-1 will honor the old permissions until T + TTL. Choose TTL based on your risk tolerance:
- High-security operations (delete, financial transactions): 30 seconds or force a fresh check
- Standard operations: 2–5 minutes
- Read-only public data: 10–15 minutes
Document this bound clearly for administrators and enforce it contractually in the service SLA.
Change Log:
Maintain a change log table with (timestamp, change_type, affected_principal). Applications can poll this log on cache miss to detect whether their cached data might be stale, providing faster convergence than waiting for TTL expiration.
Failure Modes
Authorization Service Unavailable:
Decide per resource type:
- Fail-closed (deny): For sensitive operations (write, delete, admin actions). Log the denial with a "service unavailable" reason and alert.
- Fail-open (allow): For read-only operations on non-sensitive data where availability is critical. Log every allowed request during degraded mode for post-incident audit.
Store the policy (fail-open or fail-closed) as metadata on each resource type or action. Applications query this metadata once at startup and cache it indefinitely since it changes rarely.
Stale Cache Risk:
Mitigate by monitoring the age distribution of cache entries. Alert when entries approach TTL expiration, indicating the invalidation bus may be lagging. Provide a manual "force refresh" API for administrators to call after critical revocations.
Integrity and Safety
Cycle Prevention:
When creating or modifying a role's parent list, perform a depth-first search from the new parent to ensure it doesn't already inherit from the role being modified. Reject the change if a cycle is detected.
Safe Deletion:
Before deleting a role, check for active assignments. Either block deletion (requiring administrators to revoke assignments first) or cascade the deletion with a clear audit trail.
Tenant Isolation:
Scope all queries by tenant ID to prevent cross-tenant leakage. Use row-level security in the database and validate tenant boundaries in every API call.
Rollback:
Version every role and assignment change. Provide an admin UI to view history and revert to a previous version, which issues a new change event to propagate the rollback.
Observability
Metrics:
permission_check_latency_ms(p50, p99)cache_hit_ratedenied_requests_total(by action, resource type, and reason)revocation_propagation_lag_seconds(time from change commit to last application refresh)
Audit Logs:
- Every administrative change: who, what, when
- Every denied request: principal, action, resource, reason
- Every allowed request during degraded mode (service unavailable)
Debugging Tools:
- "Why was this request denied?" API: given a principal and resource, return the missing permission and which role would grant it
- "What can this principal do?" API: enumerate all permissions for debugging
- Change replay: given a timestamp, show what permissions a principal had at that moment
Trade-offs Made
- Pre-computed permissions vs. on-the-fly evaluation: Faster checks at the cost of slightly slower writes and storage for the transitive closure
- TTL-based caching vs. strong consistency: Sub-millisecond checks at the cost of a bounded revocation delay
- Fail-open for some resource types: Higher availability at the cost of potential unauthorized access during outages (mitigated by comprehensive logging)
- No explicit deny rules: Simpler model and easier to reason about, but less expressive than AWS IAM-style policies
This design handles millions of checks per second with low latency while guaranteeing revocations take effect within a known window, balancing security, performance, and operational simplicity.