Utilizing Azure Functions Singleton Pattern on Consumption Plan

Akash Rekalwar
Villa Plus Engineering
3 min readFeb 22, 2024

Introduction

Azure Functions are a serverless compute service that allows you to run event-triggered functions without the need to provision or manage infrastructure explicitly. One of the key advantages of Azure Functions is the ability to scale automatically based on demand. However, there are scenarios where you need to ensure that only one instance of a function is running at a time. This is where the Singleton pattern comes into play.

What is Singleton Pattern

The singleton pattern ensures that only one instance of a function is executed at any given time, preventing simultaneous executions. This can be crucial in scenarios where you need to manage shared resources or ensure that certain tasks are not executed simultaneously.

Use case for the use of Azure Functions Singleton Pattern

  1. Concurrent Execution Prevention
    In some scenarios, you may want to ensure that only one instance of a function is executed at any given time. This is particularly useful for functions that perform critical tasks where concurrent execution could lead to data inconsistencies or other problems like calling external endpoints.
  2. Resource Intensive Operations
    If your function performs resource-intensive operations, such as connecting to a database or consuming an external API with rate limits, you may want to avoid multiple instances competing for these resources at the same time.
  3. Access to Shared Resources
    If several function instances use a shared resource, e.g. a file or a cache, using the singleton pattern can help to avoid conflicts and ensure proper synchronization.
  4. Multiple Events Trigger
    If a singleton function is triggered while an instance is still running, the runtime queues the additional triggers. It ensures that only one instance of the function is actively processing at any given time.
    The runtime ensures that the singleton function executes in a serialized manner. Each trigger is processed one after the other, maintaining order based on when they arrived

Implementing Singleton Pattern on Consumption Plan

On the Consumption plan, Azure Functions automatically scales based on the number of incoming events. Implementing the Singleton pattern on this plan involves leveraging the “Singleton” attribute provided by the Azure Functions runtime.

Example Code
Use the [Singleton] attribute in your function's configuration to enable the Singleton pattern.

public static class TestSingletonFunction
{
[FunctionName("TestSingletonFunction")]
[Singleton(Mode = SingletonMode.Function, LeaseTimeout = "00:05:00")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer)
{
log.LogInformation($"C# Timer trigger function:{DateTime.Now}");
}
}

In this example, the SingletonMode.Function ensures that only one instance of the function is running at any time, and LeaseTimeout specifies the maximum duration of the lease.
The Singleton attribute automatically acquires a lease on the lock and renews it as long as the function is running. This prevents other instances from acquiring the lock and ensures that only one instance of the function runs.

Options For Singleton Attribute

  1. SingletonMode.Function
    The default option. It allows one instance of the function per function app.
  2. SingletonMode.Listener
    Allows one instance of the function per listener
    • Concurrency
    Specifies the maximum number of function executions that can be in progress at the same time. Setting it to 1 ensures that only one instance is active at any given time.
    ex-
    [FunctionName("MySingletonFunction", Concurrency = 1)]
    • Enabled/Disabled
    This attribute is set to enabled or disabled to indicate whether the singleton behavior is enabled or disabled for a specific function.
    • Lock Duration
    lockDurationInSeconds
    If enabled, you could specify the duration in seconds for which a lock is held on the function. During this time, no other instances of the function would be allowed to run. If the lock duration is set to a high value, it could impact the scalability of the function.
    • Listener Lock Period
    listenerLockPeriod
    This attribute defines the time in seconds that a listener locks on to a function instance. It is the time interval during which the listener guarantees that only one instance of the function is running.
    Example of above options-
"singleton": {
"enabled": true,
"lockDurationInSeconds": 15,
"listenerLockPeriod": 60
}

Conclusion

The singleton pattern in Azure Functions on the Consumption Plan is a powerful tool for scenarios where you need to control the execution of functions to avoid conflicts and ensure efficient resource utilization. By using Azure Storage and the lease mechanism, you can easily implement the singleton pattern in your functions and improve the reliability and consistency of your serverless applications.

--

--

Akash Rekalwar
Villa Plus Engineering

Welcome to my Medium blog, where I share insights and expertise as a seasoned Software Developer .