Hey! 👋

Whether you’re an experienced PHP developer, a beginner just starting your journey, or an enthusiast interested in understanding more about the world of PHP, this is the place for you. We are a community dedicated to sharing knowledge, discussing new trends, and solving problems related to PHP.

As members of this community, we expect everyone to respect each other and foster a positive environment. Please make sure your posts are relevant to PHP, and remember to be kind and considerate in your discussions. Let’s learn from each other and help each other grow.

Whether you’re here to ask questions or to share your knowledge, we’re excited to have you here. Let’s make the most of this community together!

Welcome to /c/php! 🐘

  • msage@programming.dev
    link
    fedilink
    arrow-up
    0
    ·
    2 years ago

    Can anyone here help me understand persistent PDO connections?

    So I set up Patroni cluster, everything works as expected. But during the failover/switchover PHP remembers connections to the failed node. And I can’t figure out why. Reloading FPM is kind of PitA.

    I will use PgBouncer. But I still would like to know how PHP works with the persistent connections.

    • thgs@beehaw.org
      link
      fedilink
      arrow-up
      0
      ·
      2 years ago

      I haven’t used persistent connections although I have been tempted in the past. I believe, if you haven’t used it before, it might come with more trouble than it solves.

      As an alternative I could propose using amphp (or maybe react PHP) which will let you handle a pool of connections in a single long running process. But it’s a bigger change really, the more I think of it.

      • msage@programming.dev
        link
        fedilink
        arrow-up
        0
        ·
        2 years ago

        Yeah, but any ‘single server’ PHP defeats the purpose of PHP, so I’m not a huge fan.

        I’ve used them in the past, and they work great, until you have to switchover to another node.

        • thgs@beehaw.org
          link
          fedilink
          arrow-up
          0
          ·
          2 years ago

          Fair point.

          However why do you need persistent connections ? I am thinking that the growing rate of the connections should be very low as the instances increase, given that the queries are quick.

          • msage@programming.dev
            link
            fedilink
            arrow-up
            0
            ·
            2 years ago

            You lose about 5-6ms just connecting to the database. Keeping them open helps a lot.

            My goal is to write as simple code as possible, and everything else is super quick, just the connections aren’t handled well in PHP. Which is a shame.

            • thgs@beehaw.org
              link
              fedilink
              arrow-up
              0
              ·
              2 years ago

              It’s not that there isn’t the option, it’s just that I don’t know how to help you. MySQL has an option to reconnect, I suppose might be the same for postgres?

              The single running process that was so easily dismissed, could save tons of queries, for example! Sorry I keep thinking about that direction

              • msage@programming.dev
                link
                fedilink
                arrow-up
                1
                ·
                2 years ago

                Single process doesn’t save any queries, no idea what you mean.

                Persistent connections persist between requests just like in a single process. It’s just that pool handling is hidden in PDO.

                • thgs@beehaw.org
                  link
                  fedilink
                  arrow-up
                  1
                  ·
                  2 years ago

                  Also how’s the setup? You setup for example 5 max children in fpm and 5 persistent connections? Per server? So your overall connections to the db server will be 5x your server instances?

                  If you setup 5 fpm children and less connections, one child will eventually reuse from another, but only when the connection is free (does not do a query for another process or pdo does not consume a resultset). If it tries to do a query at that time it will have to wait and it will block. This is my understanding. Also how you do transactions with persistent connections?

                  This has evolved into such an interesting conversation.

                  • msage@programming.dev
                    link
                    fedilink
                    arrow-up
                    2
                    ·
                    2 years ago

                    From my current understanding, there is no pool, just one process keeps and reuses one database handle over and over again.

                    And it’s not PDO, but the driver, which handles that.

                    Transactions are handled within try/finally blocks. You can reset the DB connection, but it’s not free in terms of time. You get more performance making sure code doesn’t leak open transactions.

              • thgs@beehaw.org
                link
                fedilink
                arrow-up
                0
                ·
                2 years ago

                Also, to work with persistent connections you will have to have a pool right? Because when you query from instance 1, the connection is not available until you consume the result set. Or is that only for MySQL?

    • mbd@programming.devOPM
      link
      fedilink
      arrow-up
      0
      ·
      2 years ago

      Honestly I don’t know much about this but I think this can occur because PHP (or more specifically, the PDO extension) isn’t necessarily aware of the health of the PostgreSQL node it’s connected to. From PHP’s perspective, it opened a connection, and unless explicitly told otherwise, it will assume the connection remains open. The Patroni switchover/failover is happening independently of PHP. PgBouncer is probably a good solution for this.