This chapter introduces guidelines for using cache technology to improve the performance of modern web applications. You will learn about best practices with Azure Cache for Redis (also referred to as Azure Cache) and content delivery networks (CDNs). These platform as a service (PaaS) technologies help to improve loading speed for customer sites and reduce the load for backend and data storage. They also increase the availability of the static content cached on a CDN such as media, scripts, and configuration files with a hundred copies across the internet.
Another common scenario where caching technology can help is delivering updates. IoT devices and mobile devices can download and install updates synchronously without overloading the origin server. Popular packaging registries such as NuGet and registries of container images such as the Docker Hub benefit from a CDN by caching their binaries close to the clients who want to download them.
In this chapter, you will get recommendations on how to reap the maximum benefits from caching solutions, including managing data size, connection encryption, and cache expiration settings, to increase security availability and reduce costs.
In this chapter, we will cover the following main topics:
In addition to the technical requirements outlined in previous chapters, you will require the following to follow along with the exercises:
Code in Action videos for this chapter: https://bit.ly/3Bsut1l
When we start introducing caching technology, we should look back and find out what cache technology is. The cache is a service that stores data, like in the database, but provides the fastest access and does not persist data longer than requested by time-to-live settings. The data stored in the cache is usually the result of previous calculations or commonly requested data retrieved with a higher speed than from the database. In other words, the cache is a temporary storage of precomputed data. What data can be cached for modern web services? Firstly, the output of pages. Millions of page views on modern websites that retrieve the same page that is rarely updated can cache the output to return the user the previously generated response and save website resources.
The idea of caching temporary output is not new. Caching solutions were generally used for application services such as websites way before Azure built its first data center. One of the first officially supported caching algorithms was output caching. It was introduced in ASP.NET and developers can use web server memory to temporarily cache output. It works perfectly for static or almost static legacy websites, but it is not suitable for modern websites now because changes happen so often and page content expires immediately after caching. Another recommended approach is caching content based on query parameters. It helps in some cases but significantly increases memory consumption and slows down websites. Overall, output caching is not the best approach for modern websites. Meanwhile, the caching of objects used for generating pages will improve performance like upgrading a database will.
For example, an e-commerce website page contains a list of products with their given prices and stock quantities. The product list is rarely updated and can be cached for days; meanwhile, the prices may change every hour and stock items may be updated every minute. So, caching output for minutes does not improve performance but can cause inconsistency with stock counts. Clients will complain if they see that a product available on the main page cannot be ordered because it is out of stock.
How do we solve these problems and speed up performance? Let’s imagine a powerful technology established to temporarily cache the list of products for days, the price of products for hours, and the stock count for less than a minute. Let’s imagine the app can access that service faster than going to the database. Otherwise, there is no reason to have the data cached. Let’s imagine we can manually update this temporary storage when the data is changed in the source database (for example, an item is ordered and the stock count needs to decrease). Azure Cache for Redis should be considered for this kind of scenario.
The Redis cache is a powerful memory-based caching solution that can perfectly support our scenario and apply different treatments for each type of data. In other words, the Redis cache can perfectly support cache-aside patterns and can seriously improve your app performance. There is a trade-off between consistency and performance but you have all the power to manage and adjust data in the cache until it expires.
Let’s look at how we can implement a cache-aside pattern in Azure. We will also recommend two scenarios for refreshing data in the cache and you can select which works for your application better.
First, if the data is stored in the cache, it needs to be pulled at high speed. It will work best if you have data cached on the same server. Unfortunately, the local cache of one server cannot be used for another, so this prevents scaling. Instead of using local memory, the cache can be deployed on the dedicated server with high-speed connections.
Second, the data needs to be updated when it expires. For that reason, we can use a custom time to live (TTL) for each type of data you use. When the data expires, it can be cleaned up from the cache so it can be replaced by updated data from the database. Here, we can make the data client load the data and store that data in the cache to speed up the next load. The alternative option is to have the cache server generate an event to trigger your application just before the data expires to load updated values from the database.
Both options have pros and cons. If you make the client server load the data from the database when it is expired from the cache, you will have a cold request first before the hot request will reuse the data from the cache. Alternatively, if you build a service to refresh the expired data from the cache, you will avoid the first cold request but must spend more memory loading the data that is not going to be reused. Because the price you pay for caching in Azure depends on your memory usage, the preload solution can be quite expensive.
Finally, you can combine the advantages of preloading data and minimize the size of stored data by providing custom settings for each data type that you cache. Moreover, you can manually monitor changes in the database and update the cache and the database in the same transaction.
Let’s observe how the cache-aside pattern works for the e-commerce website we discussed previously. You will notice that the first request hits the database but the second request retrieved cached versions of products and decreased the database’s load:
Figure 9.1 – Implementation of the cache-aside pattern with Azure Cache for Redis
You already know the benefits of caching and the main patterns used for building effective caching strategies and reducing consistency issues. Now is the best time to take a look at what caching services are available in Azure. We will start from Azure Cache for Redis and continue with Azure CDN.
Azure Cache for Redis is the Microsoft implementation of well-known Redis software. Redis means Remote Dictionary Server and was initially implemented in 2009 as a memory management service implemented on C and hosted on a Unix-like platform. Redis provides open source NoSQL storage, allowing the persisting of complex data structures for key-value storage. Redis supports clustering technology, which prevents it from failing and losing data in memory.
Microsoft adopted Redis technology to successfully run in Azure as a PaaS with a single server tier and cluster as well. Moreover, Azure Cache for Redis provides Redis open source and a commercial product from Redis Labs (Redis Enterprise) as secure and dedicated instances with full Redis API compatibility. Azure Cache for Redis can be used for various scenarios, including a cache-aside pattern for content and data persistence, a message broker, and even a sessions service transparently integrated with the .NET platform. Azure Cache for Redis depends on the pricing tier, deployed as a standalone or cluster, along with other Azure databases such as Azure SQL or Cosmos DB. Because Redis technology is based on the TCP protocol, it requires a specific port to be opened for communication with services provisioned in Azure or on-premises.
When you provision Azure Cache for Redis, you need to select the appropriate pricing tier. There are several pricing tiers available to meet everyone’s expectations:
Each pricing tier supports several sub-tiers with the ability to granularly manage required memory and connection. The number of client connections might also limit your application if using low-tier mode. Luckily, you can monitor connections from Azure Monitor and be notified when it reaches the limit to upgrade your instance to higher sub-tiers.
In the next paragraphs, you will learn more about Azure Cache by provisioning an instance in Azure and discovering its advanced settings, including pricing tiers, console commands, and security configuration. Let’s learn how to provision the service from the Azure CLI.
Provisioning an Azure Cache for Redis instance can be completed from the Azure portal, the Azure CLI, and PowerShell. To complete provisioning, you need to choose the region where you want the service located, the unique DNS name, and the pricing tier. The region should be the same as you use for the hosting of your solution to avoid cross -data center charges for traffic. The pricing tier should be chosen based on the requirements for memory consumption. For example, for the Basic tier, you can choose sizes between 0.25 GB and 50 GB. If the consumption grows, you can upgrade your size and migrate to a higher tier.
By running the following CLI commands, you will learn how to provision your Azure Cache for Redis instance and retrieve the connection keys for the connection. The commands should be run in Azure Cloud Shell, bash, or the bash terminal, with the Azure CLI installed locally:
#create a resource group
az group create --location eastus2 --name AzureCache-RG
#to avoid name collisions generate a unique name for your account
account=azurecache$RANDOM
#create Basic SKU instance
az redis create --location eastus2 --name $account --resource-group AzureCache-RG --sku Basic --vm-size C0
#retrieve key and address
$key=$(az redis list-keys --name $account --resource-group AzureCache-RG --query primaryKey -o tsv)
echo $key
echo $account.redis.cache.windows.net
The list of commands can be found at https://github.com/PacktPublishing/Developing-Solutions-for-Microsoft-Azure-AZ-204-Exam-Guide/blob/main/Chapter09/1-redis-provision/demo.azcli.
Upon successful execution of the previous commands, you should have provisioned an instance of the Azure Cache for Redis in your Azure subscription. In further tasks, you will learn how to configure the instance and access the Redis console.
From the Azure portal, you can find the instance you built and investigate the following advanced settings for security, diagnostics, and monitoring. You can also open the Redis console from the browser to run the commands to manage the instance.
To connect to the Redis instance in Azure from the SDK or console clients, you need to provide a connection key. There are two keys provided to meet compliance requirements for the periodic rotation of key values. You can choose the first key or copy the connection string with the key; then later, you can update the connection to leverage the second key and regenerate the first key without interrupting the application. Be aware that keys provide you full access to the key values and allow you to read, write, and manage the instance. You can find keys and connection strings from the Access key section of your Redis page on the Azure portal.
Azure Cache for Redis provides a variety of options for managing networking connections. You can rebind default port numbers. The 6379 and 6380 TCP ports are used for open and encrypted SSL connections. Remember that Azure Cache for Redis uses fast TCP communication instead of slow HTTPS requests.
The Azure Redis cache provides firewall rules customization. By default, no rules are defined, and connections are allowed from any IP address. You can replace the default rule with a rule for the exact IP or IP range. The private endpoint is available for leverage to connect specific types of services directly to the cache – Azure App Services, for example.
You can provide the firewall rules in the Firewall section of your instance on the Azure portal.
There are several important metrics for Azure Cache for Redis that you need to monitor during production workload to avoid connection errors. A recommended best practice is setting up Azure alerts for these metrics to be notified when the workload exists in your instance. Here are two examples of important metrics:
For Premium and Enterprise pricing tiers, the Azure Cache for Redis instance can be configured with access from a virtual network. In this case, your cache instance can only be connected to virtual machines and applications within the virtual network of your choice.
There are several tools you can use to view and manage data stored in the cache:
You have just learned about the different types of client applications that you can leverage to monitor and observe the content stored in the cache. In the next section, you will learn how to leverage the Azure Redis console to execute commands to operate with different data types.
The Redis cache supports a variety of types to store any string or binary data in a key-value structure. It also supports the nested key-value structure. Each value associated with a key can be used as the key name of another key-value pair. The names of the keys should be self-explanatory, such as user-name or product:price:usd. A good naming approach will help you to manage the data in the cache explorer. For best performance, the value of the keys should be less than 100 KB and bigger values should be split into multiple keys.
The Redis cache supports the following types of data:
Furthermore, Azure Cache for Redis supports the following technical commands that you can also run to get additional information about a cache instance:
In the following hands-on exercise, you will be able to run these commands to understand how they work. Then, you will be able to leverage SDKs to get connected to cache instances and store and retrieve data in string, list, set, and hash data types. There are several C# SDKs you can integrate into your project:
SDK name |
Description |
ServiceStack.Redis |
The fork of the original C# client was written by Miguel De Icaza but significantly improved by adding new commands. |
StackExchange.Redis |
A well-known .NET client developed by Stack Exchange (Stack Overflow) for high-performance needs. We will use this package further for demo tasks. |
BeetleX.Redis |
A high-performance Redis client for .NET Core, the default supports JSON, the Protobuf data format, and asynchronous calls. |
FreeRedis |
This .NET client supports Redis 6.0+, clusters, sentinels, pipelines, and simple APIs. |
Table 9.1 – Popular C# SDKs to manage Redis from code
In this section, you have learned what different types of data are available in Redis. Now, we’ll move on to the next section to learn the commands to manipulate those data types from the Redis console.
To get a better understanding of how caches manage different data types, the best option is to try to execute commands from the console and observe the results. To run these commands, you need to open the console in the Azure portal and execute the commands one by one. To get connected from redis-cli, you need to provide a name for your Azure cache instance and the access keys for the connection. To run the Redis console from the browser, you can open the Azure portal, find the instance you built from the previous demo, and select the Overview section. At the top of the page, under the name of your cache instance, you can find the console icon to run the console. Run the complete list of commands that can be found here:
https://github.com/PacktPublishing/Developing-Solutions-for-Microsoft-Azure-AZ-204-Exam-Guide/blob/main/Chapter09/2-commands/commands.txt
To observe the results directly from the cache instance, you can install the Azure Cache extension for Visual Studio Code, open the Azure tab (next to the extension), and find the Cache section. If you’ve already run a command to set values, you will see DB 0 and a list of the keys that you can open to observe the value.
Now you already know how to configure, test, and monitor your cache instance. You know how to leverage commands for observing the data in the cache. In the next task, you will build a C# console app to read and write data in different formats including lists, sets, and hashes. In the same way as you operated with the previous simple command from the preceding console, you can implement code to persist objects from a database with the cache-aside pattern. The following link contains a ready-to-go project to demonstrate your connecting and data manipulating operations:
https://github.com/PacktPublishing/Developing-Solutions-for-Microsoft-Azure-AZ-204-Exam-Guide/tree/main/Chapter09/3-redis-sdk
The following table lists the classes used for accessing and manipulating data of Azure Cache for Redis in the previous code example:
Class |
Description |
RedisKey |
Represents the key stored in the cache. It can implicitly convert string to and from and binary values. |
RedisValue |
Represents the value (string or binary data) of the key. |
Class |
Description |
ConnectionMultiplexer |
Has multi-purpose usage, mostly for accessing the Redis database and also retrieving server metrics such as status, subscribers, and slots. It also handles connection state change events. This thread-safe class instance should be configured just once. |
IDatabase |
The main database interface, implementing all operations with keys and values. It maps the Redis console commands to use ReadisKey and RedisValue. It has asynchronous and synchronous commands. |
Table 9.2 – SDK classes for data manipulation in Azure Cache for Redis
In the previous code example, you explored commands to persist different types of data in the cache. In the next section’s code examples, you will build a session service for the graceful scaling of a web application.
In the following code example, we implement a session state service for sharing state information between website instances. Usually, legacy applications do not provide appropriate session handling for session information. As a result, session values get lost when the user’s request is routed to another server instance, or the number of instances is scaled in. Losing session values can lead to application crashes and harm your users. Azure Cache for Redis provides a solution with a centralized session store that makes your application pass session values to another instance through writing and reading the session data from cache.
Let’s look at the following ASP.NET Core configuration with session handling implemented. Before you run the project, you need to update the appsettings.config configuration file and provide your Redis instance connection string in the AzureRedis parameter:
https://github.com/PacktPublishing/Developing-Solutions-for-Microsoft-Azure-AZ-204-Exam-Guide/tree/main/Chapter09/4-redis-stateserver
In the project, you can observe how the session state is enabled in StartupRedis.cs by adding builder.Services.AddSession and builder.Services.AddDistributedMemoryCache to the initialization code. The StackExchange provider for Redis is also configured in the same file to persist the session.
When testing how the cache hosts the session during server restarts, you need to run the project. Then, you should enter the session value in the text field and save it by clicking on the Set button. The update automatically refreshes and pulls the value from the session to the page, and you can observe the values. The values are also saved in Azure Cache for Redis transparently.
Then, you can restart the web server, clean up the session in memory, and mimic swapping slots or scale in. When the server starts again, you can refresh the web page and retrieve the stored value. Now, the values are transparently loaded from Azure Cache for Redis back to the session and appear on the page. The following screen demonstrates the session loaded from the cache:
Figure 9.2 – The page loaded the Azure Rocks session value from the session stored on Azure Cache
In addition to session state, there are other cache scenarios when the cache is used as a message broker. For example, one application is used for updating data in the Redis cache and another application will read the updated data. Leveraging Azure Cache for Redis is not an optimal solution in terms of price, but ideal in terms of performance. Meanwhile, we recommend using Azure Service Bus or Azure Queue for implementing a message broker pattern. These messaging services will be explored in Chapter 13, Developing Message-Based Solutions.
When you design and develop an application, you need to be aware of the following guidelines about the appropriate use of Azure Cache:
You have already learned about the advantages of using Azure Cache for dynamic content managed from the application server and replacing the database. The next section of the chapter will introduce the caching technology available for static content. Let’s take a look at how caching static content can improve modern web applications.
You are already familiar with how to cache dynamic content with a cache-aside pattern. Now, let’s take a look at static content caching technology. The cache-aside pattern is still in play but now it needs to cache static files such as images, videos, JavaScript files, and CSS files. For legacy websites, these files are usually stored on the server, and delivery to the customerdelivery to the customer takes time takes time. Caching those files close to the user can significantly speed up loading and let the web server focus on processing dynamic pages. Azure proposes a solution by caching static files globally as close to users as possible with its CDN.
A CDN is a distributed network of endpoints that can host and deliver web content to users quickly and efficiently. To reduce latency, CDNs cache content on edge servers in point of presence (POP) locations close to end users. POP works like an old-school proxy in corporate networks but is geographically spread across many locations. Azure CDN provides developers with a global option for rapidly delivering high-bandwidth content to customers. Azure CDN also helps minimize traffic from the origin server and suits scenarios involving delivering application updates. The following figure represents how static content can be cached on POP servers:
Figure 9.3 – Implementation of the cache-aside pattern with CDN
Azure CDN supports two caching modes: loading assets on demand (based on the user request) and prepopulating assets from the origin to minimize delays with the first request. If you prefer to use an on-demand model, the first user request will initialize loading the asset directly from the origin and store it on the POP server. The first request might be executing slower than the others because it has to wait until the POP loads the content from its origin. All other requests can load the copy of assets directly from the POP server. The consistency issue discussed here can be avoided by configuring an appropriate TTL for each type of file.
The prepopulating or preloading approach will help you avoid the slowness of the first request, but will generate a traffic spike on the server because the content has to be loaded by the command, not demand.
The Azure CDN will leverage partner networks (Akamai and Verizon) and does not store files in Azure data centers unless you provision Azure CDN with the standard tier from Microsoft or choose content caching with Azure Front Door.
Azure Front Door is the networking service that allows access to the Microsoft global networking edge. This service provides you with fast and secure access to a web application deployed as PaaS. Front Door is an advanced load traffic manager working on Layer 7 and supports HTTP/HTTPS protocols. It can analyze traffic and protect your web application from cyberattacks. It’s a globally available service that can route customer traffic from the entry point to the nearest available application backend. Azure Front Door is based on routing settings, health monitoring checks, and failover configuration.
You might be curious as to why Azure Front Door is combined with CDN services. Originally, the classic Azure Front Door was responsible for providing scalable and secure entry points for the fast delivery of content. Now, this service is responsible for caching content, as well as for the fastest delivery of content. It means that Front Door now can offer CDN services. Previously, Microsoft offered CDN services from its data centers; now, these services have migrated into one, with a secure cloud CDN offering content caching and acceleration, intelligent threat protection, and global load balancing for your websites. We will further discuss configuration concepts with Front Door and CDN services and also discover another product provided in Azure CDN.
Caching static content is a trivial task but caching dynamic content sometimes is not possible using the traditional approach. Another technology can be leveraged with Azure CDN to help cache dynamic content. Dynamic site acceleration (DSA) is an algorithm responsible for delivering dynamic content, involving the following techniques:
You have learned about the main technologies that help Azure CDN and Azure Front Door to successfully manage traffic and to cache static and dynamic content. Now is a good time to learn how to provision your Azure CDN instance.
When you provision Azure CDN, you need to deploy the CDN profile and CDN endpoint. Several endpoints can be configured on the same CDN profile. For provisioning Azure CDN, you do not need to provide an exact location because the location is set to “Global Network.” It means the servers will be located in all available geographical regions. All you need to do is to choose a unique name and select a product. Each product you choose has a different specification and price. You cannot change the product you have selected after deploying Azure CDN. The following products are available to select:
After provisioning a CDN profile, you can configure one or more CDN endpoints. An endpoint’s configuration requires a DNS unique name, origin host and port, origin type (Azure Blob, Azure Web Apps, and custom origins are supported), a path for caching, and a content optimization type. After configuration of the CDN endpoint, you will receive the DNS address and be able to reroute user requests to the provided address.
The pricing model of Azure CDN depends on the product selected for provisioning. Features between content delivery networks will vary. The charges depend on use and consist of charges for traffic moving between zones, configured cashing rules, and enabling DSA. Traffic charges are the most significant and depend on the zone of request and the zone of origin location. Each zone charge depends on the chosen CDN and will vary.
In the script, you need to execute Azure CLI commands locally in bash or upload the files you need to Azure Cloud Shell. The script will use static files provided in the folders and upload them to the storage account. Then, you build a CDN instance and download files directly from the storage and from the CDN endpoint to compare the speed.
Please open the script to get familiar with the commands. First of all, the script will build a resource group and an Azure Blob storage account for hosting static files. The storage account is created in the South Korea data center, but you can choose another location far away from your current location (Australia, for example). You have a few files to cache: an image, video, and JavaScript file will be uploaded to the storage account. Second, in the same folder, you can find an HTML file and you need to run a command to replace the links in the file with links to your Blob storage. The file can be opened locally to observe the overall performance. The third step is provisioning a CDN profile and endpoint to cache files from Blob storage. You also test the speed of loading from the CDN. The script will create a Microsoft-tier CDN because it will be updated faster than other tiers. If you are interested in exact measures, you can use the curl tool. Refer to the following link for further details:
Now you know how to provision Azure CDN and create endpoints to cache static content. In the next section, you will learn how to use advanced CDN features, such as caching rules and global filters. You will also learn about preloading and purging content features. From the previous demo, you have provisioned the Azure CDN profile and endpoint and now you can observe the existing settings of your account.
Let’s start with caching rules and management to control caching behavior. In general, publicly available content can be cached based on caching rules. Caching rules are managed by TTL values. The default TTL for each file can be obtained using the Cache-Control header value from the origin server and can be overwritten using internal Azure CDN rules.
CDNs can override cache settings through the Azure portal by configuring CDN caching rules. The origin-provided caching settings are disregarded if you add one or more caching rules and set their caching behavior to Override or Bypass the cache. For any other content without the Cache-Control header value, Azure CDN automatically applies a default TTL of 7 days, unless it’s explicitly overridden by caching rules.
Azure CDN offers two ways to control how your files are cached: using caching rules and using query string caching. You can configure global caching rules for each endpoint in the profile to affect all requests to the endpoint. The global caching rule can override any source settings with Cache-Control headers. You also can configure custom caching rules to match specific paths and file extensions. Custom rules are processed in order and can override the global caching rule as well.
Caching rules will help users to get the refreshed content when the TTL expires, but this approach does not include cases when the content is updated or modified at the source. The CDN might still have the old version, even if the file is updated at the source. To avoid this scenario, best practice recommends generating a new URL for a new version of your assets. Another popular approach is to purge a specific path or file type/name when the file is updated at the source.
Purging cached content will force all edge nodes to retrieve newly updated assets. You can purge all files on the nodes or purge files in the specific path, for example, /pictures/logo.png. You also can purge the files or folder by providing a wildcard, for example, /pictures/*.
Be aware that purge functionality will remove content from the edge nodes but the browser cache and proxy servers can still have content cached. The files will be eventually updated after the expiration of the TTL. Additional delays can also be caused by a purge operation taking approximately 2 minutes. That is why best practice recommends generating a new URL for the new version of the content.
From the previous discussion of the CDN caching pattern, you will remember that first requests can take longer because the edge server has to request a file from the origin and save it locally for the next request. The second request for the file will be significantly faster if the file has not expired yet. To avoid this first-hit delay, CDN gurus initialize and preload the content. Preloading provides a better customer experience and will reduce spikes of network traffic on the origin server. Content preloading works best for large files, such as software updates or movies that need to be simultaneously released to a large audience. You can preload content by providing an exact path to the file or by using regular expressions.
Note
Be aware that the preloading feature is available only with Azure CDN Standard from Verizon and Azure CDN Premium from Verizon.
When a user requests a website with CDN, the content is delivered by default for all available locations based on the CDN product deployed. Meanwhile, you can limit access to the content based on the country or region. The total charges for CDN depend on the zones, so blocking some countries can prevent the caching of content in specific regions and decrease charges.
You can configure geo-filtering for specific paths and recurring folders or configure rules for the root folder (for example, /, /pictures/, or /pictures/logo.png). Neither wildcard nor regular expressions are supported in the path. Only one rule can be applied to the same relative path. When you configure the rule, you can apply a list of the countries/regions and an Allow or Block action. The Allow action lets users from the specified countries/regions get access to assets requested from the recursive path. The Block action will deny access to the content requested from the recursive path. The Allow action is configured by default so all users from all regions can access the content.
Note
Be aware that the geo-filter feature is not available for Azure CDN Standard from Microsoft. Consider the fact that applying geo-filter will apply 10 minutes after saving the settings.
This demo will continue the previous demo where you provisioned a CDN profile and endpoint. Now, files from the storage account should be cached by the CDN. Now it is time to prepare and upload your HTML page to test the CDN in action. Next, you need to replace the links to your CDN endpoint by running commands from the script. Run the command from the bash console to upload an HTML page to the static website on the storage account you built before. You can open a generated link to observe the caching performance. You can also compare it with the HTML file pointed to the storage account from the previous demo. The performance increase should be visible. Please note that files can be cached on the browser side and for ideal comparison, you should clear the cache from the F12 Developer Tool.
The next task is providing custom TTL policies for content. You will set TTL on the JavaScript file on the origin level by using the Cache-Control header. If your cache still retrieves the old version, you will use the purge command and the test page will load the latest JavaScript script. You can download the script from the following URL:
https://github.com/PacktPublishing/Developing-Solutions-for-Microsoft-Azure-AZ-204-Exam-Guide/blob/main/Chapter09/6-cdn-advanced/demo.azcli
You have already learned how to manage Front Door and CDN resources from the Azure portal and the Azure CLI. Now is a good time to get familiar with the SDKs you can leverage to manage resources. Let’s introduce the best management SDK for .NET Core, named Fluent. The exact package to manage CDN resources, Microsoft.Azure.Management.Cdn.Fluent, can be added to the project. You also can register a service account and provide it access to your subscription/resource group with contributor rights. The app ID, secret, and tenant can be saved in the configuration file.
The following code snippet will help you configure the instance of the Azure class to get access to the resources in your default subscription:
using Microsoft.Azure.Management.ResourceManager.Fluent; var credentials = SdkContext .AzureCredentialsFactory.FromFile("yourconfigfile"); var azure = Microsoft.Azure.Management.Fluent.Azure .Configure() .Authenticate(credentials) .WithDefaultSubscription();
The following example demonstrates how to create a CDN profile:
using Microsoft.Azure.Management.Cdn.Fluent; ICdnProfile profileDefinition = azure.CdnProfiles.Define("YourCdnProfileName") .WithRegion(Region.USEast) .WithExistingResourceGroup("YourResourceGroupName") .WithStandardVerizonSku();
This example will define the endpoint for the previous CDN profile:
cdnendpoint = profileDefinition.DefineNewEndpoint() .WithOrigin("your_origin") .WithHostHeader("your_origin") .WithQueryStringCachingBehavior( QueryStringCachingBehavior.IgnoreQueryString) .Attach();
As you can see from the previous code snippets, the manipulation of CDN resources is a trivial task and you have a powerful SDK to manage Azure resources. This is another example of infrastructure as code and it will be covered in Chapter 13, Developing Message-Based Solutions.
In this chapter, you have learned about caching with Azure Cache for Redis and Azure CDN. Both caching services can be successfully used for the cache-aside pattern to implement caching for static and dynamic content and configured with a custom expiration time to avoid inconsistency with the source of the data. Azure Cache for Redis is a PaaS available in Azure with a large scale of different price tiers. Azure Cache for Redis supports multiple data types such as strings, integers, lists, sets, and hashes to store strings, binaries, and object fields. Azure Cache for Redis should be used as temporary storage and the application should not rely on the cache data.
Azure CDN is designed for caching static data such as images, videos, and documents. The CDN can also help to speed up the loading of static content such as media, CSS, and JavaScript files. Azure CDN works as a proxy server for your customers and helps them to cache files very close to their location. The CDN network supports a variety of locations to store files, including Microsoft, Verizon, and Akamai data centers. Configuration settings will help you set up different caching policies for files in the path, specific extensions, and depending on the region/country of requests.
By provisioning and configuring Azure CDN and Azure Cache resources, you get experience required for the exam. Now, you can leverage caching technology in your web-based solution and recommend the appropriate size and configuration, depending on the requirements of exam questions.
In the next chapter, you will learn about monitoring technologies and tools that help you minimize downtime and proactively diagnose possible performance bottlenecks and avoid crashes. Let’s move on to the next chapter.
13.59.243.194