I have a need to generate a next number for each file type my Logic Apps is generating. My Logic App is doing some translations between an WMS and a customer's ERP. The issue is the customer is requiring us to generate and maintain a sequence number to be placed in the files. The full list of requirements are:
- Generate a unique sequence number. No two calls, for the same file type, should get the same result.
- Number sequence is maintained separately by file type. There are 3 files types, so each would have their own sequence.
- Should be able to handle spurts of 10 calls a second for a few seconds at a time, which can be the same file type.
- Data should be stored in Azure.
I'm at the point I know I'll need to write an Azure Function to do this. It will accept one parameter, "fileType", to determine which sequence number to return. I also know I'll need to store the current number "somewhere" and that the Azure Functions itself or the database/file (thru locking) needs to be pessimistic in nature. I don't know from a cost perspective what azure storage option would work best for this scenario, as well as to make sure of the lock/release to use with that technology decision. To me, it feels like using a database to store three numbers seems like overkill.
-
Hi/lo algorithm might be useful here. en.wikipedia.org/wiki/Hi/Lo_algorithmMatt Johnson-Pint– Matt Johnson-Pint02/11/2020 02:14:58Commented Feb 11, 2020 at 2:14
2 Answers 2
Fun question - sounds like an interview question.
You'll need some sort of persistence. I don't think it matters much which one. I would do this:
- Write '100' to disk (or read previous number from disk)
- Set counter to zero
- lock and increment or interlocked increment and hand out numbers
- If counter reaches '100', write '200' to disk
- Go to 3
If it crashes, you will have a gap, but you don't have to do sync I/O on every call.
In case others are interested, we decided to use Azure Blob Storage using Containers with a HOT setting. It seems to work well in testing. We haven't gone live yet.
The .NET code uses the StorageInfo, CloudBlobClient, CloudBlobContainer and CloudBlockBlob classes. Also had to adapt a routine, LockingEntity, to "lock or wait for release" to get a reference to the CloudBlockBlob.