This post was most recently updated on August 27th, 2020.Reading Time: 4 minutes.
This article explains one easy way for you to improve the performance and uptime of your Azure App Service. I’ll explain a simple but kind-of-hidden and surprisingly badly documented feature – Azure App Service Local Cache, that is – which might make a huge difference. It sure did for me!
Since it’s such a no-brainen, I’m kind of half expecting everyone else to already having known about this, but since nobody told me about it, I’m going to document it for future use.
Let’s get the Azure App Service facts straight first:
- Your App Service might have an SLA for the uptime of 99.9% or better
- Whatever your SLA might be, your app service will still occasionally be down for maintenance
- One of the biggest reasons for maintenance is Windows Update (or rather, any update mechanism for a resource your App Service has a dependency on), that’ll run on a different cadence for different resources, and these resources restarting might cause an outage to your app service as well.
That all said, one easy way to increase the uptime of your app service is to enable local cache on it.
How do you know if Azure App Service Local Cache will help you?
Easy – if your app service restarts a lot, and while investigating, you run into one of these events:
- Platform (File Server Upgrade)
- Platform (Infrastructure Upgrade)
And your App Service Diagnostics have “Web App Restart” events like below:
The Local Cache feature is going to eliminate File Server Upgrades – and depending on whatever Microsoft is doing, you might run into them even a few times per week!
I mean, apparently you shouldn’t, but you still might. Our production did :) But not after enabling the cache!
Why does my Azure App Service reboot so often?
Well, it’s simple. Even a cloud service resides on someone’s servers, and those servers need to be updated every now and then. And that update causes a restart.
Namely, during the updates, the servers (such as file servers) hosting the website content will get rebooted, and the file volumes mounted on said servers will move to different servers. This causes the web apps associated with a file volume to get recycled, as the underlying file volume is moved to a different server.
During part of the file volume movement, file access is also temporarily switched to readonly mode, which might cause any code trying to write to wwwroot to fail. And obviously, during the re-mounting, users will experience timeouts, server errors, or just quite a lot of latency.
What is “Azure App Service Local Cache”?
By default, the content of your Azure App Service site is actually stored in Azure Storage and shared between the app service instances. This enables all of the different front-end servers to use the latest version of whatever you’ve deployed right after it’s uploaded – however, it comes with a caveat.
Whenever the servers hosting your Azure Storage contents are updated (be it Windows Update or whatever), your app service will be restarted in order to point it to the new host for your files.
The Local Cache -feature will host all files in /site or /siteextensions
In case you’re fine caching the files locally (the local cache will get overwritten whenever the app service is recycled or moved to another virtual machine), I’ll explain how to enable it in the steps below:
How to enable Azure App Service Local Cache?
Time needed: 10 minutes.
The steps required to enable local cache on app service.
- Navigate to your App Service on Azure Portal
- Navigate to Settings > Configuration
- Add the following App Setting keys with the following values
WEBSITE_LOCAL_CACHE_OPTION : Always
WEBSITE_LOCAL_CACHE_SIZEINMB : 1000
Make both of them “Sticky” to make sure the value doesn’t get swapped between slots.
The second property can go up to 2048 MB, in case your site is huge.
In “Advanced editing” mode, the JSON will look somewhat like this:
"name": "WEBSITE_LOCAL_CACHE_OPTION ",
- Your App Service will restart
After the restart, your site should be hosted locally instead of on Azure Storage.
With that, You should be golden. Good job!
A couple of caveats I can think of:
- Copying the files over from the Azure Storage is not instant. Especially for bigger sites it might take a few minutes after the App Service has restarted.
- Any runtime write operations to /site or /siteextensions are discarded when the app is recycled
- Any runtime write operations to /home/data or /home/logfiles are synchronized to the shared content store periodically, but the copy-operation is not guaranteed!
- You won’t get restarts due to “File Server Upgrades” – but you will get them due to “Local Cache Event”
- Documentation says this might happen any time your site switches from “Shared Content” to “Local Content”. This obviously doesn’t make sense, because it would mean your site is going to restart always after having just restarted and switched over!
- Anyway – these restarts are very seldom.
- These look like in the picture below.
- How to configure Azure Function’s startup? - October 21, 2020
- How to list your git stashes? - October 14, 2020
- How to fix “System.InvalidOperationException: PowerShell Gallery is currently unavailable.” - October 5, 2020