What if fixing one part of your app never broke the rest?
Why Single responsibility per service in Microservices? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine a small team building a big app where one giant service tries to do everything: user login, payments, notifications, and data storage all mixed together.
When something breaks, it's hard to find the cause because everything is tangled.
With one big service handling many tasks, updates become risky and slow.
One small change can accidentally break unrelated features.
Scaling is tough because you must scale the whole service even if only one part needs more power.
Splitting the app into small services, each with a single job, makes the system easier to understand and fix.
Each service can be updated or scaled independently without affecting others.
class BigService {
void login() { ... }
void processPayment() { ... }
void sendNotification() { ... }
}class AuthService { void login() { ... } } class PaymentService { void processPayment() { ... } } class NotificationService { void sendNotification() { ... } }
This approach lets teams work faster and safer by focusing on one clear task per service.
Think of an online store where the payment system is separate from the product catalog. If the payment service needs an update, it won't affect browsing products.
One service, one clear job.
Easier to fix, update, and scale.
Reduces risk of breaking unrelated features.
Practice
single responsibility principle mean in microservices?Solution
Step 1: Understand the principle meaning
Single responsibility means one service focuses on one task or job only.Step 2: Evaluate options against this meaning
Each service should do only one specific job. matches the principle exactly; others contradict it by mixing tasks or coupling.Final Answer:
Each service should do only one specific job. -> Option DQuick Check:
Single responsibility = One job per service [OK]
- Thinking one service can do many unrelated tasks
- Assuming shared databases mean single responsibility
- Confusing tight coupling with single responsibility
Solution
Step 1: Identify naming that reflects single responsibility
The service name should clearly indicate one focused responsibility.Step 2: Check options for clarity and focus
UserManagementService clearly states it manages users only; others mix concerns or are vague.Final Answer:
UserManagementService -> Option AQuick Check:
Clear, focused name = single responsibility [OK]
- Using vague or numeric names without meaning
- Combining multiple domains in one service name
- Naming services after infrastructure components
UserService handles user data, OrderService handles orders. Which service should handle payment processing?Solution
Step 1: Analyze responsibilities of existing services
UserService manages users, OrderService manages orders, so payment is a separate concern.Step 2: Assign payment to a dedicated service
Payment processing is a distinct responsibility, so PaymentService is appropriate.Final Answer:
PaymentService -> Option BQuick Check:
Separate payment = separate service [OK]
- Assigning payment to unrelated services
- Combining payment with user or order logic
- Using generic service names that mix concerns
InventoryAndShippingService causing deployment issues. What is the best fix following single responsibility?Solution
Step 1: Identify problem with combined responsibilities
Inventory and shipping are two distinct jobs combined, causing complexity and deployment issues.Step 2: Apply single responsibility by splitting services
Splitting into InventoryService and ShippingService isolates concerns and simplifies management.Final Answer:
Split it into two services: InventoryService and ShippingService -> Option CQuick Check:
Split combined services to fix issues [OK]
- Merging unrelated services increases complexity
- Adding features to overloaded services worsens problems
- Ignoring root cause and just adding resources
Solution
Step 1: Understand the scope of each option
UserService, ProductService, OrderService, PaymentService, NotificationService splits store functions into focused services; others combine many tasks.Step 2: Match design to single responsibility principle
UserService, ProductService, OrderService, PaymentService, NotificationService clearly assigns one responsibility per service, making it scalable and maintainable.Final Answer:
UserService, ProductService, OrderService, PaymentService, NotificationService -> Option AQuick Check:
One service, one job = UserService, ProductService, OrderService, PaymentService, NotificationService [OK]
- Combining many tasks into one service
- Using too few services for complex domains
- Ignoring scalability and maintainability
