0
0
LLDsystem_design~10 mins

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

Choose your learning style9 modes available
Scalability Analysis - When to use which creational pattern
Growth Table: Usage of Creational Patterns at Different Scales
ScaleObject Creation NeedsPattern UsageImpact on System
100 usersFew objects, simple creationSimple Factory or direct instantiationMinimal overhead, easy to maintain
10K usersMore objects, need for flexibilityFactory Method or Builder for complex objectsImproved code reuse, easier object management
1M usersHigh object creation, performance criticalSingleton for shared resources, Prototype for cloningReduces resource usage, faster object creation
100M usersMassive scale, distributed systemsAbstract Factory with dependency injection, Singleton with distributed locksEnsures consistency, scalable object management
First Bottleneck

At small scale, object creation overhead is negligible. As users grow, the bottleneck becomes inefficient object creation causing CPU and memory strain. For example, creating many complex objects repeatedly slows down the system. Without proper patterns, code becomes hard to maintain and extend.

Scaling Solutions
  • Simple Factory: Use for small scale to centralize object creation.
  • Factory Method: Use when subclasses decide object types, improving flexibility.
  • Builder: Use for complex objects with many parts, improving readability and maintainability.
  • Singleton: Use for shared resources to avoid multiple instances and save memory.
  • Prototype: Use to clone objects efficiently instead of creating from scratch.
  • Abstract Factory: Use in large systems to create families of related objects, supporting scalability and consistency.
  • Combine with Dependency Injection: To manage object lifecycles and dependencies cleanly at scale.
Back-of-Envelope Cost Analysis
  • At 10K users, creating 10 objects per user per second = 100K objects/sec.
  • Without patterns, each creation costs CPU cycles and memory allocation.
  • Using Prototype can reduce creation time by cloning, saving CPU.
  • Singleton reduces memory by sharing instances.
  • At 1M users, object creation can reach millions per second, stressing CPU and memory.
  • Proper patterns reduce overhead and improve throughput.
Interview Tip

Start by explaining the problem of object creation at scale. Describe each creational pattern briefly and when it fits best. Use examples like shared resources (Singleton), complex object building (Builder), or flexible object families (Abstract Factory). Discuss bottlenecks and how patterns solve them. Show understanding of trade-offs and scalability.

Self Check

Your system creates 1000 objects per second. Traffic grows 10x to 10,000 objects per second. What do you do first?

Answer: Identify if object creation is a bottleneck. If yes, apply Prototype to clone objects or Singleton to share instances where possible. Consider Builder for complex objects to optimize creation steps. This reduces CPU and memory usage, improving scalability.

Key Result
Creational patterns help manage object creation efficiently as system scales, preventing CPU and memory bottlenecks by reusing, cloning, or centralizing object creation.