phoenix icon indicating copy to clipboard operation
phoenix copied to clipboard

WorkerMode with Primary Replica Database will not switch back to replica connection

Open sad270 opened this issue 4 weeks ago • 1 comments

Bug Report

Summary

When using FrankenPHP in Worker mode with a Primary / Read Replica connection. If in a http request we do an write action in database, the connection will switch to primary connection and doctrine will switch back to replica on a new http request.

There is a closed issue in Franken PHP repo https://github.com/php/frankenphp/issues/1444 but IMO adding a $this->connection->ensureConnectedToReplica(); in the start or the end of each request is a workaround not a fix.

As a developer, I expect that, each new HTTP request will reset the connection and we will be on replica. Like when we are not in worker mode.

Current behavior

When cennection switch to primary, it stay connected to Primary on each new HTTP request

Expected behavior

Each new HTTP request will be connected to replica even if previous http request did a write action in database

How to reproduce

  • Run a FrankenPHP server in Worker Mode with a Symfony Application.
  • Configure a postgresql server with a read replica
  • Add 2 route, one with a SELECT the other with a INSERT statement
  • call the endpoint with the INSERT statement
  • call the endpoint with the SELECT statement
  • look primary postgresql logs, you will see the select statement.

sad270 avatar Dec 03 '25 16:12 sad270

If you don't close the connection between requests, that's quite expected actually.

right now, DoctrineBundle has special logic to clear entity managers and close connections on shutdown, which was added to solve issues in functional tests when performing hard reboots (which used to be the case before). This might be an indication that those should be hooked in the ServicesResetter as well (which would then work for the worker mode and for messenger workers). I'll investigate that as there might be some side-effects (I'm thinking about the case of the DoctrineTransport for Messenger).

stof avatar Dec 03 '25 17:12 stof