0
0
LLDsystem_design~25 mins

Clean Architecture layers in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Clean Architecture Layered System
Design the layered architecture and explain responsibilities of each layer. Do not cover specific UI or database implementations.
Functional Requirements
FR1: Separate the system into clear layers to improve maintainability and testability
FR2: Ensure dependencies only point inward, from outer layers to inner layers
FR3: Allow easy replacement of UI, database, or external services without affecting core logic
FR4: Support independent testing of business rules without UI or database dependencies
Non-Functional Requirements
NFR1: System must handle up to 1000 concurrent users
NFR2: API response latency p99 under 300ms
NFR3: Availability target 99.9% uptime
NFR4: Code changes in one layer should not require changes in other layers
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Entities (business rules)
Use Cases (application logic)
Interface Adapters (controllers, presenters, gateways)
Frameworks and Drivers (UI, database, external services)
Design Patterns
Dependency Rule (dependencies point inward)
Ports and Adapters (Hexagonal Architecture)
Inversion of Control
Separation of Concerns
Reference Architecture
  +-----------------------+
  |   Frameworks & Drivers |
  |  (UI, DB, External)   |
  +-----------+-----------+
              |
  +-----------v-----------+
  |  Interface Adapters   |
  | (Controllers, Presenters, Gateways) |
  +-----------+-----------+
              |
  +-----------v-----------+
  |      Use Cases        |
  |  (Application Logic)  |
  +-----------+-----------+
              |
  +-----------v-----------+
  |       Entities        |
  |   (Business Rules)    |
  +-----------------------+
Components
Entities
Plain classes or data structures
Represent core business rules and data. Independent of UI or database.
Use Cases
Application service classes
Implement application-specific business rules and orchestrate entities.
Interface Adapters
Controllers, Presenters, Gateways
Convert data between use cases and external formats (UI, DB).
Frameworks and Drivers
Web frameworks, databases, external APIs
Provide UI, database access, and external service integration.
Request Flow
1. User interacts with UI in Frameworks and Drivers layer.
2. UI sends request to Controller in Interface Adapters layer.
3. Controller converts request to use case input and calls Use Cases layer.
4. Use Cases execute business logic using Entities layer.
5. Entities enforce business rules and return results to Use Cases.
6. Use Cases return output to Presenter in Interface Adapters.
7. Presenter formats data for UI and sends response back through Frameworks and Drivers.
8. Database gateways in Interface Adapters access database in Frameworks and Drivers as needed.
Database Schema
Not applicable - Clean Architecture focuses on layering and separation, not specific database design.
Scaling Discussion
Bottlenecks
Tight coupling between layers can reduce flexibility and increase bugs.
Slow database or external service calls in Frameworks and Drivers can increase latency.
Complex use cases can become hard to maintain if not well separated.
Testing can be difficult if layers are not properly isolated.
Solutions
Enforce strict dependency rules and use interfaces to decouple layers.
Use caching and asynchronous calls to reduce latency from external services.
Keep use cases small and focused; split complex logic into smaller use cases.
Write unit tests for Entities and Use Cases; use mocks for Interface Adapters.
Interview Tips
Time: Spend 10 minutes explaining the layers and their responsibilities, 10 minutes on data flow and dependency rules, 10 minutes on scaling and testing strategies, and 15 minutes answering questions.
Explain the Dependency Rule and why dependencies point inward.
Describe responsibilities of each layer clearly.
Show how this architecture improves maintainability and testability.
Discuss how to isolate business logic from UI and database.
Mention how to handle scaling and performance bottlenecks.