Design Policy Management Service

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-10-07

Problem Statement

Designing a Policy Management Service that manages policies (rules/configurations) applied to users, applications, or systems. Policies can control access, limits, or configurations (e.g., "Max login attempts = 5", "User must reset password every 90 days", "API rate limit = 100 requests/minute").


This service should provide a structured way to create, store, evaluate, and update policies dynamically.


Functional Requirements


1.Policy Creation & Storage
•Define a policy with metadata (name, description, scope, type).
•Policies can be attribute-based (key-value pairs), role-based, or rule-based (e.g., expressions like if role=admin then max_limit=1000).
•Support versioning of policies.
2.Policy Evaluation
•Given an input context (e.g., user, request, resource), evaluate applicable policies.
•Support rule execution (e.g., "if country=IN then allow else deny").
3.Policy Assignment
•Attach policies to users, groups, roles, or resources.
•Multiple policies may apply; need conflict resolution (priority, override rules).
4.Policy Updates
•Policies should be updatable at runtime without restarting the service.
•Old versions must be retained for auditing.
5.Audit & Logging
•Track policy evaluations and outcomes.
•Provide history of changes (who created/updated policy, when).


Non-Functional Requirements


•Scalability: Should handle thousands of policy checks per second.
•Extensibility: Support adding new policy types (e.g., ABAC, RBAC, time-based).
•Consistency: Ensure latest policies are used while keeping version history.
•Thread-Safety: Concurrent evaluations should be safe.
•Low Latency: Policy evaluation must be fast (<50 ms).


Design Constraints & Edge Cases


•Conflict when two policies contradict (e.g., one allows, one denies). → Need resolution strategy.
•Circular dependency in policies (policy A references policy B).
•Evaluating policies with missing attributes in context.
•Rollback in case a new policy version causes failures.


Design Hints


•Patterns:
•Strategy Pattern → Different evaluation strategies (RBAC, ABAC, rule-based).
•Factory Pattern → To instantiate the right type of policy evaluator.
•Observer Pattern → Notify services when a policy changes.
•Composite Pattern → For policies composed of sub-policies (AND/OR logic).
•Key Classes / Interfaces:
•Policy → Base class (id, version, type, rules).
•PolicyEvaluator (Strategy interface) → evaluate(Policy, Context): Decision.
•PolicyRepository → Stores policies with versioning.
•PolicyManager → Handles creation, updates, conflict resolution.
•Context → Input data for evaluation (user, request, resource).
•Decision → Output (ALLOW/DENY + reason).


Example Flow


1.Admin creates a policy:
1 2{ 3 "id": "P1", 4 "type": "ABAC", 5 "rule": "if user.role == 'admin' then allow else deny", 6 "version": 1 7}
1.Policy is stored in PolicyRepository.
2.Request comes in for user with role = "admin".
3.PolicyManager retrieves applicable policies and sends them to PolicyEvaluator.
4.Evaluator returns ALLOW decision.
5.Audit log is stored: "Policy P1 (v1) applied → ALLOW".


Evaluation Areas


•Designing extensible policy types and evaluators.
•Handling versioning and conflict resolution.
•Ensuring thread-safe evaluation in concurrent scenarios.
•Modeling clean abstractions for policies, evaluators, and repositories.


👉 This LLD tests clean abstractions, extensibility, and concurrency handling while simulating a real-world service (like AWS IAM, OPA, or LinkedIn access policies).

Solution

Comments