Habits that keep applications within limits.
PRINCIPLES
Do the minimum work per request Never do in a loop what can be done once Never call an external service during a page load if the visitor does not need the result Set timeouts on everything that talks to another system Process large datasets in batches Release memory when finished with it
WHAT CAUSES TROUBLE
Loading entire tables into memory Recursive operations without depth limits Regular expressions over very large strings Synchronous external calls File operations in loops Work that should be scheduled happening on every request
DEFENSIVE HABITS
Assume constrained memory and short execution limits Assume the database will occasionally be slow Assume external services will occasionally be unavailable Handle failure rather than assuming success
THE PAYOFF
Code written for a constrained environment runs well everywhere, and does not need rewriting when traffic grows.
MEASURING
Profile before optimising. The slow part is rarely where you expect, and time spent optimising fast code is wasted.