To use RavenDB's automatic HiLo ID generation, a session needs to store a new document with id = null.
RavenDB's automatic HiLo ID generator includes the collection name, unique number, and node tag to ensure that
the ID is unique across the cluster.
However, you can use the HiLo algorithm and create documents with your own, manually created ID that is based on the next HiLo ID number
provided by the client.
The manual generator does not guarantee unique IDs across a cluster because it does not attach a collection name and a node-tag to the ID.
It only provides the next number in the range that it got from the server.
RavenDB can only guarantee unique IDs when using RavenDB's ID generators.
When manually specifying your own IDs, you are responsible to ensure that the IDs are unique.
Either one of the following overload methods will return the next available ID from the HiLo numbers reserved for the client.
The returned ID number can then be used when storing a new document.
Task<long> GenerateNextIdForAsync(string database, object entity);
Task<long> GenerateNextIdForAsync(string database, Type type);
Task<long> GenerateNextIdForAsync(string database, string collectionName);
|The database to write onto.
null will create the Id for the default database set in the document store.
|The collection that the document will be added to.
|An instance of the specified collection.
|The collection entity type. It is usually the singular of the collection name.
For example, collection = "Orders", then type = "Order".
|The next available number from the HiLo range reserved for the client.
Examples: Manual HiLo Generators
The following example shows how to get the next HiLo ID number from the client.
The ID provided is the next unique number without the node tag and the collection.
This ID is then used to create and store a new document.
GenerateNextIdForAsync ensures minimal calls to the server, as the ID is generated by the client from the reserved range of numbers.
using (var session = store.OpenSession())
// Using overload - GenerateNextIdFor(string database, string collectionName);
var nextId = await store.HiLoIdGenerator.GenerateNextIdForAsync(null, "Products");
// Using overload - GenerateNextIdFor(string database, object entity);
nextId = await store.HiLoIdGenerator.GenerateNextIdForAsync(null, new Product());
// Using overload - GenerateNextIdFor(string database, Type type);
nextId = await store.HiLoIdGenerator.GenerateNextIdForAsync(null, typeof(Product));
// Now you can create a new document with the ID received.
var product = new Product
Id = nextId.ToString()
// Store the new document.
Unique IDs across the cluster
This manual generator sample is sufficient if you are using only one server. If you want to ensure unique IDs across the cluster,
we recommend using our default generator.
You may also consider using the cluster-wide Identities generator,
which guarantees a unique ID across the cluster.
It is more costly than our default HiLo generator because it requires a request from the server for each ID,
and the server needs to do a Raft consensus check
to ensure that the other nodes in the cluster agree that the ID is unique, then returns the ID to the client.