Why Use a Pool in Shiny Application?

In several places (1, 2) I find the following statement as to why I should use the pool package () to manage my database connections in a Shiny app:

Opening only one connection per app … cannot handle simultaneous requests (e.g. two sessions open, both querying the database at the same time);

My understanding of shiny apps is that they run in a single-threaded R process, hence there can never be two requests at the same time. Do I miss something here? Why would I want a pool of multiple connections per app if only a single one is used at any time anyways?

(I understand that a pool with a single connection may still be useful as the pool package handles automatic re-connection in case the connection drops.)

-- Thanks, David

2

1 Answer

Shiny server can serve multiple end-users with a single-threaded R process. Please see the diagram and description at the start of this article. The R process alternates which user it handles at any particular instant. These multiple users can all be making different requests to your DB during overlapping time intervals.

Suppose you have a block of code that makes several requests to the DB. You might think of this big block as a single thing to compute, but maybe the R process thinks it should pause in the middle and serve a different user.

3

Your Answer

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

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest