Bird
Raised Fist0
Microservicessystem_design~20 mins

High cohesion in Microservices - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
High Cohesion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is high cohesion important in microservices?

In microservices architecture, why do we aim for high cohesion within each service?

AIt allows services to share databases easily, reducing data duplication.
BIt makes each service responsible for a single, well-defined task, improving maintainability and scalability.
CIt forces all services to depend on each other, increasing integration complexity.
DIt reduces the number of services by combining unrelated functionalities.
Attempts:
2 left
💡 Hint

Think about how grouping related tasks affects service clarity and updates.

Architecture
intermediate
2:00remaining
Identifying high cohesion in microservice design

Which of the following microservice designs shows the highest cohesion?

AA service that manages orders and also sends marketing emails.
BA service that handles user authentication, payment processing, and product catalog updates.
CA service dedicated only to managing user profiles and preferences.
DA service that handles inventory updates and customer support tickets.
Attempts:
2 left
💡 Hint

Look for the service that groups closely related responsibilities.

scaling
advanced
2:30remaining
Impact of high cohesion on microservice scaling

How does high cohesion in microservices affect the ability to scale the system?

AIt allows scaling only the entire system together, increasing resource use.
BIt prevents scaling because services are too tightly coupled internally.
CIt requires scaling all services simultaneously to maintain consistency.
DIt enables scaling individual services independently based on their specific load.
Attempts:
2 left
💡 Hint

Think about how focused responsibilities affect resource allocation.

tradeoff
advanced
2:30remaining
Tradeoffs of enforcing very high cohesion in microservices

What is a potential downside of enforcing very high cohesion in microservices?

AIt can lead to too many small services, increasing deployment and management overhead.
BIt causes services to share too much data, risking data corruption.
CIt forces unrelated functionalities into one service, reducing clarity.
DIt eliminates the need for service communication, reducing flexibility.
Attempts:
2 left
💡 Hint

Consider what happens if you split responsibilities too finely.

component
expert
3:00remaining
Designing a highly cohesive microservice for an e-commerce platform

You need to design a microservice for an e-commerce platform that handles product inventory. Which set of responsibilities best ensures high cohesion?

AManaging product stock levels, updating inventory after sales, and handling supplier restock requests.
BProcessing customer payments, sending marketing emails, and managing product stock levels.
CHandling supplier restock requests, managing user reviews, and updating product descriptions.
DManaging product stock levels, processing customer payments, and sending order confirmation emails.
Attempts:
2 left
💡 Hint

Focus on grouping tasks that relate directly to inventory management.

Practice

(1/5)
1. What does high cohesion mean in microservices architecture?
easy
A. Using a single database for all microservices
B. Splitting every function into separate services regardless of relation
C. Combining unrelated tasks to reduce the number of services
D. Grouping related tasks and responsibilities within a single service

Solution

  1. Step 1: Understand the meaning of cohesion

    Cohesion means how closely related the tasks inside a module or service are.
  2. Step 2: Apply cohesion to microservices

    High cohesion means grouping related tasks in one service to keep it focused and manageable.
  3. Final Answer:

    Grouping related tasks and responsibilities within a single service -> Option D
  4. Quick Check:

    High cohesion = grouping related tasks [OK]
Hint: High cohesion means related tasks stay together [OK]
Common Mistakes:
  • Thinking high cohesion means splitting every function separately
  • Confusing cohesion with coupling
  • Believing unrelated tasks should be combined
  • Assuming database design defines cohesion
2. Which of the following is the correct way to describe a microservice with high cohesion?
easy
A. A service that manages all user-related operations like profile, login, and preferences
B. A service that mixes order processing and inventory updates randomly
C. A service that handles user authentication and payment processing
D. A service that only stores data without any business logic

Solution

  1. Step 1: Identify related tasks in options

    A service that manages all user-related operations like profile, login, and preferences groups user-related operations which are closely related.
  2. Step 2: Check other options for unrelated tasks

    Options A and B mix unrelated tasks; D lacks business logic, so not cohesive.
  3. Final Answer:

    A service that manages all user-related operations like profile, login, and preferences -> Option A
  4. Quick Check:

    High cohesion = related user tasks together [OK]
Hint: Look for grouping of related tasks in one service [OK]
Common Mistakes:
  • Choosing options that mix unrelated responsibilities
  • Ignoring business logic in cohesion
  • Confusing data storage with service responsibility
  • Assuming more tasks always mean better cohesion
3. Consider a microservice design where the OrderService handles order creation, payment processing, and shipping updates. What is the likely issue with this design regarding high cohesion?
medium
A. The service has low cohesion because it mixes unrelated responsibilities
B. The service has high cohesion because all tasks relate to orders
C. The service is scalable because it handles multiple tasks
D. The service is loosely coupled with other services

Solution

  1. Step 1: Analyze the tasks in OrderService

    Order creation, payment, and shipping are different domains with distinct logic.
  2. Step 2: Evaluate cohesion

    Mixing payment and shipping with order creation lowers cohesion because responsibilities differ.
  3. Final Answer:

    The service has low cohesion because it mixes unrelated responsibilities -> Option A
  4. Quick Check:

    Mixed tasks = low cohesion [OK]
Hint: Different domains in one service reduce cohesion [OK]
Common Mistakes:
  • Assuming all order-related tasks are always cohesive
  • Confusing scalability with cohesion
  • Ignoring domain boundaries
  • Believing loosely coupled means high cohesion
4. A microservice named InventoryService currently manages stock levels and supplier payments. What is the best fix to improve high cohesion?
medium
A. Combine InventoryService with OrderService
B. Add customer order tracking to InventoryService
C. Split supplier payments into a separate PaymentService
D. Keep all tasks in InventoryService for simplicity

Solution

  1. Step 1: Identify unrelated responsibilities

    Supplier payments are unrelated to stock level management.
  2. Step 2: Suggest separation for high cohesion

    Moving payments to a dedicated PaymentService improves cohesion by grouping related tasks.
  3. Final Answer:

    Split supplier payments into a separate PaymentService -> Option C
  4. Quick Check:

    Separate unrelated tasks to improve cohesion [OK]
Hint: Separate unrelated tasks into different services [OK]
Common Mistakes:
  • Adding unrelated tasks to the same service
  • Combining unrelated services
  • Ignoring cohesion for simplicity
  • Confusing cohesion with coupling
5. You are designing a microservices system for an e-commerce platform. To ensure high cohesion, which of the following service groupings is best?
hard
A. UserService (user profiles, payments), OrderService (orders, shipping), InventoryService (stock levels, payments)
B. UserService (user profiles, authentication), OrderService (orders, payments), ShippingService (shipping updates, tracking)
C. One big service handling users, orders, payments, shipping, and inventory
D. Split every function into its own microservice regardless of relation

Solution

  1. Step 1: Evaluate grouping of related tasks

    UserService (user profiles, authentication), OrderService (orders, payments), ShippingService (shipping updates, tracking) groups related tasks logically by domain, supporting high cohesion.
  2. Step 2: Compare with other options

    UserService (user profiles, payments), OrderService (orders, shipping), InventoryService (stock levels, payments) mixes payments in unrelated services; C is a monolith; D over-splits causing low cohesion.
  3. Final Answer:

    UserService (user profiles, authentication), OrderService (orders, payments), ShippingService (shipping updates, tracking) -> Option B
  4. Quick Check:

    Group related domain tasks for high cohesion [OK]
Hint: Group by domain responsibilities for high cohesion [OK]
Common Mistakes:
  • Mixing unrelated tasks in one service
  • Creating too many tiny services
  • Building monolithic services
  • Ignoring domain boundaries