Bird
Raised Fist0
LLDsystem_design~10 mins

Clean Architecture layers in LLD - Scalability & System Analysis

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Scalability Analysis - Clean Architecture layers
Growth Table: Clean Architecture Layers Scaling
Users / RequestsWhat Changes in Layers
100 usersSimple layer interactions; single instance; minimal caching; direct DB calls from infrastructure layer
10,000 usersIntroduce caching in infrastructure; separate read/write layers; add load balancer; start using async processing in use cases
1,000,000 usersHorizontal scaling of interface and infrastructure layers; database sharding; CQRS pattern for read/write separation; event-driven communication between layers
100,000,000 usersMicroservices splitting by bounded contexts; global distributed data stores; advanced caching/CDN; eventual consistency; multi-region deployment
First Bottleneck

At small scale, the infrastructure layer's database access becomes the bottleneck due to synchronous calls and limited DB capacity.

As users grow, the interface layer's servers hit CPU and memory limits handling requests and orchestrating layers.

At large scale, network bandwidth and data consistency across layers become critical bottlenecks.

Scaling Solutions
  • Database Layer: Add read replicas, use connection pooling, implement caching (Redis/Memcached).
  • Application Layer: Horizontal scaling with load balancers; use asynchronous messaging between layers.
  • Interface Layer: Use CDNs for static content; implement API gateways for routing and throttling.
  • Architecture: Apply CQRS and event-driven patterns to decouple layers and improve scalability.
  • Data: Shard databases by user or feature; use eventual consistency where possible.
Back-of-Envelope Cost Analysis
  • At 10,000 users, expect ~5,000 QPS; a single DB instance can handle this with caching.
  • At 1,000,000 users, ~500,000 QPS may require 50+ DB replicas and sharding.
  • Network bandwidth: 1 Gbps supports ~125 MB/s; large user base needs multi-gigabit or distributed networks.
  • Storage grows with data retention; consider tiered storage and archiving for old data.
Interview Tip

Start by explaining the Clean Architecture layers and their responsibilities.

Discuss how each layer can become a bottleneck as users grow.

Propose scaling solutions layer by layer, focusing on database, application, and interface.

Use real numbers to justify your choices and show understanding of trade-offs.

Self Check Question

Your database handles 1000 QPS. Traffic grows 10x. What do you do first?

Answer: Add read replicas and implement caching to reduce direct DB load before scaling application servers.

Key Result
Clean Architecture scales by decoupling layers, allowing independent scaling of database, application, and interface components. The database is the first bottleneck, fixed by caching and replication, followed by application server scaling and advanced patterns like CQRS and event-driven design.

Practice

(1/5)
1. Which layer in Clean Architecture contains the core business rules and logic?
easy
A. UI layer
B. Entities layer
C. Database layer
D. Frameworks layer

Solution

  1. Step 1: Understand the role of each layer

    The Entities layer holds the core business rules and logic, independent of external concerns.
  2. Step 2: Identify the correct layer for business logic

    UI, Database, and Frameworks layers handle external interactions, not core logic.
  3. Final Answer:

    Entities layer -> Option B
  4. Quick Check:

    Business logic = Entities layer [OK]
Hint: Core logic always lives in the Entities layer [OK]
Common Mistakes:
  • Confusing UI layer with business logic
  • Thinking database layer contains core rules
  • Mixing frameworks with core logic
2. In Clean Architecture, which layer is responsible for adapting data from the database to the business logic?
easy
A. Entities layer
B. Use Cases (Interactor) layer
C. UI layer
D. Interface Adapters layer

Solution

  1. Step 1: Identify the role of Interface Adapters

    Interface Adapters convert data from external sources like databases into a form usable by inner layers.
  2. Step 2: Confirm other layers' roles

    Entities hold business rules, Use Cases orchestrate logic, UI handles presentation, so adapting data fits Interface Adapters.
  3. Final Answer:

    Interface Adapters layer -> Option D
  4. Quick Check:

    Data adaptation = Interface Adapters [OK]
Hint: Data conversion happens in Interface Adapters [OK]
Common Mistakes:
  • Choosing Entities layer for data adaptation
  • Confusing Use Cases with data conversion
  • Selecting UI layer for database data handling
3. Given the following flow in Clean Architecture: UI calls Use Cases, which then call Entities. What is the correct order of dependency direction?
medium
A. UI -> Entities -> Use Cases
B. Entities -> Use Cases -> UI
C. UI -> Use Cases -> Entities
D. Use Cases -> UI -> Entities

Solution

  1. Step 1: Understand dependency rule in Clean Architecture

    Dependencies always point inward, from outer layers to inner layers.
  2. Step 2: Apply to given flow

    UI depends on Use Cases, which depend on Entities, so direction is UI -> Use Cases -> Entities.
  3. Final Answer:

    UI -> Use Cases -> Entities -> Option C
  4. Quick Check:

    Dependency direction = UI to Entities [OK]
Hint: Dependencies always point inward [OK]
Common Mistakes:
  • Reversing dependency direction
  • Confusing which layer calls which
  • Assuming Entities depend on UI
4. A developer placed database access code directly inside the Entities layer. What is the main problem with this design?
medium
A. Entities layer should not depend on external frameworks or databases
B. Entities layer must handle UI rendering
C. Database code belongs only in the UI layer
D. Entities layer should only contain database schemas

Solution

  1. Step 1: Recall Clean Architecture dependency rules

    Inner layers like Entities must be independent of external concerns like databases.
  2. Step 2: Identify why database code in Entities is wrong

    It creates tight coupling and breaks separation of concerns.
  3. Final Answer:

    Entities layer should not depend on external frameworks or databases -> Option A
  4. Quick Check:

    Entities layer independence = true [OK]
Hint: Keep Entities free from external dependencies [OK]
Common Mistakes:
  • Thinking Entities handle UI
  • Placing database code in UI layer
  • Confusing database schemas with business logic
5. You need to design a system where the UI can be changed without affecting business rules, and the database can be swapped easily. Which Clean Architecture principle helps achieve this?
hard
A. Dependency Rule: Inner layers do not depend on outer layers
B. UI layer directly accesses Entities for faster updates
C. Database layer contains business logic for flexibility
D. Use Cases layer handles UI rendering and database access

Solution

  1. Step 1: Identify the principle for independence

    The Dependency Rule states inner layers (business rules) do not depend on outer layers (UI, database).
  2. Step 2: Explain how this helps system flexibility

    This separation allows changing UI or database without impacting core business logic.
  3. Final Answer:

    Dependency Rule: Inner layers do not depend on outer layers -> Option A
  4. Quick Check:

    Dependency Rule ensures flexibility [OK]
Hint: Inner layers must be independent for easy changes [OK]
Common Mistakes:
  • Allowing UI to access Entities directly
  • Putting business logic in database layer
  • Mixing UI rendering with Use Cases