Skip to main content

Fast, time-sortable, 21-char Base58 unique ID generator

Project description

⚡ sparkid

Fast, monotonic, time-sortable, 21-char Base58 unique ID generator. Zero dependencies.

1ocmpHE1bFnygEBAPTzMK
1ocmpHE1bFnygFv4Wp4dL
1ocmpHE1bFnygGoUXUL7X

Install

pip install sparkid

Usage

from sparkid import generate_id

id = generate_id()
# => "1ocmpHE1bFnygEBAPTzMK"

Extract timestamp

from sparkid import extract_timestamp

id = generate_id()
dt = extract_timestamp(id)
print(dt.isoformat())
# => "2025-11-14T22:13:20+00:00"

Properties

Property Value
Length 21 characters, fixed
Alphabet Base58 (no 0, O, I, l)
Sortable Lexicographically, by creation time
Monotonic Strictly increasing within each thread
URL-safe Yes
Collision resistance ~58^13 (~8.4 x 10^22) combinations per millisecond
Randomness Cryptographically secure (os.urandom)
Thread-safe Yes (via threading.local)

How it works

Each ID is composed of two parts:

[8-char timestamp][13-char suffix]
  • Timestamp (8 chars): Current time in milliseconds, Base58-encoded. IDs generated in a later millisecond always sort after earlier ones.
  • Suffix (13 chars): Seeded from os.urandom (rejection-sampled, no modulo bias) at the start of each millisecond, then monotonically incremented for each subsequent ID within that millisecond. This guarantees strict ordering even when multiple IDs share a timestamp.

Ordering guarantees

IDs from a single IdGenerator instance (or a single thread using generate_id()) are strictly monotonically increasing — every ID is lexicographically greater than the one before it.

Across threads, IDs are unique but not ordered relative to each other. Each thread gets its own generator via threading.local, so there is no cross-thread coordination. This is the same guarantee provided by most UUID v7 libraries.

If you need process-wide monotonic ordering across threads, wrap a single IdGenerator in a lock:

import threading
from sparkid import IdGenerator

_lock = threading.Lock()
_gen = IdGenerator()

def generate_id_monotonic():
    with _lock:
        return _gen()

Advanced usage

For manual control, use the IdGenerator class directly:

from sparkid import IdGenerator

gen = IdGenerator()
id = gen()

Each IdGenerator instance maintains its own internal state. The module-level generate_id() function uses threading.local to automatically create one instance per thread.

Performance

python bench/benchmark.py

License

MIT

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

sparkid-1.4.0.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

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

sparkid-1.4.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file sparkid-1.4.0.tar.gz.

File metadata

  • Download URL: sparkid-1.4.0.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sparkid-1.4.0.tar.gz
Algorithm Hash digest
SHA256 fe1cdeff8b24275d5b0c414b3c0fa1a6a55f7c51d94512595838e56044197558
MD5 90cae94da9696e97188fee94d5e0bb65
BLAKE2b-256 fda9c978107ef698238ae9a45fea6802d8597bc0600e268c9c58f3dcf842105a

See more details on using hashes here.

File details

Details for the file sparkid-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: sparkid-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sparkid-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0accb1abc7eced497a3146a33b9bede089999367773b160c401c2bfc240b589b
MD5 965946eca673a6e943fdecb0ac05e68e
BLAKE2b-256 57b580f62144e04f9537f14b600ea1e76f5f13ffea5e22c6d2dbd6e67f166492

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