Bird
Raised Fist0
Microservicessystem_design~12 mins

Single responsibility per service in Microservices - Architecture Diagram

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
System Overview - Single responsibility per service

This system is designed using microservices where each service has a single responsibility. Each service handles one specific business function, making the system easier to maintain, scale, and update without affecting other parts.

Key requirements include clear separation of concerns, independent deployment, and fault isolation.

Architecture Diagram
User
  |
  v
Load Balancer
  |
  v
API Gateway
  |
  +----------------+----------------+----------------+
  |                |                |
  v                v                v
Order Service   Payment Service   Inventory Service
  |                |                |
  v                v                v
Order DB        Payment DB       Inventory DB
  |                |                |
  +----------------+----------------+----------------+
                   |
                   v
                Cache Layer
Components
User
client
Initiates requests to the system
Load Balancer
load_balancer
Distributes incoming requests evenly to API Gateway instances
API Gateway
api_gateway
Routes requests to appropriate microservices based on request type
Order Service
service
Handles all order-related operations
Payment Service
service
Manages payment processing and transactions
Inventory Service
service
Manages product inventory and stock levels
Order DB
database
Stores order data
Payment DB
database
Stores payment transaction data
Inventory DB
database
Stores inventory data
Cache Layer
cache
Speeds up data retrieval for frequently accessed data
Request Flow - 11 Hops
UserLoad Balancer
Load BalancerAPI Gateway
API GatewayOrder Service
Order ServiceCache Layer
Cache LayerOrder Service
Order ServiceOrder DB
Order DBOrder Service
Order ServiceCache Layer
Order ServiceAPI Gateway
API GatewayLoad Balancer
Load BalancerUser
Failure Scenario
Component Fails:Order DB
Impact:Order Service cannot retrieve or store order data; new orders fail but cached data can still be served
Mitigation:Use database replication for failover; cache serves stale data temporarily; alert system triggers for manual intervention
Architecture Quiz - 3 Questions
Test your understanding
Which component directs user requests to the correct microservice?
AAPI Gateway
BLoad Balancer
CCache Layer
DOrder DB
Design Principle
This architecture demonstrates the single responsibility principle by assigning each microservice one clear function with its own database. This separation improves maintainability, scalability, and fault isolation, allowing teams to work independently and deploy changes without affecting unrelated parts.

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