Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate UUID sequentially so that difference between two consecutive uuids would be 1?

I am working on cursor based pagination and need a unique column which is sequential, so it would give proper fetch result. I know this can be solved by setting column as auto_increment with datatype bigint but eventually it will hit the limit of it which is 9223372036854775807.

So I am thinking like generating a sequential UUID like below which has compareTo result is 1.

cf3ea0ca-282d-11ec-9624-a71256fa1790

cf3ea0cb-282d-11ec-9624-410ea2fdd62c

cf3ea0cc-282d-11ec-9624-3b1a2da8c7d0

cf3ea0cd-282d-11ec-9624-877d8646d279

I generated these UUIDs using https://github.com/cowtowncoder/java-uuid-generator library

UUID uuid = Generators.timeBasedGenerator().generate();

This is how UUID can be generated, but it is not giving consistent results.

It there any other way to generate UUID which is sequential in ascending order.

like image 418
pjsagar Avatar asked Sep 05 '25 04:09

pjsagar


1 Answers

You asked:

How to generate UUID sequentially so that difference between two consecutive uuids would be 1?

No, you cannot generate consecutive UUID values.

👉 You’ve misunderstood the goal of a universally unique identifier (UUID).

A UUID is an identifier that can be generated without needing to coordinate through a central authority. So that rules out a sequence such as 1, 2, 3, ….

UUIDs are intended to be (a) virtually unique, and (b) arbitrary in value. The next UUID in a procession is not predictable.

Even the type of content in a UUID may vary, with several versions of UUIDs having been defined, and some new ones proposed. One version representing a point in space and time, another version is nearly entirely random, and still others may be generated using other means. But all are valid UUIDs and can be used together.


Work is underway on new Versions 6, 7, and 8 UUIDs. In this not-yet-finished specification draft, the bits of the UUID are rearranged to make the date portion sort chronologically. This natural ordering will make indexing in databases more efficient.

like image 119
Basil Bourque Avatar answered Sep 07 '25 21:09

Basil Bourque