State of the Art
-
Use Sprine Cloud Gateway (opens in a new tab) + Bucket4j + Redis
Benefits:
- Distributed rate limiting
Rate Limit (server)
-
Use cases
- Prevent DDoS attacks
- Ensure all users can access fairly
- Enforce access limit for users of different pricing levels
-
Strategies
-
Implementations
-
Spring Cloud Gateway
-
Bucket4j
-
Redis
-
Load Balancing
Load Balancing (client)
Circuit Breaker (client)
Circuit Breaker - Spring Cloud Circuit Breaker (opens in a new tab)
Retry (client)
Resilience4j
Retry ( CircuitBreaker ( RateLimiter ( TimeLimiter ( Bulkhead ( Function ) ) ) ) )
Resilience4j - Circuit Breaker
-
Gist
For the first
minimum-number-of-callsrequests, the circuit breaker does not calculate thefailure rateand will allow all actions to proceed.Once the
minimum-number-of-callsis reached, if it'scount-based, the circuit breaker will calculate thefailure-rate(number-of-calls/sliding-window-size) and if it is above thefailure-rate-threshold, it will open the circuit.When the circuit breaker is in the
openstate, all calls are rejected duringwait-duration-in-open-state, and the circuit breaker throws aCallNotPermittedException.Once
wait-duration-in-open-stateexpires, the circuit breaker changes to thehalf-openstate and allowspermitted-number-of-calls-in-half-open-stateto see if the service is still unavailable.In the
half-openstate, the circuit breaker uses another configurable ring bit buffer to evaluate thefailure rate. If this newfailure rateis above thepermitted-number-of-calls-in-half-open-state, the circuit breaker changes back toopen; otherwise, it changes back toclosed. -
The circuit breaker pattern is implemented on the consumer (
caller), to avoid overwhelming a service which may be struggling to handle calls. -
minimumNumberOfCallsshould be >=permittedNumberOfCallsInHalfOpenState -
Resources
Circuit Breaker - Debug state transition
-
Spring Boot Actuator
Use
/actuator/circuitbreakereventsorCircuitBreakerEventsEndpoint.getAllCircuitBreakerEvents()to get all circuit breaker events.
Bulkhead
The bulkhead pattern is implemented on the service (callee), to prevent a failure during the handling of a single incoming call impacting the handling of other incoming calls.