Understanding Azure Function App — Queue Trigger vs Timer Trigger | 2024
→ This article is dedicated to understanding the serverless functions of Azure Cloud.
→ Azure Cloud has various types of function applications such as HTTP trigger, Queue trigger, Durable function trigger (i.e. for creating orchestrator), timer trigger, etc.
→ We have been recently working on creating a queue trigger and timer trigger in a production-specific application. Let us understand in layman’s language.
What is Queue Trigger?
→ In today’s time we are using Microservice based architecture to keep application light and independent maintainable
→ The simple idea is whenever some message is in a queue a serverless function triggers i.e. some function executes.
→ By message we mean data eg. objects.
→ Think like there are two apps — App A, and App B all independent microservices.
→ Now if we send some message to App A to a queue, some logic in its connected function will run App B.
How Queue Trigger Works?
- Queue: Think of a queue as a list where messages wait to be processed.
- Trigger: When a new message is added to the list, the trigger acts like a doorbell ringing.
- Function: This ringing (trigger) starts a function (a piece of code) to handle what needs to be done.
Why is Queue Trigger used?
- Automation: It saves you from manually checking for new messages and starting processes.
- Scalability: It can handle many messages and tasks without you needing to do anything extra.
- Reliability: It ensures that every message gets processed, even if it arrives when you’re not around.
Example Scenario 1: Processing Orders
Scenario: Imagine an online store where customers place orders.
- Step 1: A customer places an order.
- Step 2: The order details are added as a message to a queue (think of it as a to-do list).
- Step 3: Azure Queue trigger detects this new message in the queue.
- Step 4: It automatically runs code to process the order — like confirming it, updating inventory, or sending a receipt email.
Example Scenario 2: Image Processing
Scenario: A company that allows users to upload images for editing.
- Step 1: A user uploads an image for editing.
- Step 2: A message with the image details is placed in a queue.
- Step 3: Azure Queue trigger sees this new message.
- Step 4: It automatically runs a code to process the image — such as applying filters or resizing it.
What is a Timer Trigger?
→ The timer trigger is the same as the queue trigger with one difference. Instead of running at the same time, it will run at some interval eg. 1 minute or 1 day.
→ Assuming the function app is set to run after every 30 seconds
→ Usually, the message remains in the queue and after 30 seconds all queue messages will process at once.
Why is Timer Trigger used?
→ If your application runs a lot of serverless applications i.e. function apps. It will lead to more RU consumption simply meaning more cost.
→ Some areas in the application that can be run in bulk:
Example Scenario 1: Daily Backup
Scenario: A company needs to back up its data every night.
- Step 1: Set the timer trigger to 2 AM every day.
- Step 2: At 2 AM, the timer trigger activates.
- Step 3: It automatically runs code that backs up the company’s data to a secure location.
Example Scenario 2: Sending Reminders
Scenario: An app sends weekly reminders to users.
- Step 1: Configure the timer trigger for every Monday at 9 AM.
- Step 2: On Mondays at 9 AM, the timer trigger kicks in.
- Step 3: It runs a code to send reminder emails or notifications to users.
Example Scenario 3: Regular Data Cleanup
Scenario: A service that needs to clean up old records every month.
- Step 1: Schedule the timer trigger for the first day of each month.
- Step 2: On the first of the month, the timer trigger activates.
- Step 3: It runs a code to delete or archive old data, keeping the system clean and efficient.
How Timer Trigger Works: Simplified
- Timer Trigger: Think of it as setting an alarm clock.
- Schedule: You choose the time or interval for the alarm.
- Action: When the alarm goes off, it runs a specific task or function automatically.
Conclusion:
We now understand the difference between a Queue trigger and a Timer trigger. Both have different use cases.
For instant code execution, we use the Queue trigger and for code executing in some time or interval we use the Timer trigger.
Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and follow me on Twitter or subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.!