Sub topic 3
Bulkheads: Containing Failure to One Part of the System
Assume you know how a service makes network calls and handles exceptions. This chapter focuses on limiting damage when a dependency becomes slow or unavailable.
Bulkheads: Containing Failure to One Part
A bulkhead isolates resources for different workloads. The name comes from ship compartments: one flooded compartment should not sink the whole ship.
For example, give recommendation requests their own worker pool and connection limit. If the recommendation provider stalls, checkout requests can still run. A semaphore is a simple bulkhead:
import threading
import time
recommendation_slots = threading.BoundedSemaphore(10)
def get_recommendations(fetch, user_id):
if not recommendation_slots.acquire(blocking=False):
return {"items": [], "degraded": True, "source": "capacity_limit"}
try:
return {"items": fetch(user_id), "degraded": False}
finally:
recommendation_slots.release()The non-blocking acquire rejects excess work instead of allowing requests to wait without limit. In production, isolate more than threads. Use separate connection pools, queues, rate limits, or worker groups for important workloads.
Load Shedding: Protecting Core Functions
Load shedding deliberately refuses low-priority work when capacity is exhausted. Keep core actions, such as authentication, payment confirmation, and data retrieval. Drop optional actions, such as recommendations, previews, or analytics.
Set explicit limits and return a controlled fallback. A timeout alone is not load shedding because timed-out work may still consume resources until it ends.
Making Degraded Behavior Visible
A fallback should be visible to both operators and users. Record structured logs with the dependency, operation, outcome, and reason. Measure fallback counts, timeout counts, rejected requests, circuit state, and latency. Add a degraded field or response header when clients need to distinguish cached or partial data.
Tell users what matters: “Recommendations are temporarily unavailable. Your order is unaffected.” Do not expose internal exception details or imply that stale data is current.
> callout note > A fallback that hides every failure can create silent data-quality problems. Track when it runs and how old its data is.
Testing Fallbacks with Fault Injection
Fault injection deliberately adds failures, delays, or rejected connections in a test environment. Verify that optional failures return useful defaults, core requests remain available, limits prevent unbounded queues, and metrics and messages appear.
Test recovery too. Restore the dependency and confirm that normal responses resume without restarting the service.
Exercise
The recommendation service is timing out and consuming all workers. What should you change first?
Show answer
Isolate recommendations with a bulkhead, bound the dependency call with a timeout, and shed optional requests when that isolated capacity is full.