Skip to main content

A fast, async Snowflake ID generator for Python.

Project description

Snowflake ID Generator

A Python library for generating unique, distributed IDs using a modified Snowflake algorithm. This library allows for easy generation of Snowflake IDs, encoding/decoding to Base62, and extracting information from the generated IDs.

Features

  • Asynchronous ID generation: Supports high-throughput applications.
  • Customizable bit allocation: Configurable time, node, worker, and sequence bits.
  • Base62 encoding/decoding: Easily encode Snowflake IDs into a compact, URL-friendly format.
  • Extractable components: Retrieve timestamp, node ID, worker ID, and sequence number from a generated Snowflake ID.

Installation

You can install the library using pip after building the package:

pip install git+https://github.com/10XScale-in/snowflakeid.git

or

pip install https://github.com/10XScale-in/snowflakeid/releases/download/v0.1.0/snowflakeid-0.1.0-py3-none-any.whl

Usage

Basic Usage

Here's a quick example of how to use the Snowflake ID generator:

import asyncio
from snowflakeid import SnowflakeIDGenerator, SnowflakeIDConfig

async def main():
    generator = SnowflakeIDGenerator()
    snowflake_id = await generator.generate()
    print(f"Generated Snowflake ID: {snowflake_id}")

    # Base62 encoding
    encoded_id = SnowflakeIDGenerator.encode_base62(snowflake_id)
    print(f"Base62 Encoded ID: {encoded_id}")

    # Decoding back to Snowflake ID
    decoded_id = SnowflakeIDGenerator.decode_base62(encoded_id)
    print(f"Decoded Snowflake ID: {decoded_id}")

    # Extracting components from Snowflake ID
    components = generator.extract_snowflake_info(snowflake_id)
    print(f"Extracted Components: {components}")

asyncio.run(main())

Configuration

You can customize the ID generation by passing a SnowflakeIDConfig object to the SnowflakeIDGenerator. All configuration parameters are adjustable, allowing you to tailor the generator to your specific needs.

Custom Configuration Example (64-bit ID)

from snowflakeid import SnowflakeIDGenerator, SnowflakeIDConfig

config = SnowflakeIDConfig(
    epoch=1609459200000,  # Custom epoch (January 1, 2021)
    node_id=1,
    worker_id=2,
    time_bits=39,   # 39 bits for time
    node_bits=5,    # 5 bits for node ID (up to 32 nodes)
    worker_bits=8   # 8 bits for worker ID (up to 256 workers)
)
generator = SnowflakeIDGenerator(config=config)

Custom Configuration Examples for Different Bit Allocations

Example 1: 64-bit ID

  • Time Bits (39 bits): Encodes the timestamp relative to the epoch, allowing up to ~17 years of milliseconds.
  • Node Bits (5 bits): Allows for up to 32 unique nodes.
  • Worker Bits (8 bits): Allows for up to 256 unique workers per node.
  • Sequence Bits (12 bits): Automatically calculated to allow up to 4096 IDs to be generated per worker, per millisecond.
from snowflakeid import SnowflakeIDGenerator, SnowflakeIDConfig

config = SnowflakeIDConfig(
    epoch=1609459200000,
    node_id=1,
    worker_id=2,
    time_bits=39,
    node_bits=5,
    worker_bits=8
)
generator = SnowflakeIDGenerator(config=config)

Example 2: 32-bit ID

  • Time Bits (21 bits): Encodes the timestamp relative to the epoch, allowing up to ~2 minutes of milliseconds.
  • Node Bits (1 bit): Allows for 2 unique nodes.
  • Worker Bits (6 bits): Allows for up to 64 unique workers per node.
  • Sequence Bits (4 bits): Automatically calculated to allow up to 16 IDs to be generated per worker, per millisecond.
from snowflakeid import SnowflakeIDGenerator, SnowflakeIDConfig

config = SnowflakeIDConfig(
    epoch=1609459200000,
    node_id=1,
    worker_id=2,
    time_bits=21,
    node_bits=1,
    worker_bits=6,
    total_bits=32  # Adjust total bits to 32 for a 32-bit ID
)
generator = SnowflakeIDGenerator(config=config)

Default Bit Allocation

By default, the Snowflake ID is a 64-bit integer with the following bit allocation:

  • Total Bits (64 bits): The total number of bits used to generate the ID.
  • Time Bits (40 bits): Encodes the timestamp relative to the specified epoch.
  • Node Bits (7 bits): Identifies the node generating the ID.
  • Worker Bits (5 bits): Identifies the worker within the node.
  • Sequence Bits (12 bits): A sequence counter that ensures uniqueness within the same millisecond.

Extracting Snowflake ID Components

You can extract individual components (timestamp, node ID, worker ID, and sequence) from a Snowflake ID:

from snowflakeid import SnowflakeIDGenerator, SnowflakeIDConfig
generator = SnowflakeIDGenerator()
snowflake_id = await generator.generate()
components = generator.extract_snowflake_info(snowflake_id)
print(components)

This will return a dictionary with the following keys:

  • timestamp: The time at which the ID was generated, formatted as a human-readable string.
  • node_id: The ID of the node that generated the ID.
  • worker_id: The ID of the worker that generated the ID.
  • sequence: The sequence number within the same millisecond.

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.

Testing

To run the unit tests, use the following command:

pytest

Acknowledgements

This project is inspired by Twitter's Snowflake ID generation algorithm. Special thanks to the contributors and open-source community.

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

snowflakekit-0.1.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

snowflakekit-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file snowflakekit-0.1.0.tar.gz.

File metadata

  • Download URL: snowflakekit-0.1.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.4

File hashes

Hashes for snowflakekit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7c6374a03bef5009f5105216461c7d2b8117ede544622528b238482feee93345
MD5 ce5a2cb8ed3c1c22bd9f23ca09fb8a9b
BLAKE2b-256 c9ed675903b049ef4e3306d05c765ba1a0f0324fe706366d27f0bb2de7ee699c

See more details on using hashes here.

File details

Details for the file snowflakekit-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for snowflakekit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee0cb7d1bb481d5e71da81248b6c010c3f1b3fd38dd88ea37e81af7ed64145f9
MD5 51ee3396e00943ebf19368da451ef24f
BLAKE2b-256 abfdbfe507f8d08ee2433f91b657d2fd4770c3eb9021da00daa64066f014fa86

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