Bird
Raised Fist0
Microservicessystem_design~3 mins

Why Single responsibility per service in Microservices? - Purpose & Use Cases

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
The Big Idea

What if fixing one part of your app never broke the rest?

The Scenario

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.

The Problem

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.

The Solution

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.

Before vs After
Before
class BigService {
  void login() { ... }
  void processPayment() { ... }
  void sendNotification() { ... }
}
After
class AuthService { void login() { ... } }
class PaymentService { void processPayment() { ... } }
class NotificationService { void sendNotification() { ... } }
What It Enables

This approach lets teams work faster and safer by focusing on one clear task per service.

Real Life Example

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.

Key Takeaways

One service, one clear job.

Easier to fix, update, and scale.

Reduces risk of breaking unrelated features.

Practice

(1/5)
1. What does the single responsibility principle mean in microservices?
easy
A. Services should be tightly coupled to improve performance.
B. Each service should handle multiple unrelated tasks.
C. Services should share the same database for all tasks.
D. Each service should do only one specific job.

Solution

  1. Step 1: Understand the principle meaning

    Single responsibility means one service focuses on one task or job only.
  2. 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.
  3. Final Answer:

    Each service should do only one specific job. -> Option D
  4. Quick Check:

    Single responsibility = One job per service [OK]
Hint: One service, one job keeps design simple and clear [OK]
Common Mistakes:
  • Thinking one service can do many unrelated tasks
  • Assuming shared databases mean single responsibility
  • Confusing tight coupling with single responsibility
2. Which of the following is the correct way to name a microservice following single responsibility?
easy
A. UserManagementService
B. UserAndOrderService
C. Service123
D. DatabaseService

Solution

  1. Step 1: Identify naming that reflects single responsibility

    The service name should clearly indicate one focused responsibility.
  2. Step 2: Check options for clarity and focus

    UserManagementService clearly states it manages users only; others mix concerns or are vague.
  3. Final Answer:

    UserManagementService -> Option A
  4. Quick Check:

    Clear, focused name = single responsibility [OK]
Hint: Service name should reflect one clear job [OK]
Common Mistakes:
  • Using vague or numeric names without meaning
  • Combining multiple domains in one service name
  • Naming services after infrastructure components
3. Given these microservices: UserService handles user data, OrderService handles orders. Which service should handle payment processing?
medium
A. UserService
B. PaymentService
C. OrderService
D. DatabaseService

Solution

  1. Step 1: Analyze responsibilities of existing services

    UserService manages users, OrderService manages orders, so payment is a separate concern.
  2. Step 2: Assign payment to a dedicated service

    Payment processing is a distinct responsibility, so PaymentService is appropriate.
  3. Final Answer:

    PaymentService -> Option B
  4. Quick Check:

    Separate payment = separate service [OK]
Hint: Separate distinct jobs into separate services [OK]
Common Mistakes:
  • Assigning payment to unrelated services
  • Combining payment with user or order logic
  • Using generic service names that mix concerns
4. You find a microservice called InventoryAndShippingService causing deployment issues. What is the best fix following single responsibility?
medium
A. Merge it with UserService to reduce services
B. Add more features to InventoryAndShippingService
C. Split it into two services: InventoryService and ShippingService
D. Keep it as is and increase server resources

Solution

  1. Step 1: Identify problem with combined responsibilities

    Inventory and shipping are two distinct jobs combined, causing complexity and deployment issues.
  2. Step 2: Apply single responsibility by splitting services

    Splitting into InventoryService and ShippingService isolates concerns and simplifies management.
  3. Final Answer:

    Split it into two services: InventoryService and ShippingService -> Option C
  4. Quick Check:

    Split combined services to fix issues [OK]
Hint: Split combined services to fix complexity [OK]
Common Mistakes:
  • Merging unrelated services increases complexity
  • Adding features to overloaded services worsens problems
  • Ignoring root cause and just adding resources
5. You are designing a microservices system for an online store. Which design best follows single responsibility per service?
hard
A. UserService, ProductService, OrderService, PaymentService, NotificationService
B. StoreService handling users, products, orders, payments, and notifications
C. UserService and OrderService only, handling all tasks
D. One big service for all store functions

Solution

  1. Step 1: Understand the scope of each option

    UserService, ProductService, OrderService, PaymentService, NotificationService splits store functions into focused services; others combine many tasks.
  2. Step 2: Match design to single responsibility principle

    UserService, ProductService, OrderService, PaymentService, NotificationService clearly assigns one responsibility per service, making it scalable and maintainable.
  3. Final Answer:

    UserService, ProductService, OrderService, PaymentService, NotificationService -> Option A
  4. Quick Check:

    One service, one job = UserService, ProductService, OrderService, PaymentService, NotificationService [OK]
Hint: Split big tasks into small focused services [OK]
Common Mistakes:
  • Combining many tasks into one service
  • Using too few services for complex domains
  • Ignoring scalability and maintainability