Azure Functions: How to design scalable serverless apps

Azure Functions is a serverless platform offered by Microsoft Azure that allows developers to execute code without having to manage the underlying infrastructure, allowing them to focus on writing functions that respond to specific events. Functions supports a variety of programming languages and easily integrates with other Azure services, making it an ideal choice for rapidly creating applications, microservices, and automations. In this article, we will see in more detail what it is, how it works, what the service costs are and how to optimize them.

What you'll find in this article

  • Azure Functions: a brief introduction
  • Azure Serverless Functions: operation, use cases and Core Tools
  • Azure Durable Functions: small fragments for big tasks
  • Azure Logic Apps vs. Functions: different but complementary
  • Azure Functions Pricing: payment model and how to optimize costs
Azure Functions: How to design scalable serverless apps

Azure Functions: a brief introduction

Developing software applications can be resource-intensive, but the real problems emerge in the form of maintenance and support for users. In the past, the firmware and resources needed to perform operations were very expensive, but those times are long gone thanks to Microsoft Azure and Azure Functions, the serverless computing service provided by Azure that allows developers to create and deploy event-based applications without the need to manage the underlying infrastructure.

The service allows developers to write and manage small pieces of code, called 'functions', that can be written and run in the cloud without having to manage servers. They work in response to specific events, such as a click on a website or the arrival of a message in a queue.

These functions are very useful because they allow developers to focus only on the code they need to write, without worrying about everything else that is needed to make it work, such as servers or network infrastructure, leaving the management of the underlying infrastructure and the scalability aspect to the Azure platform.

Microsoft Azure manages all server management, including updates, security patches and scaling, freeing developers from operational burdens and allowing them to focus on code development, and because Azure Functions automatically scales based on demand, there is no need to pay for unused server resources, a real boon for managing applications with varying workloads.

Speed, efficiency and savings are the keywords behind the service. But how does it work specifically? Let's look at it better in the next sections.

Azure Serverless Functions: operation, use cases and Core Tools

Azure Functions follows the serverless computing paradigm, which means that there are no servers to configure or manage. The platform automatically manages the scalability of the infrastructure, ensuring that resources are allocated based on actual demand, making it highly cost-effective.

Solutions developed with Azure Functions are triggered by various events, such as HTTP requests, timers, queued messages, file uploads, or changes to data in databases. This event-based approach allows developers to respond to specific events in real time without the need to maintain persistent server instances.

This supports a connected design that is more efficient and easier to maintain, and the event-driven nature of Azure Functions also allows you to decouple the components of the application, facilitating the creation of scalable and resilient architectures and microservices.

Azure Functions supports several programming languages, including C#, JavaScript, Java, Python, and TypeScript. This flexibility allows developers to work with their preferred language and to seamlessly use existing codebases.

The functions developed with the service integrate perfectly with various other Azure services such as Azure Storage, Azure Cosmos DB, Azure Service Bus, Azure Event Grid and many others, allowing developers to build sophisticated applications combining the functionalities and possibilities offered by the different services.

Developing event-driven applications with Azure Functions

When to use functions developed with Azure Functions

In general, functions that can be developed with Azure Functions are more suitable for parts of your application that can run as separate processes, often running several times in parallel and triggered by specific events that may require rapid scaling from time to time.

The most common use cases for Azure Functions include:

  • Reminders and Notifications
  • Scheduled tasks and messages
  • File processing
  • Data processing or data flows
  • Performing backup tasks in the background
  • Backend calculations
  • Lightweight web API, proof of concept, MVP (Minimum Viable Product)

That said, it's important to understand when not to use them. Not all applications can or should use Azure Functions, as this is a trigger-based app. Once an event is triggered, the task runs in the background.

Azure Functions services are used to design solutions to accelerate the application development process. With serverless architecture, you can stop worrying about infrastructure challenges and focus on creating code. However, like all services, Azure Functions solutions have their limitations and costs that you need to keep in mind and that's why we want to remember that:

  • They are NOT a substitute for Web APIs. Web applications tend to use web APIs as an intermediary to group together activities related to data and business logic. Azure Functions receives input, executes its logic, and provides output. While they can be a great extension for Web APIs in specific use cases (and be used as lightweight Web APIs in other cases), they don't completely replace them.
  • They are NOT designed to perform multiple tasks at the same time. The service has been designed to perform a single task or a very limited number of activities in the shortest possible time.
  • They are NOT recommended for rare and time-sensitive tasks. “Cold starts” — when the container starts for the first time to complete a new request — result in a slight delay in response time that, if accumulated on many functions, could negatively affect the application's responsiveness experience.
  • They are NOT suitable for performing long and computationally intensive tasks. Because Azure Functions is a 'compute-on-demand' service, attempting to replace any API with many Azure Functions could result in a significant increase in development, maintenance and calculation costs.

Azure Functions Core Tools

Azure Functions Core Tools is a command line interface (CLI) provided by Microsoft that allows developers to create, test and deploy Azure Functions solutions in their local development environment and available for Windows, macOS and Linux. This tool is essential for those who want to develop without having to interact directly with Azure cloud services.

The interface allows functions to be executed locally, simulating the cloud environment, allowing developers to debug and test without requiring an Azure subscription. It supports local emulation of various Azure triggers and bindings, such as HTTP, Timer, Blob and Queue, allowing programmers to test functions as if they were running in Azure.

The tool simplifies the creation of new apps with functions and functions themselves, offering templates for various types of functions and triggers, making it easier to start with a specific configuration. It also helps configure the environment and settings necessary to perform functions, including app settings and connection strings.

Azure Functions Core Tools offers commands to distribute locally developed functions to Azure Function Apps, ensuring a smooth transition from local development to the cloud. It integrates well with CI/CD pipelines, allowing for automated distributions and updates.

The tool works with the most popular IDEs, such as Visual Studio Code, offering a seamless development experience with built-in support for Azure Functions projects. It can be extended with additional tools and plugins to improve functionality, such as integration with Azure DevOps for simplified deployment processes.

Did you know that we help our customers manage their Azure tenants?

We have created the Infrastructure & Security team, focused on the Azure cloud, to better respond to the needs of our customers who involve us in technical and strategic decisions. In addition to configuring and managing the tenant, we also take care of:

  • optimization of resource costs
  • implementation of scaling and high availability procedures
  • creation of application deployments through DevOps pipelines
  • monitoring
  • and, above all, security!

With Dev4Side, you have a reliable partner that supports you across the entire Microsoft application ecosystem.

Azure Durable Functions: small fragments for big tasks

Azure Durable Functions are an extension of the normal solutions that can be developed with Functions that allow you to manage stateful functions (that maintain state) in a serverless environment. This feature allows developers to manage state, checkpoints, and restarts within their serverless workflows. They are particularly useful for orchestrating complex processes that involve many steps, making them an essential component in any Microsoft Azure developer's toolkit.

These workflows can be paused and resumed without worrying about the loss of state information, thanks to the Durable Task Framework that takes care of the checkpoints and restarts for the user.

In addition, Durable Functions can simplify code and reduce its complexity in situations that would otherwise require complicated chains of functions or the use of external storage to maintain state. They are also very useful in scenarios where it is necessary to perform a sequence of tasks in order, manage errors or transaction rollbacks, and maintain state through a series of operations.

Below we provide a small list with some real examples where implementing Azure Durable Functions can be beneficial. Each of these scenarios involves multiple tasks that must be completed in order, may require human interaction, and require state management throughout the entire process, all of which durable functions excel at:

  • Order processing systems: Durable Functions are ideal for e-commerce order processing, which may involve various steps such as inventory control, payment processing, and order fulfillment. Each step depends on the success of the previous one, and Durable Functions can manage the orchestration of these steps efficiently. In the event of a bankruptcy, it is possible to define necessary compensatory actions, such as issuing a refund if the fulfillment of the order fails after the payment has been processed.
  • Data pipeline processing: In scenarios where large volumes of data must be processed that involve different phases (such as data validation, transformation, and loading into a data warehouse), Durable Functions can coordinate these steps. If a phase fails, Durable Functions allow for easy error handling and retry logic.
  • Transactions in distributed systems: In a microservice architecture, where a single operation could involve multiple services, Durable Functions can manage distributed transactions. This could include a 'saga' pattern where every operation in the transaction has a compensatory operation for the rollback in case of failure.
  • Approval workflow system: You can design a durable function that awaits human interaction for approval workflows. For example, in a document approval process, once a document has been submitted, it can activate a durable function to send a request for approval to a supervisor. The function then waits until the supervisor approves or rejects the document, after which it can take the appropriate action.
  • Periodic data aggregation: If it is necessary to perform a routine task, such as collecting data from various sources and producing a daily report, a Durable Function can handle these tasks. It can coordinate the collection of data, transform it as necessary, generate the report, and also handle errors or retries if a data source is temporarily unavailable.
  • Resource provisioning: In scenarios where it is necessary to configure resources, which is usually a multi-phase process and requires monitoring to complete, Durable Functions can be taken advantage of. If you're creating a new user in your system and need to configure multiple resources such as a mailbox, personal directory, and database entries, a Durable Function can orchestrate these steps and handle exceptions efficiently and precisely.

Azure Logic Apps vs. Functions: different but complementary

Azure Functions has several similarities with another service offered by Microsoft's cloud computing platform: Azure Logic Apps. However, despite the similarities, the two are different in purpose and in the way in which they are used and could even be considered complementary in some ways.

For starters, Azure Functions and Azure Logic Apps are both cloud-based services used to automate processes and workflows, but they differ in their approach to automation. While Functions focuses on executing code in response to specific events, Logic Apps provides a visual platform for creating and executing workflows that involve multiple actions and integrations.

Azure Functions, as we saw in previous sections, is ideal for running small pieces of code in response to events, such as uploading a file or a new message to a queue. It's perfect for specific, customized tasks that require programming, offering flexibility to create complex logic through code.

Azure Logic Apps, on the other hand, is a platform for creating automated workflows without the need to write code. It uses a graphical interface where it is possible to connect different services and applications, orchestrating processes that involve multiple steps and integrations with ease. It's especially useful for automating business processes, such as sending an email notification when a new contact is added to a CRM system.

Solutions developed with Azure Logic Apps are more flexible and can handle complex workflows, but can be more challenging to configure and maintain due to their less versatile visual interface and the need to manage connectors and integrations.

Azure Functions, on the other hand, are generally easier to configure and maintain, thanks to their simple code-based interface and lightweight design. However, they may not be suitable for more complex workflows that require multiple steps and services (even with the implementation of the Durable Functions mentioned in the previous section).

As can be seen, although there is a certain degree of surface overlap, the differences between the two services make them suitable for tasks of different types and entities but in a certain sense complementary. You could even consider using both for the design of your automation solutions, with Azure Functions managing the complex and customized logic that requires code, while Azure Logic Apps deals with orchestrating larger processes by connecting various services and automations without the need to program.

Developing intelligent applications with Azure Functions

Azure Functions Pricing: payment model and how to optimize costs

Azure Functions uses a pricing model that aligns with the fundamental principle of serverless computing: you pay only for what you use (a formula commonly known as pay-as-you-go). Unlike traditional calculation services, there are no costs for periods of inactivity; billing is based on actual usage metrics. In particular, the cost is determined by two parameters: the total number of executions of the functions and the time consumed by those executions in a month.

It is important to note that execution time is measured in gigabytes seconds, which is the product of the memory used by a function and the time it is running. Azure has established a minimum memory of 128 MB for its functions. Therefore, even functions that use less memory will be charged based on this minimum limit.

Azure Functions offers its users two plans: Consumption and Premium.

The Consumption plan is the most common and is based on the number of executions and the execution time of the functions. No fee is charged during downtime.

The Premium plan, on the other hand, is designed for applications that require greater processing power, longer execution times and advanced features such as connectivity to virtual networks and hybrid connections. The Premium plan offers dedicated resources that are not shared with other customers, ensuring high availability and reliability.

Unlike Consumption, the price of Premium is based on the number of vCPUs (virtual central processing units) and memory resources allocated to its function. You can choose between four different levels:

  • Premium V2 with 4 vCPUs and 14 GB of memory
  • Premium V3 with 8 vCPUs and 28 GB of memory
  • Premium V4 with 16 vCPU and 56 GB of memory
  • Premium V5 with 32 vCPU and 112 GB of memory

The price of Azure Functions is influenced not only by the plan chosen but also by several other factors that you need to consider when estimating the costs of the service. Let's see below which are the most important:

  • The complexity of the function affects execution time, memory consumption, and the allocation of resources necessary for execution. The more complex the function, the more resources needed, which can increase costs.
  • The execution frequency affects the number of executions, which directly affects costs. If the function is activated frequently, the number of executions will increase, and costs will also increase.
  • The execution time, that is, the time taken to execute the function, is charged based on the amount of memory used and the execution time. If the function takes longer to execute or consumes more memory, the costs will increase.
  • The region where the function is deployed may affect the costs of Azure Functions. Each Azure region has its own pricing, and some regions may be more expensive than others. It is therefore essential to choose the region that best suits your needs and the budget you have available.
  • The transfer of outbound data, i.e. data transferred from the function app to other services outside Azure, such as API, database or storage account, is charged based on the amount of data transferred, which can increase costs without you realizing it.

For more information on the pricing of Azure Functions, as always, we refer you to the convenient calculation tool provided by Microsoft (hither) to find out the prices of the service based on geographical region, currency and time of use (calculated in hours or months).

Azure Functions Flex Consumption Plan

How to optimize Azure Functions costs

Although Azure Functions offers a convenient way to create serverless applications, it is essential to optimize and refine the use of the service to reduce operating costs. The lack of attention and care can lead to a whole series of significant problems that can compromise our finances and the efficient use of the service.

Below we therefore provide some tips to reduce the costs of using Azure Functions for your business and make the most of the potential of the service, while spending wisely:

  • Choosing the right plan based on your application requirements is critical. If you require advanced features such as connectivity to virtual networks, hybrid connections, or more processing power, the Premium plan may be the best choice. However, if the application is used sporadically, the Consumption plan may be cheaper in the long run.
  • Optimizing the code to reduce execution time and memory usage allows you to reduce the number of executions and the time of the latter, significantly reducing costs.
  • Use the cache to decrease the number of executions and the execution time. The cache allows the most frequently used data to be stored in memory, reducing the need to perform the function each time.
  • Obviously, regularly monitoring usage is a good practice to identify any peaks in usage that may increase costs. Azure Monitor can help us track the number of executions, execution time, and memory usage. Its use can save us significant amounts, so why not use it?
  • Automate scalability based on usage models to ensure you're using only the resources you need. Azure Functions offers advanced auto-scaling capabilities, which allow you to adjust the consumption of your function apps based on the number of requests and usage patterns.

Conclusions

To conclude our overview, we cannot fail to emphasize once again that Azure Functions is a very useful tool for all users and developers who work every day on the Microsoft cloud computing platform.

The ability of functions to automate, with small fragments of customized code, all those tasks that would once have required extra work on the underlying infrastructure is not to be underestimated and can lead to significant savings in time, money and effort, allowing companies and developers to focus on their most important tasks without becoming fossilized on the minutiae.

The service is therefore confirmed as one of the most useful and convenient offered by Azure and we just have to warmly invite you to try it and experience its potential for yourself. You will be pleasantly surprised by its potential.

FAQ on Azure Functions

What are Azure Functions?

Azure Functions is a serverless compute service provided by Microsoft Azure that allows you to run event-driven code without needing to explicitly manage infrastructure. It's designed to simplify the process of building and deploying small units of code that can be triggered by various events.

How do Azure Functions support scalability?

Azure Functions automatically scales based on demand. This means that it can scale out by adding more instances during peak usage and scale in during low demand, ensuring efficient resource use and cost management.

What programming languages can be used with Azure Functions?

Azure Functions support a variety of programming languages, including C#, JavaScript, Python, Java, and PowerShell. This flexibility allows developers to use their preferred languages when creating functions.

How can Azure Functions be triggered?

Azure Functions can be triggered by various events, such as HTTP requests, messages from Azure Queue Storage, events from Event Grid, timers, and more. This makes it easy to integrate with different Azure services and external systems.

What are the pricing models for Azure Functions?

Azure Functions offers two main pricing models: the Consumption Plan and the Premium Plan. The Consumption Plan charges based on the number of executions and the resources consumed, while the Premium Plan provides enhanced performance and features like VNET integration at a fixed cost.

How does the development process for Azure Functions work?

The development process for Azure Functions typically involves writing code in a preferred programming language, defining triggers, and deploying the function to Azure. Azure provides tools like Visual Studio and Visual Studio Code for streamlined development and debugging.

What are the use cases for Azure Functions?

Azure Functions are ideal for scenarios that require event-driven execution, such as processing data streams, running scheduled tasks, automating workflows, and building microservices. They are particularly useful for applications that need to scale dynamically based on event load.

How can I monitor the performance of Azure Functions?

Azure Functions can be monitored using Azure Application Insights, which provides detailed metrics, logs, and diagnostics. This helps in tracking the performance, identifying issues, and optimizing function execution.

Can Azure Functions be integrated with other Azure services?

Yes, Azure Functions can be seamlessly integrated with other Azure services like Azure Storage, Event Grid, Cosmos DB, and more. This enables the creation of complex, event-driven architectures within the Azure ecosystem.

What are the benefits of using Azure Functions for serverless computing?

The key benefits of using Azure Functions include reduced infrastructure management, automatic scaling, cost efficiency, and the ability to focus on writing code rather than managing servers. This makes it a powerful option for developers looking to build scalable, event-driven applications.

Find out why to choose the team

Infra & Sec

The Infra & Security team focuses on the management and evolution of our customers' Microsoft Azure tenants. Besides configuring and managing these tenants, the team is responsible for creating application deployments through DevOps pipelines. It also monitors and manages all security aspects of the tenants and supports Security Operations Centers (SOC).