Explain the difference between monolithic and microservices architecture.
4 minintermediatemicroservicesarchitecturemonolith
Quick Answer
A monolith is a single deployable unit where all modules run in one process and typically share one database — simple to build, test, and deploy initially, but harder to scale selectively and evolve as it grows. Microservices split functionality into independently deployable services with their own data, enabling targeted scaling and autonomy at the cost of distributed complexity. Start monolithic and extract services when scale or team boundaries justify it.
Detailed Answer
Monolithic Architecture:
- Single, unified codebase and deployment unit
- All components tightly coupled within one application
- Shared database for all modules
- Scaling requires scaling the entire application
- Simple deployment but limited flexibility
Microservices Architecture:
- Multiple independent services
- Loosely coupled with clear boundaries
- Each service has its own database (database per service pattern)
- Individual services can be scaled independently
- Complex deployment but high flexibility
Comparison Table:
| Aspect | Monolithic | Microservices |
|---|---|---|
| Deployment | Single unit | Multiple independent services |
| Database | Shared database | Database per service |
| Scaling | Vertical (entire app) | Horizontal (per service) |
| Technology | Single stack | Polyglot architecture |
| Development | Simple initially | Complex from start |
| Team Structure | Single large team | Multiple small teams |
Monolithic Example (.NET Core):
// All in one application
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
// All services in one application
}
}
Microservices Example (.NET Core):
// Product Service - Separate application
public class ProductServiceStartup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped();
}
}
// Order Service - Separate application
public class OrderServiceStartup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped();
services.AddHttpClient("ProductService", c =>
c.BaseAddress = new Uri("http://product-service"));
}
}