java
31 lines · 7 steps
Bulkhead-protected HTTP calls in Spring
A Spring service caps concurrent warehouse calls with a semaphore bulkhead and degrades gracefully when it saturates or fails.
Explained by
highlit
1@Service
2public class InventoryService {
3
4 private final RestClient warehouseClient;
5
6 public InventoryService(RestClient.Builder builder) {
7 this.warehouseClient = builder
8 .baseUrl("https://warehouse.internal/api")
9 .build();
10 }
11
12 @Bulkhead(name = "warehouse", type = Bulkhead.Type.SEMAPHORE, fallbackMethod = "cachedStock")
13 public StockLevel currentStock(String sku) {
14 return warehouseClient.get()
15 .uri("/stock/{sku}", sku)
16 .retrieve()
17 .body(StockLevel.class);
18 }
19
20 private StockLevel cachedStock(String sku, BulkheadFullException ex) {
21 log.warn("Warehouse bulkhead saturated for sku={}, serving degraded stock", sku);
22 return StockLevel.unknown(sku);
23 }
24
25 private StockLevel cachedStock(String sku, Throwable ex) {
26 log.error("Warehouse lookup failed for sku={}", sku, ex);
27 return StockLevel.unknown(sku);
28 }
29
30 private static final Logger log = LoggerFactory.getLogger(InventoryService.class);
31}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A semaphore bulkhead bounds concurrent calls to a dependency so one slow service can't exhaust your threads.
- 2Fallback methods let you return a degraded-but-valid response instead of propagating failures to callers.
- 3Matching fallbacks by exception type lets you distinguish saturation from genuine downstream errors.
Related explainers
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
java
public final class EncodingDetector { public enum Encoding { UTF_8, UTF_16LE, UTF_16BE, UTF_32LE, UTF_32BE, ASCII, UNKNOWN
Detecting text encoding from raw bytes in Java
byte-manipulation
encoding-detection
bitwise-operations
Intermediate
8 steps
java
public final class ImportOrganizer { private static final Pattern IMPORT_LINE = Pattern.compile("^import\\s+(static\\s+)?([\\w.]+(?:\\.\\*)?)\\s*;\\s*$");
Sorting Java imports with a regex pass
regex
sorting
text-processing
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/bulkhead-protected-http-calls-in-spring-explained-java-e93d/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.