Live data from Hacker News

About Database Connection Pool Sizing

github.com

11–20 of 32 posts

Re: About Database Connection Pool Sizing

#11
post #8
post #5

This article would be better if it was called "connection pool sizing for MySQL". If you had a better database, you would have to worry way less about configuring pool size on the clients. The proper way to do this would be for the server (i.e. database) itself to have maximum active transaction limits and ways to setup quotas for different use cases, especially as your company gets large, and you have many different…

What other database do that out of the box without third party tool?

[deleted]

Re: About Database Connection Pool Sizing

#12
post #5

This article would be better if it was called "connection pool sizing for MySQL". If you had a better database, you would have to worry way less about configuring pool size on the clients. The proper way to do this would be for the server (i.e. database) itself to have maximum active transaction limits and ways to setup quotas for different use cases, especially as your company gets large, and you have many different…

The article showcases Postgres benchmarks and quotes the Postgres documentation, and references material put out by Oracle for their flagship database product. MySQL is never mentioned once. Your opening sentence is deeply misleading as to the contents of this article.

Re: About Database Connection Pool Sizing

#13
The other half of this is correctly using the connection pool in the application layer to begin with.

One of the most common performance errors I see in random http applications is grabbing db connections for too long, like in middleware, or too aggressively in parallel queries.

Re: About Database Connection Pool Sizing

#14
post #5

This article would be better if it was called "connection pool sizing for MySQL". If you had a better database, you would have to worry way less about configuring pool size on the clients. The proper way to do this would be for the server (i.e. database) itself to have maximum active transaction limits and ways to setup quotas for different use cases, especially as your company gets large, and you have many different…

What are you referring to in your 3rd paragraph? Idle connections in MySQL pose few issues. They just take up some memory for session-level buffers, and that amount of memory depends on what you've configured those buffer sizes to. They also take up a slot in terms of whatever you've configured max_connections to, but that's fully configurable as well.

MySQL has long had some abilities to limit resources on a per-user basis, see https://dev.mysql.com/doc/refman/5.6/en/user-resources.html for example. This includes the ability to set max simultaneous connections per user.

MySQL's default connection model dynamically uses a thread per connection, which actually tends to handle high connection counts out-of-the-box better than process-per-conn approaches like Postgres's. In my experience, using a proxy like pgbouncer is much more common in Postgres than using a proxy is in MySQL.

I'm not bashing Postgres overall, it's a great DB. But in terms of connection-handling your criticism of MySQL here feels substantially off-the-mark.

(Source: have been using MySQL for 15 years, including at largest scale in the world)

Re: About Database Connection Pool Sizing

#15
post #10
post #2

Alternatively, and specifically for PostgreSQL, if you can live with pgbouncer's constraints then you should be using it. It effectively self-manages an optimal shared connection pool for all of the application processes that connect to it.

I would like to dissent. If your application is long lived you will get more benefit using the pooling functionality provided by the postgresql client libraries for your platform/language. pgbouncer is an intimidatory, and thus another thing to break- it has it's own constraints (mainly things like prepared statements will block a connection/cannot be reused) and its application should be well understood. For instanc…

Coming from ActiveRecord gem, pgbouncer has been great. I'd say the real advantage is transaction level pooling, which will free up connections when they're not actively in a transaction (vs request level isolation). I don't see how this is possible across a large number of isolated servers without a cooperating agent like pgbouncer.

I would advocate for pgbouncer at the DB ingress level and your languages connection pooler at the application level.

Re: About Database Connection Pool Sizing

#16

The other half of this is correctly using the connection pool in the application layer to begin with. One of the most common performance errors I see in random http applications is grabbing db connections for too long, like in middleware, or too aggressively in parallel queries.

This is so true and easy to overlook until a post mortem. A great exercise is to audit how connections are maintained in your application, before it's too late.

Re: About Database Connection Pool Sizing

#17
Why is that Oracle instance using a max thread pool size of 2048 on a 12-core host? That seems like the real problem. Why not configure a smaller thread pool like cores * 2, and do more queuing on the server?

It seems wrong IMO to expect clients to tune connection pools based on how much concurrency they think a server can productively handle. The client doesn't necessarily know how many cores the server has, or more importantly, how many other clients are connected.

In HTTP/2, clients can configure a maximum number of streams (SETTINGS_MAX_CONCURRENT_STREAMS), and the RFC recommends setting it "no smaller than 100". If servers worked on all pending requests concurrently, a value of 100 would be unreasonably large. But the implicit expectation is that servers will decide for themselves how many requests to process concurrently, and queue the rest. In other words, SETTINGS_MAX_CONCURRENT_STREAMS is intended to limit the server's queue size, not its thread pool size.

Re: About Database Connection Pool Sizing

#18
post #5

This article would be better if it was called "connection pool sizing for MySQL". If you had a better database, you would have to worry way less about configuring pool size on the clients. The proper way to do this would be for the server (i.e. database) itself to have maximum active transaction limits and ways to setup quotas for different use cases, especially as your company gets large, and you have many different…

What are you referring to in your 3rd paragraph? Idle connections in MySQL pose few issues. They just take up some memory for session-level buffers, and that amount of memory depends on what you've configured those buffer sizes to. They also take up a slot in terms of whatever you've configured max_connections to, but that's fully configurable as well. MySQL has long had some abilities to limit resources on a per-use…

Start of every new transaction does a scan of all connected sessions (including idle) ones. I am pretty sure this is true in 5.6 too, and most likely in newer versions too. So if the queries that you are issuing are large or the total connection count is 100k you will definitely have significant perf issues because of the idle connections. 100k is a big number for sure, but it is definitely possible to hit those limits if the application layer is done in languages like node, ruby, python, when you need to run many application processes since each app process can't properly utilize more than single CPU core. Thus you end up having a lot of separate processes each with its own connection pool to the backing database.

As for per-user limits, those resource limits aren't that useful for stability. Setting max simultaneous connections is not as useful, because you don't know how many of those are idle or active. You want limits on active sessions that are actually doing work, not how many idle sessions exist. Unless of course you plan on not doing any session reuse, and always make a new session for each transaction, which will lead to huge other set of performance issues because establishing new sessions is pretty expensive.

As for the other "per hour" limits, they are also not useful to provide protections against burst traffic which is a common way a MySQL instance can enter into a bad feedback loop and slow down to crawl. (Example: there is a burst traffic from one use case, creating lots of new active transactions at the same time, because of that, MySQL perf slows down, so now you have even more active transactions because everything is slower, which leads to even more slow down, so even after the burst traffic is over, database is in a bad state since now you continuously have too many active transactions and it is unable to recover on its own to handle the same steady state traffic as it was able to handle before the burst).

Re: About Database Connection Pool Sizing

#19

Earlier quoted context omitted.

What are you referring to in your 3rd paragraph? Idle connections in MySQL pose few issues. They just take up some memory for session-level buffers, and that amount of memory depends on what you've configured those buffer sizes to. They also take up a slot in terms of whatever you've configured max_connections to, but that's fully configurable as well. MySQL has long had some abilities to limit resources on a per-use…

Start of every new transaction does a scan of all connected sessions (including idle) ones. I am pretty sure this is true in 5.6 too, and most likely in newer versions too. So if the queries that you are issuing are large or the total connection count is 100k you will definitely have significant perf issues because of the idle connections. 100k is a big number for sure, but it is definitely possible to hit those limi…

In my experience, many high-scale MySQL configurations use a global max_connections in the single-digit thousands (4000-5000 is common at social networks doing high-volume OLTP), and an aggressive wait_timeout (~10 seconds) to kill idle conns from misbehaving/stalled clients.

I've never seen a max_connections configured anywhere near 100k. That would be an extreme edge-case, and is generally unwise unless there's some very specific unusual reason that you need that. My assumption would be something is very wrong at the application architecture level if this is needed.

What "scan of all connected sessions" are you referring to? I've never heard anything about this, and never seen general performance issues purely related to high idle connection count, but I've also never configured max_connections to such an insanely high value.

As for other databases, I don't see how Postgres would be able to handle 100k connections (without a proxy) either, given that means 100k OS processes in Postgres.

> always make a new session for each transaction, which will lead to huge other set of performance issues because establishing new sessions is pretty expensive

Fire-and-forget (connection per web request) is somewhat common in MySQL, especially with languages like PHP. Establishing a connection is relatively low overhead on the server side in MySQL, since it just involves spawning a thread. Usually the bigger issue is network latency, especially if cross-region SSL connections are involved. In this case, a client-side connection pool or proxy certainly makes sense, and that is true regardless of what DB technology is used.

It sounds like you have a lot of application servers maintaining connection pools to a single database. In this case it is generally beneficial to tune the connection pools to aggressively prune idle connections, or use proxies that multiplex connections (ProxySQL is great), and then set max_connections to a more sane value as a circuit-breaker.

Re: About Database Connection Pool Sizing

#20
post #16

The other half of this is correctly using the connection pool in the application layer to begin with. One of the most common performance errors I see in random http applications is grabbing db connections for too long, like in middleware, or too aggressively in parallel queries.

This is so true and easy to overlook until a post mortem. A great exercise is to audit how connections are maintained in your application, before it's too late.

I'm reminded of a systems failure described in, IIRC, the O'Reilly book "Release It!". Java ODBC exception swallowed resulted in eventually running out of connections.

Very tough problem to troubleshoot without extensive monitoring.

Post reply on HN