Monolithic Architecture là kiến trúc truyền thống trong đó toàn bộ ứng dụng được xây dựng như một đơn vị duy nhất. Tất cả components - UI, business logic, data access - đều nằm trong một codebase và được deploy cùng nhau.
┌─────────────────────────────────────────────────┐
│ Web Server │
├─────────────────────────────────────────────────┤
│ UI Layer │
│ ┌─────────────────────────────────────────┐ │
│ │ Controllers/Views │ │
│ └─────────────────────────────────────────┘ │
├─────────────────────────────────────────────────┤
│ Business Logic Layer │
│ ┌─────────────────────────────────────────┐ │
│ │ Services, Business Rules │ │
│ └─────────────────────────────────────────┘ │
├─────────────────────────────────────────────────┤
│ Data Access Layer │
│ ┌─────────────────────────────────────────┐ │
│ │ Repositories, ORM, SQL │ │
│ └─────────────────────────────────────────┘ │
├─────────────────────────────────────────────────┤
│ Database │
└─────────────────────────────────────────────────┘
// Monolithic Project Structure
MyApp/
├── Controllers/
│ ├── ProductsController.cs
│ ├── OrdersController.cs
│ └── CustomersController.cs
├── Services/
│ ├── ProductService.cs
│ ├── OrderService.cs
│ └── CustomerService.cs
├── Models/
│ ├── Product.cs
│ ├── Order.cs
│ └── Customer.cs
├── Repositories/
│ ├── ProductRepository.cs
│ ├── OrderRepository.cs
│ └── CustomerRepository.cs
├── Views/
│ ├── Products/
│ ├── Orders/
│ └── Customers/
└── AppDbContext.cs
// Controllers/OrdersController.cs
public class OrdersController : Controller
{
private readonly OrderService _orderService;
public OrdersController(OrderService orderService)
{
_orderService = orderService;
}
[HttpGet]
public async Task<IActionResult> Index()
{
var orders = await _orderService.GetAllOrdersAsync();
return View(orders);
}
[HttpPost]
public async Task<IActionResult> Create(CreateOrderViewModel model)
{
if (!ModelState.IsValid)
return View(model);
await _orderService.CreateOrderAsync(model);
return RedirectToAction(nameof(Index));
}
}
// Services/OrderService.cs
public class OrderService
{
private readonly OrderRepository _orderRepository;
private readonly ProductRepository _productRepository;
private readonly EmailService _emailService;
public OrderService(
OrderRepository orderRepository,
ProductRepository productRepository,
EmailService emailService)
{
_orderRepository = orderRepository;
_productRepository = productRepository;
_emailService = emailService;
}
public async Task CreateOrderAsync(CreateOrderViewModel model)
{
var order = new Order
{
CustomerId = model.CustomerId,
OrderDate = DateTime.Now,
Status = OrderStatus.Pending
};
foreach (var item in model.Items)
{
var product = await _productRepository.GetByIdAsync(item.ProductId);
order.AddItem(product, item.Quantity);
}
await _orderRepository.SaveAsync(order);
await _emailService.SendOrderConfirmation(order);
}
}
// Repositories/OrderRepository.cs
public class OrderRepository
{
private readonly AppDbContext _context;
public OrderRepository(AppDbContext context)
{
_context = context;
}
public async Task SaveAsync(Order order)
{
_context.Orders.Add(order);
await _context.SaveChangesAsync();
}
}
Advantage Description
Simplicity Easy to develop và debug
Performance In-process calls are fast
Consistency Single codebase, shared resources
Transaction Easy to maintain ACID transactions
Deployment Simple deployment (single package)
Development Good for small teams
Disadvantage Description
Scalability Khó scale theo component
Technology Hard to adopt new technologies
Reliability One failure affects entire app
Deployment Phải redeploy toàn bộ app
Development Codebase grows large, hard to maintain
Coupling Tightly coupled components
Small to medium applications
Teams mới hoặc limited experience
Rapid prototyping
Applications với tight integration requirements
Projects với limited scope
Large, complex applications
Applications requiring frequent scaling
Teams cần technological flexibility
Microservices-based requirements
// Load balancing configuration
services.AddLoadBalancing(options =>
{
options.MaxParallelRequests = 10;
});
// Using sticky sessions or session state
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.Cookie.Name = ".MyApp.Session";
options.IdleTimeout = TimeSpan.FromMinutes(20);
});
// Read/Write splitting
public class OrderRepository
{
private readonly AppDbContext _readContext;
private readonly AppDbContext _writeContext;
public async Task<List<Order>> GetOrdersAsync()
{
return await _readContext.Orders.ToListAsync();
}
public async Task SaveAsync(Order order)
{
await _writeContext.Orders.AddAsync(order);
await _writeContext.SaveChangesAsync();
}
}
// Modular Monolith - Separate by feature
src/
├── Modules/
│ ├── Orders/
│ │ ├── OrdersController.cs
│ │ ├── OrderService.cs
│ │ ├── OrderRepository.cs
│ │ └── OrdersModule.cs // Module registration
│ ├── Products/
│ │ ├── ProductsController.cs
│ │ ├── ProductService.cs
│ │ ├── ProductRepository.cs
│ │ └── ProductsModule.cs
│ └── Customers/
│ ├── CustomersController.cs
│ ├── CustomerService.cs
│ ├── CustomerRepository.cs
│ └── CustomersModule.cs
└── Shared/
├── Database/
├── Logging/
└── Authentication/
Use Layers : Separate concerns within the monolith
Modularize : Group code by feature, not by technical layer
Configuration : Externalize configuration
Logging : Implement centralized logging
Monitoring : Add health checks và metrics
Monolithic → Modular Monolithic → Microservices
↓ ↓ ↓
Single app Feature modules Distributed