💻 Code Example
Typical Monolithic Structure
myapp/
├── src/
│ ├── controllers/ # 모든 HTTP 컨트롤러
│ │ ├── UserController.ts
│ │ ├── OrderController.ts
│ │ └── ProductController.ts
│ ├── services/ # 비즈니스 로직 (모두 같은 폴더)
│ │ ├── UserService.ts
│ │ ├── OrderService.ts
│ │ └── ProductService.ts
│ ├── models/ # 데이터베이스 모델 (공유)
│ │ ├── User.ts
│ │ ├── Order.ts
│ │ └── Product.ts
│ └── app.ts # 단일 진입점
├── package.json
└── Dockerfile # 하나의 이미지
Calls Between Monolithic Services
// services/OrderService.ts
import { UserService } from './UserService';
import { ProductService } from './ProductService';
import { Order } from '../models/Order';
export class OrderService {
constructor(
private userService: UserService, // 직접 의존
private productService: ProductService // 직접 의존
) {}
async createOrder(userId: string, productIds: string[]) {
// 함수 호출로 바로 접근 (네트워크 비용 없음)
const user = await this.userService.findById(userId);
const products = await this.productService.findByIds(productIds);
// 같은 DB 트랜잭션 가능
const order = new Order({ user, products });
await order.save();
return order;
}
}
Problematic Pattern (Big Ball of Mud)
// 안티패턴: 순환 의존, 명확하지 않은 경계
import { OrderService } from './OrderService';
import { UserService } from './UserService';
import { NotificationService } from './NotificationService';
export class ProductService {
// 모든 서비스가 서로를 알고 있음
constructor(
private orderService: OrderService,
private userService: UserService,
private notificationService: NotificationService
) {}
async updatePrice(productId: string, newPrice: number) {
// 도처에서 다른 서비스 호출
await this.product.updatePrice(newPrice);
// 연관된 주문 가격도 업데이트 (경계 침범)
await this.orderService.recalculateOrdersWithProduct(productId);
// 관심 있는 사용자에게 알림 (더 복잡해짐)
const users = await this.userService.findWatchingProduct(productId);
await this.notificationService.notifyPriceChange(users, productId);
}
}