Today I’m talking about code scalability.
Pro developers take scalability seriously.
In fact, scalability is one of the factors that really make the difference between average and high quality code.
Your code is scalable if:
It keeps working when data and users increase. This is known as performance scalability.
You can easily expand and upgrade it as new functionalities are required. This is known as code scalability.
Scalability is a vast topic.
But to help you get started, here are 5 advanced tips to make your PHP code scalable.
1. Write modular code
Your code is modular if each operation is done by a specific function or class method.
This is in contrast with monolithic code, where a small number of huge, complex functions perform all the operations.
If you adopt a modular approach, you will have many small functions, each of them performing a very specific task.
This approach makes your code scalable and you should always adopt it, especially for medium-sized and large projects.
2. DRY
DRI stands for “don’t repeat yourself”.
It means that you should not write the same code twice.
Sometimes it seems easier to just replicate a small piece of code, instead of creating a separate function and including it where needed.
However, as your project gets more complex, duplicate code becomes a huge problem.
If your code needs to be fixed or upgraded, you need to remember to check all the different places where you wrote it.
This will easily introduce bugs and consistency problems.
3. Analyze your SQL queries
SQL queries can become slow as the amount of database data increases.
To make sure your code is scalable, it’s important to analyze and profile your queries.
Check how much time each query needs to run. You can do this by using the microtime() function before and after the query execution.
Simple queries should run in less than 0.001s.
If a query seems to slow, check how the table is structured. Make sure proper indexes are in place and minimize joins and “where” conditions.
4. Data pre-elaboration
The data your web app uses is stored on the database.
Usually, your app simply reads the data it needs, elaborates it and displays the result to the remote user.
However, in some cases the time required to elaborate this data may become too much.
For example, let’s say you need to display statistical data. If you have one year (or more) worth of data, elaborating it in real-time may require too much time.
A solution is to pre-elaborate the statistical data so that the web application just needs to read it and display it, without performing any further operation.
You can perform such data pre-elaboration with a different PHP script that runs periodically. For example, you can run it daily with a Cronjob, or you can implement a PHP daemon that automatically keeps the data up to date.
This is a very powerful solution to increase performance scalability.
5. Profile your code
Last but not least: make sure your code is optimized.
A piece of code may be particularly slow or use too much system memory.
This happens more often in loops and in operations with database data.
In such cases, it’s a good idea to profile your code using microtime() to check how much time it needs to run, and with memory_get_usage() to check how much memory it uses.
(You can refer to this tutorial for more details on how to check your scripts’ memory consumption.)
If your code is not fast enough, analyze it and find the reason why.
That’s if for today.
Do you know other ways you can make your code scalable? I wait for your reply.
Have a nice day,
Alex
Did you like this email?
Click here to share it with someone you know.
Let’s keep in touch
Alex Web Develop
My blog where you can find my tutorials.
Alex PHP café
My Facebook group where you can ask me questions.
My PHP courses
PHP Security Mastery
My professional PHP security course