0
0
LLDsystem_design~10 mins

Factory Method pattern in LLD - Scalability & System Analysis

Choose your learning style9 modes available
Scalability Analysis - Factory Method pattern
Growth Table: Factory Method Pattern
UsersObjects CreatedComplexityImpact on System
100 usersLow (hundreds)SimpleFactory handles creation easily, no performance issues
10,000 usersModerate (thousands)ManageableFactory method may need optimization, caching objects helps
1,000,000 usersHigh (millions)ComplexObject creation cost adds up, factory needs pooling or reuse
100,000,000 usersVery High (hundreds of millions)Very ComplexFactory method alone insufficient; must combine with caching, pooling, and distributed creation
First Bottleneck

The first bottleneck is the object creation overhead in the factory method. As user count grows, creating many objects repeatedly consumes CPU and memory, slowing down the system.

Scaling Solutions
  • Object Pooling: Reuse objects instead of creating new ones each time to reduce CPU and memory load.
  • Caching: Cache created objects when possible to avoid repeated creation.
  • Lazy Initialization: Create objects only when needed, not upfront.
  • Horizontal Scaling: Distribute factory instances across multiple servers to share load.
  • Asynchronous Creation: Create objects in background threads to avoid blocking main flow.
Back-of-Envelope Cost Analysis

Assuming each user triggers 1 object creation per second:

  • At 1,000 users: 1,000 creations/sec, manageable by a single server.
  • At 1,000,000 users: 1 million creations/sec, requires multiple servers and pooling.
  • Memory per object ~1KB, so 1 million objects = ~1GB memory usage if all live simultaneously.
  • CPU usage grows linearly with creation rate; pooling reduces CPU by reusing objects.
Interview Tip

Start by explaining what the Factory Method pattern does: it creates objects without specifying exact classes. Then discuss how object creation cost grows with users. Identify bottlenecks like CPU and memory. Suggest solutions like pooling and caching. Finally, mention horizontal scaling and asynchronous creation to handle very high loads.

Self Check

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

Answer: Since the database is the bottleneck, first add read replicas or caching to reduce load. For Factory Method pattern, similarly, first optimize object creation by adding pooling or caching before scaling horizontally.

Key Result
Factory Method pattern scales well at low user counts but faces CPU and memory bottlenecks at high loads due to object creation overhead. Solutions include object pooling, caching, and horizontal scaling.