Oh Dear is all-in-one solution to monitor your site that my buddy Mattias Geniar and I have created. It can monitoring uptime, certificates, broken links, scheduled jobs, and much more.
Under the hood, Oh Dear is a large Laravel application that performs many queries all of the time. To power future features, we’ve recently changed our database structure and refactored some pieces in our code base. We increased performance by decreasing the number of queries.
In this blog post, we’d like to to share some techniques that might be helpfull to increase the performance of your Laravel app too.
Our MySQL server does a lot of work
Since we’ve launched our service a couple of years ago, we’ve steadily seen our service grow at a nice pace. In our early days, we only monitored a handful of websites. This didn’t result in any significant load on the server we were using at the time.
Right now, we are monitoring tens of thousands of sites. For each of those sites we perform multiple checks: uptime, certificate health, broken links, mixed content, scheduled jobs… Before our optimisation improvements, at peaks we performed about 4 000 queries per second for all of these sites and checks.
When coding up Oh Dear initially, we weren’t really focused on performance. We didn’t think our service would become as big as it is now. We were not really worried about the amount of queries. So, when we needed a piece of data in a check, we just performed a query. This works when you only have a couple of sites to monitor, but at the current amount of sites we monitor, this was becoming problematic.
We have a beefy server that can manage our current load. We were worried that performance problems would hit us hard with our current growth at some point in the near future. So the past few months, we’ve made changes to decrease the amount of queries.
Decreasing the query count when dispatching new runs
For each type of check, we have an Artisan command that will dispatch a run to perform that check. The command loops over each team. For each of the sites of team we’ll get the Check
model that represents a check and has all the customisation options for a check. Here’s the simplified code.
<span class="line"><span>class</span><span> </span><span>DispatchNewUptimeRunsCommand</span><span> </span><span>extends</span><span> </span><span>Command</span></span>
<span class="line"><span>{</span></span>
<span class="line"><span> </span><span>protected</span><span> $signature </span><span>=</span><span> </span><span>'ohdear:dispatch-new-runs:uptime'</span><span>;</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>public</span><span> </span><span>function</span><span> </span><span>handle</span><span>()</span></span>
<span class="line"><span> {</span></span>
<span class="line"><span> </span><span>$this</span><span>-></span><span>info</span><span>(</span><span>'Dispatching new uptime check runs ...'</span><span>);</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>Team</span><span>::</span><span>with</span><span>(</span><span>'sites'</span><span>, </span><span>'sites.checks'</span><span>)</span><span>-></span><span>each</span><span>(</span><span>function</span><span> (</span><span>Team</span><span> $team) {</span></span>
<span class="line"><span> </span><span>if</span><span> (</span><span>!</span><span> $team</span><span>-></span><span>hasActiveSubscriptionOrIsOnGenericTrial</span><span>()) {</span></span>
<span class="line"><span> </span><span>return</span><span>;</span></span>
<span class="line"><span> }</span></span>
<span class="line"></span>
<span class="line"><span> $team</span></span>
<span class="line"><span> </span><span>-></span><span>sites</span></span>
<span class="line"><span> </span><span>-></span><span>each</span><span>(</span><span>function</span><span> (</span><span>Site</span><span> $site) </span><span>use</span><span> ($team) {</span></span>
<span class="line"><span> $uptimeCheck </span><span>=</span><span> $site</span><span>-></span><span>getCheck</span><span>(</span><span>CheckType</span><span>::</span><span>UPTIME</span><span>());</span></span>
<span class="line"><span> </span></span>
<span class="line"><span> </span><span>// dispatch a run that will perform the actual check</span></span>
<span class="line"></span>
Before dispatching a run of a check, we first verify if a team is subscribed using that hasActiveSubscriptionOrIsOnGenericTrial
function. It is executed a lot of times: for each team in our database multiplied by the amount of checks we have. Let’s take a look it’s implementation.
<span class="line"><span>public</span><span> </span><span>function</span><span> </span><span>hasActiveSubscriptionOrIsOnGenericTrial</span><span>()</span><span>:</span><span> </span><span>bool</span></span>
<span class="line"><span>{</span></span>
<span class="line"><span> </span><span>if</span><span> (</span><span>$this</span><span>-></span><span>onGenericTrial</span><span>()) {</span></span>
<span class="line"><span> </span><span>return</span><span> </span><span>true</span><span>;</span></span>
<span class="line"><span> }</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>return</span><span> </span><span>$this</span><span>-></span><span>hasActiveSubscription</span><span>();</span></span>
<span class="line"><span>}</span></span>
<span class="line"></span>
It should be easy to grasp what is going on in the snippet above. In the onGenericTrail
and hasActiveSubscription
functions, multiple queries are being performed.
We can greatly decrease the amount of queries by caching the result of this function.
<span class="line"><span>public</span><span> </span><span>function</span><span> </span><span>hasActiveSubscriptionOrIsOnGenericTrial</span><span>()</span><span>:</span><span> </span><span>bool</span></span>
<span class="line"><span>{</span></span>
<span class="line"><span> $key </span><span>=</span><span> </span><span>"hasActiveSubscriptionOrIsOnGenericTrial-team-{</span><span>$this</span><span>-></span><span>id</span><span>}"</span><span>;</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>return</span><span> </span><span>cache</span><span>()</span><span>-></span><span>remember</span><span>($key, </span><span>3600</span><span>, </span><span>function</span><span> () {</span></span>
<span class="line"><span> </span><span>if</span><span> (</span><span>$this</span><span>-></span><span>onGenericTrial</span><span>()) {</span></span>
<span class="line"><span> </span><span>return</span><span> </span><span>true</span><span>;</span></span>
<span class="line"><span> }</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>return</span><span> </span><span>$this</span><span>-></span><span>hasActiveSubscription</span><span>();</span></span>
<span class="line"><span> });</span></span>
<span class="line"><span>}</span></span>
<span class="line"></span>
For each team, we are now going to determine the subscription status only once on hour. This change will lead to tens of thousands less queries in an hour.
The only downside that this takes is that we potentially keep on running checks for teams without a subscriptions a couple of minutes longer than needed. But the decrease in queries is worth that downside.
Decreasing the amount of queries performed in a run
Before our performance improvements, to run a check we simply created a Run
model with a status
of pending
. Here’s the simplified code for the uptime check.
<span class="line"><span>// in DispatchNewUptimeRunsCommand</span></span>
<span class="line"><span>$check </span><span>=</span><span> $site</span><span>-></span><span>getCheckOfType</span><span>(</span><span>CheckType</span><span>::</span><span>UPTIME</span><span>());</span></span>
<span class="line"></span>
<span class="line"><span>// create a run model</span></span>
<span class="line"><span>$run </span><span>=</span><span> </span><span>Run</span><span>::</span><span>createForCheck</span><span>($check);</span></span>
<span class="line"></span>
<span class="line"><span>dispatch</span><span>(</span><span>new</span><span> </span><span>PerformUptimeCheckJob</span><span>($run));</span></span>
<span class="line"></span>
In the PerformUptimeCheckJob
the actual check is being performed. Inside of that job we performed multiple queries to retrieved the check, site, notification preferences, … The job would also update the run to save the results of the check.
This way of running a check isn’t only used for the uptime check, but for all checks Oh Dear performs. Inside the PerformUptimeCheckJob
(and other type of jobs) only a few queries were executed. As Oh Dear executes tens of thousands of runs per minute, those few queries in a single run lead to a lot of queries in total.
To minimise the amount of queries performed inside a job, we converted our code to make use of what we call, an InMemoryRun
class. That class is basically a DTO that has all the necessary information to perform a run of a check.
<span class="line"><span>class</span><span> </span><span>InMemoryUptimeRun</span><span> </span><span>extends</span><span> </span><span>InMemoryRun</span></span>
<span class="line"><span>{</span></span>
<span class="line"><span> </span><span>public</span><span> </span><span>function</span><span> </span><span>__construct</span><span>(</span></span>
<span class="line"><span> </span><span>public</span><span> </span><span>int</span><span> $checkId,</span></span>
<span class="line"><span> </span><span>public</span><span> </span><span>string</span><span> $uptimeCheckLocation,</span></span>
<span class="line"><span> </span><span>public</span><span> </span><span>string</span><span> $checkUrl,</span></span>
<span class="line"><span> </span><span>// other pieces of info needed by a run</span></span>
<span class="line"><span>}</span></span>
<span class="line"></span>
Here’s the simplified version of the refactored DispatchNewUptimeRunsCommand
where the InMemoryUptimeRun
is used.
<span class="line"><span>// in DispatchNewUptimeRunsCommand</span></span>
<span class="line"></span>
<span class="line"><span>$check </span><span>=</span><span> $site</span><span>-></span><span>getCheckOfType</span><span>(</span><span>CheckType</span><span>::</span><span>UPTIME</span><span>());</span></span>
<span class="line"></span>
<span class="line"><span>// make an in memory run DTO</span></span>
<span class="line"><span>$inMemoryRun </span><span>=</span><span> $check</span><span>-></span><span>makeInMemoryRunForCheck</span><span>($check);</span></span>
<span class="line"></span>
<span class="line"><span>dispatch</span><span>(</span><span>new</span><span> </span><span>PerformUptimeCheckJob</span><span>($inMemoryRun));</span></span>
<span class="line"></span>
Because that PerformUptimeCheckJob
now gets an object that contains all the bits of info it needs to perform a check,PerformUptimeCheckJob
doesn’t need to execute queries itself anymore. The total number of queries performed in jobs drastically decreased.
Also notice that in the previous we were creating a Run
model for each performed in DispatchNewUptimeRunsCommand
. In the refactored code we don’t store Run
with a state of pending
anymore. In the PerformUptimeCheckJob
we simply create a Run
model with the results. Avoiding creating a Run
with a pending
status in DispatchNewUptimeRunsCommand
results again in tens of thousands less insert queries.
Prevent queries by optimizing circular relationships
After reading the section above, you might think that we’ve simply moved executing queries to get the Team
, Site
models, … from inside the run job, to the command that schedules those jobs.
In our command, we were already getting Team
and Site
as we are looping over all teams and sites. The Check
model has a method makeInMemoryRunForCheck
that creates that InMemoryRun
discussed in the previous section. Under the hood, it will use the relation to a site and team to retrieve the needed bits of info on those models.
To avoid Check
from performing a query to load its relations, we can reuse the instances of teams and sites we got when looping over all teams and sites. These instances can be set using Laravel’s setRelation
method.
<span class="line"><span>class</span><span> </span><span>DispatchNewUptimeRunsCommand</span><span> </span><span>extends</span><span> </span><span>Command</span></span>
<span class="line"><span>{</span></span>
<span class="line"><span> </span><span>protected</span><span> $signature </span><span>=</span><span> </span><span>'ohdear:dispatch-new-runs:uptime'</span><span>;</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>public</span><span> </span><span>function</span><span> </span><span>handle</span><span>()</span></span>
<span class="line"><span> {</span></span>
<span class="line"><span> </span><span>$this</span><span>-></span><span>info</span><span>(</span><span>'Dispatching new uptime check runs ...'</span><span>);</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>Team</span><span>::</span><span>with</span><span>(</span><span>'sites'</span><span>, </span><span>'sites.checks'</span><span>)</span><span>-></span><span>each</span><span>(</span><span>function</span><span> (</span><span>Team</span><span> $team) {</span></span>
<span class="line"><span> </span><span>if</span><span> (</span><span>!</span><span> $team</span><span>-></span><span>hasActiveSubscriptionOrIsOnGenericTrial</span><span>()) {</span></span>
<span class="line"><span> </span><span>return</span><span>;</span></span>
<span class="line"><span> }</span></span>
<span class="line"></span>
<span class="line"><span> $team</span></span>
<span class="line"><span> </span><span>-></span><span>sites</span></span>
<span class="line"><span> </span><span>-></span><span>each</span><span>(</span><span>function</span><span> (</span><span>Site</span><span> $site) </span><span>use</span><span> ($team) {</span></span>
<span class="line"><span> </span></span>
<span class="line"><span> $uptimeCheck </span><span>=</span><span> $site</span><span>-></span><span>getCheck</span><span>(</span><span>CheckType</span><span>::</span><span>UPTIME</span><span>());</span></span>
<span class="line"><span> </span></span>
<span class="line add"><span> </span><span>// prevent performing a query when using the relations</span></span>
<span class="line add"><span> $uptimeCheck</span></span>
<span class="line add"><span> </span><span>-></span><span>setRelation</span><span>(</span><span>'site'</span><span>, $site)</span></span>
<span class="line add"><span> </span><span>-></span><span>setRelation</span><span>(</span><span>'site.team'</span><span>, $team);</span></span>
<span class="line"><span> </span></span>
<span class="line"><span> $check </span><span>=</span><span> $site</span><span>-></span><span>getCheckOfType</span><span>(</span><span>CheckType</span><span>::</span><span>UPTIME</span><span>());</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>// make an in memory run DTO</span></span>
<span class="line"><span> $inMemoryRun </span><span>=</span><span> $check</span><span>-></span><span>makeInMemoryRunForCheck</span><span>($check);</span></span>
<span class="line"><span> </span></span>
<span class="line"><span> </span><span>dispatch</span><span>(</span><span>new</span><span> </span><span>PerformUptimeCheckJob</span><span>($inMemoryRun));</span></span>
<span class="line"></span>
If you want to know more about setRelation
and circular references, check out this excellent blog post by Jonathan Reinink.
Using DynamoDB to increase performance
Oh Dear can monitor scheduled tasks as well. This works by letting our users define their tasks in our UI (or via our schedule monitor package) and letting scheduled tasks pinging our monitoring endpoint when they end. When a ping doesn’t come in on time, we send a notification to our user.
This way of monitoring results of a lot of HTTP requests being sent to our monitoring endpoint in a very short timespan. To handle this load we had decided to host that endpoint on AWS Lambda (via Laravel Vapor).
We require each request to use a specific UUID in the called URL to filter out any unwanted requests. In a previous version of our code we kept all valid UUIDs in the MySQL DB and we made a query whenever a ping request comes in.
Here is the route binding of cronCheckUuid
that was used in the routes that handle incoming ping request.
<span class="line"><span>protected</span><span> </span><span>function</span><span> </span><span>registerRouteModelBindings</span><span>()</span><span>:</span><span> </span><span>self</span></span>
<span class="line"><span>{</span></span>
<span class="line"><span> </span><span>Route</span><span>::</span><span>bind</span><span>(</span><span>'cronCheckUuid'</span><span>, </span><span>function</span><span> (</span><span>string</span><span> $cronCheckUuid) {</span></span>
<span class="line"><span> </span><span>if</span><span> (</span><span>!</span><span> $cronCheckDefinition </span><span>=</span><span> </span><span>CronCheckDefinition</span><span>::</span><span>findByUuid</span><span>($cronCheckUuid)) {</span></span>
<span class="line"><span> </span><span>abort</span><span>(</span><span>Response</span><span>::</span><span>HTTP_UNAUTHORIZED</span><span>, </span><span>'not authorized'</span><span>);</span></span>
<span class="line"><span> }</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>return</span><span> $cronCheckDefinition;</span></span>
<span class="line"><span> });</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>return</span><span> </span><span>$this</span><span>;</span></span>
<span class="line"><span>}</span></span>
<span class="line"></span>
Under normal circumstances, this worked just fine. But our endpoints that handle incoming pings receive so many requests that the db sometimes isn’t fast enough, resulting in failed requests.
A MySQL database can often be the bottleneck in a serverless application. In most cases, it’s a good idea to avoid using a MySQL database on high traffic endpoints. In our case, we avoid making a query by leveraging DynamoDB, a high performance key-value storage that’s optimised for read operations. Laravel Vapor uses DynamoDB as its default store for caching.
To leverage DynamoDB in our Laravel app on Vapor, we first created a new cache store.
<span class="line"><span>// in the `stores` key of `config/cache.php`</span></span>
<span class="line"></span>
<span class="line"><span>'cron-check'</span><span> </span><span>=></span><span> [</span></span>
<span class="line"><span> </span><span>'driver'</span><span> </span><span>=></span><span> </span><span>'dynamodb'</span><span>,</span></span>
<span class="line"><span> </span><span>'key'</span><span> </span><span>=></span><span> </span><span>env</span><span>(</span><span>'AWS_ACCESS_KEY_ID'</span><span>),</span></span>
<span class="line"><span> </span><span>'secret'</span><span> </span><span>=></span><span> </span><span>env</span><span>(</span><span>'AWS_SECRET_ACCESS_KEY'</span><span>),</span></span>
<span class="line"><span> </span><span>'region'</span><span> </span><span>=></span><span> </span><span>env</span><span>(</span><span>'AWS_DEFAULT_REGION'</span><span>),</span></span>
<span class="line"><span> </span><span>'table'</span><span> </span><span>=></span><span> </span><span>'cron-check'</span><span>,</span></span>
<span class="line"><span> </span><span>'endpoint'</span><span> </span><span>=></span><span> </span><span>env</span><span>(</span><span>'DYNAMODB_ENDPOINT'</span><span>),</span></span>
<span class="line"><span>],</span></span>
<span class="line"></span>
Next, we created a value object CachedCronCheckDefinition
which has a save
method that will write its values to DynamoDB.
<span class="line"><span>// in the save method of `CachedCronCheckDefinition`</span></span>
<span class="line"></span>
<span class="line"><span>public</span><span> </span><span>function</span><span> </span><span>save</span><span>()</span><span>:</span><span> </span><span>self</span></span>
<span class="line"><span>{</span></span>
<span class="line"><span> </span><span>Cache</span><span>::</span><span>store</span><span>(</span><span>'cron-check'</span><span>)</span><span>-></span><span>set</span><span>(</span><span>"cron-check-definition-{</span><span>$this</span><span>-></span><span>uuid</span><span>}"</span><span>, </span><span>json_encode</span><span>(</span><span>$this</span><span>-></span><span>allProperties));</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>return</span><span> </span><span>$this</span><span>;</span></span>
<span class="line"><span>}</span></span>
<span class="line"></span>
We still want to use MySQL as our source of truth, so we can easily make modifications using Eloquent. We sync a CronCheckDefinition
in MySQL with a CachedCronCheckedDefinition
in DynamoDB using a couple of model lifecycle hooks.
<span class="line"><span>// in the `CronCheckDefinition` model</span></span>
<span class="line"></span>
<span class="line"><span>public</span><span> </span><span>static</span><span> </span><span>function</span><span> </span><span>booted</span><span>()</span></span>
<span class="line"><span>{</span></span>
<span class="line"><span> </span><span>static::</span><span>saving</span><span>(</span><span>function</span><span> (</span><span>CronCheckDefinition</span><span> $cronCheckDefinition) {</span></span>
<span class="line"><span> </span><span>try</span><span> {</span></span>
<span class="line"><span> (</span><span>new</span><span> </span><span>CachedCronCheckDefinition</span><span>(</span></span>
<span class="line"><span> </span><span>$this</span><span>-></span><span>attributesToCache</span><span>()</span></span>
<span class="line"><span> ))</span><span>-></span><span>save</span><span>();</span></span>
<span class="line"><span> } </span><span>catch</span><span> (</span><span>Exception</span><span> $exception) {</span></span>
<span class="line"><span> </span><span>report</span><span>($exception);</span></span>
<span class="line"><span> }</span></span>
<span class="line"><span> });</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>static::</span><span>deleting</span><span>(</span><span>function</span><span> (</span><span>CronCheckDefinition</span><span> $cronCheckDefinition) {</span></span>
<span class="line"><span> </span><span>try</span><span> {</span></span>
<span class="line"><span> </span><span>CachedCronCheckDefinition</span><span>::</span><span>find</span><span>($cronCheckDefinition</span><span>-></span><span>uuid)</span><span>?-></span><span>delete</span><span>();</span></span>
<span class="line"><span> } </span><span>catch</span><span> (</span><span>Exception</span><span> $exception) {</span></span>
<span class="line"><span> </span><span>report</span><span>($exception);</span></span>
<span class="line"><span> }</span></span>
<span class="line"><span> });</span></span>
<span class="line"><span>}</span></span>
<span class="line"></span>
Finally in our RouteServiceProvider
, we update the binding to make use of the CachedCronCheckDefinition
.
<span class="line"><span>Route</span><span>::</span><span>bind</span><span>(</span><span>'cronCheckUuid'</span><span>, </span><span>function</span><span> (</span><span>string</span><span> $cronCheckUuid) {</span></span>
<span class="line"><span> </span><span>if</span><span> (</span><span>!</span><span> $cronCheckDefinition </span><span>=</span><span> </span><span>CachedCronCheckDefinition</span><span>::</span><span>find</span><span>($cronCheckUuid)) {</span></span>
<span class="line"><span> </span><span>abort</span><span>(</span><span>Response</span><span>::</span><span>HTTP_UNAUTHORIZED</span><span>, </span><span>'not authorized, are you sure the UUID is correct?'</span><span>);</span></span>
<span class="line"><span> }</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>return</span><span> $cronCheckDefinition;</span></span>
<span class="line"><span>});</span></span>
<span class="line"></span>
With this set up, the controller the handles incoming pings will have the CachedCronCheckDefinition
injected, and no single query to MySQL gets made.
We’re aware that CachedCronCheckDefinition
and the way it is set up is a bit verbose, and we could easily hide the inner workings a bit. As avoiding a MySQL query is so important, we think that it’s good to let the naming of things hint that the cache is being used.
Avoiding making a query improved the performance of our app drastically. In this tweet you’ll find some screenshots from our Grafana installation. I’ve omitted the actual numbers in these screenshots, but you can easily see the trend.
Made some changes at the Lamda endpoint that receives the ping from all scheduled commands at @OhDearApp
Refactored so that MySQL isnât used, instead we leverage DynamoDB now.
Result: much faster endpoint, less concurrency needed. Here are some graphs from Grafana. pic.twitter.com/KPdRKrzPry
— Freek Van der Herten ð (@freekmurze) September 6, 2021
If you want to learn how to use Grafana on AWS, check out this blogpost by Michael Dyrynda who also helped us setting all of this up.
In our test suite we have multiple tests to make sure the HTTP endpoints that receive the pings are working correctly. In those test we use a function assertNoQueriesExecuted
that makes sure that no single query is being executed. The implementation of that assertion is pretty simple.
<span class="line"><span>protected</span><span> </span><span>function</span><span> </span><span>assertNoQueriesExecuted</span><span>()</span><span>:</span><span> </span><span>self</span></span>
<span class="line"><span>{</span></span>
<span class="line"><span> </span><span>$this</span><span>-></span><span>assertCount</span><span>(</span><span>0</span><span>, </span><span>DB</span><span>::</span><span>getQueryLog</span><span>());</span></span>
<span class="line"></span>
<span class="line"><span> </span><span>return</span><span> </span><span>$this</span><span>;</span></span>
<span class="line"><span>}</span></span>
<span class="line"></span>
You can see that this assertion makes use of Laravel’s query log. In order for this assertion to work, you must enable the query log. We did that by adding DB::enableQueryLog
to the setUp
method of our base test case.
Just in time upgrades
Besides making performance optimisations, we made some other changes that will make it easier for us to build new features. One of those changes is that we made a change in how we store results on the runs
table. That table has a column results
where the results of a particular run are saved. We wanted to change the format in how the results are stored in that column.
The runs
table is probably the biggest one in Oh Dear. For each check we perform (and we perform tens of thousands of checks every minute), a record is inserted. We use this information on past runs to determine if we should send a notification (we only do that after a few failed runs). These runs
are also used on the history screen where our users can check the history of all checks we perform.
The runs table contains millions of rows. Because we perform checks all the time, it is very much a “living” table, where new data is inserted and cleaned up continously.
Having to update the results
field from the old value to the new value for all records would take a lot of time. That’s why we opted for another strategy: something that we call a “just in time upgrade”.
Whenever we call getResults
on a Run
model and detect that it is still in the old format, we quickly convert it to the new format.
<span class="line"><span>public</span><span> </span><span>function</span><span> </span><span>getResults</span><span>()</span><span>:</span><span> </span><span>CheckResults</span><span> </span></span>
<span class="line"><span>{</span></span>
<span class="line"><span> </span><span>if</span><span> (</span><span>$this</span><span>-></span><span>resultsAreStoredInOldFormat</span><span>()) {</span></span>
<span class="line"><span> </span><span>$this</span><span>-></span><span>updateResultsToNewFormat</span><span>();</span></span>
<span class="line"><span> }</span></span>
<span class="line"><span> </span></span>
<span class="line"><span> </span><span>return</span><span> </span><span>new</span><span> </span><span>CheckResults</span><span>(</span><span>$this</span><span>-></span><span>results)</span></span>
<span class="line"><span>}</span></span>
<span class="line"></span>
We can get away with a “just in time” upgrade because results
column isn’t used in queries, but only on that history and detail of a run screens.
In my mind this isn’t really a mind-blowing technique, but I hope this example of how we handled things can help you think out of the box: you don’t always need to convert everything in the database to a new format.
Closing thoughts
With all these changes, we have drastically improved the performance of our entire app. Adding a bit of cache at the right places, and preloaded bits of info, got us a long way. We’re now confident that our app will be able to handle our continued growth and new features we’ll be adding soon.
Now is the perfect time to try out Oh Dear. We can send you a notification whenever your app is offline, your SSL certificate will expire or is expired, whenever a broken link appears on your site, and much more. You can also create awesome looking public status pages, like this one from Flare. We aim to be the all-in-one service for monitoring an app.
We hope you’ve enjoyed reading this post and learned something new to improve the performance of your app.