Operations 5 min read

Why Do Stale MySQL Sleep Connections Appear After HAProxy Timeout Mismatch?

A MySQL server experienced max_connection errors due to lingering sleep connections caused by mismatched timeout settings between LVS and HAProxy, and adjusting HAProxy's timeout resolved the issue and prevented further connection exhaustion.

ITPUB
ITPUB
ITPUB
Why Do Stale MySQL Sleep Connections Appear After HAProxy Timeout Mismatch?

Background

The database reported max_connection errors, preventing clients from connecting. One application had historically increased the max_connections setting to cope with a legacy issue. Meanwhile, another service observed many long‑running sleep connections even after all its processes were restarted.

Scenario

After a restart, roughly 10 seconds of new connections were normal, but about 3,000 connections remained abnormal. No reproducible test environment existed.

Initial Analysis

The first hypothesis was that leftover processes on the application server kept connections open. After confirming no processes were accessing the database, the investigation moved to the network layer.

Network Investigation

All application processes were stopped, yet MySQL still showed several thousand‑second sleep connections. TCP ESTABLISHED connections were visible on the DB server, indicating they persisted.

Checking the HAProxy front‑end revealed matching ports and connections. Reverse‑looking up HAProxy connection details identified the source IP and port of the application servers (see screenshot).

Root Cause Explanation

The client connects to the database via LVS, which forwards traffic to HAProxy. LVS’s timeout is shorter than HAProxy’s. When the client remains idle, LVS forces the client to re‑establish a new connection to HAProxy, but the old HAProxy connection remains open because its timeout has not yet expired. This creates “orphaned” sleep connections that have no visible source on the application server.

Resolution

The HAProxy instance’s timeout was increased to be slightly higher than LVS’s timeout. After this change, the orphaned sleep connections disappeared within days, and the max_connection errors ceased.

MySQL error logs later showed NOTE entries indicating that HAProxy had closed some connections, confirming the timeout adjustment worked.

Lessons Learned

Even when application code correctly closes database connections, mismatched timeout settings between load‑balancing layers can generate persistent sleep connections that exhaust the connection pool. Adjusting timeout hierarchies provides a defensive layer, while developers should still ensure proper DB connection handling.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Operationsmysqlconnection timeoutdatabase troubleshootingHAProxyLVS
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.