Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

A chat feature uses WebSockets through NGINX and a cloud load balancer to Spring Boot. In development it works, but in production connections drop exactly every 60 seconds when nobody types, and some clients never connect, receiving HTTP 400 or 426 on the upgrade.

Explain both problems and the configuration needed at each hop.

The failed upgrades happen because NGINX does not forward the hop-by-hop Upgrade and Connection headers by default and talks HTTP/1.0 to the backend, so Spring sees a plain GET and answers 400 or 426; we fix it with proxy_http_version 1.1 and explicit Upgrade and Connection headers. The 60-second drops are idle timeouts: NGINX's proxy_read_timeout and many cloud load balancers default to 60 seconds, so we send heartbeats every 20 to 30 seconds, raise the timeouts to a few minutes, and make clients reconnect and resume.

Why this question is asked

WebSockets work beautifully on a laptop because there is nothing between the browser and the server. In production there are usually two or three proxies, and each of them has its own opinion about upgrades and idle connections. Interviewers use this scenario because it has two separate faults with two separate causes, and a good answer explains each at the level of headers and timers rather than guessing. In this answer, we will recap the upgrade handshake, diagnose the 400 and 426 errors, explain the 60-second drops, then configure every hop and the client so the connection survives quiet periods and the inevitable drops.

1A Quick Recap of the WebSocket Upgrade

A WebSocket connection starts as an HTTP/1.1 request carrying Upgrade: websocket and Connection: Upgrade, plus a Sec-WebSocket-Key. The server answers 101 Switching Protocols, and from then on the TCP connection carries WebSocket frames in both directions. Our path here has three hops: browser to cloud load balancer, balancer to NGINX, and NGINX to the Spring Boot application. The upgrade request must survive every one of them with its headers intact, and the open connection must then survive every hop's idle timer.

2Problem 1: Why Some Upgrades Fail With 400 or 426

Upgrade and Connection are hop-by-hop headers. HTTP says they describe only the current connection, so a proxy must remove them before forwarding a request. NGINX follows that rule, and by default it also speaks HTTP/1.0 to upstream servers. So unless we tell it otherwise, the request that reaches Spring is an ordinary GET /chat with no Upgrade header. Spring's WebSocket handler rejects it: depending on the server and the missing header, the client sees 400 Bad Request or 426 Upgrade Required.

Why only some clients? Usually because not every request takes the same path. Perhaps one NGINX instance or one location block was configured correctly and another was not, or some clients connect through a corporate proxy that also strips the headers, or a fallback such as SockJS succeeds over HTTP for some and the native WebSocket fails for others. The NGINX access log helps here: a working upgrade logs status 101, while the failing ones log 400 or 426 from the upstream.

Java
# Broken: the defaults
location /chat {
proxy_pass http://chat_backend; # HTTP/1.0 upstream, Upgrade and Connection dropped
}
# access log: "GET /chat HTTP/1.1" 400 (or 426)
# Fixed
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
location /chat {
proxy_pass http://chat_backend;
proxy_http_version 1.1; # upgrades need HTTP/1.1
proxy_set_header Upgrade $http_upgrade; # forward the client's Upgrade header
proxy_set_header Connection $connection_upgrade; # "upgrade" for WebSockets
proxy_set_header Host $host;
proxy_read_timeout 300s; # see problem 2
}
# access log: "GET /chat HTTP/1.1" 101

The map block is a common refinement: it sends Connection: upgrade when the client asked for an upgrade and Connection: close otherwise, so normal requests through the same location still behave. Setting the literal value "upgrade" also works for a location used only for WebSockets. We also check the cloud load balancer: application load balancers on the major clouds support WebSockets, but some older or classic balancers in HTTP mode do not, and they must be switched to a type that does or to TCP mode.

3Problem 2: Why Quiet Connections Die After Exactly 60 Seconds

An exact, repeatable number like 60 seconds almost always points to a timer, not to a network failure. Two timers on this path default to that value. In NGINX, proxy_read_timeout is the longest time NGINX waits between two reads from the upstream; for a proxied WebSocket, it means that if the server sends nothing for 60 seconds, NGINX closes the connection. And many cloud load balancers have an idle timeout of 60 seconds by default, counted in both directions.

In development there is no NGINX and no balancer, so no timer, and the connection lives forever. In production, as soon as a chat room goes quiet for a minute, the first timer to expire cuts the connection. The browser sees a close event, often with code 1006, which means the connection ended without a proper close frame.

Step throughA quiet chat connection in production
BrowserCloud LBNGINXSpring Boot
WebSocket open (101 through every hop)
messages while users type
no data for 60 s: proxy_read_timeout
close the connection
TCP closed, browser gets close code 1006

5 messages in order. Play them, or step through them one at a time.

The first idle timer on the path to expire decides when quiet connections die.

4Fix 1: Heartbeats Shorter Than the Shortest Timeout

The robust fix is to make sure a connection is never idle long enough for any timer to fire. We send a heartbeat more often than the shortest idle timeout on the path: with 60-second timers, every 20 to 30 seconds is a safe choice. This can be a WebSocket ping frame, which the browser answers automatically with a pong, or an application message. With STOMP over WebSocket, as Spring's message broker support uses, heartbeats are part of the protocol and both sides negotiate them.

Java
@Configuration
@EnableWebSocketMessageBroker
class ChatConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.initialize();
registry.enableSimpleBroker("/topic")
.setHeartbeatValue(new long[] {25_000, 25_000}) // send and expect a beat every 25 s
.setTaskScheduler(scheduler);
}
}
// browser, with @stomp/stompjs
// client.heartbeatOutgoing = 25000; client.heartbeatIncoming = 25000;

Heartbeats help in both directions. Traffic from the server keeps NGINX's read timer and the balancer's timer alive, and traffic from the client proves to the server that the client is still there. That second point matters for half-open connections, which we come back to in the FAQ.

5Fix 2: Longer, but Finite, Timeouts at Each Hop

As a second line of defence, we raise the idle timeouts so a missed heartbeat or a slow client does not cause a drop. A few minutes is typical: proxy_read_timeout and proxy_send_timeout of 300 seconds in NGINX, and an idle timeout of a few minutes on the cloud balancer. We keep them finite on purpose. A connection whose client vanished, for example a phone that lost signal, never sends a close frame, and a finite timeout is what eventually frees its memory and file descriptor. The rule to remember is that the heartbeat interval must be shorter than every timeout on the path, including any corporate proxy or mobile carrier NAT we do not control, which is why 20 to 30 seconds is a common choice.

6Fix 3: Clients That Reconnect and Resume

Even with perfect configuration, connections will drop: deploys restart NGINX and the application, phones switch networks, and laptops sleep. So the client must treat a close as normal. It reconnects with exponential backoff and random jitter (for example 1, 2, 4 seconds up to 30, each multiplied by a random factor) so that thousands of clients do not reconnect in the same second after a deploy. And it resumes: each chat message carries a sequence number, the client remembers the last one it received, and after reconnecting it asks for everything after that number, which the server reads from its message store. Without resume, every drop silently loses messages.

7Follow-up questions

How would you detect half-open connections on the server side?

A half-open connection is one where the client is gone but the server never received a close, so the socket looks open. The server only finds out when a write fails or a timer fires. With STOMP heartbeats, the server expects a beat from the client every 25 seconds and closes the session if none arrives within a grace period. With raw WebSockets, the server sends ping frames and closes the session if no pong comes back in time. TCP keepalive also exists, but its default timing is about two hours, far too slow on its own.

Why not just set the timeouts to a day and skip heartbeats?

Because we do not control every timer: corporate proxies, mobile carrier NAT tables and home routers drop idle flows too, often after a few minutes. Very long timeouts also keep dead connections around, wasting memory. Heartbeats work across all of these; long timeouts only cover our own hops.

Does the Spring Boot embedded server have its own idle timeout?

It can. Servlet containers apply a session idle timeout to WebSocket sessions, configurable through the container or Spring's ServletServerContainerFactoryBean (setMaxSessionIdleTimeout). Heartbeats keep that timer from firing as well; check the default for your server version.

8Conclusion

In this answer, we separated two faults. Failed upgrades with 400 or 426 come from NGINX dropping the hop-by-hop Upgrade and Connection headers and speaking HTTP/1.0 upstream, fixed with proxy_http_version 1.1 and explicit headers, plus a balancer that supports WebSockets. Drops every 60 seconds come from idle timers, NGINX's proxy_read_timeout and the balancer's idle timeout, fixed mainly by heartbeats every 20 to 30 seconds, backed by longer but finite timeouts. Finally, clients reconnect with jittered backoff and resume from the last message, because connections will always drop sometimes.

What to say in the interviewAbout 80 seconds aloud

Open with

There are two different faults. The 400 and 426 responses mean the upgrade never reaches Spring. Upgrade and Connection are hop-by-hop headers, so NGINX strips them by default and also talks HTTP/1.0 to the upstream; Spring receives a plain GET and rejects it.

Then, if they want more

The fix in NGINX is proxy_http_version 1.1, proxy_set_header Upgrade $http_upgrade and proxy_set_header Connection upgrade, and I would check that every NGINX instance and location has it and that the cloud balancer type supports WebSockets.

The drops every 60 seconds are idle timers: NGINX closes a proxied WebSocket when nothing is read for proxy_read_timeout, which defaults to 60 seconds, and many cloud load balancers also have a 60-second idle timeout. In development neither exists.

The main fix is heartbeats more often than the shortest timeout on the path, say every 25 seconds, using ping frames or STOMP heartbeats, which Spring's broker supports. Then I raise the timeouts to a few minutes as a second line, but keep them finite so dead connections get cleaned up.

And the client should reconnect with jittered exponential backoff and resume from the last message sequence number, because deploys and network changes will still drop connections.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain both problems and the configuration needed at each hop.": https://scrivio-interview-library.netlify.app/architecture/load-balancing-networking/c11c170f52/