AZ-204 Microsoft Azure Developer Associate – Monitor, troubleshoot, and optimize solutions part 2
- AZ-203/ 204 – Lab – Azure Cache for Redis
So here we are in Azure. Let’s go ahead and add a resource. We can choose Azure Cache for redis. Click on Create, give a name for the cache which will then formulate the whole DNS name. I’ll choose an existing resource group. I’ll leave the location centered us us for the pricing tier. Since this is just a demo, I’ll choose the basic tier and let’s go ahead and hit on Create. Now, once you have the Redis cache in place, let’s now go on to our code and let’s see how we can work with Azeoredis from Net code. Hi and welcome back. So now let’s look at the code which you can use to work with Azure Cache for Redis. So first let’s go over onto Tools. Let’s go over onto NuGet package Manager let’s inspect the NuGet packages for the solution. So if I go on to the install, what I’ve done is that I’ve installed a package known as Stack Exchange Redis. So this is available to work with Redis itself.
And we can use the same packages from net to also work with Azure Cache for Redis. Now, the first thing that we have to do is to make sure that we set up a connection to a zero cache for Redis. So the way that we do that is if I scroll all the way down, there is code in place which uses something known as the connection multiplexer that is actually used to connect to a Redis database or Redis instance. So we’re going to be using the same thing to actually connect to a zero cache A for Redis. So we need to get our connection string. So to get our connection string, again, very easy if you go on to a zero, if you go on to your cache, and if you go on to access keys. So you can copy either the primary or the secondary connection string going back to the program.
So this code is in place to basically establish a connection to a zero cache for Redis. The next thing what we have to do is to basically connect to the database cache so we get a handle to database using that connection which we establish with a zero cache for Redis. Now, once we do that, setting a key value pair. So remember, that Redis cache is a combination of key value pairs is very easy. So you can use the string set method to set the key and to set the value. So here I am setting the key as average orders and I’m setting the value as 120. If you want to get a value back from the cache, then what you can do is that you can go on to the cachet and use the string get method and use the key to get the value from various cache. So that’s how we can actually set and get values.
Now, you can also set an expiry period. So remember that you have to ensure that you have some invalidation or Eviction policy for the key value pairs that are setting in your cache. So one way is to basically set an expiration for the key. So when you do that, you specify what is the key and you specify a time frame. So here I’m specifying a value of 30 seconds. Now, going on now, you can also store objects in redis. So it’s not only key value pairs in terms of strings, you can also have an object in place. So over here I have a very simple class. So if I go on to Customer, I just have a simple Customer class which has customer ID and Customer name. Let me go back on to the program. So what I am doing is that I am creating a new object.
I’m using the Jsonconvert series object to seal the object and basically set it in the cache. And when I want to get the object back again, I use the desertized object method of the type Customer and again string get. And if you go a little bit ahead, so basically it’s string get and the object. So what is the key? Basically I’m setting in the cache and then I’m just populating the values onto the console, right, very simple. Let’s go ahead and run this program and see how it works. So let’s go ahead and run our program. So what I will do first to that I’ll set a key value pair and then I’ll go ahead and fetch the key value pair. So let’s run that first and see how it works. So I’ve run the program and I’m getting the key value pair. So the program is setting a key value and then we are fetching it from Redis.
Now, if I go ahead and stop the program now, let me remove the string set and let’s have the statement just to get the value from Redis cache. So you can see you’re getting the value because the key value has already been set by our program. Now, if you want, you can go ahead and delete the entire key or you can set an expiration for the key. Apart from that, we can also have an entire object. So let’s go ahead and run this program ensuring that an object is placed in our cache. And here you see you’re getting the value of the object. This is the object which has been retrieved from the Redis cache, right? So this marks the end of this lab in which we have seen how to work as your cache A for Redis using a net program.
- AZ-203/ 204 – Azure Content Delivery Network –Introduction
And welcome back. Now, in this chapter, let’s have an introduction on to the Azio Content Delivery Network service. So before we understand the service, let’s take a use case scenario. Now let’s say that you have a Web application. You are hosting it, let’s say, in Azure. It could be on a virtual machine. It could be in the ASIO App service. Now, let’s say you’re hosting that in the Central US. Region. Now let’s say that you have users across the world who are accessing your Web application. Now, obviously, users who are located in Central US. Would get the ideal response time since your Web application is in the same location. But let’s say you have users across the world, in North Europe or in East Asia. Now, when they try to access your Web application because of the distance between the user and the Web application, there would be a slight difference in the response time.
So they might get more latency. And that means they may not get the most ideal end user experience when they’re trying to access your Web application. Some of the pages might load slowly because of the high latency. Now, one of the things that you can do is to copy your application or make copies of your application onto different regions where your users are located in. But from a cost perspective, there is a better option, and that is to use the Azeo Content Delivery Network. So what exactly is the Azio Content Delivery Network? So in the Azure Content Delivery Network, we have something known as a point of presence. Now, there are different points of presence located across the world. So in this point of presence, you have something known as edge servers. So these are servers located in a point of presence. And these edge servers, I said are located globally.
So at different regions across the world, you have these edge servers. So now instead of users making a direct call to your Web application, what they would do is that they would make a call onto something known as a profile of the Azeo Content Delivery Network service. Now, what this Aze, your Content Delivery Network service, will do is that it will take the request from the user. It will look at the region for the user. It will then direct the request on to the closest point of presence in terms of latency. So you could have a point of presence with Ed servers located in the North Europe region. So the request from the user will then be directed onto those Et servers. Now, these edge servers can actually cache responses as well.
So if the response to the request so let’s say the request is being made onto a Web page for your application. Now let’s say the response is already present on the Web server. And now let’s say that the response is already present on the edge server. That same response will be sent back to the user so you have a faster response time. Now, behind of the Azure Content Delivery Network, you actually have your origin. So you’ll connect your origin has an end point to your Content Delivery Network profile. So your origin could be your Azure Web App. It could even be blob stored in a storage account. Or it could be your own custom origin. So you could have a Web app running on Azure Virtual Machine or maybe a Web app running on your local on premise data center.
So in case when the user makes a request onto the point of presence let’s say the response is not present on the edge server then this point of presence will make a request to your Web application to your origin. It will get the response. It will make sure that it sends a response back to the user. And it will also cache the response so that if another user makes the same request again, that response will come back from the cache itself. So is a response going all the way to the origin? If the response is already present in the cache itself of the edge server, that response will be sent back to the user. Now, obviously, in the cache itself, you can specify a time to live because not all responses to the request will be the same.
If the response changes, then ideally the cache will also have that change response. So you can decide on the type of caching that you can actually use for the Azure Content Delivery Network service. So when you want to have an application can be easily accessed by users across the world. So if you want users across the world to have the same experience when accessing your Web application, then consider using the Azure Content Delivery Network to distribute your information from your Web application to user across the world. Right. So this marks the end of this chapter in which we have looked at the basics of the Azure Content Delivery Network Service.
- AZ-203/ 204 – Lab – Azure Content Delivery Network
Hi and welcome back. So in this lab, let’s look at how we can implement the Azure Content Delivery Network service. So in this lab, I only have a storage account already in place. What we’re going to do is we’re going to create something known as a CDN profile. That’s a content delivery network profile. We’ll then add a sample HTML page to a container in a storage account, we’ll create something known as a CDN endpoint. So a CDN endpoint will actually point to the storage account and it will traverse the endpoint. So let’s go ahead and see how we can implement this. So here we are in Azure. Now let’s go ahead and add a new resource. So I’ll search for CDN. Hit on Create. Let’s give a name for the profile. I’ll choose the resource group. We can choose the pricing tier. Now, I’m going to be choosing the pricing tier of Standard Akamai. Now, the reason for this is that the propagation of the content takes only a minute.
So when we attach our storage account to the CDN profile as an endpoint, we can automatically traverse the contents in our storage account after a minute. If we choose standard Verizon, then what happens is that the propagation happens after 90 minutes. So just to ensure that we can have a quick demo, let’s choose Standard Akamai and let me go ahead and choose that. So I’ll select that we can now go ahead and also create the cedion endpoint. So one thing is a profile and then you can have multiple endpoints as part of the profile. So the endpoint can point to a storage account. So first let’s give an endpoint name and in the origin type. So we could choose a storage service, a cloud service, a web app, or even a custom origin. So if you have a web application running on your on premise infrastructure or a web application running on a virtual machine, you can choose that as the origin type as well.
If you choose a custom origin, you have to give the custom origin host name. But since we want to connect to a storage account, we can see that we are getting the block service because we already have a storage account in place and it’s in the same region as Central US. So let me go ahead and now create the seed in profile and the CDN endpoint. Now, while this operation is being carried out, let’s go on to all resources. Let’s go on to our storage account, let’s go on to the Blob service and let’s create a container in the public access level. Let’s give Blob anonymous access. Click on OK, let me go on to the production container and let me upload a sample or HTML file. So I’m uploading the file onto our container. So now we have the file in place. So let’s come back once our CDN profile and endpoint are in place.
Now, once the profile and the endpoint is in place. So there will be two separate resources created. One is the profile and the other is the endpoint. So the endpoint basically points to now our storage account. So if you go on to the origin, you’ll actually see the details of the storage account. Now, if you go on to the overview, you can take the endpoint hostname. If I go on to a new tab, if I go on to the container, I can get my sample HTML file. So remember now this is going via our endpoint, and we are not going to the URL of a storage account. It’s actually being done via the Azure CDN. So this is good. When users across the world have to access the objects which are in your storage account, please know that for the endpoint, you can also map your own custom domain. So if you have a company domain, you can actually map it to the endpoint point, right? So this marks the end of this chapter.
- AZ-203/ 204 – Azure Content Delivery Network Cache
Hi and welcome back. Now, let’s go over the objective given in the exam when it comes to Azure Content Delivery Network, and that’s caching. So Caching is available in the content delivery network. Now, you can control the caching of responses in Azure CDN with the use of caching rules. Now, these caching rules are available when you choose the SKU type of Azure CDN standard from Verizon or Akamai. And there are three caching behaviors you can set for an endpoint. So the first is to bypass the cache totally. So here, Azure CDN will not cache anything and also ignore any cache directive headers set by the origin. So your application or the origin, remember, can also set cache directives in the header itself. So if you set the CDN caching behavior as bypass cache, that means zero CDN will also ignore whatever are the cache directives in the header as well.
Next you have the override. So in Azure CDN also you can set a cache duration. So in the override, what happens is that, again here, if there are any cache directive headers set by the origin in the request, they are ignored. And here the cache duration that you specify in Azure CDN for the endpoint will be used. And finally we have a combination of both so that’s set if missing. So here, if there are any cache directive headers that are set by the origin, they will be used first. If not, then the Azure CDN cache a duration will be used. So, all important aspects when it comes to caching with Azure CDN. Now, when it comes to caching of requests which contain query string parameters, so first we have the option of igno query string. So this is the default mode.
So here, if a request is made with a query string parameter, the request along with the query string parameter is sent to the origin and then the response is cached. Any subsequent request will then start ignoring the query string parameters. Then you have the bypass caching for query strings. So here if the query strings are passed, then the request is sent directly to the origin and the response is not cached. So if you have any request to query sync parameters, that response object will not be cached in Azure CDN. And then you have cache every unique URL. So if you have a URL like this which has a query string parameter, that response will be cached. So every unique URL which has a request string parameter will be cached in Azure CDN. So let me just show you where you can actually set all of these settings in Azure CDN. So if you go to your endpoint, there is caching rules. So in the caching rules, the default caching behavior is set if missing.
So this means that if there are any cache directors, remember, in the header, those will be adhered to first. If not, then the cache duration of expiry of 70s will be set otherwise, you can also set the caching behavior. So if you want to ensure that you override any cache directives in the request, and you want to make sure that this cache duration expiration is applied on all requests, then set it to override. And here, if you set it to 1 minute, that means whatever is in the cache will expire after a minute. And then again, you have the option when it comes to query strings. So if you’re passing query strings as part of a request, you can decide on the query caching behavior. Now, you could also have caching rules based on certain conditions. So if you have a path for the request, you can actually create a caching behavior based on the path or on the file extension, right? So this marks the end of this chapter when it comes to caching for Azure CDN.