What are microservices and how do they differ from a monolithic architecture?
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
A monolith packages and deploys several application capabilities together. It can still have clean modules, and it does not have to use one database. Microservices place capabilities behind separately deployable service boundaries with explicit network or messaging contracts and usually distinct ownership.
For example, a modular monolith might contain orders and payments as Java modules in one process and one release. In a microservice design they are separate deployable services, so a payment call crosses a network and can time out even while the order service is healthy. That independent deployment can help different teams release or scale separately.
The cost is distributed-system and operational work: contract compatibility, partial failure, tracing, authentication, data consistency, and many deployments. 'Small service' or 'database per service' alone is not the definition. I choose microservices only when the independent boundary provides enough organizational or technical benefit to justify that cost.
What are microservices and how do they differ from a monolithic architecture
Direct Answer
A monolith deploys a set of application capabilities together as one unit. Microservices divide capabilities into separately deployable services with explicit network contracts and team ownership. A microservice often owns its persistence boundary, but separate physical databases are a design choice; a monolith can still be modular and can use several databases.
Code Example
// MONOLITH: one app, one DB, an in-process call wrapped in one transaction
@Service
class CheckoutService {
@Transactional
public void checkout(Order order) {
orderRepo.save(order);
inventoryRepo.decrement(order.productId()); // same DB, atomic
}
}
// MICROSERVICES: separate services over the network, no shared transaction
@Service
class OrderService {
private final WebClient inventory; // points at inventory-service
public void checkout(Order order) {
orderRepo.save(order); // OrderService DB
inventory.post().uri("/reserve") // network call
.bodyValue(order).retrieve()
.toBodilessEntity().block(); // can fail independently
}
}
Common Mistakes & Pitfalls
- Premature microservices: Freshers often advocate splitting a greenfield project into microservices immediately because it sounds modern. This creates complexity before you understand your domain boundaries, leading to services that constantly need to call each other—negating independence.
- Ignoring the distributed data problem: Assuming you can join data across services like in a monolith. In microservices, UserService and OrderService have separate databases—you can't write a SQL join. You need API calls, data duplication, or event sourcing, which freshers underestimate.
- Overlooking operational costs: Not realizing that 10 microservices mean 10 deployment pipelines, 10 log streams, 10 health checks, and 10 places where things can fail. Monoliths have one deployment, one log file, one debugger session.
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.