David K
David K

Reputation: 78

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 (https://github.com/rstudio/pool) 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

Upvotes: 4

Views: 593

Answers (1)

Michael Dewar
Michael Dewar

Reputation: 3258

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.

Upvotes: 2

Related Questions