| Users/Scale | Code Complexity | Module Size | Change Impact | Testing Effort |
|---|---|---|---|---|
| 100 users | Low | Small, focused classes | Easy to change one responsibility | Simple unit tests |
| 10,000 users | Moderate | More classes, each with single responsibility | Changes isolated, less risk | Automated tests for each module |
| 1,000,000 users | High | Many small modules, clear boundaries | Parallel development possible | Comprehensive test suites |
| 100,000,000 users | Very High | Distributed services, each with single responsibility | Independent deployment and scaling | Continuous integration and testing |
Single Responsibility Principle in LLD - Scalability & System Analysis
When SRP is not followed, the first bottleneck is code maintainability. As the system grows, modules that handle multiple responsibilities become large and complex. This leads to slower development, more bugs, and harder testing. The system becomes fragile and difficult to scale in terms of team productivity and code quality.
- Refactor large modules: Break down classes or components so each has one responsibility.
- Modular design: Design small, focused modules that can be developed and tested independently.
- Microservices: At very large scale, split system into services each responsible for a single business capability.
- Automated testing: Use unit tests to ensure each module works correctly and changes do not break others.
- Code reviews and documentation: Maintain clear boundaries and responsibilities to avoid overlap.
Assuming a system with 1 million users and 1000 modules:
- Each module handles a single responsibility, reducing complexity and bugs by ~30%.
- Testing effort per module is smaller, enabling faster releases and less downtime.
- Development cost is optimized as teams can work in parallel on different modules.
- Maintenance cost decreases over time due to easier debugging and updates.
When discussing SRP in an interview, start by explaining what SRP means in simple terms: each module should do one thing well. Then describe how this principle helps keep code clean and maintainable as the system grows. Use examples of what happens if SRP is ignored (large, complex modules). Finally, explain how you would refactor or design a system to follow SRP for better scalability and team productivity.
Your codebase has a module handling multiple responsibilities and it slows down development. What do you do first?
Answer: Refactor the module into smaller modules, each with a single responsibility. This reduces complexity, improves testability, and speeds up development.
Practice
Solution
Step 1: Understand the principle's core idea
The Single Responsibility Principle states that a class or module should have one and only one reason to change, meaning it should do one job.Step 2: Match the explanation to the options
Each class or module should have only one job or responsibility. clearly states that each class or module should have only one job, which matches the principle.Final Answer:
Each class or module should have only one job or responsibility. -> Option BQuick Check:
Single Responsibility Principle = One job per class [OK]
- Thinking one class should do many tasks
- Believing all code should be in one place
- Confusing SRP with code reuse
Solution
Step 1: Analyze class names for single responsibility
Classes with multiple responsibilities in their names violate SRP. UserManagerAndLogger, DataProcessorAndReportGenerator, and FileHandlerAndNetworkSender suggest multiple jobs.Step 2: Identify the class with one clear responsibility
UserAuthenticator clearly focuses on one task: authenticating users, which aligns with SRP.Final Answer:
UserAuthenticator -> Option AQuick Check:
Class name with one job = UserAuthenticator [OK]
- Choosing classes with multiple jobs in name
- Ignoring SRP and mixing responsibilities
- Assuming longer names are better
class OrderProcessor:
def process_order(self, order):
self.validate_order(order)
self.save_order(order)
self.send_confirmation_email(order)
def validate_order(self, order):
pass
def save_order(self, order):
pass
def send_confirmation_email(self, order):
pass
Solution
Step 1: Identify responsibilities in the class
The class OrderProcessor validates orders, saves orders, and sends confirmation emails. These are three distinct jobs.Step 2: Check SRP compliance
SRP requires one responsibility per class. Mixing validation, saving, and emailing violates SRP.Final Answer:
The class handles order validation, saving, and email sending, mixing responsibilities. -> Option AQuick Check:
Multiple jobs in one class = SRP violation [OK]
- Ignoring multiple responsibilities in one class
- Thinking multiple methods mean multiple classes
- Assuming SRP allows all related tasks in one class
Solution
Step 1: Identify the problem with the current class
The class does two jobs: calculating taxes and generating invoices, which breaks SRP.Step 2: Apply SRP by separating responsibilities
Splitting into two classes, each with one responsibility, fixes the violation and improves maintainability.Final Answer:
Split the class into two: one for tax calculation and one for invoice generation. -> Option CQuick Check:
Separate classes for separate jobs = SRP fix [OK]
- Adding unrelated methods to the same class
- Combining jobs into one method
- Removing needed functionality instead of splitting
Solution
Step 1: Analyze each design for responsibility separation
Designs with a single class handling books, users, loans, and notifications, or a single class handling everything, put many jobs into one class, violating SRP. Designs with one class for books and users, and another for loans and notifications, mix multiple jobs in each class.Step 2: Identify the design with clear single responsibilities
Separate classes: 'BookManager' for books, 'UserManager' for users, 'LoanManager' for loans, and 'NotificationService' for alerts. separates concerns into distinct classes, each with one responsibility, following SRP best.Final Answer:
Separate classes: 'BookManager' for books, 'UserManager' for users, 'LoanManager' for loans, and 'NotificationService' for alerts. -> Option DQuick Check:
Separate classes for separate jobs = SRP followed [OK]
- Grouping unrelated responsibilities in one class
- Mixing multiple jobs in fewer classes
- Ignoring SRP for convenience
Ready to prove it?
Pass the test and this pattern moves unknown
