Microsoft Azure logo

Enabling local cache for an Azure App Service

This post was most recently updated on March 28th, 2023.

4 min read.

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-brainer, I’m kind of half expecting everyone else to already have known about this, but since nobody told me about it, I’m going to document it for future use.

Background

Let’s get the Azure App Service facts straight first:

  1. Your App Service might have an SLA for an uptime of 99.9% or better
  2. Whatever your SLA might be, your app service will still occasionally be down for maintenance
  3. 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 a 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 the below:

Azure App Service restart due to "File Server Upgrades" - thanks, Windows Update.
Azure App Service restart due to “File Server Upgrades” – thanks, Windows Update.

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 read-only 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.

  1. Navigate to your App Service on Azure Portal

  2. Navigate to Settings > Configuration

    Settings > Configuration on Azure App Service.

  3. 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 ",
    "value": "ALWAYS",
    "slotSetting": true
    },
    {
    "name": "WEBSITE_LOCAL_CACHE_SIZEINMB",
    "value": "2048",
    "slotSetting": true
    }
    ]

  4. 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!

Caveats

A couple of caveats I can think of:

  • Copying the files over from 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”
    • The 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.
Azure App Service restart due to "Local Cache Event" - yeah, this happens too.
Azure App Service restart due to “Local Cache Event” – yeah, this happens too.

References

mm
4 1 vote
Article Rating
Subscribe
Notify of
guest

3 Comments
most voted
newest oldest
Inline Feedbacks
View all comments