It is almost always a mistake to implement your own. Small unique numbers require a truly reliable record of the last used number. It is difficult to understand, there are malfunctions on hard drives, registries are sometimes damaged. You will need to organize it so that there is one point of failure. Similar to a dbase auto-increment column. Make sure there is a loss of the last number, there is also a complete data loss. This means that for one there should be no way for the user to copy data without also copying the record of the last number. Or you always have a reliable copy of all the data and you can restore the last number from it. Quickly.
You will also have a hard time making the number increase atomically. Just running two instances of your program is usually enough to cause chaos. This is difficult to fix, you will need an independent arbiter process in which the number will be saved.
Then there is the burden of guessing what will happen in 10 or 20 years. Will your application scale to meet the requirements of that time? This is very rare, considering that cars are getting faster and running out. Now the local unique number becomes globally unique. Very different requirements for this.
Donβt mess with it, 16 bytes is nothing. Use the manual.
Hans Passant Aug 31 '10 at 21:07 2010-08-31 21:07
source share