| Users/Requests | What Changes? |
|---|---|
| 100 requests/sec | Simple DI container in memory, no concurrency issues, fast object creation. |
| 10,000 requests/sec | Need thread-safe DI container, caching of created instances, reduce reflection overhead. |
| 1,000,000 requests/sec | Distribute DI container across multiple app servers, use ahead-of-time code generation, minimize runtime overhead. |
| 100,000,000 requests/sec | Use microservice architecture with local DI containers, service mesh for communication, aggressive caching, and load balancing. |
Dependency injection framework in LLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
The first bottleneck is the object creation and dependency resolution in the DI container. At low scale, this is fast and simple. As requests grow, the container's reflection or runtime analysis slows down, causing CPU overhead and latency.
- Code Generation: Generate dependency wiring code at compile time to avoid runtime reflection.
- Caching: Cache created instances (singletons) to avoid repeated creation.
- Thread Safety: Make DI container thread-safe for concurrent requests.
- Horizontal Scaling: Run multiple app servers each with its own DI container to distribute load.
- Microservices: Split large apps into smaller services each with simpler DI needs.
- At 10,000 requests/sec, if each DI resolution takes 1ms CPU, total CPU needed is 10 CPU cores (assuming 1 core ~1000ms/sec).
- Caching singletons reduces CPU by 70% as fewer objects are created.
- Memory usage grows with number of cached instances; plan for 100MB-500MB RAM per app server.
- Network bandwidth is minimal for DI itself but grows with app traffic.
Start by explaining what a DI framework does simply. Then discuss how it handles object creation and wiring. Identify the bottleneck as runtime overhead. Suggest caching and code generation. Finally, mention horizontal scaling and microservices for very large scale.
Your DI framework handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Implement caching of created instances and use code generation to reduce runtime overhead before scaling horizontally.
Practice
dependency injection framework?Solution
Step 1: Understand what dependency injection means
Dependency injection means giving the parts your code needs automatically instead of creating them inside the code.Step 2: Identify the role of the framework
A dependency injection framework helps by managing and providing these parts for you, making your code easier to change and test.Final Answer:
To automatically provide parts (dependencies) to your code -> Option BQuick Check:
Dependency injection = automatic parts supply [OK]
- Confusing dependency injection with data storage
- Thinking it speeds up code execution directly
- Believing it replaces manual coding completely
Solution
Step 1: Recall the registration syntax
In most dependency injection frameworks, you register a service by calling a method on the injector object and passing the service class.Step 2: Match the correct syntax
The correct syntax isinjector.register(ServiceClass), which tells the injector to manage that service.Final Answer:
injector.register(ServiceClass) -> Option AQuick Check:
Register service = injector.register() [OK]
- Calling register on the service class instead of injector
- Mixing method order or names
- Using non-existent methods like inject() on service
serviceA.getName() output?class ServiceA {
getName() { return 'Service A'; }
}
injector.register(ServiceA);
const serviceA = injector.get(ServiceA);
console.log(serviceA.getName());Solution
Step 1: Understand registration and retrieval
The code registersServiceAwith the injector, then asks the injector to give an instance ofServiceA.Step 2: Check the method call on the instance
The instance has a methodgetName()that returns the string 'Service A'. So callingserviceA.getName()returns 'Service A'.Final Answer:
Service A -> Option DQuick Check:
Registered service returns its name [OK]
- Assuming injector.get returns undefined or null
- Forgetting to register before getting
- Expecting an error without registration
class ServiceB {}
const serviceB = injector.get(ServiceB);
injector.register(ServiceB);Solution
Step 1: Check the order of registration and retrieval
The code tries to getServiceBfrom the injector before registering it, which causes an error because the injector doesn't know aboutServiceByet.Step 2: Confirm correct usage order
Services must be registered before they can be retrieved from the injector.Final Answer:
ServiceB is registered after trying to get it -> Option AQuick Check:
Register before get = correct order [OK]
- Trying to get service before registration
- Confusing method names like get vs fetch
- Thinking constructor is required for registration
class Logger {
log(msg) { console.log(msg); }
}
class UserService {
constructor(logger) {
this.logger = logger;
}
createUser(name) {
this.logger.log(`User ${name} created`);
}
}
injector.register(Logger);
injector.register(UserService);How should you get UserService with Logger injected?
Solution
Step 1: Understand constructor injection
UserService expects a Logger instance in its constructor. The injector knows how to create Logger and UserService because both are registered.Step 2: Use injector to get UserService with dependencies
Callinginjector.get(UserService)lets the injector create UserService and automatically provide Logger to it.Final Answer:
const userService = injector.get(UserService); -> Option CQuick Check:
Injector provides dependencies via constructor [OK]
- Manually creating dependencies instead of using injector
- Getting Logger instead of UserService
- Calling UserService without new keyword
