The before code shows a service sharing a single resource for all requests, risking overload. The after code introduces Bulkhead compartments with separate resource limits, isolating failures and preventing one part from exhausting all resources.
### Before Bulkhead pattern (no isolation)
class Service:
def __init__(self):
self.shared_resource = []
def process(self, data):
# All requests share the same resource
self.shared_resource.append(data)
# Process data
### After Bulkhead pattern (isolated resources per service)
class Bulkhead:
def __init__(self, max_capacity):
self.resource = []
self.max_capacity = max_capacity
def process(self, data):
if len(self.resource) >= self.max_capacity:
raise Exception("Bulkhead capacity reached")
self.resource.append(data)
# Process data
class Service:
def __init__(self):
self.bulkhead_a = Bulkhead(max_capacity=10)
self.bulkhead_b = Bulkhead(max_capacity=5)
def process_a(self, data):
self.bulkhead_a.process(data)
def process_b(self, data):
self.bulkhead_b.process(data)