Reader small image

You're reading from  Azure Serverless Computing Cookbook. - Third Edition

Product typeBook
Published inJun 2020
PublisherPackt
ISBN-139781800206601
Edition3rd Edition
Concepts
Right arrow
Author (1)
Praveen Kumar Sreeram
Praveen Kumar Sreeram
author image
Praveen Kumar Sreeram

Praveen Kumar Sreeram is an author, Microsoft Certified Trainer, and certified Azure Solutions Architect. He has over 15 years of experience in the field of development, analysis, design, and the delivery of applications of various technologies. His projects range from custom web development using ASP.NET and MVC to building mobile apps using the cross-platform Xamarin technology for domains such as insurance, telecom, and wireless expense management. He has been given the Most Valuable Professional award twice by one of the leading social community websites, CSharpCorner, for his contributions to the Microsoft Azure community through his articles. Praveen is highly focused on learning about technology, and blogs about his learning regularly. You can also follow him on Twitter at @PrawinSreeram. Currently, his focus is on analyzing business problems and providing technical solutions for various projects related to Microsoft Azure and .NET Core.
Read more about Praveen Kumar Sreeram

Right arrow

10. Implementing best practices for Azure Functions

In this chapter, we'll learn some of the best practices that can be followed while working with Azure functions, such as the following:

  • Adding multiple messages to a queue using the IAsyncCollector function
  • Implementing defensive applications using Azure functions and queue triggers
  • Avoiding cold starts by warming the app at regular intervals
  • Sharing code across Azure functions using class libraries
  • Migrating C# console application to Azure functions using PowerShell
  • Implementing feature flags in Azure functions using the App Configuration service

Introduction

This chapter covers some of the most important and common best practices that are followed in cloud-native applications. Along with the best practices, you will also understand how to overcome some of the limitations of Azure functions. Furthermore, you will learn how to migrate jobs from on-premises to serverless environments.

Adding multiple messages to a queue using the IAsyncCollector function

In the Saving profile picture paths to queues using queue output bindings recipe of Chapter 1, Accelerating cloud app development using Azure Functions, you learned how to create a queue message for each request coming from the HTTP request. Now let's assume that each user is registering their devices using client applications (such as desktop apps, mobile apps, or any client websites) that can send multiple records in a single request. In these cases, the back-end application should be smart enough to handle the oncoming load; there should be a mechanism to create multiple queue messages at once and asynchronously. You will learn how to create multiple queue messages using the IAsyncCollector interface.

Let's look at a diagram that depicts the data flow from different client applications to the Back-End Web API.

At a given point of time, as shown in Figure 10.1:

  • iOS App sends two messages...

Implementing defensive applications using Azure functions and queue triggers

For many applications, even after performing multiple tests of different environments, there might still be unforeseen reasons that an application might fail. Developers and architects cannot predict all unexpected inputs throughout the lifespan of an application being used by business users or general users. So, it's good practice to make sure that your application alerts you if there are any errors or unexpected issues with the application.

In this recipe, we'll learn how Azure functions help us handle (and receive alerts about) errors with minimal code.

Getting ready

Before starting the recipe, please make sure you have done the following:

  • Create a storage account using the Azure portal if you have not created one.
  • Install Azure Storage Explorer from http://storageexplorer.com/ if you have not installed it yet.

How to do it…

In this section, we'll perform...

Avoiding cold starts by warming the app at regular intervals

By now, you might be aware of the fact that you can create Azure functions in the following three hosting plans:

  • App Service plan
  • Consumption plan
  • Premium plan

One of the benefits of being serverless is the fact that you are charged based on the number of executions. This benefit is available only when you create the function app using the Consumption plan. However, one of the concerns that developers report about using the Consumption plan is something called cold starting, which refers to spinning up an Azure function to serve requests when there have been no requests for quite some time. To learn more about this topic, go to azure.microsoft.com/blog/understanding-serverless-cold-start/?ref=msdn.

Note

The Premium plan and App Service plan have a dedicated instance reserved for us and they can always be warm even if there are no requests for quite a while. Having a dedicated instance always running...

Sharing code across Azure functions using class libraries

Let's say that we have developed a common library across various applications being used in our project, such as a web app or a Windows Presentation Foundation (WPF) application, and now we would like to re-use some functionality in an Azure function app. It's definitely possible to re-use it. In this recipe, we'll develop and create a new .dll file and we'll learn how to use the classes and their methods in Azure functions.

How to do it…

Let's create a class library by performing the following steps:

  1. Create a new Class Library application using Visual Studio as shown in Figure 10.7:
    Creating a new class library application
    Figure 10.7: Visual Studio—creating a class library project
  2. Create a new class named Helper and paste the following code in the new class file:
    namespace Utilities
    {
        public class Helper
        {
          public static string...

Migrating C# console application to Azure functions using PowerShell

Currently, many business applications are being hosted in private clouds or on-premises datacenters. Some of them have started migrating their applications to Azure using various methods.

The following are just a few methods for quick migration to Azure:

  • Lift and shift the legacy application to the Infrastructure as a Service (IaaS) environment: This method should be straightforward, as you have complete control over the virtual machines that you create. You could host all your web applications, schedulers, databases, and so on without making any changes to your application code. You can even install any third-party software or libraries. Though this option provides full control for your application, it would be expensive in most cases as the background application might not be running all the time.
  • Convert legacy applications to a Platform as a Service (PaaS)–compatible environment: This method...

Implementing feature flags in Azure functions using App Configuration

Usually, when we are working on enterprise projects, we are working on multiple large applications where we have individual app settings stores for every application. The app settings would be either specific to an application or common across all applications.

For example, if we have one database that is used by multiple applications, then we have to have the same connection string in each of those applications. If we have to change something (such as a password) in the connection string, we would need to change it in all the configurations of all the projects.

In order to solve this problem, Azure provides a service called App Configuration, which can be used to externalize configuration items. When we take configurations out of the scope of the individual project, we can use them in multiple applications.

In this recipe, we'll learn how to do the following:

  • Externalize app configurations...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Azure Serverless Computing Cookbook. - Third Edition
Published in: Jun 2020Publisher: PacktISBN-13: 9781800206601
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Praveen Kumar Sreeram

Praveen Kumar Sreeram is an author, Microsoft Certified Trainer, and certified Azure Solutions Architect. He has over 15 years of experience in the field of development, analysis, design, and the delivery of applications of various technologies. His projects range from custom web development using ASP.NET and MVC to building mobile apps using the cross-platform Xamarin technology for domains such as insurance, telecom, and wireless expense management. He has been given the Most Valuable Professional award twice by one of the leading social community websites, CSharpCorner, for his contributions to the Microsoft Azure community through his articles. Praveen is highly focused on learning about technology, and blogs about his learning regularly. You can also follow him on Twitter at @PrawinSreeram. Currently, his focus is on analyzing business problems and providing technical solutions for various projects related to Microsoft Azure and .NET Core.
Read more about Praveen Kumar Sreeram