What if your app parts worked like a well-organized team instead of a tangled mess?
Why High cohesion in Microservices? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a big app where each part tries to do everything: user login, payments, notifications, and data storage all tangled together in one place.
When you want to fix or add a feature, you have to dig through a huge mess of code that mixes many jobs.
This all-in-one approach makes changes slow and risky.
One small fix can break unrelated parts because everything is tightly connected.
Teams get stuck waiting on each other, and bugs multiply.
High cohesion means each microservice focuses on one clear job, like only handling payments or just managing user profiles.
This keeps code clean and easy to understand.
Teams can work independently, and changes become safer and faster.
class Service {
void login() {}
void processPayment() {}
void sendNotification() {}
void storeData() {}
}class PaymentService { void processPayment() {} } class NotificationService { void sendNotification() {} }
High cohesion unlocks faster development, easier maintenance, and smoother teamwork by keeping each service focused and independent.
Think of an online store where the payment system is its own microservice, separate from the product catalog and user accounts.
This way, payment updates don't risk breaking product listings or user data.
High cohesion means one service does one job well.
It reduces bugs and speeds up changes.
Teams can work independently without stepping on each other's toes.
Practice
high cohesion mean in microservices architecture?Solution
Step 1: Understand the meaning of cohesion
Cohesion means how closely related the tasks inside a module or service are.Step 2: Apply cohesion to microservices
High cohesion means grouping related tasks in one service to keep it focused and manageable.Final Answer:
Grouping related tasks and responsibilities within a single service -> Option DQuick Check:
High cohesion = grouping related tasks [OK]
- Thinking high cohesion means splitting every function separately
- Confusing cohesion with coupling
- Believing unrelated tasks should be combined
- Assuming database design defines cohesion
Solution
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.Step 2: Check other options for unrelated tasks
Options A and B mix unrelated tasks; D lacks business logic, so not cohesive.Final Answer:
A service that manages all user-related operations like profile, login, and preferences -> Option AQuick Check:
High cohesion = related user tasks together [OK]
- Choosing options that mix unrelated responsibilities
- Ignoring business logic in cohesion
- Confusing data storage with service responsibility
- Assuming more tasks always mean better cohesion
OrderService handles order creation, payment processing, and shipping updates. What is the likely issue with this design regarding high cohesion?Solution
Step 1: Analyze the tasks in OrderService
Order creation, payment, and shipping are different domains with distinct logic.Step 2: Evaluate cohesion
Mixing payment and shipping with order creation lowers cohesion because responsibilities differ.Final Answer:
The service has low cohesion because it mixes unrelated responsibilities -> Option AQuick Check:
Mixed tasks = low cohesion [OK]
- Assuming all order-related tasks are always cohesive
- Confusing scalability with cohesion
- Ignoring domain boundaries
- Believing loosely coupled means high cohesion
InventoryService currently manages stock levels and supplier payments. What is the best fix to improve high cohesion?Solution
Step 1: Identify unrelated responsibilities
Supplier payments are unrelated to stock level management.Step 2: Suggest separation for high cohesion
Moving payments to a dedicated PaymentService improves cohesion by grouping related tasks.Final Answer:
Split supplier payments into a separate PaymentService -> Option CQuick Check:
Separate unrelated tasks to improve cohesion [OK]
- Adding unrelated tasks to the same service
- Combining unrelated services
- Ignoring cohesion for simplicity
- Confusing cohesion with coupling
Solution
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.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.Final Answer:
UserService (user profiles, authentication), OrderService (orders, payments), ShippingService (shipping updates, tracking) -> Option BQuick Check:
Group related domain tasks for high cohesion [OK]
- Mixing unrelated tasks in one service
- Creating too many tiny services
- Building monolithic services
- Ignoring domain boundaries
