My PHP Application Is Getting Really Popular, Now What?

An overview of how to horizontally scale a PHP application

Dirk Hoekstra
Nov 25, 2019 · 4 min read
Image for post
Image for post
Photo by Maria Molinero on Unsplash — Scaling up

PHP gets a lot of hate in my experience. So much hate that I’m almost afraid to say that I like the language. Version 7 solved a lot of issues and I specifically love the type-checking features that were added.

And, if you use the popular Laravel framework, all the ugly parts of PHP are hidden away and you can work with the beautiful libraries Laravel provides for you.

One argument that comes up a lot when I talk about PHP is scalability. There seems to be the notion that PHP applications don’t scale.

In this article, I will show you what is needed to scale up a PHP application.

Vertical Scaling

By far the easiest way to scale up your application is to buy a better server. Adding more CPUs and RAM will speed up your website, but it comes at a cost as buying a high-end server is expensive.

Furthermore, it is hard to dynamically scale your application this way. Suppose you have almost no users at night and around a million users during the day, then you have a very expensive server that you only use half of the time.

This method of scaling is called vertical scaling.

Horizontal Scaling

Almost all big web applications use horizontal scaling. This means adding more servers to scale up the website — instead of buying a better server.

Typically, there is a load balancer that will redirect users to one of the application servers.

It is important to separate the database from the application servers. That way, each application server will have access to the same database.

Image for post
Image for post

PHP Sessions

There is a problem with this method though. The server must be stateless.

PHP uses sessions to keep track of the user’s state. If a user logs in on Server 01 then that server knows the user is logged in. But if the load-balancer sends the next request to Server 02 then this server will not know that the user is logged in.

Solution 1. Store the session in the database

You can configure PHP sessions to store session data in the database.

However, this means that for each request, a database query must be made. This scales badly as most often the database is the performance bottleneck in large web applications.

Solution 2. Store the sessions in a Redis server

Redis is an extremely fast in-memory key-value data store. You can set up a Redis server that will store all PHP session data. Check out this article for a practical guide on how to do this.

Solution 3. Decouple your app into a back end and front end

You could choose to not use PHP sessions at all. A lot of websites have a stateless API as back end and use JSON web tokens as an authentication system.

This is the most scalable solution, in my opinion, as all the session management is done on the client's side.

Adding More Databases

Most applications perform more read queries than write queries. You can use this information to scale up your databases by adding read-only databases.

To keep the data consistent, these read-only databases sync with a single write-only database.

If you need to add more write-only databases, you enter the land of database sharding, which is very complicated and I would not recommend doing yourself. Check out this article if you want to learn more about sharding.

Handling Uploaded Files

If a user uploads a file to Server 01 then Server 02 will also need to know that the file was uploaded.

A solution could be to use Gluster. Gluster will automatically sync a filesystem over multiple nodes in a network.

Another solution could be to create a single object-storage server, the only purpose of this server is to store static files. As a bonus, you could create multiple object-storage servers and use Gluster to keep them in sync!

Cloud Computing

Successfully scaling up a web application is a lot of work. That is why most companies turn to cloud computing to scale their applications.

With the Google Cloud platform, you can spin up a Google Cloud database as your database server. Use Cloud Run to host your application server. Create a Google Storage bucket to host static files. And use Google Cloud Firestore as your key-value session server.

The amazing thing is that the Google Cloud platform will automatically dynamically scale your application. No work on your part required!

Conclusion

Hopefully, this article gave a good overview of the different components needed to scale a web application. The easiest way to do it is to use a cloud provider and let them handle the scaling for you!

But, it can definitely be fun to set up the components yourself.

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store