Skip to main content

A zero-lookup, partition-aware UUIDv8 implementation with microsecond precision.

Project description

MicroShard UUID (Python Implementation)

A zero-dependency, Partition-Aware UUIDv8 library for Python.

Unlike standard UUIDs (v4/v7), MicroShard embeds a 32-bit Shard ID directly into the 128-bit identifier. This enables Zero-Lookup Routing, allowing your application to determine the database shard, tenant, or region of a record simply by decoding its Primary Key.

📦 Features

  • Zero-Lookup Routing: Extract Shard/Tenant IDs instantly from the UUID.
  • Microsecond Precision: 54-bit timestamp ensures strict chronological sorting.
  • Massive Scale: Supports 4.29 Billion unique Shards/Tenants.
  • Collision Resistant: 36 bits of randomness per microsecond per shard.
  • Stateless & Stateful: Supports both functional usage and class-based generators.
  • Standard Library Only: No external dependencies (uses os, struct, uuid).

🛠 Installation

Requires Python 3.6+.


📐 Architecture: The 54/32/36 Split

MicroShard utilizes the UUIDv8 (Custom) format defined in IETF RFC 9562.

Component Bits Description Capacity
Time 54 Unix Microseconds Valid until Year 2541
Ver 4 Fixed Version 8 RFC Compliance
Shard ID 32 Logical Shard / Tenant 4.29 Billion IDs
Var 2 Fixed Variant 2 RFC Compliance
Random 36 Entropy 68.7 Billion per microsecond

🚀 Usage

1. Stateless Generation (Functional)

Best for simple scripts or when the Shard ID changes dynamically per request.

from microshard_uuid import generate, get_shard_id

# 1. Generate an ID for Shard #500
uid = generate(shard_id=500)
print(f"Generated: {uid}")

# 2. Extract Shard ID (Routing)
target_shard = uid.get_shard_id()
assert target_shard == 500

2. Stateful Generation (Class-Based)

Best for application configuration or dependency injection where the Shard ID is fixed for the lifecycle of the service.

from microshard_uuid import Generator

# Configure once at startup
id_gen = Generator(default_shard_id=101)

# Generate anywhere in your app without passing the ID
uid = id_gen.new_id()

3. Backfilling Data (Explicit Time)

If you are migrating legacy data, you can generate UUIDs for past timestamps while maintaining correct sorting order.

from datetime import datetime, timezone
from microshard_uuid import from_timestamp

# Create an ID for a specific past event (e.g., Jan 1, 2023)
past_date = datetime(2023, 1, 1, 12, 0, 0, tzinfo=timezone.utc)

legacy_uid = from_timestamp(past_date, shard_id=50)

# Supports integer microseconds as well
legacy_uid_int = from_timestamp(1672574400000000, shard_id=50)

4. Extracting Metadata

You can decode any MicroShard UUID to retrieve its creation time and origin shard.

from microshard_uuid import get_timestamp, get_shard_id

# Option A: Get Python Datetime object
dt = uid.get_timestamp()
# datetime.datetime(2025, 12, 12, 10, 0, 0, 123456, tzinfo=datetime.timezone.utc)

# Option B: Get ISO String (Strict format with Z)
iso = uid.get_iso_timestamp()
# "2025-12-12T10:00:00.123456Z"

# Get Origin Shard
shard = uid.get_shard_id()
print(f"Origin Shard: {shard}")

🛡️ Safety & Performance

Collision Probability

Uniqueness is guaranteed by the combination of: (Timestamp + Shard ID + Randomness).

  • Global Uniqueness: Guaranteed if Shard IDs are unique.
  • Per-Shard Uniqueness: A single shard has 36 bits of randomness per microsecond.
  • Risk: A collision only occurs if a single specific shard generates billions of IDs within a single microsecond. This is physically impossible on current hardware.

Performance

  • Generation: ~2-3 microseconds per ID (comparable to uuid.uuid4()).
  • Sorting: UUIDs are generated with the Timestamp in the highest bits, ensuring that standard string/byte sorting yields strict chronological order.

🧪 Running Tests

This library includes a comprehensive test suite using unittest.

# Run tests from the implementation folder
make test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

microshard_uuid-1.0.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

microshard_uuid-1.0.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file microshard_uuid-1.0.0.tar.gz.

File metadata

  • Download URL: microshard_uuid-1.0.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for microshard_uuid-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5f59e1af4f48a637ba3cb6434df48957df94aa5d36f004e7d9d5ecaa09aa7075
MD5 37e01c85fd680ee4dc2aefd02a515629
BLAKE2b-256 dc5a20ebdceba50d424b8aeb9d307b5e44f170d2f6988b5fc4546165f305cb9a

See more details on using hashes here.

File details

Details for the file microshard_uuid-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for microshard_uuid-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23c681041b20d4aab2b6e229c1d51c78a663265c19b9f8bb4eebd23d955bfe03
MD5 237a0c17e565ab489a6784cf22a4a27a
BLAKE2b-256 0a3e1d0060c47fda43ab12b0010f47c0399427bf9a5fab3d97e8835cf11d3b47

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page