question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MonotonicUlid vs Ulid

See original GitHub issue

Can you please tell me the use cases where a “non-monotonic” Ulid should be preferred over a “monotonic” Ulid? I expected that the generation of a MonotonicUlid is much slower than a non-monotonic Ulid, but according to the benchmark, MonotonicUlid are much faster than non-monotonic Ulids created by UlidCreator.getUlid().

Ulid_fast                              thrpt    5  34523,147 ± 1022,114  ops/ms (9.98)
Ulid_fast_toString                     thrpt    5  19161,375 ±  662,563  ops/ms
-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
UlidCreator_getUlid                    thrpt    5   4276,614 ±   11,069  ops/ms (1.23)
UlidCreator_getUlid_toString           thrpt    5   3645,088 ±   85,478  ops/ms
-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
UlidCreator_getMonotonicUlid           thrpt    5  32921,698 ± 1286,983  ops/ms (9.51)
UlidCreator_getMonotonicUlid_toString  thrpt    5  18541,252 ±  710,281  ops/ms

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nimo23commented, Nov 2, 2022

Oh yes the synchronized makes it slower. Good to know. Thanks!

1reaction
fabiolimacecommented, Nov 2, 2022

I implemented now the Ulid.fastMonotonic() method to test whether it is worth having a monotonic generator with SplittableRandom.

	// updates must be synchronized
	private static long lastTime = -1;
	private static Ulid lastUlid = null;

	public static synchronized Ulid fastMonotonic() {
		final long time = System.currentTimeMillis();
		if (time == lastTime) {
			lastUlid = lastUlid.increment();
		} else {
			lastUlid = fast();
		}
		lastTime = time;
		return new Ulid(lastUlid);
	}

But the throughput of Ulid.fastMonotonic() is worse (-26% less) than Ulid.fast();

------------------------------------------------------------------------
Benchmark                       Mode  Cnt      Score      Error   Units
------------------------------------------------------------------------
Throughput.Ulid_fast           thrpt    5  32754.654 ± 1276.342  ops/ms 
Throughput.Ulid_fastMonotonic  thrpt    5  24322.012 ± 1260.401  ops/ms (-26%)
------------------------------------------------------------------------

As you can see there is no performance gain, but contrary to what we expect. So I think it’s not useful because you have to synchronize the Ulid.fastMonotonic() method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How probable are collisions with ULID's monotonic option?
ULID features a monotonic option, but we had concerns about whether using it increased the probability of collisions.
Read more >
ULID vs UUID: Sortable Random ID Generators for JavaScript
Monotonic ULIDs & Seed Time. ULID allows you to get IDs with the same timestamp by passing a seed time. For example, if...
Read more >
The canonical spec for ulid - GitHub
Universally Unique Lexicographically Sortable Identifier. UUID can be suboptimal for many use-cases because: It isn't the most character efficient way of ...
Read more >
ULID — Universally Unique Lexicographically Sortable Identifier
ULID ensures that the IDs can be monotonically ordered and can be sorted even when generated within a millisecond.
Read more >
ULIDs and Primary Keys - Dave Allie
A ULID is a 128-bit label, just like a UUID. It's sortable, has millisecond precision, and is monotonically increasing, just like UUIDv7.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found