Microservices vs Monolith — System Design

Dua pendekatan arsitektur yang paling diperdebatkan: Monolith (satu aplikasi besar) vs Microservices (banyak service kecil). Monolith: ┌────────────────────────

Dua pendekatan arsitektur yang paling diperdebatkan: Monolith (satu aplikasi besar) vs Microservices (banyak service kecil).

Monolith:

┌────────────────────────────┐
│        Satu Aplikasi       │
│ ┌──────┐ ┌──────┐ ┌─────┐ │
│ │ Auth │ │Order │ │Email│ │
│ └──────┘ └──────┘ └─────┘ │
│ ┌──────┐ ┌──────┐ ┌─────┐ │
│ │ User │ │ Pay  │ │Notif│ │
│ └──────┘ └──────┘ └─────┘ │
│     Satu Database          │
└────────────────────────────┘

Microservices:

┌──────┐  ┌──────┐  ┌──────┐  ┌──────┐
│ Auth │  │Order │  │ Pay  │  │Email │
│  DB  │  │  DB  │  │  DB  │  │  DB  │
└──┬───┘  └──┬───┘  └──┬───┘  └──┬───┘
   └────────┬┴───────┬─┘         │
         API Gateway / Message Queue

Perbandingan:

Aspek Monolith Microservices
Deploy Satu deploy Deploy per service
Scaling Scale seluruh app Scale per service
Codebase Satu repo Repo per service
Team Satu team Team per service
Debugging Mudah (satu proses) Sulit (distributed)
Data Satu database Database per service
Latency Function call (nanoseconds) Network call (milliseconds)
Failure Satu bug = semua mati Satu service mati ≠ semua mati
Complexity Kode kompleks Infrastruktur kompleks

Kapan pakai Monolith:

Kapan pakai Microservices:

Tantangan microservices:

  1. Distributed transactions — bagaimana memastikan konsistensi data antar service?
  2. Service discovery — bagaimana service menemukan satu sama lain?
  3. Network latency — setiap call antar service ada overhead
  4. Data consistency — eventual consistency bisa membingungkan
  5. Monitoring — harus bisa melacak request yang melewati banyak service (distributed tracing)
  6. Testing — integration testing antar service lebih kompleks

Pola komunikasi antar microservices:

// 1. Synchronous (REST/gRPC) — request langsung
const user = await fetch("http://user-service/api/users/42");
const orders = await fetch("http://order-service/api/users/42/orders");

// 2. Asynchronous (Message Queue) — fire and forget
await queue.publish("order.created", { orderId: 123, userId: 42 });
// Email service dan inventory service subscribe ke event ini

Rekomendasi: Start with Monolith. Mulai dengan monolith yang well-structured (modular monolith), lalu extract ke microservices hanya jika ada kebutuhan nyata. Martin Fowler menyebutnya "Monolith First".

<title>Monolith vs Microservices</title> Monolith Auth Orders Users Payment Email Admin Shared Database Microservices Auth DB Orders DB Payment DB Users DB Email DB Admin DB API Gateway / Message Queue
Monolith: satu deployable dengan modul internal dan satu database. Microservices: service terpisah, masing-masing dengan database sendiri, terkoneksi via API atau message queue.

🎭 Analogi sehari-hari: Restoran besar.

💡 "Monolith First" rule (Martin Fowler): Hampir semua perusahaan unicorn mulai dari monolith. Twitter, Uber, Netflix, Airbnb — semua. Microservices = solusi organizational scaling, bukan technical scaling. Kalau team kamu < 20 orang, monolith lebih cepat ship + lebih mudah debug.

⚠️ Jebakan klasik:

🎯 Decision tree monolith vs microservices:

TL;DR: Mulai monolith yang well-modulare. Split ke microservices saat ada kebutuhan nyata (team scaling, fault isolation, polyglot tech). Microservices = kompleksitas operasional besar — wajib observability + distributed tracing + idempotent operations.

Yang akan kamu pelajari