Skip to main content

Timeflakes are 64-bit roughly-ordered, globally-unique, URL-safe UUIDs.

Project description

timeflake

Timeflakes are 64-bit (unsigned), roughly-ordered, URL-safe UUIDs. Inspired by Twitter's Snowflake and Instagram's UUID.

It supports incremental sequence per shard ID, and cryptographically strong pseudo-random numbers.

The IDs store the following in binary form (in this order):

  • Time since custom epoch in seconds (32 bits).
  • Logical shard ID (10 bits).
  • Sequence number (22 bits).

Example

timeflake = Timeflake()
timeflake.random()
>>> 'eihdZ7Hqa'

The resulting string efqCcXufN contains the following information:

uint64 = 4085399177663909
timestamp = 1578784406
shard_id = 123
sequence_number = 5541

Properties

Some nice properties of having an auto-incrementing sequence as the most significant part of the resulting ID are:

  • Reduced performance impact when using clustered indices on relational databases (vs random UUIDs).
  • The IDs are (roughly) sortable, so you can tell if one ID was created a few seconds before or after another.

The .random() method returns a new UUID using cryptographically strong pseudo-random numbers for the sequence number.

When using the random method, the probability of a collision per logical shard per second is 0.00000024 (about 1 in 4 million). If you do not specify the shard_id, a random one will be selected for you.

You can also use the .next() method to use an auto-incrementing number for the sequence number:

timeflake = Timeflake(shard_id=7)
timeflake.next()
>>> 'eicbZeGxe'
# uint64=4090831824755682 timestamp=1578785671 shard_id=7 sequence_number=7138

By default they are encoded in base57 for (nice) URL-safe representation. This means they are concise (max length of 11 characters).

If you prefer to work with the unsigned 64-bit integer form, simply pass encoding='uint64' to the instance:

timeflake = Timeflake(encoding='uint64')
timeflake.random()
>>> 4085399177663909

The default alphabet for base57 encoding is: 23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz. It intentionally removes visually similar characters (like 1 and l) while keeping the resulting ID short with a max of 11 characters.

When using the default epoch (2020-01-01), the IDs will run out at around 2088-01-19.

Supported versions

I'll be adding tests for various python versions. But I only intend to support Python 3.7+ at this moment.

Dependencies

No dependencies other than the standard library.

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

timeflake-0.1.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

timeflake-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file timeflake-0.1.1.tar.gz.

File metadata

  • Download URL: timeflake-0.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.7.4 Darwin/19.0.0

File hashes

Hashes for timeflake-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f6e78672b5553ac848a7fce5fe7d8d682aa70c76ff96c3fb671c0b1f06a46152
MD5 c2adbe051f608533aadc94bb8ecd8402
BLAKE2b-256 db86e2fc8b95600f2bc569b04f4b1a16918001b26faa0602ce2a64b6cfeae187

See more details on using hashes here.

File details

Details for the file timeflake-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: timeflake-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.7.4 Darwin/19.0.0

File hashes

Hashes for timeflake-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c347086baaee968df2590dd7d23ac246f254a7b8c7eced18cb18516c57c8c6fb
MD5 00ea1dd892c253d28604d3ee6033c2f1
BLAKE2b-256 3018ba30e8cef256dc5b80b3d9c689e04fe647485894f3fdf1c9726855bea633

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