I have a dev question related to concurrency. Here's the problem:

I need to generate a sequential reference number for each document uploaded to a specific document library. The numbers basicaly look like this:

DocumentFooX12 - Sequence Number: 2014-0001 DocumentWhatever2a - Sequence Number: 2014-0002

Now this typically is not difficult to do with an Event Receiver and I have no problem writing the code. The problem though is related to concurrency.

We have a multi server farm and use Network Load Balancing. Whenever two people work at the same time, they might be working on different servers. I cannot guarantee that the numbers will really be sequential.I've tried a Lock Object in my event receiver but that doesn't make a difference because it only locks the thread on Server1, but not server 2.

Has anyone done some work with the State Service that might help me?

I've also tried a Timer Job, but the requirement dictates that the sequence number is generated almost immediately.


Have you looked at Flake ? I imagine distributed sequential ID generation to be a very complex subject. I'm almost sure you won't get an implementation that is robust and returns perfectly sequential id's. In Flake's case, client's can generate the ID's themselves but they use timeslots / ranges, so they are not perfectly sequential (e.g. the number will jump from 101 to 105 instead of 102):

  • Id is composed of:
    • time - 41 bits (millisecond precision)
    • configured instance id - 10 bits - gives us up to 1024 instances
    • sequence number - 12 bits - usually 0, incremented when more than one id is requested in the same millisecond and reset to 0 when the clock ticks forward. Rolls over every 4096 per machine.
      • The generator has protection mechanism for sequence roll over in the same millisecond.

Good luck!

You may find more information when you look outside of a SharePoint implementation, especially when you need a distributed implementation.


Can you give us a little more details about the implementation?

How are you generating the sequential number and what exactly did you lock?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.