Bird
Raised Fist0
LLDsystem_design~10 mins

When to use which structural pattern in LLD - Scalability & System Analysis

Choose your learning style10 modes available
Scalability Analysis - When to use which structural pattern
Growth Table: Structural Patterns Usage at Different Scales
Users / ScaleSmall (100 users)Medium (10K users)Large (1M users)Very Large (100M users)
Pattern ComplexitySimple patterns like Facade or Adapter for easy integrationComposite and Decorator to manage growing component complexityProxy and Flyweight to optimize resource use and access controlCombination of multiple patterns with distributed systems and microservices
System ComponentsFew components, direct connectionsMultiple components, need for flexible compositionHigh number of components, need for resource sharing and controlThousands of components, need for scalability and fault tolerance
Performance NeedsLow latency, simple callsModerate latency, some cachingHigh performance, resource optimization criticalExtreme performance, distributed caching, and load balancing
MaintenanceEasy to maintain, minimal abstractionModerate abstraction for flexibilityHigh abstraction to manage complexityVery high abstraction, automated deployment and monitoring
First Bottleneck

At small scale, the bottleneck is usually the lack of flexibility in component integration, causing slow development or rigid code.

At medium scale, the bottleneck shifts to managing component complexity and communication overhead.

At large scale, resource usage and access control become bottlenecks, especially if patterns do not optimize sharing or proxying.

At very large scale, the bottleneck is system scalability and fault tolerance, requiring distributed pattern implementations and orchestration.

Scaling Solutions
  • Small Scale: Use simple structural patterns like Facade to simplify interfaces and Adapter to integrate external systems easily.
  • Medium Scale: Employ Composite to build tree structures of components and Decorator to add responsibilities dynamically without changing code.
  • Large Scale: Use Proxy to control access and Flyweight to share common data efficiently, reducing memory footprint.
  • Very Large Scale: Combine multiple patterns with microservices architecture, distributed caching, and orchestration tools to handle complexity and scale.
Back-of-Envelope Cost Analysis

Requests per second (RPS):

  • Small: ~100-500 RPS, handled by single server with simple patterns.
  • Medium: ~10K RPS, requires multiple servers and patterns supporting modularity.
  • Large: ~100K RPS, needs resource optimization patterns and horizontal scaling.
  • Very Large: ~1M+ RPS, demands distributed systems and complex pattern combinations.

Storage and Memory:

  • Small: Minimal, simple object structures.
  • Medium: Moderate, composite objects and decorators increase memory.
  • Large: High, flyweight reduces memory but proxy adds overhead.
  • Very Large: Very high, requires distributed caches and memory optimization.

Bandwidth:

  • Small: Low, direct calls.
  • Medium: Moderate, more component communication.
  • Large: High, proxy and flyweight reduce bandwidth but increase complexity.
  • Very Large: Very high, requires load balancing and CDN.
Interview Tip

Start by explaining the system scale and complexity. Then describe which structural pattern fits best at each scale and why. Discuss bottlenecks and how patterns help solve them. Finally, mention trade-offs and how you would evolve the design as the system grows.

Self Check

Your system uses a Composite pattern to manage UI components. Traffic grows 10x, and response time increases. What do you do first?

Answer: Analyze if the Composite structure causes deep recursion or heavy object creation. Introduce caching or flatten the component tree. Consider adding Flyweight to share common parts and reduce memory and processing overhead.

Key Result
Structural patterns should be chosen based on system scale and complexity: simple patterns for small systems, composite and decorator for medium scale, proxy and flyweight for large scale, and combined distributed patterns for very large scale systems.

Practice

(1/5)
1. Which structural pattern is best suited for representing part-whole hierarchies like folders and files in a system?
easy
A. Facade pattern
B. Composite pattern
C. Proxy pattern
D. Decorator pattern

Solution

  1. Step 1: Understand the problem context

    Part-whole hierarchies like folders and files form tree structures where each node can be a composite or a leaf.
  2. Step 2: Match the pattern to the structure

    The Composite pattern is designed to treat individual objects and compositions uniformly, perfect for tree-like structures.
  3. Final Answer:

    Composite pattern -> Option B
  4. Quick Check:

    Tree structures = Composite pattern [OK]
Hint: Tree or hierarchy? Use Composite pattern [OK]
Common Mistakes:
  • Confusing Decorator with Composite
  • Using Proxy for hierarchy management
  • Choosing Facade for internal structure
2. Which pattern should you use if you want to add responsibilities to objects dynamically without changing their class?
easy
A. Proxy pattern
B. Facade pattern
C. Composite pattern
D. Decorator pattern

Solution

  1. Step 1: Identify the need to add features dynamically

    When you want to add new behavior to objects at runtime without altering their class, you need a flexible pattern.
  2. Step 2: Select the pattern that wraps objects

    The Decorator pattern wraps objects to add responsibilities dynamically, without changing the original class.
  3. Final Answer:

    Decorator pattern -> Option D
  4. Quick Check:

    Add features dynamically = Decorator [OK]
Hint: Add features on the fly? Use Decorator [OK]
Common Mistakes:
  • Choosing Proxy instead of Decorator
  • Using Facade to add features
  • Confusing Composite with Decorator
3. Consider a system where you want to control access to a resource and add lazy loading. Which structural pattern fits best?
class ResourceProxy {
  constructor() {
    this.resource = null;
  }
  access() {
    if (!this.resource) {
      this.resource = new RealResource();
    }
    return this.resource;
  }
}
medium
A. Proxy pattern
B. Decorator pattern
C. Composite pattern
D. Facade pattern

Solution

  1. Step 1: Analyze the code behavior

    The code delays creating the real resource until it is accessed, controlling access and adding lazy loading.
  2. Step 2: Identify the matching pattern

    This behavior matches the Proxy pattern, which controls access to another object and can add lazy initialization.
  3. Final Answer:

    Proxy pattern -> Option A
  4. Quick Check:

    Control access + lazy load = Proxy [OK]
Hint: Control access or lazy load? Use Proxy [OK]
Common Mistakes:
  • Confusing Proxy with Decorator
  • Using Facade for access control
  • Choosing Composite for resource control
4. You have a complex subsystem with many classes. You want to provide a simple interface to clients. Which pattern should you use? A developer wrote this code but clients still see complex calls:
class ComplexSystem {
  operationA() {}
  operationB() {}
  operationC() {}
}

class SimpleInterface {
  constructor() {
    this.system = new ComplexSystem();
  }
  simpleOperation() {
    this.system.operationA();
    this.system.operationB();
    this.system.operationC();
  }
}
What is the likely mistake?
medium
A. They did not hide the ComplexSystem instance properly
B. They used Proxy instead of Facade
C. They used Composite instead of Facade
D. They forgot to add Decorator to SimpleInterface

Solution

  1. Step 1: Understand Facade pattern purpose

    Facade provides a simple interface hiding complex subsystem details from clients.
  2. Step 2: Identify the mistake in code usage

    If clients still see complex calls, likely the ComplexSystem instance is exposed or clients bypass the Facade.
  3. Final Answer:

    They did not hide the ComplexSystem instance properly -> Option A
  4. Quick Check:

    Facade hides complexity = hide subsystem [OK]
Hint: Facade hides complexity; don't expose subsystem [OK]
Common Mistakes:
  • Confusing Proxy with Facade
  • Exposing subsystem objects to clients
  • Using Decorator to simplify interface
5. You are designing a graphic editor. You want to represent shapes that can be simple (circle, square) or groups of shapes. You also want to add features like border color dynamically and control access to some shapes. Which combination of structural patterns should you use?
hard
A. Facade for groups, Proxy for features, Composite for access control
B. Decorator for groups, Composite for features, Facade for access control
C. Composite for groups, Decorator for features, Proxy for access control
D. Proxy for groups, Facade for features, Decorator for access control

Solution

  1. Step 1: Match patterns to requirements

    Groups of shapes form tree structures best handled by Composite. Adding features dynamically fits Decorator. Controlling access fits Proxy.
  2. Step 2: Combine patterns correctly

    Use Composite for shape groups, Decorator to add border color dynamically, and Proxy to control access to shapes.
  3. Final Answer:

    Composite for groups, Decorator for features, Proxy for access control -> Option C
  4. Quick Check:

    Groups=Composite, Features=Decorator, Access=Proxy [OK]
Hint: Groups=Composite, Features=Decorator, Access=Proxy [OK]
Common Mistakes:
  • Mixing Facade with group representation
  • Using Proxy for adding features
  • Confusing Decorator with access control
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard