Skip to content

Commit d74531f

Browse files
fix(proxy): reduce SSL connection overhead by setting TCP_NODELAY
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>
1 parent 5ade47a commit d74531f

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

postgresql_proxy/proxy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def _create_pg_connection(self, address, context):
7171
redirect_config = self.instance_config.redirect
7272

7373
pg_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
74+
pg_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
7475
pg_sock.connect((redirect_config.host, redirect_config.port))
7576
pg_sock.setblocking(False)
7677

@@ -130,6 +131,7 @@ def accept_wrapper(self, sock: socket.socket):
130131

131132
# Accept the raw connection
132133
clientsocket, address = sock.accept()
134+
clientsocket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
133135
# On macOS, accepted sockets inherit O_NONBLOCK from the listening socket.
134136
# SSL negotiation uses blocking recv, so we must set blocking explicitly here.
135137
clientsocket.setblocking(True)

0 commit comments

Comments
 (0)