Skip to content

fix(proxy): reduce SSL connection overhead by setting TCP_NODELAY#15

Open
nik-localstack wants to merge 1 commit into
masterfrom
pnx-768-tcp-nodelay-ssl-proxy
Open

fix(proxy): reduce SSL connection overhead by setting TCP_NODELAY#15
nik-localstack wants to merge 1 commit into
masterfrom
pnx-768-tcp-nodelay-ssl-proxy

Conversation

@nik-localstack
Copy link
Copy Markdown

@nik-localstack nik-localstack commented May 14, 2026

Summary

  • Set TCP_NODELAY on both the client-facing socket and the proxy-to-PostgreSQL socket
  • Disables Nagle's algorithm, which was buffering small packets for up to 40ms waiting to batch them — the opposite of what a request/response protocol needs

Background

SSL connections through the proxy showed ~3x latency overhead compared to no-SSL for workloads that open many short-lived connections (the customer-reported pattern). Root cause analysis showed the overhead was entirely in connection setup, not per-query processing (a single reused connection had no measurable difference between SSL and no-SSL).

PostgreSQL connection startup is a rapid exchange of small messages (auth, parameter status, ready-for-query). With SSL there are even more round trips (SSLRequest → "S" → TLS handshake → startup). Nagle's algorithm was delaying each of these small packets, compounding the latency.

TCP_NODELAY is the standard setting for interactive protocol proxies. libpq and JDBC both set it unconditionally.

Results

Measured with 101 connections × 3 queries each:

no-SSL SSL Delta
Before 3s 9s +6s
After 3s 5s +2s

Related readings

https://en.wikipedia.org/wiki/Nagle%27s_algorithm
https://brooker.co.za/blog/2024/05/09/nagle.html

Set TCP_NODELAY on both the client-facing and proxy-to-PostgreSQL sockets
to disable Nagle's algorithm. PostgreSQL's connection startup involves rapid
small-message exchanges (auth, parameter status, ready-for-query), and with
SSL there are additional round trips for the SSLRequest handshake. Nagle's
buffering was delaying these small packets by up to 40ms each, compounding
into significant latency for workloads that open many short-lived connections.

Measured improvement on 101 connections x 3 queries: SSL overhead reduced
from +6s to +2s vs no-SSL baseline. Per-query overhead with connection reuse
is unaffected (remains ~0s).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@nik-localstack nik-localstack self-assigned this May 14, 2026
@nik-localstack nik-localstack marked this pull request as ready for review May 14, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant