How to connect to a pool of servers in php?

The documentation code all assumes that the user will connect to just one machine, but what if that one machine goes down?

Is there a way to configure the connection string in PHP/PDO (for example) so that it knows to loop to a second or third machine if the first fails to respond?

So there are two slightly different issues to consider here:

  1. Connecting to a set of multiple known cockroach nodes directly at the client level for failover tolerance/performance.
  2. Being able to seamlessly take advantage of cluster changes (e.g. a cockroach cluster growing to more nodes, or migrating nodes), without needing clients to be aware.

You could configure clients that support connection pooling over multiple database URLs. This is client specific. While I have not worked with PHP, PHP PDO supports many underlying database drivers, and some of them do support load balancing over multiple database URLs.

Alternatively, you could also set up a proxy server (such as HAProxy) that provides a single connection URL for the client, and have HAProxy provide load balancing/failover resilience, or a DNS aliases with DNS round-robin load balancing. This way clients aren’t complicated.

We do have a goal to document some of these strategies, with example code in a few languages in Q1 2017.

Thank you. I think the HAProxy method is what I’m looking for. It didn’t occur to me that I could do it in that way.

I will wait for the documentation.

Hello, Are there documented examples of connecting to a pool of servers / auto discovery yet?

HAProxy is now included in our manual deployment docs

1 Like

OK Thanks. So there’s no plan to support this at the driver level ala MongoDB then?

I’ll probably try settingup some R/RW pools on our F5s.

Cheers,

Rhys

Hi Rhys,
I was interested in doing it using the same method as how MySQL MS is done in PHP.

But, the HAProxy method is actually quite easy to setup. The documentation makes a few assumptions, but it’s easy to do once you’ve read it a few times.

I use MongoDB as well, but how I manage that is that all the Mongo servers are connected to through two Mongo routers, as suggested in their own documentation.

You can treat the HAProxy in this case as being similar to the Mongo routers.

Not at this time; we are currently aiming for compatibility with existing PostgreSQL drivers instead of developing a new collection of cockroach-specific drivers. If there are PostgreSQL drivers out there that support connecting to multiple servers, those techniques should work for CockroachDB as well.