How to improve the performance of your PHP app ?
This a day-to-day question that every PHP developer should ask. Someone asked me that few days ago. I made an answer, but it was not totally complete. I would like to try to make a complete one (or an overview) here.
First, your server and project’s dependencies should ALWAYS be updated.
For some developers, this is obvious. But in my career, i met some developers that don’t agree with that statement. Sometimes for good reasons (several projects on one server — you don’t have time in order to update every projects, code compatibility, etc.)…
Every PHP release try to improve the server performance. The last release from PHP (7) improves a lot the performance from your server and your code interpretation.
Using the last version of PHP, you can use all latest features and can improve your app’s performance.
Update your project’s dependencies can be more difficult because it can introduce some bugs. But using a changelog from your open source dependency, it can save time and your app performance too. For a long term app, it can be benefic for you and your company.
Horizontally scale your server architecture
If you have visitors from all around the world, you should choose a solution to split connections to several servers.
For European people, you can provide a server from an european country (like Ireland, France or Germany). the server response will be faster than a server located in the US. All big companies have several data centers all around the world (Apple, Facebook, Google). Cloud solutions can provide you to choose your server location.
You can use several caches to improve performance of your PHP App :
- Database’s cache:
Every database can have a cache, just enable it!
- Memcached, Redis, etc. :
You can cache datas and get them quickly later. Memcached, for exemple, can do that easily in PHP.
- Your ORM/Framework/CMS Cache
Doctrine can use cache, Symfony Drupal or Wordpress have their own caching system, you should enable it and use it properly.
- Proxy Cache
You should use varnish cache to cache the server response.
- Browser cache
You can tell to the browser to keep some data in their cache (images, css, JS) and some page. You can say to the browser to keep some page for 1h or more. It will display the cache page during the next hour, and ask later the server to know if the page change.
Nowadays, it’s something that avery one is talking about. It can improve your performance.
Do you need to send a confirmation email, or an alert email just during the confirmation page execution?
Do you need to generate a big ZIP file, or can you let the user know that is on its way?
In your PHP code, you certainly have several code block that can be executed later because it’s not an urgent task. Asynchronous PHP can help you to do that.
A client is confirming his basket. During the confirmation, you have to check your bank account to know if the payment is OK, send a confirmation email, generate a bill, send an email to the admin, an email for your command preparation service or whatever. All those tasks can be very awful for your server performance. You can improve just checking the payment, and making your e-mails and the PDF generation later.
You can do that with some tools like RabbitMQ, ReactPHP, etc. You need to know what is important task and what is a secondary task and do it later.
There’s a lot of solutions to improve your app performance, you need to take time to think what is necessary to implement depending your needs.