What's up with max_db_connections?

Lately, I've been seeing a lot of people set the max_db_connections parameter to something very high. Don't do that!

Let me repeat: do not change max_db_connections unless you know what you are doing.

The long story: sometimes when you find that DBMail doesn't work as expected, you are tempted to turn a couple of knobs, change some parameters, see how that helps. For some reason the max_db_connections parameter looks too attractive not to touch.

This is mostly due to an apparently common misperception: max_db_connections is not about the number of concurrent clients you are able to handle. It's strictly about the number of concurrent database connections you are allowed to use. Libzdb is a very efficient database connection-pool. Connections in the pool are shared between all concurrently connected clients, as needed. Libzdb will tune-down the number of connections to the database if only a few are needed. No immediate risk of a DOS attack on your database there. But DBMail does create a pool of worker threads the size of - max_db_connections.

DBMail's IMAP server uses a pool of worker threads to handle communication with the database. IMAP often requires queries that take a non-trivial amount of time, and we don't want to block the main thread - which talks to your clients - while some query is in progress. So we offload that to worker threads. The size of the worker thread-pool is fixed so every possible database connection can operate in the context of it's own worker thread.

This means that when you set max_db_connections to say 100, you will create 100 threads, and something like 5 database connections. How work is distributed over the pool of threads is up to GLib, but most threads will be idle most - if not all - of the time. However, what this does to your CPU is potentially quite bad. Just google for context-switching, or run 'vmstat 1' on your DBMail machine.

The only situation where you want to increase the max_db_connections parameter is when you see that all database connections in the libzdb pool are busy most of the time. An obvious symptom is when you see lots of

Thread is having trouble obtaining a database connection. Try [1]

especially when the value between brackets is higher than 1

Trackbacks

    No Trackbacks

Comments

Display comments as (Linear | Threaded)

    No comments


The author does not allow comments to this entry