Key Takeaways

  • The Theory of Constraints (TOC) is a management paradigm that views any manageable system as being limited in achieving more of its goals by a very small number of constraints. In the context of PHP, this theory emphasizes that the performance of a PHP application is only as good as its weakest part, and any improvements made anywhere besides the bottleneck are an illusion.
  • Tools like Blackfire and Xdebug can help identify bottlenecks in PHP applications by providing in-depth graphs of the application’s execution stack and indicating code bottlenecks. These tools, along with others, can help optimize the application once the constraint has been identified. However, it’s important to remember that optimizing just for the sake of optimization isn’t a good idea.
  • When a bottleneck is identified in a PHP application, it should be prioritized and resolved before moving onto other tasks. This could mean pulling critical people off of other tasks and having everyone who’s qualified focus on that one problem until it’s resolved. This approach is beneficial for the entire system as it often resolves other high priority tickets that were actually caused by the bottleneck.

The post “The Theory of Constraints in PHP” is the editorial for the PHP Channel’s newsletter on July 11th. To stay on top of PHP news, posts, and relevant links, make sure to keep up to date here.


I had been reading The Phoenix Project, a great novel about IT (you read that right), which presents day to day IT and devops problems at a large Amazon-like company in a way which makes mortals understand the complexities and chaos of 21st century technology.

Without giving away any spoilers, at one point in the book the Theory of Constraints is mentioned. As per Wikipedia:

The Theory of Constraints (TOC) is a management paradigm that views any manageable system as being limited in achieving more of its goals by a very small number of constraints. There is always at least one constraint, and TOC uses a focusing process to identify the constraint and restructure the rest of the organization around it.

The Theory of Constraints can be distilled to the idea that the chain is only as strong as its weakest link.

Chain with weak link

In the book it was phrased thusly:

Any improvements made anywhere besides the bottleneck are an illusion.

For some reason, this resonated with me much more than the chain idiom. There’s just something about building something that’s ineffective that’s more relatable to me than breaking something that’s weakly built.

Factories and Browsers

As (initially) presented in the book and slightly dumbed down here, a factory floor was seeing a pile-up of work orders at one desk, with other areas of the factory working somewhat satisfactorily. Without working on optimizing this one desk’s throughput and by (either coincidentally or intentionally) upgrading the process that precedes or supersedes it, the factory became clogged with work because the former lead to a stack of products before the desk that had no destination, and the latter lead to idle workers at the other end of the facility, waiting for the products to reach them for shipping, but all stuck in slow processing at the central desk.

Pile of paperwork on desk

To go off on a slight tangent here – if you follow me on Twitter, you know I like to rant about browsers. I especially lose it when they put in updates like native OSX notifications but fail to address the decade old RAM use problem. Some people are quick to address this as an appeal to worse problems fallacy, but I disagree. While, yes, this is an appeal to a worse problem, I don’t see it as fallacy – rather, I see it as a lack of identified constraints on Google’s part. As a company, they seem completely clueless of what’s in need of optimization, or are devoid of resources to dedicate to the fixing of this problem (not sure which is worse). So we have all these “improvements” made outside the bottleneck, driving power users mad and resulting in attempts at revamping the browser by slapping another skin on it (Vivaldi, Brave, Opera, Ghost…), but unable to make progress because, again, the bottleneck remains. If you move the desk and the whole factory setup from one building to the next, the desk is still the problem. Fix the desk.

Theory of Constraints in PHP

So how does this apply to web apps? Or, better yet, PHP apps in particular?

These days, it’s really easy to follow this principle and identify the constraint(s). We have so many amazing tools at our disposal that identifying the bottleneck within a few hours is generally a given:

  • Blackfire will provide you with fantastic in-depth graphs of your application’s execution stack, indicating code bottlenecks with ease. These will include database bottlenecks as well, if executed in the code stack you’re analyzing, making the whole thing infinitely easier to optimize.

  • Not many people know this, but Xdebug has that same execution stack diagram with time analysis, letting you do that same thing locally!

  • There’s a bunch of valuable QA tools which can help you test your app to bits – super long test times are often indicative of bottlenecks, and pinpointing them there while they’re running on mock data and test examples can be incredibly rewarding.

  • A bit of an oldie but goldie, this set of posts helps you optimize your MySQL once you’ve identified it as a bottleneck..

    But remember that optimizing it just for the sake of optimization isn’t a good idea!

  • It helps if you nuke your app with requests, and compare before/after results – it’ll be a clear indicator on much your optimizations boosted the app as a whole. A tool like Siege can help with that, and we have an in-depth guide for just that.

The tools above are a good start, but far from exhaustive, and far from applicable to every problem you might run into. Additional articles from this Premium PHP Anthology about Better PHP Development might be useful, but I’m honestly hoping for more diverse examples and links from you! Shoot!

Conclusion

The ToC is a valid concern in software development, and just as applicable as anywhere else. We’ve all encountered bottlenecks, and we’ve all stupidly tried to improve upon anything other than the bottleneck – or if you haven’t fallen into this trap yet, I promise that you will.

Luckily, the PHP ecosystem is ripe with tools that help with identifying bottlenecks and turning them into system constraints. If your application is having performance or throughput issues, find the bottleneck, tag it, then drop all other work until the bottleneck has been resolved. Pull critical people off of other tasks, even if it means ignoring incoming unrelated high priority tasks, and have everyone who’s qualified focus on that one problem until it’s resolved. The entire system will thank you for it because, more often than not, that one bottleneck is the reason for those other high priority tickets in the first place. Don’t Brent yourself into a corner! (To understand that reference, you’ll need to read the book)

If you’re flying solo, the same principles apply – as you’re looking at your columns of tasks in Trello, Zenkit, Freedcamp, or another project management tool, identify the bottleneck one, paint it red, and move all others into a column off-screen or one that can be collapsed until the constraint has been resolved and the system remodeled around it. Doing anything less will incur mountains of tech debt that’ll bury you later.

How do you find your bottlenecks? Do you remodel the system around the identified constraint, or do you remodel the constraint instead and try to hack your way through it? Which tools/tutorials have I missed in the above list? Let me know in the comments – let’s build a comprehensive list together – and remember: “Improving daily work is even more important than doing daily work“! (Another great quote from the book)

Frequently Asked Questions about PHP Constraints

What are the different types of constraints in PHP?

In PHP, constraints are rules applied to data columns on a table. They are used to limit the type of data that can go into a table to maintain the accuracy and reliability of the data. The different types of constraints in PHP include Primary Key, Foreign Key, Unique, Not Null, and Check constraints. Each of these constraints serves a unique purpose in data validation and integrity.

How do I implement a Primary Key constraint in PHP?

A Primary Key constraint is a field in a table which uniquely identifies each row/record in a database table. Primary keys must contain unique values. In PHP, you can implement a Primary Key constraint when creating a table using the “PRIMARY KEY” keyword. For example:
CREATE TABLE Employees (
ID int NOT NULL,
Name varchar(255) NOT NULL,
Age int,
PRIMARY KEY (ID)
);
In this example, the “ID” column is the primary key.

What is the purpose of the Foreign Key constraint in PHP?

A Foreign Key constraint is used to prevent actions that would destroy links between tables. It provides a way to enforce referential integrity within your database. A foreign key means that values in one table must also appear in another table.

How can I use the Unique constraint in PHP?

The Unique constraint ensures that all values in a column are different. This provides a way to ensure that no duplicate values are entered into specific columns that should hold unique values. For example, this can be useful when you want to ensure that no two people have the same email address.

What does the Not Null constraint do in PHP?

The Not Null constraint is a rule that prevents null values from being entered into specific columns in your database tables. This means that when creating records, values for these columns must always be specified.

How can I implement the Check constraint in PHP?

The Check constraint allows you to specify a condition on each row in a table. This ensures that certain conditions are met before an operation (such as insert or update) is performed on the data. For example, you can use a Check constraint to ensure that a field always receives a numerical value.

Can I have multiple constraints on a single column in PHP?

Yes, you can have multiple constraints on a single column in PHP. For example, you can have a column that has both a Not Null and Unique constraint. This would ensure that the column always has a value and that the value is unique.

How can I remove a constraint from a column in PHP?

You can remove a constraint from a column in PHP by using the ALTER TABLE command. This command allows you to add, delete/drop or modify columns in an existing table.

What happens if a constraint is violated in PHP?

If a constraint is violated in PHP, the operation that caused the violation is rolled back, and an error message is returned. This helps maintain the integrity of the data in your database.

Can I use constraints with PHP frameworks like Symfony?

Yes, you can use constraints with PHP frameworks like Symfony. Symfony provides a Validator component that enables you to apply constraints on any property of your object classes. You can use this to ensure that the data in your application meets certain criteria before it is processed.

Bruno SkvorcBruno Skvorc
View Author

Bruno is a blockchain developer and technical educator at the Web3 Foundation, the foundation that's building the next generation of the free people's internet. He runs two newsletters you should subscribe to if you're interested in Web3.0: Dot Leap covers ecosystem and tech development of Web3, and NFT Review covers the evolution of the non-fungible token (digital collectibles) ecosystem inside this emerging new web. His current passion project is RMRK.app, the most advanced NFT system in the world, which allows NFTs to own other NFTs, NFTs to react to emotion, NFTs to be governed democratically, and NFTs to be multiple things at once.

bottleneckBrunoSeditorialOOPHPoptimizationperformancePHP
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week