Skip to content

Consider connecting to multiple hosts in parallel instead of sequentially #3610

Description

@roji

When multiple hosts are specified on the connection string (#732), we first try to obtain an idle connector, and if we fail, we try to open a new physical connector (and then wait if all pools are exhausted). This means that if one server is down, we are stuck for the duration of the timeout.

It has been proposed to attempt connecting in parallel - this would allow us to return the first successful connection immediately, and not block the application.

For the other connections, we can either discard them, or just put them into their pools as idle connections, subject to the regular MaxPoolSize and pruning logic. This means that we potentially consume more server resources than is needed; however, opening a new physical connection is typically going to be a rare event etc.

/cc @vonzshik @mintsoft

Activity

  1. added this to the vNext milestone on Mar 21, 2021
  2. mintsoft commented on Mar 21, 2021

    @mintsoft
    Contributor

    Just to add information around this, the SQL Server driver handles this scenario with a rather weird "fast fail" mechanism. They have a hardcoded initial connection timeout of 25% (I think) of the total connection timeout on the first connection in a failover configuration (I can't find the code right now, but I'll reference it when I can find it).

    The comments around it suggest it's for exactly this situation, i.e. the application shouldn't feel any pain when the "primary" server is down, if the servers are supposed to hot-failover

  3. roji commented on Mar 21, 2021

    @roji
    MemberAuthor

    @mintsoft thanks. The SqlClient behavior you describe sounds related to #3607 (comment), which is about whether the timeout specified on the connection string is for the total open attempt (across all servers), or for each one.

    The two questions - whether the timeout is global or per-host, and whether to connect in parallel - are related though ultimately are probably orthogonal... Even if the timeout is per-host, it could still be desirable to not make the application hang for 50% of the configured timeout just because a server is down.

  4. mintsoft commented on Mar 21, 2021

    @mintsoft
    Contributor

    Yeah agreed, The "best" behaviour (in my eyes anyway) is to achieve a successful connection to the right type of server in the shortest possible duration, measured from the perspective of the client application.

    To add to my previous information, the way the SQLClient (i.e. SQLServer) deals with this is to do no parallelism at all, however it has this "fast fail" timeout concept.

    I've just found my previous internal post about our last set of problems with that so I'm armed with gory details for that now.

    The "fast failover" effectively shortens the connection timeout on the primary server to 0.08 (ADP.FailoverTimeoutStep) of the actually defined Connection Timeout (https://referencesource.microsoft.com/#System.Data/System/Data/SqlClient/SqlInternalConnectionTds.cs,1745) with the default connection timeout (15 seconds), this leads to an initial connection timeout of 1.2 seconds. Basically this means that if the first packet sent to the SQL Server doesn't get a reply in 1.2 seconds then the driver will attempt to connect to the secondary. This in itself seems perfectly reasonable, however the core issue with this is that windows won't resend a TCP packet until it's been 3 seconds since it was transmitted without response. So essentially if any network equipment drops the first packet it will cause the driver to attempt to connect to the failover server and throw that error. The workaround we've tested so far is to bump the Connection Timeout up so that the 0.08 * value is just over 3 seconds, thus giving the TCP stack the opportunity to retransmit the initial packet. That worked, but it's now obscene because we have a "connection timeout" set to about 40 seconds which is about 35 seconds longer than we really want.

    I'd say that approach is pretty poor for 2 reasons:

    • the consuming application has no control over the 0.08 proportion of the initial host timeout
    • even with this failure, if the first server in the list is unavailable (i.e. timing out, rather than rejecting the connection) then you still see a pause for every new connection

    Also, that behaviour is undocumented and if you disable connectionPooling it does something completely different which makes it very difficult to debug.

  5. roji commented on Mar 24, 2021

    @roji
    MemberAuthor

    A small note on what I wrote earlier:

    The two questions - whether the timeout is global or per-host, and whether to connect in parallel - are related though ultimately are probably orthogonal... Even if the timeout is per-host, it could still be desirable to not make the application hang for 50% of the configured timeout just because a server is down.

    Thinking about this again, if connections are made in parallel, there's obviously no difference between per-host or global timeout...

    @mintsoft I agree that the SqlClient approach doesn't seem very robust. However, although I'm leaning towards connecting in parallel, I'm also not sure that it's appropriate for all scenarios - so this may need to be an opt-in/opt-out behavior. There's notably the question of a loadBalanceHosts option similar to what pgJDBC supports, which controls whether we try hosts in the order in which they're specified (loadBalanceHosts=false), or in round-robbin/random order (loadBalanceHosts=true). Note that in pgJDBC the former is the default, and unless I'm mistaken libpq itself only supports by-order connecting; and by-order necessarily implies connecting in serial.

  6. roji commented on Mar 24, 2021

    @roji
    MemberAuthor

    Note also that connecting in parallel can cause quite a thundering herd effect if there are multiple application servers running Npgsql, as each one tries to physically connect to all PostgreSQL backends in parallel.

  7. modified the milestones: 6.0.0, 7.0.0, Backlog on Oct 5, 2021
  8. added theissue type on Jun 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions