Bird
Raised Fist0
Microservicessystem_design~10 mins

Single responsibility per service in Microservices - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a microservice that handles only user authentication.

Microservices
class AuthService:
    def __init__(self):
        self.users = {}

    def [1](self, username, password):
        # Authenticate user
        return self.users.get(username) == password
Drag options to blanks, or click blank then click option'
Alog_activity
Bregister
Cauthenticate
Dsend_email
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name like 'register' which belongs to user registration service.
Including unrelated methods like 'send_email' or 'log_activity' in this service.
2fill in blank
medium

Complete the code to separate order processing into its own microservice method.

Microservices
class OrderService:
    def [1](self, order_id):
        # Process the order
        print(f"Processing order {order_id}")
Drag options to blanks, or click blank then click option'
Asend_invoice
Bprocess_order
Cupdate_inventory
Dcancel_order
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that belong to other services like 'cancel_order' or 'send_invoice'.
Mixing inventory updates inside order processing service.
3fill in blank
hard

Fix the error in the microservice code to ensure it only handles payment processing.

Microservices
class PaymentService:
    def process_payment(self, amount):
        print(f"Processing payment of ${amount}")

    def {{BLANK_2}}(self, order_id):
        print(f"Sending receipt for order {order_id}")
Drag options to blanks, or click blank then click option'
Aamount
Bupdate_order_status
Csend_receipt
Drefund_payment
Attempts:
3 left
💡 Hint
Common Mistakes
Including order status updates in payment service.
Using incorrect variable names causing runtime errors.
4fill in blank
hard

Fill both blanks to correctly separate user profile and notification services.

Microservices
class UserProfileService:
    def update_profile(self, user_id, data):
        print(f"Updating profile for user {user_id}")

class NotificationService:
    def [1](self, user_id, message):
        print(f"Sending [2] to user {user_id}")
Drag options to blanks, or click blank then click option'
Asend_notification
Bsend_email
Cnotification
Dupdate_profile
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update_profile' method in notification service.
Printing 'send_email' when the method is generic notification.
5fill in blank
hard

Fill all three blanks to design a microservice that only manages product inventory.

Microservices
class InventoryService:
    def [1](self, product_id, quantity):
        print(f"Adding {quantity} units to product {product_id}")

    def [2](self, product_id):
        print(f"Checking stock for product {product_id}")

    def [3](self, product_id, quantity):
        print(f"Removing {quantity} units from product {product_id}")
Drag options to blanks, or click blank then click option'
Aadd_stock
Bcheck_stock
Cremove_stock
Dupdate_price
Attempts:
3 left
💡 Hint
Common Mistakes
Including 'update_price' method in inventory service.
Using unclear method names that do not reflect inventory management.

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