0

I've a node application deployed on Azure App Service and we've scaled it to 5 instances.

Now in our application we have exposed few API's which adds data into our mongo DB collection called detail.

Now these details needs to be pushed to another application with a unique reference id. So While putting the data in the detail collection, apart from the mongo DB Object Id, we are generating a reference_id in a sequential manner.

reference_id  = order_category_id + order_sub_category_id + sequence_number + 1

But when multiple requests are coming at the same time for same category and adding the data into the detail collection its generating the same reference_id.

How could I prevent this?

I've tried using the unique Schema property of mongoose, but that is also not working. I want to generate the unique reference Ids in my current system.

1 Answer 1

0

This is usually solved by adding a "machine" or "instance" reference to an ID, so that concurrent requests across scaled instances don't overlap.

This is what MongoDB does for its ObjectId as well and is a common approach to distributed ID generation at scale.

Also, if order of the reference id is important, you would want to include the timestamp and ensure it is the most significant digits/part of the id to sort based on it, while keeping the total length the same.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.