Aman Baloch

Software Engineer from Melbourne

View on GitHub
4 March 2024

Maximizing Cost Efficiency with Azure Container Instances (ACI) versus Azure WebJobs for Scheduled Tasks

:clock3: 6 minutes read

In the ever-evolving landscape of cloud computing, efficiency and cost-effectiveness are paramount considerations for businesses of all sizes. As organizations strive to optimize their operations, the choice of deployment methods can significantly impact both performance and expenditure. Azure, Microsoft’s cloud computing platform, offers a plethora of services tailored to meet diverse business needs. In this article, we’ll explore how Azure Container Instances (ACI) outshine Azure WebJobs in terms of cost savings and operational efficiency.

Understanding Azure WebJobs

Azure WebJobs provide a convenient way to run background tasks in the context of a web application. Whether it’s processing queues, performing scheduled tasks, or handling continuous integration and deployment processes, WebJobs offer a straightforward solution tightly integrated with Azure App Service.

Azure WebJobs Implementation:

In the case of Azure WebJobs, the scheduled task would typically be implemented as a console application or script deployed alongside the web application within an Azure App Service. The WebJobs SDK provides built-in support for scheduling tasks using CRON expressions.

public class ScheduledTask
{
    public static void ProcessData([TimerTrigger("0 0 0 * * *")] TimerInfo timer)
    {
        // Logic to process batch of data
    }
}

Cost Consideration

With Azure WebJobs, the business would need to provision an Azure App Service plan, which incurs a fixed monthly cost regardless of usage. Even if the scheduled task runs for only a few minutes each day, the business must pay for the entire month’s worth of resources.

Other Challenges With Azure WebJobs

While Azure WebJobs offer simplicity and ease of use, they come with certain limitations that can impact scalability and cost-effectiveness:

  1. Resource Utilization: Azure WebJobs are hosted within the same infrastructure as the web application, which means they share resources such as CPU and memory. This can lead to contention issues, especially during peak usage periods.

  2. Scaling: Scaling Azure WebJobs can be challenging. Although Azure App Service allows for horizontal scaling, the entire application, including WebJobs, scales together. This can result in over-provisioning and increased costs.

Enter Azure Container Instances (ACI)

Azure Container Instances (ACI) provide a lightweight and flexible solution for running containers in the cloud. Unlike Azure WebJobs, which are tightly coupled with the web application, ACI offers a standalone environment for running containerized workloads.

Azure Container Instances Implementation

Using Azure Container Instances, the scheduled task can be executed within a containerized environment, providing greater flexibility and cost efficiency. The task can be orchestrated using a container orchestration service like Azure Kubernetes Service (AKS) or simply triggered via Azure Logic Apps or Azure Functions.

apiVersion: '2018-10-01'
location: australiaeast
name: aci-scheduled-task
properties:
  containers:
  - name: scheduled-task-container
    properties:
      image: <container-image-url>
      resources:
        requests:
          cpu: 0.5
          memoryInGB: 1.5
      volumeMounts: []
  osType: Linux
  restartPolicy: Never
tags: null
type: Microsoft.ContainerInstance/containerGroups

Cost Consideration

With Azure Container Instances, the business pays only for the actual compute resources consumed by the container during task execution, based on a pay-per-second billing model. This results in significant cost savings, especially for sporadic or short-lived workloads like scheduled tasks.

Pricing Example 1

A scheduled task which runs once a month for 5 hours

Memory duration:

Number of container groups * memory duration (seconds) * GB * price per GB-s

1 container group * 18000 seconds * 8 GB * $0.0000023 per GB-s = $0.3312

vCPU duration: Number of container groups * vCPU duration (seconds) * vCPU(s) * price per vCPU-s

1 container groups * 18000 seconds * 1 vCPU * $0.0000207 per vCPU-s = $0.3726

Total billing: Memory duration (seconds) + vCPU duration (seconds) = total cost

$0.3312 + $0.3726 = $0.7038

Pricing Example 2

A scheduled task which runs every day of the month for 1 hour

Memory duration:

Number of container groups * memory duration (seconds) * GB * price per GB-s * number of days

1 container group * 3600 seconds * 8 GB * $0.0000023 per GB-s * 30 = $1.9872

vCPU duration: Number of container groups * vCPU duration (seconds) * vCPU(s) * price per vCPU-s * number of days

1 container groups * 3600 seconds * 1 vCPU * $0.0000207 per vCPU-s * 30 = $2.2356

Total billing: Memory duration (seconds) + vCPU duration (seconds) = total cost

$1.9872 + $2.2356 = $4.2228

Other Advantages of Azure Container Instances

  1. Resource Isolation: With ACI, each container runs in its own isolated environment, ensuring consistent performance and resource utilization. This eliminates contention issues and improves overall reliability.

  2. Granular Scaling: ACI allows for granular scaling, enabling businesses to provision resources precisely according to workload requirements. Whether it’s running a single container or hundreds, ACI scales seamlessly without over-provisioning.

  3. Container Flexibility: ACI supports a wide range of container types, including Docker containers, enabling businesses to leverage their existing investments in container technology seamlessly.

Integration With Logic Apps

Azure Logic Apps provide a serverless workflow orchestration service that can integrate with various Azure services and external systems. Scheduled tasks can be implemented as Logic Apps workflows triggered by time-based triggers.

Azure Logic Apps have native integration with Azure Container Instances

# Azure Logic App workflow definition YAML
definition:
  $schema: "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#"
  triggers:
    Recurrence:
      recurrence:
        frequency: Day
        interval: 1
      type: Recurrence
  actions:
    Create_container_group:
      inputs:
        containerGroupProperties:
          containers:
            - name: report-processing-task
              properties:
                image: "<container-image-url>"
                resources:
                  requests:
                    cpu: 1.0
                    memoryInGB: 8.0
          restartPolicy: OnFailure
        resourceGroupName: "<resource-group-name>"
        containerGroupName: "<container-group-name>"
        location: "<azure-region>"
      runAfter: {}
      type: "Microsoft.ContainerInstance/containerGroups"

Logic Apps Azure Container Instance Integration

Conclusion

While Azure WebJobs offer simplicity and integration with Azure App Service, they may not be the most cost-effective solution for all workloads, especially those with sporadic or bursty resource requirements. Azure Container Instances provide a lightweight, scalable, and cost-efficient alternative, allowing businesses to optimize their cloud spending while maintaining performance and reliability.

By leveraging the flexibility and pay-per-second billing of Azure Container Instances, organizations can achieve substantial cost savings without compromising on functionality or scalability. As businesses continue to embrace containerization and cloud-native technologies, ACI stands out as a powerful tool for driving efficiency and innovation in the cloud.

tags: azure - containers - serverless