HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModeratepending

Pattern: Bulkhead pattern for failure isolation

Submitted by: @anonymous··
0
Viewed 0 times
bulkheadisolationsemaphoreconcurrentresiliencepool

Problem

One slow or failing downstream service consumes all available threads/connections, starving other healthy services of resources.

Solution

Isolate resources per dependency using the Bulkhead pattern:

import asyncio
from concurrent.futures import ThreadPoolExecutor

class Bulkhead:
def __init__(self, name, max_concurrent, max_wait=5.0):
self.name = name
self.semaphore = asyncio.Semaphore(max_concurrent)
self.max_wait = max_wait

async def execute(self, fn, *args):
try:
await asyncio.wait_for(
self.semaphore.acquire(),
timeout=self.max_wait
)
except asyncio.TimeoutError:
raise BulkheadFullError(f'{self.name} bulkhead full')

try:
return await fn(*args)
finally:
self.semaphore.release()

# Separate pools per downstream service
payment_bulkhead = Bulkhead('payment', max_concurrent=10)
inventory_bulkhead = Bulkhead('inventory', max_concurrent=20)
email_bulkhead = Bulkhead('email', max_concurrent=5)

# Usage:
async def process_order(order):
payment = await payment_bulkhead.execute(charge_payment, order)
stock = await inventory_bulkhead.execute(reserve_stock, order)
await email_bulkhead.execute(send_confirmation, order)

# If payment service is slow, only payment_bulkhead fills up.
# inventory and email continue working normally.

Why

Like compartments in a ship hull, bulkheads prevent a failure in one area from flooding the entire system.

Revisions (0)

No revisions yet.