Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,761 members, 7,817,103 topics. Date: Saturday, 04 May 2024 at 05:41 AM

Scaling PHP Apps, How To? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Scaling PHP Apps, How To? (2249 Views)

(2) (3) (4)

(1) (Reply) (Go Down)

Scaling PHP Apps, How To? by DharkPoet(m): 8:49am On Oct 14, 2017
Hi fellow devs, so I've been coding for sometime now, and one thing I've always failed to cater for was consideration for scaling. How to set up infrastructure for scaling.

Should one consider scaling from the onset, or should one start thinking of scaling only when the requests grow to (tens of)thousands of req/sec.

What are the best ways to effectively achieve scaling a PHP app on a LAMP infrastructure?

I would really love to get your feedback.

Thanks.
Re: Scaling PHP Apps, How To? by Nobody: 11:46am On Oct 16, 2017
It depends on budget and time, if you're building an app that needs to get to the market as quickly as possible, then it's okay to leave scalability till when it is needed but however your code base should be optimized, you should move to a VPS and fine tune your application so you can be certain you've done everything app wise to make sure your application is a bit scalable, then you'd want to enable caching for static pages on your web app and also serve static pages via an Nginx server (its faster cor static pages) and also add a simple load balancer in front of your Servers... This little technique I stated can help your app survive it's first few thousand users...when you get to the next slow point you'd know what to do...
You may want to consider a separate database for reading data and another database for writing data, a data recycle action will be performed by your database platform..this technique can buy you some more breathing space...

Next you may consider either horizontal or vertical scaling...
Vertical scaling is simply adding more powerful hardware resources as the need to scale increases. This seems like the way to go for the lazy and rich programmer who doesn't want anything too complicated. Simply upgrade your resources and the app scales well.
Imagine having a server serving the web app in question. The server has 4GB of RAM, an i5 CPU, and a 1TB HDD. It performs its function well, but to better tolerate a higher influx of traffic, you decide to replace the 4GB of RAM with 16GB, you put in an i7 CPU, and you add a PCIe SSD/HDD Hybrid drive. The server is now much more powerful and can handle a higher load. This is known as vertical scaling

Horizontal scaling means adding more zombie servers and distributing requests through those servers, the server with lesser load receives the next request and once it can no longer handle any more requests another server picks up... This option is the go to option for the developer that can't afford to horizontally scale his app or thinks he'll benefit more from vertical scaling.

However, the best thing is to scale both Horizontally and Vertically. The need for your app to scale means that you have lots of users which means you're making money and so affording to scale shouldn't be a problem. The problem is people try to scale apps that may not even last two years on the internet.

DharkPoet:
Hi fellow devs, so I've been coding for sometime now, and one thing I've always failed to cater for was consideration for scaling. How to set up infrastructure for scaling.

Should one consider scaling from the onset, or should one start thinking of scaling only when the requests grow to (tens of)thousands of req/sec.

What are the best ways to effectively achieve scaling a PHP app on a LAMP infrastructure?

I would really love to get your feedback.

Thanks.

4 Likes

Re: Scaling PHP Apps, How To? by bearnana(f): 4:22pm On Oct 16, 2017
I don't think scalability of applications is much about performance.

Its more about how to manage, if a lot of hands were to get on deck.

To explain my point I'd share a personal experience since you are talking PHP:
I worked on an application where the devs who handled the code before used procedural and the client wanted to build a Software Department to manage the application. It was difficult to understand the application and the variable types the devs before had done. So we were tasked to build the application from ground up in a very short space of time.

Some rules I have learnt in my short time on this coded planet.

1: Your application won't scale if it wasn't designed to scale. i.e. Don't use procedural if your application would one day have a very large number of users and would have a software department.

2: Simple Architecture is good if you are starting i.e Try to optimize your code base for speed (this is different fro performance; your code performs and your code performs fast are two different things. Every codebase is designed to perform, not all codebase is designed to perform fast...)

3: Think about database type. Since you mentioned LAMPP, I am assuming that you are using MYSQL for backend. It may not be as important as the first two rules when starting out but you would have to consider database engine type ISAM, MyISAM, InnoDB.

These are the ones I can think off the top of my head. The other ones are when you have reached about 500k users. You might want to think of load balancers and redundant firewalls, maybe more webservers for performance. By then you would also have to factor splitting your database read and writes. Before you get to this stage, there should be enough money to hire me grin grin to help you out...

But first off, if you are thinking of scaling at this early stage, THINK OF YOUR CODEBASE!!!

1 Like

Re: Scaling PHP Apps, How To? by Nobody: 5:12pm On Oct 16, 2017
Nope, think of your user base..
Nice write up, I only disagree with the listing of MyIsam/InnoDB as database types, those are database engines not types. To even scale properly MySql should not be considered an option for database type, even if you're to use a RDBMS, consider PostgreSql as it has been chosen as database of choice by Oracle, owners of MySql.

And also, you're managing your resources to achieve better performance . That's scaling.

Welcome to Nairaland @bearnana


bearnana:

But first off, if you are thinking of scaling at this early stage, THINK OF YOUR CODEBASE!!!
Re: Scaling PHP Apps, How To? by bearnana(f): 5:31pm On Oct 16, 2017
DanielTheGeek:
Nope, think of your user base..
Nice write up, I only disagree with the listing of MyIsam/InnoDB as database types, those are database engines not types.

Welcome to Nairaland @bearnana



I think I mentioned that MyIsam/InnoDB are database engine types.
If you don't consider your codebase I wonder how developers would work on it to manage better performance.

Thanks for the welcome. smiley
Re: Scaling PHP Apps, How To? by simiolu1(m): 8:11pm On Oct 16, 2017
First off, scaling is majorly about 3 things

1. Your codebase.

Is your software efficient? Can you squeeze a little more juice out of it by refactoring? Are you using queues in the right places? Anything that can be delayed until a later time should be queued. Examples include transactional e-mails like Password resets, invoice recepits etc. If you are transcoding audio files or video files; abeg use a queue to do that then send notifications to the user when it is done (Facebook does this when you upload videos). If your users are uploading files and you are storing those files in the cloud using Dropbox, S3, G Cloud, Cloudinary etc, find a way to make sure those files are being uploaded to the cloud straight away instead of them being stored on your server first before being moved to the cloud. If you are using an MVC framework or architecture, make sure your controllers are not doing too much or everything. Make use of traits and services.

2. Your infrastructure. I don't think you would want to build a software and scale it using a shared hosting solution. Get a cloud service for your infrastructure. DanielTheGeek has talked about that.
However, from my experience using AWS Elastic Load Balancers, you can configure an auto-scaling group that would scale your servers based on traffic. You can configure your group to have at least 2 servers running (the best configuration) and the load balancer routes requests to each one simultaneously in such a way that one server is not overworked at the expense of another.

3. Your database. I would not like to go into database engines and the best one for your software but to scale your DB, cache the most frequent queries your users have. Memcached and Redis are the best in this category. Caching reduces the number of queries hitting your database and allows your DB to rest a little. Then have more than one DB server running. You would then set a time of the day when you generally have reduced traffic that these DBs would sync.

2 Likes

Re: Scaling PHP Apps, How To? by paranorman(m): 9:23pm On Oct 16, 2017
Been eons since I had to read something as enlightening as this on NL p-section.

1 Like

Re: Scaling PHP Apps, How To? by monkeybonana: 9:26pm On Oct 16, 2017
paranorman:
Been eons since I had to read something as enlightening as this on NL p-section.
This may be because the number of wild trolls running around are much, and very few reasonable people left.

1 Like

Re: Scaling PHP Apps, How To? by DharkPoet(m): 10:59pm On Oct 16, 2017
DanielTheGeek:
It depends on budget and time, if you're building an app that needs to get to the market as quickly as possible, then it's okay to leave scalability till when it is needed but however your code base should be optimized, you should move to a VPS and fine tune your application so you can be certain you've done everything app wise to make sure your application is a bit scalable, then you'd want to enable caching for static pages on your web app and also serve static pages via an Nginx server (its faster cor static pages) and also add a simple load balancer in front of your Servers... This little technique I stated can help your app survive it's first few thousand users...when you get to the next slow point you'd know what to do...
You may want to consider a separate database for reading data and another database for writing data, a data recycle action will be performed by your database platform..this technique can buy you some more breathing space...

Next you may consider either horizontal or vertical scaling...
Vertical scaling is simply adding more powerful hardware resources as the need to scale increases. This seems like the way to go for the lazy and rich programmer who doesn't want anything too complicated. Simply upgrade your resources and the app scales well.
Imagine having a server serving the web app in question. The server has 4GB of RAM, an i5 CPU, and a 1TB HDD. It performs its function well, but to better tolerate a higher influx of traffic, you decide to replace the 4GB of RAM with 16GB, you put in an i7 CPU, and you add a PCIe SSD/HDD Hybrid drive. The server is now much more powerful and can handle a higher load. This is known as vertical scaling

Horizontal scaling means adding more zombie servers and distributing requests through those servers, the server with lesser load receives the next request and once it can no longer handle any more requests another server picks up... This option is the go to option for the developer that can't afford to horizontally scale his app or thinks he'll benefit more from vertical scaling.

However, the best thing is to scale both Horizontally and Vertically. The need for your app to scale means that you have lots of users which means you're making money and so affording to scale shouldn't be a problem. The problem is people try to scale apps that may not even last two years on the internet.

Actually, @DanielTheGeek your post on one of the threads here made me check out scaling. It was one where you were talking about MySQL data replication. I've also read about the two scaling types, Vertical and Horizontal.

But just like you rightly pointed out, the user base factor ought to be considered before even contemplating scaling, although code-bases need to be efficient too.

Thanks for your input.
Re: Scaling PHP Apps, How To? by DharkPoet(m): 11:06pm On Oct 16, 2017
bearnana:
I don't think scalability of applications is much about performance.

Its more about how to manage, if a lot of hands were to get on deck.

To explain my point I'd share a personal experience since you are talking PHP:
I worked on an application where the devs who handled the code before used procedural and the client wanted to build a Software Department to manage the application. It was difficult to understand the application and the variable types the devs before had done. So we were tasked to build the application from ground up in a very short space of time.

Some rules I have learnt in my short time on this coded planet.

1: Your application won't scale if it wasn't designed to scale. i.e. Don't use procedural if your application would one day have a very large number of users and would have a software department.

2: Simple Architecture is good if you are starting i.e Try to optimize your code base for speed (this is different fro performance; your code performs and your code performs fast are two different things. Every codebase is designed to perform, not all codebase is designed to perform fast...)

3: Think about database type. Since you mentioned LAMPP, I am assuming that you are using MYSQL for backend. It may not be as important as the first two rules when starting out but you would have to consider database engine type ISAM, MyISAM, InnoDB.

These are the ones I can think off the top of my head. The other ones are when you have reached about 500k users. You might want to think of load balancers and redundant firewalls, maybe more webservers for performance. By then you would also have to factor splitting your database read and writes. Before you get to this stage, there should be enough money to hire me grin grin to help you out...

But first off, if you are thinking of scaling at this early stage, THINK OF YOUR CODEBASE!!!
grin grin Lol!!! Who knows, I could have been the one who wrote it in procedural. Well, for sometime now, I've been battling picking up OOP as a concept, don't get me wrong, I understand the fundamentals but I've not been able to implement them, I think one of the reasons is due to the fact that I've not really embarked on any project that would make me use OOP (or maybe I'm just comfortable of this status quo).

This has made me not to pick up any PHP framework, a part of me feels I need to master OOP to an extent I'm order to start using any Framework. (Correct me if I'm wrong.)

Maybe one day I'd hire you, though I'd like to work with you. (Yeah, nobody wants to work with a rookie, I get)
Re: Scaling PHP Apps, How To? by DharkPoet(m): 11:13pm On Oct 16, 2017
simiolu1:
First off, scaling is majorly about 3 things

1. Your codebase.

Is your software efficient? Can you squeeze a little more juice out of it by refactoring? Are you using queues in the right places? Anything that can be delayed until a later time should be queued. Examples include transactional e-mails like Password resets, invoice recepits etc. If you are transcoding audio files or video files; abeg use a queue to do that then send notifications to the user when it is done (Facebook does this when you upload videos). If your users are uploading files and you are storing those files in the cloud using Dropbox, S3, G Cloud, Cloudinary etc, find a way to make sure those files are being uploaded to the cloud straight away instead of them being stored on your server first before being moved to the cloud. If you are using an MVC framework or architecture, make sure your controllers are not doing too much or everything. Make use of traits and services.

2. Your infrastructure. I don't think you would want to build a software and scale it using a shared hosting solution. Get a cloud service for your infrastructure. DanielTheGeek has talked about that.
However, from my experience using AWS Elastic Load Balancers, you can configure an auto-scaling group that would scale your servers based on traffic. You can configure your group to have at least 2 servers running (the best configuration) and the load balancer routes requests to each one simultaneously in such a way that one server is not overworked at the expense of another.

3. Your database. I would not like to go into database engines and the best one for your software but to scale your DB, cache the most frequent queries your users have. Memcached and Redis are the best in this category. Caching reduces the number of queries hitting your database and allows your DB to rest a little. Then have more than one DB server running. You would then set a time of the day when you generally have reduced traffic that these DBs would sync.
Quick question, how does one implement queues say in PHP?
Re: Scaling PHP Apps, How To? by DharkPoet(m): 11:13pm On Oct 16, 2017
I was beginning to lose hope, when I saw the views without the replies. Y'all remain blessed!!! Thanks Fam.
Re: Scaling PHP Apps, How To? by simiolu1(m): 11:33pm On Oct 16, 2017
bearnana:
I don't think scalability of applications is much about performance.

Its more about how to manage, if a lot of hands were to get on deck.

To explain my point I'd share a personal experience since you are talking PHP:
I worked on an application where the devs who handled the code before used procedural and the client wanted to build a Software Department to manage the application. It was difficult to understand the application and the variable types the devs before had done. So we were tasked to build the application from ground up in a very short space of time.

Some rules I have learnt in my short time on this coded planet.

1: Your application won't scale if it wasn't designed to scale. i.e. Don't use procedural if your application would one day have a very large number of users and would have a software department.

2: Simple Architecture is good if you are starting i.e Try to optimize your code base for speed (this is different fro performance; your code performs and your code performs fast are two different things. Every codebase is designed to perform, not all codebase is designed to perform fast...)

3: Think about database type. Since you mentioned LAMPP, I am assuming that you are using MYSQL for backend. It may not be as important as the first two rules when starting out but you would have to consider database engine type ISAM, MyISAM, InnoDB.

These are the ones I can think off the top of my head. The other ones are when you have reached about 500k users. You might want to think of load balancers and redundant firewalls, maybe more webservers for performance. By then you would also have to factor splitting your database read and writes. Before you get to this stage, there should be enough money to hire me grin grin to help you out...

But first off, if you are thinking of scaling at this early stage, THINK OF YOUR CODEBASE!!!

I want to disagree a little with some things you wrote.

In the perfect developer world, everyone writes OOP, deploys with Git and can set up AMP on a Linux server from scratch. In reality, a lot of developers still write spaghetti code, have never hosted outside shared hosting, don't know how to use git and have never even set up AMP talkless of using SSH.

My point is that we should allow people to learn and outgrow things. Believe me, when a developer grasps OOP, no one would tell him or her to drop spaghetti code. Then he or she would start to wonder how dumb they were coding like that in the first place. Saying that spaghetti code can't scale is not entirely true. It can if you scale out the infrastructure but one day while writing a new feature, someone is going to break.something.

I signed up on Twitter late 2008 and back then, the service broke almost every day. Instagram in its early days broke a couple of times too. But those people and teams learnt from those mistakes. I once heard Jan Koum (the CEO) of WhatsApp say that the only reason why WhatsApp has been stable was that the two founders were former employees of Yahoo who had experienced how to scale a digital product at Yahoo.

Scaling a product is hard; only a few developers would get to do it in their lifetimes. A lot of those would make mistakes when doing it for the first time. They would learn from it and move on.

2 Likes

Re: Scaling PHP Apps, How To? by simiolu1(m): 11:37pm On Oct 16, 2017
DharkPoet:

Quick question, how does one implement queues say in PHP?

Unfortunately, that question is beyond the scope of this thread. Also, I cannot give you a comprehensive anser just like that. Google it, visit stack overflow and different blogs. By the time you read 10blog posts and try a few times, you would get it.
Re: Scaling PHP Apps, How To? by HottestFire: 8:15am On Oct 17, 2017
DharkPoet:

grin grin Lol!!! Who knows, I could have been the one who wrote it in procedural. Well, for sometime now, I've been battling picking up OOP as a concept, don't get me wrong, I understand the fundamentals but I've not been able to implement them, I think one of the reasons is due to the fact that I've not really embarked on any project that would make me use OOP (or maybe I'm just comfortable of this status quo).

This has made me not to pick up any PHP framework, a part of me feels I need to master OOP to an extent I'm order to start using any Framework. (Correct me if I'm wrong.)

Maybe one day I'd hire you, though I'd like to work with you. (Yeah, nobody wants to work with a rookie, I get)



Lol, you must be joking about your not too interested in OOP, well never too late to develop frameworks or work with over the shelves.

You don't want your codebase to be problematic for others to understand.
Re: Scaling PHP Apps, How To? by HottestFire: 8:21am On Oct 17, 2017
simiolu1:


Unfortunately, that question is beyond the scope of this thread. Also, I cannot give you a comprehensive anser just like that. Google it, visit stack overflow and different blogs. By the time you read 10blog posts and try a few times, you would get it.



Lol, I once had problem with javascript for a payment gateway, I went as far as talking/sharing my code with a professional developer in europe and the embarassing response I got from him made me sit up...

Sometimes being naughty has a way of passing the right knowledge. grin

1 Like

Re: Scaling PHP Apps, How To? by bearnana(f): 12:32pm On Oct 17, 2017
Well, it seems the OP has learnt enough to last him a year. If there are any specifics, please ask, I'd be more than glad to give my input.
Re: Scaling PHP Apps, How To? by Nobody: 3:47pm On Oct 17, 2017
Good point.

I was tired of hearing people mentioning starting out with a scale supportive codebase... Most people, starting out want to get to the market as soon as possible with their app so they tend to write spag code or something that just works...

That app may never reach the point where scaling would be considered so it's better to be yourself and do what you can, when you realize you could've done it better then you can always refactorize your codebase. Once you even get to the point of scaling remember I said earlier "money won't be a problem" and so you can pay a team to help you refactorize and scale.

POF was handcoded from scratch by one person under a week (I guess, not sure about the figs..)... Trust me if we all had the chance to check the codebase out, the codebase would have similar resemblance to the devils diary...but today, they're still alive and their app is very scalable now.

Even YouTube was shitty at some point, it started out with PHP and now runs on Python, Java and Go with BigTable and MariaDB as databases of choice.

Facebook was also shitty at the beginning, but still survived.

Bottom line: forget about scaling for now, if your app is meant to survive then scalability will take care of itself when the time arises.

Note: YouTube's switch over from PHP doesn't mean a YouTube kinda site can't survive on PHP. There's HHVM, and stats show that Facebook is nearing YouTube on video uploads and playbacks.

simiolu1:


I want to disagree a little with some things you wrote.

In the perfect developer world, everyone writes OOP, deploys with Git and can set up AMP on a Linux server from scratch. In reality, a lot of developers still write spaghetti code, have never hosted outside shared hosting, don't know how to use git and have never even set up AMP talkless of using SSH.

My point is that we should allow people to learn and outgrow things. Believe me, when a developer grasps OOP, no one would tell him or her to drop spaghetti code. Then he or she would start to wonder how dumb they were coding like that in the first place. Saying that spaghetti code can't scale is not entirely true. It can if you scale out the infrastructure but one day while writing a new feature, someone is going to break.something.

I signed up on Twitter late 2008 and back then, the service broke almost every day. Instagram in its early days broke a couple of times too. But those people and teams learnt from those mistakes. I once heard Jan Koum (the CEO) of WhatsApp say that the only reason why WhatsApp has been stable was that the two founders were former employees of Yahoo who had experienced how to scale a digital product at Yahoo.

Scaling a product is hard; only a few developers would get to do it in their lifetimes. A lot of those would make mistakes when doing it for the first time. They would learn from it and move on.

2 Likes

Re: Scaling PHP Apps, How To? by Nobody: 4:15pm On Oct 17, 2017
DharkPoet:
I was beginning to lose hope, when I saw the views without the replies. Y'all remain blessed!!! Thanks Fam.

Yeah, I didn't find this on the section's front page, I found it while viewing your profile and thought to myself "why is this great question not receiving answers? Let me start, others will join..."
Re: Scaling PHP Apps, How To? by bearnana(f): 4:16pm On Oct 17, 2017
DanielTheGeek:


Even YouTube was shitty at some point, it started out with PHP and now runs on Python, Java and Go with BigTable and MariaDB as databases of choice.


Note: YouTube's switch over from PHP doesn't mean a YouTube kinda site can't survive on PHP. There's HHVM, and stats show that Facebook is nearing YouTube on video uploads and playbacks.


Wow. Didn't know Youtube, started out with PHP. I wonder why they switched. Language inconsistency maybe.
Re: Scaling PHP Apps, How To? by Nobody: 4:19pm On Oct 17, 2017
Oops, my bad. I see you're getting acquainted, more of your type is needed. Welcome once again.

bearnana:


I think I mentioned that MyIsam/InnoDB are database engine types.
If you don't consider your codebase I wonder how developers would work on it to manage better performance.

Thanks for the welcome. smiley
Re: Scaling PHP Apps, How To? by Nobody: 4:35pm On Oct 17, 2017
Check the way back machine yourself

I can build what they had in 2005/06 now in 6 straight hours (or less) thanks to language upgrade.

PHP wasn't a shiny tool back then, but it helped them get to the market on time. There was no JIT (Just In Time) compiler so their service broke often, want proof? Check how many times YouTube wasn't captured due to downtime in 05 here

bearnana:


Wow. Didn't know Youtube, started out with PHP. I wonder why they switched. Language inconsistency maybe.

1 Like

Re: Scaling PHP Apps, How To? by kanayoNickel: 10:01am On Oct 18, 2017
I LoVE you All.
This is super enlightening.
Re: Scaling PHP Apps, How To? by DharkPoet(m): 2:28pm On Oct 18, 2017
bearnana:
Well, it seems the OP has learnt enough to last him a year. If there are any specifics, please ask, I'd be more than glad to give my input.
On the contrary I've not learnt enough, can someone really learn enough?

2 Likes

Re: Scaling PHP Apps, How To? by DharkPoet(m): 2:33pm On Oct 18, 2017
HottestFire:




Lol, you must be joking about your not too interested in OOP, well never too late to develop frameworks or work with over the shelves.

You don't want your codebase to be problematic for others to understand.
You misunderstood me, I didn't imply I am/was never interested in OOP, in fact I wish I could be seamlessly integrate into my work/thought process. I just need to get out of this comfort zone of procedural programming.
Re: Scaling PHP Apps, How To? by DharkPoet(m): 2:34pm On Oct 18, 2017
HottestFire:




Lol, I once had problem with javascript for a payment gateway, I went as far as talking/sharing my code with a professional developer in europe and the embarassing response I got from him made me sit up...

Sometimes being naughty has a way of passing the right knowledge. grin


I wish I could get people to review my codes.
Re: Scaling PHP Apps, How To? by HottestFire: 6:41pm On Oct 18, 2017
DharkPoet:


I wish I could get people to review my codes.



Ok, any terms or reference.. grin

(1) (Reply)

. / Halide: A New Language For Image Processing And Computational Photography / Need To Secure Your Compiled Software (.msi Or .exe) To 1/PC?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 100
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.