| Users / Scale | 100 Users | 10K Users | 1M Users | 100M Users |
|---|---|---|---|---|
| Number of Product Families | Few (2-3) | Moderate (5-10) | Many (20-50) | Very Many (100+) |
| Factory Instances Created | Low (few factories) | Moderate (multiple factories) | High (many factories) | Very High (complex factory hierarchies) |
| System Complexity | Simple to moderate | Moderate | High | Very High |
| Performance Impact | Negligible | Low | Moderate (object creation overhead) | High (factory management overhead) |
| Memory Usage | Low | Moderate | High | Very High |
| Maintenance Effort | Low | Moderate | High | Very High |
Abstract Factory pattern in LLD - Scalability & System Analysis
The first bottleneck is the factory management and object creation overhead. As the number of product families and factories grows, the system spends more time creating and managing objects through abstract factories. This can slow down response times and increase memory usage, especially if factories create complex or many objects per request.
- Object Pooling: Reuse created objects instead of creating new ones each time to reduce creation overhead.
- Lazy Initialization: Create objects only when needed to save resources.
- Factory Caching: Cache factory instances to avoid repeated instantiation.
- Horizontal Scaling: Distribute factory usage across multiple servers to handle more requests concurrently.
- Modularization: Split factories into modules or microservices to isolate complexity and scale independently.
- Code Generation: Automate factory code creation to reduce maintenance effort at large scale.
- Assuming 1000 requests per second per server, each requiring 5 factory-created objects.
- At 10K users, ~10 servers needed to handle 10K requests/sec.
- Memory per object ~1KB, so 5KB per request, 50MB/sec memory churn at 10K users.
- Network bandwidth mostly unaffected by pattern, but horizontal scaling adds inter-service communication overhead.
- Maintenance cost grows with number of factories and product families due to complexity.
When discussing Abstract Factory scalability, start by explaining what the pattern does: it creates families of related objects without specifying their concrete classes. Then, discuss how as the number of product families grows, object creation overhead and factory management become bottlenecks. Finally, propose concrete solutions like object pooling, caching, and horizontal scaling. Always connect your points to real-world impacts like memory use and response time.
Your system uses Abstract Factory pattern and your database handles 1000 QPS. Traffic grows 10x. What do you do first?
Answer: First, optimize object creation by implementing object pooling or caching factory instances to reduce overhead. Then, scale horizontally by adding more servers to distribute load. This addresses the factory bottleneck before database scaling.
Practice
Abstract Factory pattern in system design?Solution
Step 1: Understand the role of Abstract Factory
The Abstract Factory pattern is designed to create related objects without exposing their concrete classes.Step 2: Compare options with the pattern's purpose
To create families of related objects without specifying their concrete classes correctly describes this purpose, while others describe different patterns or unrelated concepts.Final Answer:
To create families of related objects without specifying their concrete classes -> Option AQuick Check:
Abstract Factory = Create related objects [OK]
- Confusing Abstract Factory with Singleton pattern
- Thinking it creates only one object
- Mixing it up with Builder or Factory Method
Solution
Step 1: Identify correct interface syntax
Abstract Factory is usually declared as an interface with methods to create related products.Step 2: Analyze each option
interface AbstractFactory { createProductA(): ProductA; createProductB(): ProductB; } correctly declares an interface with creation methods. Options A, B, and C do not follow interface or abstract factory conventions.Final Answer:
interface AbstractFactory { createProductA(): ProductA; createProductB(): ProductB; } -> Option DQuick Check:
Abstract Factory interface = interface AbstractFactory { createProductA(): ProductA; createProductB(): ProductB; } [OK]
- Using concrete class instead of interface
- Returning product instances directly in constructor
- Confusing factory function with abstract factory interface
class ModernFactory {
createChair() { return 'Modern Chair'; }
createSofa() { return 'Modern Sofa'; }
}
class VictorianFactory {
createChair() { return 'Victorian Chair'; }
createSofa() { return 'Victorian Sofa'; }
}
function client(factory) {
console.log(factory.createChair());
console.log(factory.createSofa());
}
client(new VictorianFactory());Solution
Step 1: Identify which factory is used
The client function is called withnew VictorianFactory(), so VictorianFactory methods will be used.Step 2: Trace method calls and outputs
VictorianFactory'screateChair()returns 'Victorian Chair' andcreateSofa()returns 'Victorian Sofa'. These are printed.Final Answer:
Victorian Chair Victorian Sofa -> Option AQuick Check:
VictorianFactory output = Victorian Chair\nVictorian Sofa [OK]
- Assuming ModernFactory is used instead
- Confusing method names or return values
- Expecting errors due to missing interface
class Factory {
createProduct() {
return new Product();
}
}
class Product {}
const factory = new Factory();
const product = factory.createProduct();
console.log(product.name.toUpperCase());Solution
Step 1: Analyze the Product class and usage
The Product class is empty and does not define a 'name' property.Step 2: Understand the console.log statement
Accessingproduct.namereturnsundefined, and callingtoUpperCase()on it causes a TypeError runtime error.Final Answer:
Product class does not have a 'name' property, causing undefined access -> Option BQuick Check:
Accessing missing property = Product class does not have a 'name' property, causing undefined access [OK]
- Thinking Factory cannot instantiate Product
- Expecting interface is mandatory at runtime
- Assuming createProduct returns null or undefined
Solution
Step 1: Understand the need for consistency
Abstract Factory helps create families of related objects that match a theme or style.Step 2: Evaluate design options
Create separate factories for Light and Dark themes, each producing themed Buttons and Checkboxes creates separate factories for each theme, ensuring all components are consistent. Other options break consistency or delegate creation improperly.Final Answer:
Create separate factories for Light and Dark themes, each producing themed Buttons and Checkboxes -> Option CQuick Check:
Theme consistency = Separate factories [OK]
- Mixing themes in one factory
- Creating components directly in client code
- Partial factory implementations missing products
Ready to prove it?
Pass the test and this pattern moves unknown
