Why to go Serverless? What is Serverless Computing?
- With PaaS , Azure offers App Service and Web Jobs
- If you drill down to Azure App Servie and Web Jobs, we all know they are hosted on Azure Virtual Machines which are hosted on Microsoft Data centers and managed by Microsoft .
- By specifying Azure App Service plan we can define the underlying VM location,tiers and other configuration settings on which our Web Applications, Mobile Apps or API Apps are going to be hosted.
- Along with Azure App Service and Web Apps, Microsoft implemented Azure Web Jobs to host long running or short running,scheduled or continous running background processes easily.
- You can host Web Jobs within an app service plan either stand-alone or alongside a Web App.
- You can also share the resources of an App Service Plan between Web App and Web Job to save on hosting cost or alternatively you can choose to host the Web Jobs in their own dedicated App Service plan.
- With PaaS offering approach, you might make a Node or C# application,check it into Git or deploy it via other FTP tools to a Web Application template created on Azure and get an endpoint.
- You might scale it up (includes more CPU/Memory Disk) or scale it out(by having multiple instances of your web application and applying round-robin scheduling to implement load-balanced set). But you are always aware of your underlying servers where your code is hosted or scalled.
- If you go with IaaS, you directly have to deal with and manage Virtual Machines. So here also later or sooners you going to stuck with Servers.
- Thus,in PaaS and IaaS, directly or indirectly you end-up with managing(messing) with servers.
- In Serverless computing there is no server you need to worry about. This doesn't mean there is no server at all. It means, Servers are there but you focus on your code which is divided into small discrete functions which would be auto-scaled to meet your load or demand.
Azure Functions aka Functions as a Service
- Microsoft has introduced Azure Functions where are implementation of Serverless Computing.
- Azure Functions are built on the top of Azure WebJobs but not only meant to perform background processing tasks.
- Unlike Web Jobs instead of builidng an entire application you only need to write and deploy individual functions.
- In Azure Functions your length code would be devided into small methods which you can schedule to run seconds later or start it manually or connect it with some event to trigger it.
- You are not goint to write a code that runs for two hours in a single execution but you have to break down your code into many different steps like queues.
- Azure Functions supports auto-scaling so no need to worry about your infrastructure.Here, you don't know about your infrastructures and platform auto-scales you based on demand.
- Azure Functions comes with payper execution. Which means you only pay for the time your code is running.
How much does Azure Function Cost ? How the cost would be measured?
As of now we have two plans available.
- Consumption Plan
- App Service Plan
Consumption Plan:
- This is the most cost-effective way to run your code in Cloud Environment.
- When your function runs, Azure provides all of necessary computational resources. Here, you don't need to worry about resource management and you only pay for the time your code runs.
- It is very cheap in comparision to Web Jobs and Web Apps.
- Microsoft measures your execution in the unit of Milliseconds and amount of memory you consume.It is called GB-s (gigabyte-second).
- Cost goes up by as much memory as you comsume per miliseconds.
- You only have to define your functions, you define which event is going to trigger the functions and your memory and underlying server would be defined by Azure at the time of execution of those particular functions.
- Azure will consider what your system is trying to consume and if needed scale your system such that it meets your memory needs to an upper limit of 1.5 GB per execution and you will be charged accordingly.
App Service Plan:
- This is a more costly option than Consumption plan. Just like your Web Apps and Web Jobs you would be charged for the reserved resources of the underlying VM.
How can we convert existing Web Service with Azure Functions?
- You should start with athelast piece of your Web Service functionality and you should convert it with Azure Functions, than second last piece.
- If you have already done lot of investment in Web Sites or Web Jobs, you can start with a last functionality in chain and move upwards and stop once you don't want to go ahead.
What we get in Free Tier of Azure Functions?
- In the Free Tier you can have a million executions a month for a free.
What is a maximum timeout threshold to how long Azure Functions can execute?
- Azure wants you to write down short running code. As a result if your Azure Functions are hosted using Consumption Plan the maximum timeout threshold is 5 minutes. If your Azure Functions completes within this timeout, then you will be able to use the Comsumption Plan pricing without any issue.If your Azure Functions take longer than 5 minutes to complete execution, then it will timeout and the process will be terminated.To fit into timeout threshold of Azure Functions Consumption Plan you have to break up your Function into smaller seperate Functions.These separate Functions would be then implemented using one or more message queues to communicate.
- Longer running Azure Functions won't run well with the Consumption Plan due to its timeout threshold. So, here you should go with App Service Plan to host and execute Azure Functions. Because it has no timeout threshold. Here you do not need to break out your Functions into smaller functions.
What is the benefit of breaking code into small Functions?
- You can host your and execute your Azure Functions under Consumption Plan and as a result you will get the benefit of payper execution.
- It would be helpful in case of failure also.If you write down a Function which takes 30 minutes for execution and if it fails halfway and succeeds in the second efforts so your total execution time will be 45 minutes.B ut if you break down your code into queues it is more reliable.If one piece fails, the rese don't fail and one can retry itself catch up and it will reduce processing time.
Monitoring and Error Handling in Azure Functions...
- Using Azure WebJobs dashboard.
- It also supports Application Insight and it will allow you to see a live metrics view of what's happening real-time across all apps.
- So you can also see in-depth exception message into portal in real-time from all your various instances.
- It also depicts CPU percent utilization and memory per instance. So though it is serverless it exposes you the information per server that is running uncer cover.
- Here, remote debugging is not possible but you can attach a debugger.
- With new tools you can debug locally while consuming resources in cloud.
Unit testing of Azure Functions...
- In the Visual Studio tools we can write Functions as class libraries.
- That means we can use any standard .NET Unit Testing Frameworks to test .NET class libraries like NUnit.
- For Node.js we can write something like modules and run it using Mocha or other testing libraries.
Sending Push Notification using Azure Functions...
- In Azure Functions we have concepts like binding, which means you can push data out of a function and we have binding for Azure Notification Hub.
- So you can write a Function that can push a notification to all of the mobile platforms.
- You can also schedule functions so like every morning you can trigger a function to push your schedule of the day to your mobile phones.
Use of Azure Functions...
- Data processing/clean up/archive in scheduled way
- Serverless Single-Page-Applicaiton back-end via HTTP bindng
- IOT event processiong
- Delivery method of microservices and Azure service event-based processing
- Image processing / order processing
Azure Functiosn Triggers...
- BlobTrigger
- HttpTrigger
- QueueTrigger
- TimerTrigger
- Generic Webhook
- Github Webhook
- ServiceBusQueue Trigger
- ServiceBusTopic Trigger
How Azure Functions work internally in case of .NET?
- It supports C# as well as F#.
- To work with C# we have two ways: C# Scripting and Compiled Assemblies
- In C# Scripting, you can import other libraries and use project.json files to pull NuGet packages.All these would be compiled and rsesults in a single method called a run method which would be loaded in C# scripting.
- In Compiled Assemblies, you have to give Azure an assembly and then you have to specify what class and what method you want and mention all this in a config file.Azure will go ahead, load the assembly into runtime, find that method and every single time an event comes there, Azure call your method on your assembly with data.