All posts

Fix Load Balancer Error in Java

Fix Java application errors behind load balancers including Spring Boot proxy configuration, session affinity, and timeout tuning.

Java Apps Behind Load Balancers

Java web applications — particularly Spring Boot — need explicit proxy configuration when deployed behind a load balancer. Without it, redirect URLs are wrong and session management breaks.

Spring Boot Proxy Headers

Tell Spring Boot to trust forwarded headers:

# application.yml
server:
  forward-headers-strategy: framework
  tomcat:
    remoteip:
      remote-ip-header: X-Forwarded-For
      protocol-header: X-Forwarded-Proto

Or in newer Spring Boot versions, simply:

server:
  forward-headers-strategy: native

Session Stickiness

If you use HTTP sessions, either enable sticky sessions on the load balancer or externalize sessions:

<!-- pom.xml -->
<dependency>
  <groupId>org.springframework.session</groupId>
  <artifactId>spring-session-data-redis</artifactId>
</dependency>
spring:
  session:
    store-type: redis
  redis:
    host: redis-service

Health Check Actuator

Spring Boot Actuator provides health endpoints out of the box:

management:
  endpoints:
    web:
      exposure:
        include: health
  endpoint:
    health:
      show-details: never

Point the load balancer at /actuator/health.

Timeout Alignment

The load balancer timeout must be shorter than the server's:

server:
  connection-timeout: 120000  # 120 seconds

Set ALB idle timeout to 90 seconds so it closes the connection before Tomcat does, preventing 502 errors.

Bugsly's Java SDK auto-captures exceptions with request context including the original client IP from X-Forwarded-For, so errors are attributed to real users, not the load balancer.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free