Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database connection pool strategy for micro services

We are trying to convert our monolithic application to a micro services based architecture. We use Postgresql as one of our database in the monolithic application with BoneCP for connection pooling.

When this monolith is split to a number of independent micro-services with each of them running in a different JVM, I can think about two options for connection pooling

  1. BoneCP or any decent connection pool for each microservice - My initial research shows that this is the primary choice. It is possible to have a fine grained control of connection requirements for each service.But, down side is that as the number of services increase, number of connection pool also increases and eventually there will be too many idle connections assuming that minimum connections in each pool is greater than 0.
  2. Rely on database specific extensions like PGBouncer - This approach has the advantage that connection pool is managed by a central source rather than a pool for each micro service and hence number of idle connections can be brought down. It is also language/technology agnostic. Down side is that these extensions are database specific and some of the functionalities in JDBC may not work. For eg: Prepared statments may not work with PGBouncer in Transaction_Pooling mode.

In our case most of the micro-services(at least 50) will be connecting to the same Postgres server even though the database can be different. So, if we go with option 1, there is a higher chance of creating too many idle connections.The traffic to most of our services are very moderate and the rationale behind moving to micro-service is for easier deployment, scaling etc.

Has anyone faced a similar problem while adopting micro-services architecture? Is there a better way of solving this problem in micro-service world?

like image 363
Justin Jose Avatar asked Mar 18 '16 11:03

Justin Jose


Video Answer


2 Answers

Maybe group some smaller number of microservices into modulith and use karaf, or other osgi container as a runtime for them. Then you can create bundle that will represent a connection-pool for your database so other bundles — microservices can use it. But I'm not sure if it will solve your architecture problem.

like image 132
jakubM Avatar answered Sep 18 '22 15:09

jakubM


I don't see how pgbouncer will solve any of the problems you would have with the first approach. There are many reasons to use pgbouncer but I don't think they are really applicable here.

Also, in my experience, while idle connections can be an issue, they probably will not be on the scale you are talking about. I mean we are not talking hundreds of idle connections right?

More critically, one key thing that a microservices approach would give you is an ability to move dbs off to other servers. If you do this, then having your connection pool centrally managed makes this harder to do.

Per-service pool is generally more flexible and it makes your infrastructure quite a bit more flexible too.

like image 43
Chris Travers Avatar answered Sep 16 '22 15:09

Chris Travers