f4b6a3 / uuid-creator

UUID Creator is a Java library for generating Universally Unique Identifiers.
MIT License
430 stars 44 forks source link

Add time interval parameter for ShortPreffix/SuffixCombFactory constructors #64

Closed dmurat closed 2 years ago

dmurat commented 2 years ago

With the time interval parameter in constructors, it would be possible to change the time of prefix/suffix roll-over.

I'm not quite sure about its usefulness. Still, it looks like 45 days to roll over with one-minute default might be too much for some users from a privacy perspective (UUID creation time can be directly calculated until prefixes start to overlap).

I'm not a database expert, but it looks like the time interval should not have any (or at least not significant) influence on database performance.

What do you think? Tnx

fabiolimace commented 2 years ago

Do you want to use another prefix interval other than the default 60,000 milliseconds? Do you mean it below?

Random random = new SecureRandom();
Clock clock = Clock.systemUTC();
int interval = 1000; // increment the prefix every 1 second interval
ShortPrefixCombFactory factory = new ShortPrefixCombFactory(random, clock, interval);

I'm also not sure if it's useful or not to change the default value. I just utilize 60,000 because it's the interval used by Tomas Vondra in his article. But he says in the same article that the interval depends on the application:

For timestamp-based UUID generator, the prefix is incremented regularly, e.g. each 60 seconds (or any arbitrary interval, depending on your application). And after a certain number of such increments, the prefix wraps around and starts from scratch.

If it is what you need, I have no objections. So I think another parameter can be added to the constructor.

dmurat commented 2 years ago

Yes, this was exactly what I meant :-) Additional constructor parameter denoting time interval of incremental prefix/suffix change.

dmurat commented 2 years ago

I believe I figured out some consequences on database performance in the meantime. Having, for example, a prefix that updates with double speed (say 30 seconds vs 60 seconds), you will pass over a whole range of prefix values twice as fast. This means you will access twice as many index pages over the same period with the same workload.

Actually, I'm currently writing an article about random UUIDs, and uuid-creator is referenced a few times. It is not finished yet, but you can take a look if you want: https://github.com/croz-ltd/klokwrk-project/blob/feature_currentWork/support/documentation/article/random-uuid-as-database-primary-key/random-uuid-as-database-primary-key.md

Any comments are very welcome, of course :-) Tnx

fabiolimace commented 2 years ago

It is an excellent article! It explains things in a progressive and very clear way.

I hadn't read anything about UUIDs in the context of event-based architectures yet.

I will definitely reference that text in the documentation and Wiki (and update the link when the text is finished).

fabiolimace commented 2 years ago

Released version v4.6.1.

dmurat commented 2 years ago

Hi,

I'm very glad you liked the article :-) Thank you. In the meantime, I finished it. The link is now https://github.com/croz-ltd/klokwrk-project/blob/master/support/documentation/article/random-uuid-as-database-primary-key/random-uuid-as-database-primary-key.md

Tnx