RoyalMail API cost reduction by Azure Functions
I would like to describe you one of the last cases of Azure Functions usage. In one of my projects I was using RoyalMail API to find address that is connected with provided post code. System has been delivered to users from England. In that country when you provide post code you are able to find out quite precise address and you can speed up form filling.
Target solution should work in that way that each time when user is entering character it will get more detailed results:
From technical perspective, each time when user is entering new character our website is sending new request to RoyalMail. As a result we will get list of possible addresses. On animation you can see that solution is working well. There is only one issue – price plan. From my perspective it is to high.
Average cost of one request is between 0,04 and 0,06 GBP. And you should remember that request is generated each time when you are entering additional character.
It means that cost of whole address finding is higher. And of course we would like to reduce this cost as much as it is possible.
Azure Storage Table has been used for storing query results from RoyalMail.
In this solution Azure Function has been used for request handling. Instead of direct communication with RoyalMail API, we are sending request to our function. Then function is checking in Azure Storage Table whether we already have saved response for that request. If yes – then we will return result only from our solution. If no – we need to ask RoyalMail API and before returning result to front-end we should store it in Azure Storage Table.
I know that this solution is not coherent with single responsibility rule. We should divide our Azure Function to smaller elements. In this case I believe that this is enough.
And definitely we need to estimate cost of it. I assume that we will use the most expensive plan – 15000 requests. It will cost us:
- 15000 executions of function (128 MB x 2 sec) – cost 0 GBP
- 30000 calls to 1 GB Azure Storage Table – cost 8,1 GBP (this is the worst scenario in which we need to save every call to storage; in normal usage this cost should be lower).
As you can see total cost of Azure environment is not higher that 8,1 GBP per 15000 requests.
I hope that I managed to convince you that sometimes you can reduce costs by using very simple solution. With described approach cost of this service is between 4,08 GBP (in case when all results will be taken from our cache – Azure Storage Table) and 583,1 GBP (in case when we will need to save all results into Azure Storage Table).
Please remember that on the one hand this simple solution allows us to reduce costs but on the other hand it introduces additional dependency to our system. It means also – additional point of failure.
And also we are not able to disconnect totally from RoyalMail system because each day they are updating about 3000 records. We should remember about it and add some additional check into our solution. It means that we should retrieve data from RoyalMail each time when data stored in Azure Storage Table are too old.