Service Mesh — System Design

Service Mesh adalah lapisan infrastruktur yang mengatur komunikasi antar microservice, memindahkan logika networking (retry, circuit breaker, observability) kel

Service Mesh adalah lapisan infrastruktur yang mengatur komunikasi antar microservice, memindahkan logika networking (retry, circuit breaker, observability) keluar dari kode aplikasi.

Masalah tanpa Service Mesh:

// Setiap service harus implementasi sendiri:
class OrderService {
  async callUserService(userId) {
    // Retry logic
    for (let i = 0; i < 3; i++) {
      try {
        const resp = await fetch("http://user-service/users/" + userId);
        return resp.json();
      } catch (e) {
        if (i === 2) throw e;
        await sleep(Math.pow(2, i) * 1000); // backoff
      }
    }
  }
  // + circuit breaker, timeout, tracing, mTLS... semua harus tulis sendiri!
}

Kode networking yang sama ditulis berulang di setiap service. Sulit distandarkan dan di-update.

Solusi: Sidecar Proxy

Setiap service mendapat proxy (sidecar) yang mengurus semua komunikasi:

┌─────────────────────────────────┐
│  Pod / Container Group          │
│  ┌──────────────┐ ┌──────────┐  │
│  │ Order Service│ │ Sidecar  │  │
│  │ (bisnis)     │◄►│ (Envoy)  │  │
│  └──────────────┘ └────┬─────┘  │
└────────────────────────┼────────┘
                         │ mTLS
                         ▼
┌────────────────────────────────────┐
│  Pod / Container Group             │
│  ┌──────────────┐  ┌──────────┐   │
│  │ User Service │  │ Sidecar  │   │
│  │ (bisnis)     │◄►│ (Envoy)  │   │
│  └──────────────┘  └──────────┘   │
└────────────────────────────────────┘

Yang dikerjakan Sidecar secara otomatis:

Komponen Service Mesh:

Data Plane:
  Sidecar proxy (Envoy) di setiap pod — menangani traffic aktual

Control Plane:
  Mengkonfigurasi semua sidecar secara terpusat
  Contoh: Istio, Linkerd, Consul Connect

Konfigurasi traffic dengan Istio (declarative):

# Traffic shifting — canary deployment
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: order-service
spec:
  http:
  - route:
    - destination:
        host: order-service
        subset: v1
      weight: 90          # 90% ke versi lama
    - destination:
        host: order-service
        subset: v2
      weight: 10          # 10% ke versi baru (canary)
# Retry policy — otomatis di semua service
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
spec:
  http:
  - retries:
      attempts: 3
      perTryTimeout: 2s
      retryOn: gateway-error,connect-failure

Keuntungan Service Mesh:

Fitur Tanpa Mesh Dengan Mesh
mTLS Manual per service Otomatis semua
Retry/timeout Kode di tiap service Konfigurasi YAML terpusat
Observability Harus tambahkan sendiri Built-in
Canary deploy Kompleks Virtual service YAML

Kapan pakai Service Mesh:

Jangan pakai untuk monolith atau sedikit service — overhead kompleksitas tidak sebanding.

Tools populer: Istio, Linkerd, Consul Connect, AWS App Mesh

🎭 Analogi sehari-hari: Service mesh = sopir bus + cleaning service di kantor. Karyawan (service) fokus kerjaan masing-masing. Sopir antar mereka ke meeting di kantor lain (routing), cleaning service jaga keamanan + cek ID (mTLS). Kalau sopir ganti rute (canary deploy), karyawan gak perlu tau detail.

💡 Service Mesh = ngangkat networking concerns dari aplikasi: Tanpa mesh, tiap service implement retry, mTLS, circuit breaker, tracing — duplicate code. Dengan mesh, semua di sidecar = aplikasi cuma fokus business logic.

⚠️ Jebakan klasik:

🎯 Service Mesh decision:

TL;DR: Service Mesh = sidecar proxy (Envoy) handle networking concerns. mTLS, retry, circuit breaker, tracing built-in. Wajib K8s + tim platform. Jangan adopt premature — overhead kompleks. Istio (powerful), Linkerd (simple).