Raspberry Pi Zero: Not executing a trillion lines of PHP
A number of people noticed that Raspberry Pi had launched their $5 Pi Zero yesterday. We had advance warning that something was going to happen, even if we didn’t know exactly what. When the Pi2 launched we had some difficulties keeping up with comment posting and cache invalidation. We gave a very well received talk on the history and launch at the UK Network Operators Forum which you can see below.
- Containment: One part of the site can’t significantly impact the performance of another.
- Scalability: We can pull VMs into our public cloud and duplicate them if required.
- Flexibility: We no longer have to have a single software stack that supports everything.
For the Pi 2 launch we sustained around 4500 simultaneous users before we really started struggling with comment posting and cache invalidation. So our new plan was to be able to manage over 5,000 simultaneous site users before we needed to start adding more VMs. This equates to around 1000 hits per second.
In order to do this, we need to make sure we can serve any of the 90% of the most common requests without touching the disks or the database; and without using more than 10ms of CPU time. We want to reserve all our capacity for pages that have to be dynamic – comment-posting and forums, for example – and make all the common content as cheap as possible.
So we deployed a custom script staticify. This automatically takes the most popular and important pages, renders them to static HTML and rewrites the webserver configuration to serve the static pages instead. It runs frequently so the cache is never more than 60 seconds old, making it appear dynamic. It also means that we serve a file from filesystem cache (RAM) instead of executing WordPress. During the day we improved and deployed this same code to the MagPi site including some horrid hackery to cache popular GET request combinations.
Found and hacked around the last non-cacheable item on the Magpi homepage, load now 6, was 130.
— Mythic Beasts (@Mythic_Beasts) November 26, 2015
Some very vague back-of-the-envelope calculations give us:
98 line perl cache code ran 1500 times, 400k lines of php didn’t run 2.5m times, I saved the world from 1 trillion lines of php today #proud
— Mythic Beasts (@Mythic_Beasts) November 26, 2015
It’s fair to say that we exceeded our target of 5,000 simultaneous users,
Well, this is fun. Over 10,000 people on the site right now. Not to mention those pesky JS-disablers. #pizero pic.twitter.com/MGImcSXNdk
— Ben Nuttall (@ben_nuttall) November 26, 2015
Liz Upton was quite pleased:
10,000 of you on our website at once, and it’s running as smooooooth as butter. Thanks @Mythic_Beasts!
— Raspberry Pi (@Raspberry_Pi) November 26, 2015
Not to mention a certain amount of respect from our peers
Gotta say, watching @Mythic_Beasts today, something to be said for lost art of site performance optimisation in today’s autoscaled cloud env
— Nathan Kunicki (@nathankunicki) November 27, 2015
If you deployed the blog unoptimised to AWS and just had auto-magic scaling, we’d estimate the monthly bills to be many tens of thousands of dollars per month, money that instead can be spent on education. In addition you’d still need to make sure you can effortlessly scale to thousands of cores without a single bottleneck somewhere in the stack causing them all to lie idle. The original version of the site (with hopeless analytics plugin that processed the complete site logs on every request) would consume more computer power than has ever existed under the traffic mentioned above. At this scale optimisation is a necessity, and if you’re going to optimise, you might as well optimise well.
That said, we think some of our peers possibly overstated our importance in the big scheme of things,
@mythic_beasts Fine work indeed, humanity owes you one.
— Ben Charlton (@bencc) November 26, 2015