r/PHPhelp • • May 15 '23

Scaling a PHP application to use multiple servers. Good tutorials? Blogs? Books?

I'm starting from the beginning here. All I know how to do is create a php/mysql application on a single server. But I have zero experience in splitting it across multiple servers.

I've spent a couple of days reading up on things like load balancing, microservices, distributed transactions, and my head is spinning. I still don't really have the first clue how to implement any of it. I need a dummies' guide.

Can anyone recommend any easy to follow resources for splitting a php/mysql application across multiple servers?

There seem to be a lot of things to consider. Sharing data like uploaded files, sessions, replicating data between databases etc. I don't know how to do any of this. Are there any thorough tutorials that start from the ground, up? I honestly barely understand where to begin, no-one has taught this to me.

Sorry for the noob question. I've been doing php and mysql for 20 years and have never had to even consider this before. I have no practical idea how to split up an application, or if there are multiple ways, and what to choose.

8 Upvotes

5 comments sorted by

13

u/MateusAzevedo May 15 '23

From the PHP and your app point of view, you only need to centralize "shared data".

Database: you need a database server running on a server that's accessible from all your app servers. They can all connect to that same database, no need to worry about distributed transactions or anything (at least, not at the start, until you really need to scale).

Sessions: by default, PHP session data is stored in local files in the server. PHP can be configured to use another storage mechanism. A library can help here, since it can abstract the underlying storage behind a common interface (take a look at some framework documentation to understand more). Common options for storage include the database or some in memory storage like Redis (again, running in a central server).

Files (uploads): it's the same thing again. This could be a network mounted volume or, more common, a service like Amazon S3.

Basically, everything that needs to be available to all servers running your app, need to be in a separated server. This is the part related to PHP.

The rest then is a infrastructure concern not suitable to this sub. I'm not a DevOps guy myself, so I don't have any practical experience with this and can't recommend anything concrete. Maybe you can ask in /r/DevOps...

5

u/Innominate8 May 16 '23

PHP is particularly good for this. Some other popular web languages make it easy to build a monolith application that stores state. With PHP, we typically use databases or redis to store state.

Probably the most important starting point is to realize that different applications have different server needs. There are rules of thumb that make good starting points, but especially once you cross into using multiple servers, you'll have a lot more tuning to do, and only benchmarking and profiling your specific application will get the best results.

PHP itself is trivial here. Assuming your application only uses a database and PHP you can just copy the same code to multiple webservers, point it at the same database, and expect it to work. Redis is an excellent addition to replace smallish things commonly stored on disk, e.g. cache files, sessions, and is widely supported.

Where you will hit problems first will be the filesystem. Crossing over to multiple servers, using the local filesystem becomes impractical. Cache can't be kept on disk, or else different nodes risk having different cached data. Any kind of uploaded files needs to be somehow synced to other hosts. There's lots of practical ways to do this so I won't go into them but I'll touch on a few gotchas.

  • Source and static files are relatively easy, you can rsync them to the web hosts when changes are made and call it a day. Static files can also be kept in something like b2 or s3 to be used with a CDN. At the very least use CloudFlare to speed up the site and reduce the static file bandwidth charges. S3/CloudFront or B2/Fastly will help more but require more configuration.

  • User uploaded files are trickier. They need a writable area and should be visible on all web hosts asap. NFS is one good way to do this. Be aware NFS not particularly fast, it works fine for serving specific files but isn't great at stat() calls. A shared NFS directory for uploaded files works well, but a shared nfs directory for all of the source code and static files is unnecessary.

  • Synchronization. Locking will need to be done in the database or redis. flock() can no longer be counted on.

  • Cron. Easiest to just run on the database host.

  • Encryption. Be sure connections between your internal services are secure. Either in an AWS VPC, some other equivalent private network, or are using TLS connections when the open internet is involved.

0

u/ardicli2000 May 16 '23

Experience+knowledge rules...