Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide custom clock source for monotonicity #2

Closed
CMCDragonkai opened this issue Sep 28, 2021 · 6 comments
Closed

Provide custom clock source for monotonicity #2

CMCDragonkai opened this issue Sep 28, 2021 · 6 comments

Comments

@CMCDragonkai
Copy link

I'm concerned that the usage of Date is not monotonic. I've tested this myself and Date can give back the same datetime when done very closely.

Furthermore if the OS clock drifts or is set backwards, I may want my application to be monotonic even then. So having the ability to provide a custom clock where I enforce monotonicity would be useful.

Can be helpful for testing and mocking if the clocksource can be dependency injected.

@kripod
Copy link
Owner

kripod commented Sep 28, 2021

Thank you for raising this concern!

I think the monotonic seq counter helps avoiding collisions as shown – when a timestamp matches the previous one, the clock sequence is increased by 1, otherwise it gets reset to 0:

uuidv7/src/_common.ts

Lines 12 to 18 in 501d4ef

let prevTimestamp = -1;
let seq = 0;
return () => {
const timestamp = Date.now();
seq = timestamp === prevTimestamp ? seq + 1 : 0;
prevTimestamp = timestamp;

OS clock drifts may possibly happen, but is there a sufficient alternative for Date.now() in JS to avoid those? We may use performance.timing.navigationStart + performance.now() or something similar:

unlike Date.now(), the values returned by performance.now() always increase at a constant rate, independent of the system clock (which might be adjusted manually or skewed by software like NTP)

@CMCDragonkai
Copy link
Author

CMCDragonkai commented Sep 28, 2021 via email

@kripod
Copy link
Owner

kripod commented Sep 28, 2021

v0.2.2 has just introduced a fix for system clock drifts: 2161e35

Your custom clock use-case would be suited better by UUIDv8, which features highly customizable timestamps. I’m closing this for now, as this project aims to offer a lightweight implementation rather than providing low-level custom options. Thanks again for sharing your ideas 😊

@kripod kripod closed this as completed Sep 28, 2021
@CMCDragonkai
Copy link
Author

@kripod thanks for that implementation, however I don't think that solution works when the process context can restart.

My situation involves process restart, and so I intend to persist the clock state on disk so I can ensure that my ids are always monotonic even after process restart.

@kripod
Copy link
Owner

kripod commented Sep 29, 2021

I see, thanks for sharing that insight. I’m not sure why persisting clock state to disk would be necessary, as time passes in a monotonic manner. There is almost no chance that a process could restart within a single millisecond in the foreseeable future. Even if that happens, it’d take ~17 years in order to have a 1% probability of at least one collision when generating 1000 IDs per hour within the same millisecond.

@CMCDragonkai
Copy link
Author

I'm building a decentralized application that will run on user machines. Users who may reset their clock. I'd prefer to be foolproof and ensure that my uuid generation isn't broken inside the app even if the OS clocks have been reset. My application is persistent, so I can save the clock state or last generated ID and use that as a reference point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants