Skip to content

add: use SO_REUSEPORT on platform supporting it#4703

Draft
mkleczek wants to merge 4 commits intoPostgREST:mainfrom
mkleczek:so-reuseport
Draft

add: use SO_REUSEPORT on platform supporting it#4703
mkleczek wants to merge 4 commits intoPostgREST:mainfrom
mkleczek:so-reuseport

Conversation

@mkleczek
Copy link
Collaborator

@mkleczek mkleczek commented Mar 10, 2026

DISCLAIMER:
This commit was authored entirely by a human without the assistance of LLMs.

Fixes #4694

Stacked on top of #4702 as it is not enough to start a new instance, it is also necessary not to fail in-flight requests on the old instance.

@mkleczek mkleczek force-pushed the so-reuseport branch 3 times, most recently from c252511 to 27c16d7 Compare March 10, 2026 15:43
@steve-chavez
Copy link
Member

This is failing all tests, I'd suggest to put these type of PRs as draft.

@mkleczek
Copy link
Collaborator Author

This is failing all tests, I'd suggest to put these type of PRs as draft.

Hmm... worked before latest push. Will switch to draft and fix.

@mkleczek mkleczek marked this pull request as draft March 10, 2026 16:31
@mkleczek
Copy link
Collaborator Author

This is failing all tests, I'd suggest to put these type of PRs as draft.

Hmm... worked before latest push. Will switch to draft and fix.

@steve-chavez - it looks like the issue is that on my machine PostgREST startup is fast enough so that it loads the schema cache before it accepts any requests. Here in CI the new instance fails with 503 because it didn't yet load the schema cache.

The question: is there any particular reason why we return 503, instead of simply not start listening on the socket until schema cache is loaded? The way we have it right now means we can't really support zero-downtime upgrades because once we start the new instance but before it loads the schema cache, some clients will get 503.

@steve-chavez
Copy link
Member

steve-chavez commented Mar 10, 2026

The question: is there any particular reason why we return 503, instead of simply not start listening on the socket until schema cache is loaded?

We were aiming to have requests wait instead of 503, this waiting does happen during schema cache reload but not on startup; we discussed this on #4129. Would it be better to not listen on the socket? How would clients behave in this case?

@develop7
Copy link
Collaborator

They would get a "connection refused" error, which means nobody there and is imo more confusing that any 5xx error. UX-wise I would prefer some waiting to a presumably hard fail any day.

@mkleczek
Copy link
Collaborator Author

mkleczek commented Mar 11, 2026

We were aiming to have requests wait instead of 503, this waiting does happen during schema cache reload but not on startup; we discussed this on #4129. Would it be better to not listen on the socket? How would clients behave in this case?

They would get a "connection refused" error, which means nobody there and is imo more confusing that any 5xx error. UX-wise I would prefer some waiting to a presumably hard fail any day.

I am not convinced, see below.

This is a complex topic so let's dig into it a little more. The startup sequence right now is:

  1. Postgrest is not running.
  2. Clients get connection refused
  3. Postgrest starts listening on a socket
  4. Clients get 503
  5. Postgrest loaded schema cache
  6. Normal traffic

The alternatives are:
a - blocking during schema cache loading

  1. Postgrest is not running.
  2. Clients get connection refused
  3. Postgrest starts listening on a socket
  4. Clients are blocked and potentially timeout getting some network error
  5. Postgrest loaded schema cache
  6. Normal traffic

b - listening on a socket only after schema cache loaded

  1. Postgrest is not running.
  2. Clients get connection refused
  3. Postgrest loaded schema cache and starts listening
  4. Normal traffic

So from the point of view of the clients (they don't know when Postgrest was started), we have 3 alternatives:

  1. connection refused -> 503 -> normal
  2. connection refused -> blocked/time out -> normal
  3. connection refused -> normal

I am not sure what value clients get from the first two options comparing to the third one. Diagnostics and readiness checks should be done using admin server anyway.

In case of SO_REUSEPORT the situation is even worse if we start listening early. We have the following situation: instance 1 is running, instance 2 is started. From the point of view of clients:

  1. Today: normal traffic -> some clients get 503 (both instances are listening only one is ready) -> normal traffic
  2. Blocking: normal traffic -> some clients blocked/time out (both instances are listening one is blocking) -> normal traffic
  3. Not listening: just normal traffic (there are no disruptions at all because all requests are handled by instance 1 until instance 2 is ready and starts listening).

So the first two options cause disruptions whereas the third one is fully zero-downtime and transparent to the clients.

My take on it would be:

  1. Start admin server as early as possible.
  2. Load schema cache.
  3. Start listening on main socket.

This would require splitting binding from listening on the main socket (ie. we need to bind without listening first so that we can pass the socket to the admin server).

@steve-chavez @develop7 thoughts?

@mkleczek
Copy link
Collaborator Author

Dependent on resolving (or having a workaround to) yesodweb/wai#853

@mkleczek mkleczek force-pushed the so-reuseport branch 2 times, most recently from 64bb6ca to 2cb6503 Compare March 11, 2026 18:49
@steve-chavez
Copy link
Member

So the first two options cause disruptions whereas the third one is fully zero-downtime and transparent to the clients.
My take on it would be:

Agree, sounds much better.

@mkleczek mkleczek force-pushed the so-reuseport branch 2 times, most recently from dbb615a to 7343823 Compare March 12, 2026 20:17
@steve-chavez
Copy link
Member

Start admin server as early as possible.
Load schema cache.
Start listening on main socket.

@mkleczek Q: why is the above dependent on yesodweb/wai#853? It looks like it can be done without it?

@mkleczek
Copy link
Collaborator Author

Start admin server as early as possible.
Load schema cache.
Start listening on main socket.

@mkleczek Q: why is the above dependent on yesodweb/wai#853? It looks like it can be done without it?

@steve-chavez - changing startup sequence is independent and can be done now (will raise a PR, it should be easy thing to do).

But this PR and prerequisite #4702 needs warp to be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Start Warp with SO_REUSEPORT on supporting platforms

3 participants