A Java implementation of graceful degradation under concurrent load — a simplified bulkhead pattern.
When traffic suddenly increases, expensive non-critical operations can consume resources needed by core business operations.
For example, a recommendation or ML-based operation may be useful, but it should not consume all available resources when the system is under heavy load.
The idea behind this spike is simple:
Normal load
↓
Run expensive operation
High load
↓
Skip expensive operation
↓
Return lightweight fallback
For example, instead of running an expensive recommendation operation:
HEAVY_ML_RECOMMENDATION
the system can temporarily return:
FALLBACK_STATIC_RECOMMENDATION
The goal is to keep the important parts of the system responsive even when demand suddenly increases.
Concurrency is tracked with AtomicInteger and AtomicBoolean, providing thread-safe access to the shared load and manual shedding state.
| Method | Description |
|---|---|
LoadAdaptiveFeatureGuard(int concurrencyThreshold) |
Max concurrent heavy operations allowed before shedding. |
<T> T executeWithFallback(Supplier<T> heavy, Supplier<T> fallback) |
Runs heavy if under threshold, else fallback. |
void setManualSheddingMode(boolean enabled) |
Forces fallback mode on/off, independent of load — useful for ops-triggered degradation during an incident. |
int getActiveThreads() |
Current count of in-flight heavy operations. |
The project includes a concurrency test that simulates multiple heavy operations running at the same time.
A CountDownLatch is used to hold heavy operations open temporarily, allowing the test to simulate a situation where the configured concurrency threshold has been reached.
The test verifies that a new request receives the fallback response instead of executing the heavy operation.
Run the tests with:
mvn clean test- Java 21
- Maven
- JUnit 5
- Java Concurrency Utilities
Built as an implementation of graceful degradation and concurrency control.