1

I am connecting to a remote MySQL database in my node.js server code for a web app. Is there any advantage to using a connection pool when I only have a single instance of a node.js application server running?

1 Answer 1

1

Connection pools are per instance of application. When you connect to the db, you are doing it from that particular instance and hence the pool is in the scope of that instance. The advantage of creating a pool is that you don't create / close connections very often, as this is, in general, a very expensive process. Rather, you maintain a set of connections open, in idle state, ready to be used if there is a need.

Update

In node there is async.parallel() construct which allows you to launch a set of tasks in async manner. Immagine that those tasks represent each one a single query. If you have a single connection to use, each process should use that same one, and it will quickly become a bottelneck. Instead, if you have a pool of available connections, each task can use a separate connection until the pool is completely used. Check this for more detailed reference.

Sign up to request clarification or add additional context in comments.

3 Comments

I think I am misunderstanding something. If I were to connect to the database with a single persistent connection I would only create the connection once, and never close it. In that situation how is a connection pool advantageous?
What about having a multi-threadead application where, for instance, you have a client where multiple users can perform opperations on the database in concurrent manner?
That makes sense, but if I am using node.js, which is single threaded, would this provide any advantage? Additionally, as I understand it the database performs write operations synchronously to prevent overwriting/corruption.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.