Choosing a Fast Unique Identifier (UUID) for Lucene
Join the DZone community and get the full member experience.Join For Free
most search applications using apache lucene assign a unique id, or primary key, to each indexed document. while lucene itself does not require this (it could care less!), the application usually needs it to later replace, delete or retrieve that one document by its external id. most servers built on top of lucene, such as elasticsearch and solr , require a unique id and can auto-generate one if you do not provide it.
sometimes your id values are already pre-defined, for example if an external database or content management system assigned one, or if you must use a uri , but if you are free to assign your own ids then what works best for lucene?
one obvious choice is java's uuid class, which generates version 4 universally unique identifiers , but it turns out this is the worst choice for performance: it is 4x slower than the fastest. to understand why requires some understanding of how lucene finds terms.
blocktree terms dictionary
the purpose of the terms dictionary is to store all unique terms seen during indexing, and map each term to its metadata (
), as well as the postings (documents, offsets, postings and payloads). when a term is requested, the terms dictionary must locate it in the on-disk index and return its metadata.
the default codec uses the blocktree terms dictionary , which stores all terms for each field in sorted binary order, and assigns the terms into blocks sharing a common prefix. each block contains between 25 and 48 terms by default. it uses an in-memory prefix-trie index structure (an fst ) to quickly map each prefix to the corresponding on-disk block, and on lookup it first checks the index based on the requested term's prefix, and then seeks to the appropriate on-disk block and scans to find the term.
in certain cases, when the terms in a segment have a predictable pattern, the terms index can know that the requested term cannot exist on-disk. this fast-match test can be a sizable performance gain especially when the index is cold (the pages are not cached by the the os's io cache) since it avoids a costly disk-seek. as lucene is segment-based, a single id lookup must visit each segment until it finds a match, so quickly ruling out one or more segments can be a big win. it is also vital to keep your segment counts as low as possible!
given this, fully random ids (like uuid v4 ) should perform worst, because they defeat the terms index fast-match test and require a disk seek for every segment. ids with a predictable per-segment pattern, such as sequentially assigned values, or a timestamp, should perform best as they will maximize the gains from the terms index fast-match test.
i created a simple performance tester to verify this; the full source code is here . the test first indexes 100 million ids into an index with 7/7/8 segment structure (7 big segments, 7 medium segments, 8 small segments), and then searches for a random subset of 2 million of the ids, recording the best time of 5 runs. i used java 1.7.0_55, on ubuntu 14.04, with a 3.5 ghz ivy bridge core i7 3770k.
since lucene's terms are now fully binary as of 4.0 , the most compact way to store any value is in binary form where all 256 values of every byte are used. a 128-bit id value then requires 16 bytes.
i tested the following identifier sources:
sequential ids (0, 1, 2, ...), binary encoded.
zero-padded sequential ids (00000000, 00000001, ...), binary encoded.
nanotime, binary encoded. but remember that
nanotime is tricky
, derived from a timestamp, nodeid and sequence counter, using
, randomly generated using java's
- flake ids , using this implementation .
for the uuids and flake ids i also tested binary encoding in addition to their standard (base 16 or 36) encoding. note that i only tested lookup speed using one thread, but the results should scale linearly (on sufficiently concurrent hardware) as you add threads.
zero-padded sequential ids, encoded in binary are fastest, quite a bit faster than non-zero-padded sequential ids. uuid v4 (using java's uuid.randomuuid()) is ~4x slower.
but for most applications, sequential ids are not practical. the 2nd fastest is uuid v1 , encoded in binary. i was surprised this is so much faster than flake ids since flake ids use the same raw sources of information (time, node id, sequence) but shuffle the bits differently to preserve total ordering. i suspect the problem is the number of common leading digits that must be traversed in a flake id before you get to digits that differ across documents, since the high order bits of the 64-bit timestamp come first, whereas uuid v1 places the low order bits of the 64-bit timestamp first. perhaps the terms index should optimize the case when all terms in one field share a common prefix.
i also separately tested varying the base from 10, 16, 36, 64, 256 and in general for the non-random ids, higher bases are faster. i was pleasantly surprised by this because i expected a base matching the blocktree block size (25 to 48) would be best.
there are some important caveats to this test (patches welcome)! a real application would obviously be doing much more work than simply looking up ids, and the results may be different as hotspot must compile much more active code. the index is fully hot in my test (plenty of ram to hold the entire index); for a cold index i would expect the results to be even more stark since avoiding a disk-seek becomes so much more important. in a real application, the ids using timestamps would be more spread apart in time; i could "simulate" this myself by faking the timestamps over a wider range. perhaps this would close the gap between uuid v1 and flake ids? i used only one thread during indexing, but a real application with multiple indexing threads would spread out the ids across multiple segments at once.
i used lucene's default tieredmergepolicy , but it is possible a smarter merge policy that favored merging segments whose ids were more "similar" might give better results. the test does not do any deletes/updates, which would require more work during lookup since a given id may be in more than one segment if it had been updated (just deleted in all but one of them).
finally, i used using lucene's default codec, but we have nice postings formats optimized for primary-key lookups when you are willing to trade ram for faster lookups, such as this google summer-of-code project from last year and memorypostingsformat . likely these would provide sizable performance gains!
Published at DZone with permission of Michael Mccandless, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.