Search code examples
phpmysqlslim-3

how Slim 3 handles multiple connections to MySql


I'm using the Slim-Skeleton to setup an API.

If I have 300 people hit a Slim 3 API endpoint, how does slim handle the database connections?

Does it open a connection for each user? And are the connections closed once data is returned?

Cheers Phil


Solution

  • Slim 3 doesn't itself handle database connections. Instead, you need to create a bit of code to do that, either using PDO or mysqli (for MySQL). Look for tutorials and examples on the intertoobz.

    Both those database access libraries support connection pooling. So, when you have a whole mess of users hit your slim app, each request handler grabs a shared connection to your database, uses it, and then releases it. If the pool of unused connections gets empty, request handlers will wait, queued up, until a connection is available. All this queuing, grabbing, and releasing is built into the access library.

    Unless your requests take a very long time to satisfy, a modest maximum number of connections in the pool--perhaps 10--will serve you very well. (If your requests take a very long time to satisfy, 300 active users will give you more problems than connection pooling.)