0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · Lesson

Bulkheads & Rate Limiting for Resilience

Isolate failures and protect downstream services using bulkhead isolation and the gateway's RequestRateLimiter filter.

Bulkheads & Rate Limiting for Resilience is a free API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Containing the Blast Radius

Circuit breakers stop calls to a broken service, but a flood of traffic to one route can still starve others. Bulkheads and rate limiting keep one busy route from sinking the whole gateway.

The Bulkhead Pattern

Named after a ship's watertight compartments, a bulkhead caps how many concurrent calls a route may have. If one service slows down, only its compartment fills, sparing the rest.

Resilience4j Bulkhead Config

Resilience4j offers a bulkhead that limits concurrent calls per instance.

resilience4j:
  bulkhead:
    instances:
      orders:
        maxConcurrentCalls: 20

Why Rate Limiting Differs

A bulkhead caps concurrency; a rate limiter caps requests over time. Together they protect both fast bursts and sustained load.

The RequestRateLimiter Filter

Spring Cloud Gateway ships a RequestRateLimiter filter backed by a Redis token bucket. It rejects excess requests with HTTP 429.

filters:
  - name: RequestRateLimiter
    args:
      redis-rate-limiter.replenishRate: 10
      redis-rate-limiter.burstCapacity: 20

Replenish Rate and Burst

replenishRate is the steady tokens per second; burstCapacity is the most that can be spent in a spike. Burst should be greater than or equal to replenish.

Choosing a KeyResolver

A KeyResolver decides what to limit by, such as user, API key, or IP. Here we limit per user from a header.

@Bean
KeyResolver userKeyResolver() {
    return exchange -> Mono.just(
        exchange.getRequest().getHeaders()
            .getFirst("X-User-Id"));
}

Redis Backing Store

The limiter needs Redis so counts are shared across gateway instances. Add the reactive Redis starter.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>

Combining Patterns

Stack resilience filters on a route: circuit breaker first, then rate limiter, so traffic is shaped before reaching a possibly fragile backend.

filters:
  - name: CircuitBreaker
    args:
      name: ordersCB
      fallbackUri: forward:/fallback/orders
  - name: RequestRateLimiter
    args:
      redis-rate-limiter.replenishRate: 10
      redis-rate-limiter.burstCapacity: 20

Graceful Rejection

When the limit is exceeded the client gets 429. You can customize the status, but always return a clear, retryable response so clients can back off.

args:
  redis-rate-limiter.replenishRate: 5
  statusCode: TOO_MANY_REQUESTS

Observing the Limits

Watch metrics for 429 counts and bulkhead rejections. Tune the numbers based on real downstream capacity, not guesses.

Quick Check

What is the core difference between a bulkhead and a rate limiter?

Recap

You added two more resilience tools:

  • Bulkheads cap concurrent calls per route
  • RequestRateLimiter uses a Redis token bucket
  • replenishRate and burstCapacity shape traffic
  • A KeyResolver defines the limiting key

Combined with circuit breakers, retries, and fallbacks, your gateway degrades gracefully under stress.

Frequently asked questions

Is the “Bulkheads & Rate Limiting for Resilience” lesson free?

Yes — the full text of “Bulkheads & Rate Limiting for Resilience” is free to read here on the web, and the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course, upgrade to CoddyKit PRO.

What will I learn in “Bulkheads & Rate Limiting for Resilience”?

Isolate failures and protect downstream services using bulkhead isolation and the gateway's RequestRateLimiter filter. You practise API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

No prior experience is required. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Bulkheads & Rate Limiting for Resilience” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson?

Yes. Every API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Circuit Breakers with Resilience4j
  2. Retries & Timeouts Configuration
  3. Error Handling & Fallbacks
  4. Bulkheads & Rate Limiting for Resilience
← Back to API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)