Skip to main content

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

Project description

sparkid

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

1ocYKWj8qj8XxUmCeZ9cHo
1ocYKWj8MvH3wKafkuX8V7
1ocYKWj8RGe6aGYLZ7ScYY

Install

pip install sparkid

Usage

from sparkid import generate_id

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

Properties

Property Value
Length 22 characters, fixed
Alphabet Base58 (no 0, O, I, l)
Sortable Lexicographically, by creation time
URL-safe Yes
Collision resistance ~58^14 (~1.8 x 10^24) 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][14-char random]
  • Timestamp (8 chars): Current time in milliseconds, Base58-encoded. IDs generated later always sort after earlier ones.
  • Random (14 chars): Rejection-sampled from os.urandom with no modulo bias. Ensures uniqueness even within the same millisecond.

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

~2.5 million IDs/sec on Python 3.12 (~0.4 us/call):

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.0.0.tar.gz (3.9 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.0.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sparkid-1.0.0.tar.gz
Algorithm Hash digest
SHA256 cc838844ac1874a32be463c36f6789b2b07b93db67639d461c148d12a6dd6230
MD5 f4012b1eb9111fb328cd33b68a112176
BLAKE2b-256 5d7b371774827b9215da8622ec189de35c27904188c9be1e6cf18a8ec8d51fd4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sparkid-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for sparkid-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98b8850e8c76b7a3d6da922a15c9b3af005d263b120faf60b3d8ea615a16fae3
MD5 b3d0a9f54d8e22de7d2105f225d004d5
BLAKE2b-256 9451f0bfebfca22810bdc076e597196a44e6487e296bc775d2c980e08b8fe081

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