🏗️ Architecture

Bulkhead Pattern

Bulkhead Pattern, Isolation Pattern

A fault isolation pattern derived from a ship's bulkhead. Separates system resources (thread pools, connection pools, memory, and so on) to prevent a failure in one service from spreading to the entire system.

📖 Detailed Explanation

The origin of the bulkhead comes from ship design. A ship is divided into compartments by bulkheads so that even if one compartment floods, the entire ship does not sink. In software, the same principle is applied to isolate failures.

There are several resource isolation methods: thread pool separation (assigning a dedicated thread pool to each service), connection pool separation (separate connection pools per database or external service), semaphore-based limits (limiting the number of concurrent requests), and container-based isolation (per-Pod resource limits in Kubernetes).

Usage scenarios include managing dependencies between services in a microservices environment, preventing failure propagation when calling external APIs, managing database connections, and isolating tenants from one another in a multi-tenant environment.

The advantages are better overall system stability through failure isolation, predictable resource usage, guaranteed resources for critical services, and easier SLA compliance. The disadvantages are reduced resource efficiency (unused resources), increased configuration complexity, and the difficulty of sizing correctly.

It is even more effective when used together with the Circuit Breaker pattern. When the Circuit Breaker quickly detects and cuts off a failure, the Bulkhead protects the remaining resources.

💻 Code Example

// Bulkhead Pattern - Java Resilience4j 구현
import io.github.resilience4j.bulkhead.*;
import io.github.resilience4j.bulkhead.ThreadPoolBulkhead;

public class BulkheadExample {

    // 1. 세마포어 기반 Bulkhead (동시 호출 수 제한)
    public static void semaphoreBulkhead() {
        BulkheadConfig config = BulkheadConfig.custom()
            .maxConcurrentCalls(10)           // 최대 동시 호출 10개
            .maxWaitDuration(Duration.ofMillis(500))  // 대기 시간 500ms
            .build();

        Bulkhead bulkhead = Bulkhead.of("paymentService", config);

        // Bulkhead로 감싸기
        Supplier<String> decoratedSupplier = Bulkhead
            .decorateSupplier(bulkhead, () -> paymentService.processPayment());

        try {
            String result = decoratedSupplier.get();
        } catch (BulkheadFullException e) {
            // Bulkhead가 가득 찬 경우 - fallback 처리
            return "결제 서비스가 혼잡합니다. 잠시 후 다시 시도하세요.";
        }
    }

    // 2. 스레드 풀 기반 Bulkhead (전용 스레드 풀 할당)
    public static void threadPoolBulkhead() {
        ThreadPoolBulkheadConfig config = ThreadPoolBulkheadConfig.custom()
            .maxThreadPoolSize(10)            // 최대 스레드 10개
            .coreThreadPoolSize(5)            // 코어 스레드 5개
            .queueCapacity(100)               // 대기 큐 크기 100
            .keepAliveDuration(Duration.ofMillis(1000))
            .build();

        ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.of("orderService", config);

        // CompletableFuture로 비동기 실행
        CompletableFuture<String> future = bulkhead.executeSupplier(
            () -> orderService.createOrder(orderRequest)
        );

        future.thenAccept(result -> System.out.println("주문 완료: " + result))
              .exceptionally(ex -> {
                  System.err.println("주문 실패: " + ex.getMessage());
                  return null;
              });
    }

    // 3. 서비스별 Bulkhead 설정 (Spring Boot)
    @Configuration
    public class BulkheadConfiguration {

        @Bean
        public BulkheadRegistry bulkheadRegistry() {
            // 결제 서비스 - 높은 우선순위, 더 많은 리소스
            BulkheadConfig paymentConfig = BulkheadConfig.custom()
                .maxConcurrentCalls(20)
                .maxWaitDuration(Duration.ofSeconds(1))
                .build();

            // 알림 서비스 - 낮은 우선순위, 적은 리소스
            BulkheadConfig notificationConfig = BulkheadConfig.custom()
                .maxConcurrentCalls(5)
                .maxWaitDuration(Duration.ofMillis(100))
                .build();

            return BulkheadRegistry.of(Map.of(
                "payment", paymentConfig,
                "notification", notificationConfig
            ));
        }
    }

    // 4. 어노테이션 기반 사용
    @Service
    public class PaymentService {

        @Bulkhead(name = "payment", fallbackMethod = "paymentFallback")
        public PaymentResult processPayment(PaymentRequest request) {
            return paymentGateway.charge(request);
        }

        private PaymentResult paymentFallback(PaymentRequest request, Exception e) {
            // 결제 서비스 혼잡 시 대기열에 추가
            paymentQueue.enqueue(request);
            return PaymentResult.queued("결제 요청이 대기열에 추가되었습니다");
        }
    }
}
# Bulkhead Pattern - Python asyncio 구현
import asyncio
from asyncio import Semaphore, Queue
from dataclasses import dataclass
from typing import Callable, Any
from functools import wraps
import time

@dataclass
class BulkheadConfig:
    """Bulkhead 설정"""
    max_concurrent_calls: int = 10      # 최대 동시 호출 수
    max_wait_time: float = 1.0          # 최대 대기 시간 (초)
    name: str = "default"

class Bulkhead:
    """세마포어 기반 Bulkhead 구현"""

    def __init__(self, config: BulkheadConfig):
        self.config = config
        self.semaphore = Semaphore(config.max_concurrent_calls)
        self.metrics = {
            'total_calls': 0,
            'rejected_calls': 0,
            'successful_calls': 0
        }

    async def execute(self, func: Callable, *args, **kwargs) -> Any:
        """Bulkhead로 보호된 함수 실행"""
        self.metrics['total_calls'] += 1

        try:
            # 타임아웃 내에 세마포어 획득 시도
            acquired = await asyncio.wait_for(
                self.semaphore.acquire(),
                timeout=self.config.max_wait_time
            )
        except asyncio.TimeoutError:
            # Bulkhead가 가득 참 - 요청 거부
            self.metrics['rejected_calls'] += 1
            raise BulkheadFullException(
                f"Bulkhead '{self.config.name}' is full. "
                f"Max concurrent calls: {self.config.max_concurrent_calls}"
            )

        try:
            result = await func(*args, **kwargs)
            self.metrics['successful_calls'] += 1
            return result
        finally:
            self.semaphore.release()

    def get_metrics(self) -> dict:
        """Bulkhead 메트릭 반환"""
        return {
            **self.metrics,
            'available_permits': self.semaphore._value,
            'utilization': 1 - (self.semaphore._value / self.config.max_concurrent_calls)
        }


class BulkheadFullException(Exception):
    """Bulkhead가 가득 찼을 때 발생"""
    pass


# 데코레이터로 사용
def bulkhead(name: str, max_concurrent: int = 10, max_wait: float = 1.0):
    """Bulkhead 데코레이터"""
    config = BulkheadConfig(
        max_concurrent_calls=max_concurrent,
        max_wait_time=max_wait,
        name=name
    )
    bh = Bulkhead(config)

    def decorator(func):
        @wraps(func)
        async def wrapper(*args, **kwargs):
            return await bh.execute(func, *args, **kwargs)
        wrapper.bulkhead = bh  # 메트릭 접근용
        return wrapper
    return decorator


# 사용 예제
class PaymentService:
    """결제 서비스 - Bulkhead로 보호"""

    @bulkhead(name="payment", max_concurrent=5, max_wait=2.0)
    async def process_payment(self, payment_id: str, amount: float) -> dict:
        """결제 처리 - 최대 5개 동시 실행"""
        print(f"결제 처리 중: {payment_id}")
        await asyncio.sleep(1)  # 외부 API 호출 시뮬레이션
        return {"payment_id": payment_id, "status": "completed", "amount": amount}


class NotificationService:
    """알림 서비스 - 별도 Bulkhead"""

    @bulkhead(name="notification", max_concurrent=20, max_wait=0.5)
    async def send_notification(self, user_id: str, message: str) -> bool:
        """알림 전송 - 최대 20개 동시 실행"""
        await asyncio.sleep(0.1)
        return True


# 테스트
async def main():
    payment_service = PaymentService()
    notification_service = NotificationService()

    # 10개의 결제 요청 동시 실행 (Bulkhead 제한: 5개)
    tasks = [
        payment_service.process_payment(f"PAY-{i}", 100.0 * i)
        for i in range(10)
    ]

    results = await asyncio.gather(*tasks, return_exceptions=True)

    for i, result in enumerate(results):
        if isinstance(result, BulkheadFullException):
            print(f"요청 {i}: 거부됨 - {result}")
        else:
            print(f"요청 {i}: 성공 - {result}")

    # 메트릭 확인
    print(f"\n결제 Bulkhead 메트릭: {payment_service.process_payment.bulkhead.get_metrics()}")


if __name__ == "__main__":
    asyncio.run(main())
# Bulkhead Pattern - Kubernetes 리소스 격리

# 1. Pod 리소스 제한 (컨테이너 레벨 Bulkhead)
apiVersion: v1
kind: Pod
metadata:
  name: payment-service
  labels:
    app: payment
    tier: critical
spec:
  containers:
  - name: payment-api
    image: payment-service:latest
    resources:
      # Bulkhead: 리소스 보장 및 제한
      requests:
        memory: "512Mi"    # 최소 보장 메모리
        cpu: "500m"        # 최소 보장 CPU (0.5 코어)
      limits:
        memory: "1Gi"      # 최대 사용 메모리
        cpu: "1000m"       # 최대 사용 CPU (1 코어)
    env:
    - name: THREAD_POOL_SIZE
      value: "10"
    - name: CONNECTION_POOL_SIZE
      value: "20"

---
# 2. Namespace 레벨 리소스 쿼터 (서비스 그룹 격리)
apiVersion: v1
kind: ResourceQuota
metadata:
  name: payment-quota
  namespace: payment-services
spec:
  hard:
    # 네임스페이스 전체 리소스 제한
    requests.cpu: "4"
    requests.memory: "8Gi"
    limits.cpu: "8"
    limits.memory: "16Gi"
    pods: "20"
    services: "10"

---
# 3. LimitRange - 기본 리소스 제한
apiVersion: v1
kind: LimitRange
metadata:
  name: default-limits
  namespace: payment-services
spec:
  limits:
  - default:
      cpu: "500m"
      memory: "512Mi"
    defaultRequest:
      cpu: "100m"
      memory: "128Mi"
    type: Container

---
# 4. PriorityClass - 중요 서비스 우선순위
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: critical-service
value: 1000000
globalDefault: false
description: "결제, 인증 등 핵심 서비스용"

---
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: non-critical-service
value: 100000
description: "알림, 분석 등 비핵심 서비스용"

---
# 5. Istio를 활용한 서비스 메시 레벨 Bulkhead
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: payment-bulkhead
spec:
  host: payment-service
  trafficPolicy:
    connectionPool:
      tcp:
        maxConnections: 100          # TCP 연결 제한
        connectTimeout: 30s
      http:
        h2UpgradePolicy: UPGRADE
        http1MaxPendingRequests: 50  # 대기 요청 제한
        http2MaxRequests: 100        # HTTP/2 최대 요청
        maxRequestsPerConnection: 10
        maxRetries: 3
    outlierDetection:
      consecutive5xxErrors: 5
      interval: 30s
      baseEjectionTime: 60s

---
# 6. HPA (Horizontal Pod Autoscaler) - 동적 Bulkhead 조정
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: payment-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: payment-service
  minReplicas: 3
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80

🗣️ Say It Like This in Practice

💬 In a system outage analysis meeting
"This outage happened because the external payment API slowed down and exhausted the thread pool. If we introduce a Bulkhead for the payment service and separate the thread pool, other services can keep working normally even when payment is slow."
💬 In an architecture design review
"Let's apply the Bulkhead pattern to calls between microservices. Separate the connection pool and thread pool per service, and combined with a Circuit Breaker, the system becomes more robust. You can use Resilience4j's ThreadPoolBulkhead."
💬 In a Kubernetes operations meeting
"I implemented the Bulkhead by setting a ResourceQuota per namespace. The payment namespace is guaranteed 4 CPU cores and 8GB of memory, and no matter how many resources the notification service uses, it won't affect the payment service."

⚠️ Cautions & Best Practices

❌
Undersizing the Bulkhead

A Bulkhead that is too small rejects even normal traffic. Determine the right size through load testing and leave a margin of 1.5 to 2 times peak traffic.

❌
Over-allocating resources

If you give every service a generous Bulkhead, overall resources run short. Allocate differentially according to service importance.

❌
No monitoring

If you don't collect metrics such as Bulkhead utilization and rejection rate, you can't detect problems early. Set an alert for when utilization exceeds 80%.

✅
Bulkhead best practices

Guarantee more resources for critical services, use it together with a Circuit Breaker, implement fallback logic, review sizing regularly, and verify with load testing.

🔗 Related Terms

📚 Learn More