Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Python UUIDv7: time-ordered IDs without treating them as a clock

Last updated: 1 Oct 20265 min read
tutorial
IntermediateBy AITrove Editorial

uuid.uuid7 creates identifiers with a millisecond timestamp component and local ordering support.

Download Python source kit

Operation contract

Two new order IDs are generated and decoded. Both have version seven, distinct bytes, and nondecreasing embedded millisecond times in this process. The program prints properties rather than the IDs themselves because the values depend on the run.

Failure boundary

An ID is not an authorization token or proof that an order existed at the embedded time. Clocks can shift, distinct hosts have no single strict issue order, and an attacker can construct a UUID with chosen bits. Store a trusted creation timestamp separately when audit or cross-host ordering matters.

Working program

python
import uuid

first_order_id = uuid.uuid7()
second_order_id = uuid.uuid7()
print("versions", first_order_id.version, second_order_id.version)
print("distinct", first_order_id != second_order_id)
print("time_non_decreasing", first_order_id.time <= second_order_id.time)

Output

Output
versions 7 7
distinct True
time_non_decreasing True

Costs and limits

Generation uses a fixed-size identifier and constant-size metadata. Sorting many IDs still costs O(n log n), and a timestamp component does not eliminate an indexed database access path.

Common Mistakes

  • Do not make access decisions from an ID's timestamp bits.
  • Do not claim a global strict order across independent hosts.
  • Do not equate a generated ID with a durable database commit.

Connected lessons

Test this contract.

python
uuid7-ordered-identifiers
Storage details