Serverless Architecture with Azure Functions

Rupesh K. Roy  Print 
28 Aug 2018
 
Intermediate
866

Serverless computing is one of the most coined terms in the cloud these days. Unleashing the PaaS feature with immense amount compute power without any server to host an app has really impacted the web development a big way. The idea is to do compute without hosting the app on a server. Basically, we can think of it to be a cab aggregator or even food delivery startups which provide us a platform to book a cab or order food but the actual service will be provided by a third-party vendor. On a similar note, we put our logic on a serverless computing module which can connect to different third-party services to do the job.

Breaking the Monolithic Architecture

Earlier we used to build one big application with much complex architecture to maintain which was the integration of all modules of an application and was tightly dependent on each other. Any failure on any of the module was making the entire application go down. To resolve this issue, we moved on to segregate the modules into different services and with this, we also get the facility of using different technology stack for different microservice and we can deploy each of the microservice independently.

Now the number of microservices increased and the challenge came to host different instances of microservice up in multiple servers in order to keep the application running. Serverless computing helps us mitigate such issues by using Azure Functions as we have only functions that take care of different modules interact with third-party services like cognitive services or Sigle sign-on services, Graph and chart image services etc.

Cloud service providers manage to identify and allocate resources dynamically rather than pre-provisioning a set of virtual blocks of the resource with some limited set of computing power. And that’s how Serverless compute modules can be scaled on runtime.

Azure Services based on Server-less Architecture

Microsoft offers the below Azure services to facilitate on Serverless computing-

  1. Serverless Coding

    Azure functions are event triggered piece of code with logic to perform a specific task by connecting to different Azure or third-party services. Compute power and resource is utilized only when the function executes. We only need to pay per request to the function.

  2. Storage

    Azure Storage is a highly reliable and scalable service that can be used to store structured or unstructured data. It connects with other Azure services seamlessly and can act as a third-party data source for images, videos or any other content

  3. Database

    Azure Cosmos DB can act as a third-party data source service. Azure functions can connect to Cosmos DB and can read data to process. The important feature of cosmos DB is it can replicate to a different region with the highly scalable option to provide low latency.

  4. Sign In

    Azure AD can be used as login service to connect with cloud-based identity and access management. All user data and role can be managed via Azure AD user management.

  5. Messaging

    Azure Service Bus is a messaging service in order to process data in an order. We can make use of Queues or Topics to ensure that every message that comes in the pipeline is processed. This can be used as a fully managed messaging service provider.

  6. Workflow

    Azure logic apps are used to orchestrate functional workflows by integrating various azure and third-party services to connect data into the apps.

  7. Intelligence

    Cognitive services like vision and speech recognition, language detection, emotion and sentiment detection help to add artificial intelligence into the application.

Azure Functions

Azure functions are serverless compute modal element which runs code on demand and without the need for hosting it on a server. This also frees us from any kind of application architecture complexity as we only write the code which is required to perform a specific task.

Key Features

  1. Independent Module - Functions can be developed as an independent module. We can import NuGet libraries and use them to build small but robust applications. These can be further integrated into other applications or workflows.

  2. Pay per use - Unlike other PaaS Services where a dedicated amount of resources and compute power is charged with Azure App Service plan, Azure functions are charged on basis of the number of times it runs.

  3. Language Choice - Azure supports Functions to be written in variety of choice of language like C#, F#, Node.js, PHP or Java.

  4. Security - Http Triggered Functions can be secured using OAuth Identity providers such as Azure AD. The token generated using AD can be passed into the HTTP request header which can be validated in the code.

  5. Easy Development - We don’t even need to necessarily write code in an IDE, we can directly write code in Azure portal and test it or setup continuous integration and deploy code through VSTS, GitHub.

Create A Function App

  1. Click on Create a resource on azure portal.

  2. Select Compute option from marketplace and click on Function app.

  3. Provide the function app name, resource group, hosting plan and storage account details and click create.

  4. Check on Notification icon at top right corner and click on Go to resource button.

  5. Click on the add (+) symbol to add an Azure function.

  6. A Http trigger function is created with some default code. We can test the app by clicking on Run button and passing some text as input body of request.

Uses of Azure Functions

  1. Data Processing through connected services. When a new file is uploaded into blob storage and some specific processing is required.

  2. Triggering a chain event on some event on some connected Azure Service like when a new message arrives on a queue.

  3. They can also act as a middleware between different services in microservice architecture.

Azure Function Pricing

Azure Functions can be hosted with two different kind of pricing

  1. Consumption plan

    It provides all necessary resources to the function app dynamically and will charge only when the function runs.

  2. App Service plan

    If there is already an app service plan you are using for other app services like Web app, API app etc., the same hosting plan can be used here with no extra cost and will be charged only for the number of times the function app executes.

Limitations

  1. Lack of Troubleshooting ways - Its difficult to identify an issue in production since there is no way to debug a function. Azure provides app insights feature by default with Azure function to help analyze the logs.

  2. Third-Party API Trust - Since Serverless computing heavily relies on third-party services and we need to trust the response and security of these services.

  3. Vendor Locking - Outbound requests may also be expected to return quickly which can further be processed. Any lock state in third party services will cause delay and unexpected behavior.

  4. Over Complex Architecture - Too many functions in an application may also result in a very complex architecture which will be difficult to manage.

  5. Latency - Initial booting time and latency over network caused because of hoping from one function to other increases latencies and may impact the SLAs.

Summary

Serverless is a style of architecture where we do not worry about hosting the application or the required infrastructure to make the app running. The code runs on a readymade container and serves a very specific task. Though, the architecture does not suit as a solution to every problem but we can integrate a part of it any many applications. This helps us to use many of the third-party services and can run independently based on some triggering event. Interestingly, cloud modules which inherit this architecture like Azure functions are charged based on the number of times it executes.

Hands-on Learning
+