Serverless idempotency with Azure Functions

What is idempotence?

Idempotence is the property of certain operations in mathematics and computer science that they can be applied multiple times without changing the result beyond the initial application.

Why is idempotence important?

By meriting of running in the cloud, assuming the worst case scenario: the underlying hardware might have a power outage at any time. That could be in the middle of your function execution of even after your function gets to the last line of your user code (so it looks like it was successful). This failure will lead to a retry of your function. This operation happens naturally for triggers like QueueTrigger, and even an HTTP client may resend the message if it has a retry loop. This means your function could get executed multiple times and needs to be idempotent so that it’s resilient to retries.

REST-API

Database

Send e-mail

Queue

Idempotency sample
{ “id”: 8,
“name”: “ushio”
}

2018–05–02T07:58:01 Welcome, you are now connected to log-streaming service.
2018–05–02T07:58:22.866 [Info] Function started (Id=4e8ac2da-773e-4b8c-b0ef-7e527073c52a)
2018–05–02T07:58:23.162 [Info] JavaScript queue trigger function processed work item: { RowKey: 8, name: ‘ushio’ }
2018–05–02T07:58:23.162 [Info] new id
2018–05–02T07:58:23.303 [Info] Function completed (Success, Id=4e8ac2da-773e-4b8c-b0ef-7e527073c52a, Duration=444ms)
2018–05–02T07:58:37.414 [Info] Function started (Id=46d4b3b1–51f0–4927-bd52–750ca849966e)

2018–05–02T07:58:37.445 [Info] JavaScript queue trigger function processed work item: { RowKey: 8, name: ‘ushio’ }
2018–05–02T07:58:37.445 [Info] already done it
2018–05–02T07:58:37.445 [Info] Function completed (Success, Id=46d4b3b1–51f0–4927-bd52–750ca849966e, Duration=28ms)

2018–05–02T08:02:03.799 [Info] Function started (Id=6d04eed3–048e-440d-bb1b-09e04868563b)
2018–05–02T08:02:03.799 [Info] JavaScript queue trigger function processed work item: { RowKey: 8, name: ‘ushio’ }
2018–05–02T08:02:03.946 [Error] Exception while executing function: Functions.QueueTriggerJS1. mscorlib: Something happens exception.
2018–05–02T08:02:04.009 [Error] Function completed (Failure, Id=6d04eed3–048e-440d-bb1b-09e04868563b, Duration=206ms)
2018–05–02T08:02:04.135 [Info] Function started (Id=a6438c66–05b9–4c5f-90b6–7b84328b6f40)
2018–05–02T08:02:04.151 [Info] JavaScript queue trigger function processed work item: { RowKey: 8, name: ‘ushio’ }
2018–05–02T08:02:04.291 [Error] Exception while executing function: Functions.QueueTriggerJS1. mscorlib: Something happens exception.
2018–05–02T08:02:04.434 [Error] Function completed (Failure, Id=a6438c66–05b9–4c5f-90b6–7b84328b6f40, Duration=294ms)

Conclusion

Retrospectives

{
"type": "table",
"name": "inputTable",
"tableName": "inTable",
"partitionKey": "Test",
"rowKey": "{rowKey}",
"take": 50,
"connection": "AzureWebJobsDashboard",
"direction": "in"
},
context.log(context);
{ 
invocationId: '73e4503a-f033-41ab-9155-0f58e1955371', executionContext:
{ invocationId: '73e4503a-f033-41ab-9155-0f58e1955371',
functionName: 'QueueTriggerJS1',
functionDirectory: 'D:\\home\\site\\wwwroot\\QueueTriggerJS1' },
bindings:
{ myQueueItem: { RowKey: 8, name: 'ushio' },
inputTable: { Name: 'ushio', PartitionKey: 'Test', RowKey: '8' } },
log:
{ [Function]
error: [Function],
warn: [Function],
info: [Function],
verbose: [Function],
metric: [Function] },
bindingData:
{ queueTrigger: '{"RowKey":8,"name":"ushio"}', dequeueCount: 3,
expirationTime: '5/9/2018 8:48:35 AM +00:00',
id: 'c7b3c3af-6979-4cda-aaff-79845ede184f',
insertionTime: '5/2/2018 8:48:35 AM +00:00',
nextVisibleTime: '5/2/2018 8:58:36 AM +00:00',
popReceipt: 'AgAAAAMAAAAAAAAAgv50wfPh0wE=',
sys:
{ methodName: 'QueueTriggerJS1',
utcNow: 2018-05-02T08:48:36.273Z },
rowKey: '8',
name: 'ushio',
invocationId: '73e4503a-f033-41ab-9155-0f58e1955371' }, done: [Function] }

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store