{"id":464086,"date":"2025-06-21T21:00:09","date_gmt":"2025-06-21T21:00:09","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=464086"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2025-06-21T21:00:09","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=464086","title":{"rendered":"<span>What May Surprise You About UUIDv7<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d05\/f3e\/940\/d05f3e9400b9a1318a278b700cdfa1a4.jpg\" alt=\"IDs are bearings of any DB\" title=\"IDs are bearings of any DB\" width=\"800\" height=\"600\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/d05\/f3e\/940\/d05f3e9400b9a1318a278b700cdfa1a4.jpg 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d05\/f3e\/940\/d05f3e9400b9a1318a278b700cdfa1a4.jpg 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>IDs are bearings of any DB<\/figcaption><\/div>\n<\/figure>\n<p><a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc9562#name-uuid-version-7\" rel=\"noopener noreferrer nofollow\">UUIDv7<\/a>\u00a0was inspired by ULID. Like ULID, it is a 128-bit identifier, containing a timestamp on the left side and random data on the right side. In databases and distributed systems, a properly implemented UUIDv7 is always preferred over any other identifier type, including natural keys, autoincrement, UUIDv4, TypeID, ULID, KSUID, CUID, NanoID, and Snowflake ID.<\/p>\n<p>There are notable distinctions of UUIDv7:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc9562\" rel=\"noopener noreferrer nofollow\">RFC 9562<\/a>\u00a0establishes many reasonable requirements for UUIDv7 in its various sections, such as mandatory usage of a cryptographically secure pseudorandom number generator (CSPRNG). However, you need to ensure that the implementation you use satisfies these requirements in your target deployment environment<\/p>\n<\/li>\n<li>\n<p>There are various field and bit layout options in different implementations designed to provide monotonicity (increasing order of generation) within a millisecond, including cases when the system clock is rolled back. For example, UUIDv7 may contain a randomly initialized counter (in some programming languages) (with the leftmost bit optionally zeroed), or a timestamp-based counter (<a href=\"https:\/\/www.postgresql.org\/docs\/18\/functions-uuid.html\" rel=\"noopener noreferrer nofollow\">in PostgreSQL<\/a>), or both. Minor violations of monotonicity do not affect performance but may complicate debugging, pagination, log searching, and the use of time series in databases<\/p>\n<\/li>\n<li>\n<p>Additionally, a mutex (<a href=\"https:\/\/clickhouse.com\/docs\/sql-reference\/functions\/uuid-functions#generateUUIDv7\" rel=\"noopener noreferrer nofollow\">in ClickHouse<\/a>) or a sub-millisecond timestamp segment (<a href=\"https:\/\/www.postgresql.org\/docs\/18\/functions-uuid.html\" rel=\"noopener noreferrer nofollow\">in PostgreSQL<\/a>) may be used to ensure monotonicity when the generation rate in parallel processes is high. Server-side generation ensures better monotonicity compared to client-side generation<\/p>\n<\/li>\n<li>\n<p>Extracting and using the timestamp from UUIDv7 is not recommended. Consider explicit timestamp columns. However, both the timestamp extracted from UUIDv7 and the entire UUIDv7 can be used as a partitioning key in a database<\/p>\n<\/li>\n<li>\n<p>The option of shifting timestamp value by any technically feasible range (<a href=\"https:\/\/www.postgresql.org\/docs\/18\/functions-uuid.html\" rel=\"noopener noreferrer nofollow\">in PostgreSQL<\/a>,\u00a0<a href=\"https:\/\/docs.percona.com\/percona-server\/8.4\/uuid-versions.html#uuid-generator-functions\" rel=\"noopener noreferrer nofollow\">in Percona Server for MySQL<\/a>) allows you to hide the true date of record creation, prevents lock contention during parallel generation of UUIDs in several processes, and also ensures monotonicity while generation on remote clients<\/p>\n<\/li>\n<li>\n<p>UUIDv7 uses the widely compatible \u201chex-and-dash\u201d string format, but RFC 9562 does not prohibit UUIDv7 from using the more readable, compact, and easily copied Crockford\u2019s Base32, as in ULID<\/p>\n<\/li>\n<li>\n<p>UUIDv7 should be stored in 128-bit binary format in databases whenever possible. It reduces storage overhead by 30\u201340% compared to string representations, while maintaining index efficiency<\/p>\n<\/li>\n<li>\n<p>Identifiers containing UUIDs used as keys may be right-extended beyond 128 bits with any metadata and checksum<\/p>\n<\/li>\n<li>\n<p>UUIDv7 contains a 4-bit version (7) field (0b0111) and a 2-bit variant field (0b10)<\/p>\n<\/li>\n<\/ul>\n<p>UUIDv7 and bigint demonstrate equivalent search and write performance confirmed by benchmarks. The rate of UUIDv7 generation is always sufficient and does not affect the performance of databases.<\/p>\n<p>Although UUIDv7 is twice as long as a bigint, the difference in actual disk space used is much smaller. Besides, using UUID instead of bigint eliminates the need for intermediate tables and data layers.<\/p>\n<p>UUIDv7, with the same performance, eliminates the following shortcomings of auto-increment:<\/p>\n<ul>\n<li>\n<p>The need to generate new keys and synchronize them with keys from the data source when exporting and importing data, or when generating records in parallel by multiple processes<\/p>\n<\/li>\n<li>\n<p>Difficulty preventing key collisions when merging data from different database tables<\/p>\n<\/li>\n<li>\n<p>Possible errors due to key collisions when merging data<\/p>\n<\/li>\n<li>\n<p>Disclosure of the total number of records in a database table<\/p>\n<\/li>\n<li>\n<p>Ease of brute-forcing valid keys<\/p>\n<\/li>\n<li>\n<p>Impossibility of global search<\/p>\n<\/li>\n<\/ul>\n<p>You can use UUIDv7 in the same column where other versions of UUID (or ULID in binary format) are already used.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/920610\/\"> https:\/\/habr.com\/ru\/articles\/920610\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\">\n<div><figcaption>IDs are bearings of any DB<\/figcaption><\/div>\n<\/figure>\n<p><a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc9562#name-uuid-version-7\" rel=\"noopener noreferrer nofollow\">UUIDv7<\/a>\u00a0was inspired by ULID. Like ULID, it is a 128-bit identifier, containing a timestamp on the left side and random data on the right side. In databases and distributed systems, a properly implemented UUIDv7 is always preferred over any other identifier type, including natural keys, autoincrement, UUIDv4, TypeID, ULID, KSUID, CUID, NanoID, and Snowflake ID.<\/p>\n<p>There are notable distinctions of UUIDv7:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc9562\" rel=\"noopener noreferrer nofollow\">RFC 9562<\/a>\u00a0establishes many reasonable requirements for UUIDv7 in its various sections, such as mandatory usage of a cryptographically secure pseudorandom number generator (CSPRNG). However, you need to ensure that the implementation you use satisfies these requirements in your target deployment environment<\/p>\n<\/li>\n<li>\n<p>There are various field and bit layout options in different implementations designed to provide monotonicity (increasing order of generation) within a millisecond, including cases when the system clock is rolled back. For example, UUIDv7 may contain a randomly initialized counter (in some programming languages) (with the leftmost bit optionally zeroed), or a timestamp-based counter (<a href=\"https:\/\/www.postgresql.org\/docs\/18\/functions-uuid.html\" rel=\"noopener noreferrer nofollow\">in PostgreSQL<\/a>), or both. Minor violations of monotonicity do not affect performance but may complicate debugging, pagination, log searching, and the use of time series in databases<\/p>\n<\/li>\n<li>\n<p>Additionally, a mutex (<a href=\"https:\/\/clickhouse.com\/docs\/sql-reference\/functions\/uuid-functions#generateUUIDv7\" rel=\"noopener noreferrer nofollow\">in ClickHouse<\/a>) or a sub-millisecond timestamp segment (<a href=\"https:\/\/www.postgresql.org\/docs\/18\/functions-uuid.html\" rel=\"noopener noreferrer nofollow\">in PostgreSQL<\/a>) may be used to ensure monotonicity when the generation rate in parallel processes is high. Server-side generation ensures better monotonicity compared to client-side generation<\/p>\n<\/li>\n<li>\n<p>Extracting and using the timestamp from UUIDv7 is not recommended. Consider explicit timestamp columns. However, both the timestamp extracted from UUIDv7 and the entire UUIDv7 can be used as a partitioning key in a database<\/p>\n<\/li>\n<li>\n<p>The option of shifting timestamp value by any technically feasible range (<a href=\"https:\/\/www.postgresql.org\/docs\/18\/functions-uuid.html\" rel=\"noopener noreferrer nofollow\">in PostgreSQL<\/a>,\u00a0<a href=\"https:\/\/docs.percona.com\/percona-server\/8.4\/uuid-versions.html#uuid-generator-functions\" rel=\"noopener noreferrer nofollow\">in Percona Server for MySQL<\/a>) allows you to hide the true date of record creation, prevents lock contention during parallel generation of UUIDs in several processes, and also ensures monotonicity while generation on remote clients<\/p>\n<\/li>\n<li>\n<p>UUIDv7 uses the widely compatible \u201chex-and-dash\u201d string format, but RFC 9562 does not prohibit UUIDv7 from using the more readable, compact, and easily copied Crockford\u2019s Base32, as in ULID<\/p>\n<\/li>\n<li>\n<p>UUIDv7 should be stored in 128-bit binary format in databases whenever possible. It reduces storage overhead by 30\u201340% compared to string representations, while maintaining index efficiency<\/p>\n<\/li>\n<li>\n<p>Identifiers containing UUIDs used as keys may be right-extended beyond 128 bits with any metadata and checksum<\/p>\n<\/li>\n<li>\n<p>UUIDv7 contains a 4-bit version (7) field (0b0111) and a 2-bit variant field (0b10)<\/p>\n<\/li>\n<\/ul>\n<p>UUIDv7 and bigint demonstrate equivalent search and write performance confirmed by benchmarks. The rate of UUIDv7 generation is always sufficient and does not affect the performance of databases.<\/p>\n<p>Although UUIDv7 is twice as long as a bigint, the difference in actual disk space used is much smaller. Besides, using UUID instead of bigint eliminates the need for intermediate tables and data layers.<\/p>\n<p>UUIDv7, with the same performance, eliminates the following shortcomings of auto-increment:<\/p>\n<ul>\n<li>\n<p>The need to generate new keys and synchronize them with keys from the data source when exporting and importing data, or when generating records in parallel by multiple processes<\/p>\n<\/li>\n<li>\n<p>Difficulty preventing key collisions when merging data from different database tables<\/p>\n<\/li>\n<li>\n<p>Possible errors due to key collisions when merging data<\/p>\n<\/li>\n<li>\n<p>Disclosure of the total number of records in a database table<\/p>\n<\/li>\n<li>\n<p>Ease of brute-forcing valid keys<\/p>\n<\/li>\n<li>\n<p>Impossibility of global search<\/p>\n<\/li>\n<\/ul>\n<p>You can use UUIDv7 in the same column where other versions of UUID (or ULID in binary format) are already used.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/920610\/\"> https:\/\/habr.com\/ru\/articles\/920610\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-464086","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/464086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=464086"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/464086\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=464086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=464086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=464086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}