🚀 My Eloquent Performance Patterns course is now available! Find it here.
Jonathan
Reinink

Your database is a shared resource

Posted on April 7, 2020

As web developers, we have the tendency to think of every two requests as largely independent of one another. If one request is slow, it's unlikely to affect the other...right?

That's very much not the case with your database.

Your database must run whatever queries are fired at it, collectively. And it has limits (connections, CPU, memory). It can only run so many queries at a time. Meaning every endpoint in your app matters from a database performance perspective.

Even just one poorly designed endpoint can hurt the performance of the whole system.

So, be careful with thinking "it's okay if this page in my app is slow". Your admin dashboard might only be used by support staff, but your database isn't. One slow page can strain your database, affecting the performance of your entire app.

Treat your database as a limited resource that's shared by all your users. Every query you run is using part of this finite resource. Run too many, and it will crash.

Use monitoring software to determine which queries are using the most resources. Find those running the most frequently and the slowest. These are the worst offenders. Fix them first.

I use Heroku, and they provide the following "expensive queries" metric automatically:

  • Most time consuming
  • Most frequently invoked
  • Slowest execution time
  • Slowest I/O

New Relic also provides excellent database metrics, and there are other tools available.

Remember, any query you can make faster, or avoid entirely, will help the performance of the whole system. Every query matters!

Premium video course

Hey there! If you found this article helpful, be sure to check out my Eloquent Performance Patterns video course. You’ll learn how to drastically improve the performance of your Laravel applications by pushing more work to the database, all while still using the Eloquent ORM. Plus, you’ll support me, so I can continue putting out free content like this! 😊