Skip to main content

A distributed unique ID generator inspired by Twitter's Snowflake

Project description

sonyflake

Sonyflake is a distributed unique ID generator inspired by Twitter’s Snoflake.

This is a python rewrite of the original sony/sonyflake project, written in Go.

Sonyflake focuses on lifetime and performance on many host/core environment. So it has a different bit assignment from Snowflake. By default, a Sonyflake ID is composed of:

  • 39 bits for time in units of 10 msec

  • 8 bits for a sequence number

  • 16 bits for a machine id

As a result, Sonyflake has the following advantages and disadvantages:

  • The lifetime (174 years) is longer than that of Snowflake (69 years)

  • It can work in more distributed machines (2^16) than Snowflake (2^10)

  • It can generate 2^8 IDs per 10 msec at most in a single instance (fewer than Snowflake)

However, if you want more generation rate in a single host, you can easily run multiple Sonyflake or AsyncSonyflake instances in parallel using threads or asyncio tasks.

In addition, you can adjust the lifetime and generation rate of Sonyflake by customizing the bit assignment and the time unit.

Installation

Python 3.11 or higher is required

Stable

# Linux/macOS
python -m pip install -U sonyflake

# Windows
py -3 -m pip install -U sonyflake

Development

# Linux/macOS
python -m pip install -U "sonyflake @ git+https://github.com/iyad-f/sonyflake"

# Windows
py -3 -m pip install -U "sonyflake @ git+https://github.com/iyad-f/sonyflake"

Usage

Creating a new Sonyflake instance.

Sync

from sonyflake import Sonyflake

sf = Sonyflake()

Async

from sonyflake import AsyncSonyflake

sf = AsyncSonyflake()

You can configure Sonyflake with the following options:

  • bits_sequence is the bit length of a sequence number. If bits_sequence is not provided, the default bit length is used, which is 8. If bits_sequence is 31 or more, an error is raised.

  • bits_machine_id is the bit length of a machine ID. If bits_machine_id is not provided, the default bit length is used, which is 16. If bits_machine_id is 31 or more, an error is raised.

  • time_unit is the time unit of Sonyflake. If time_unit is not provided, the default time unit is used, which is 10 msec. If time_unit is less than a millisecond, an error is raised.

  • start_time is the time since which the Sonyflake time is defined as the elapsed time. If start_time is not before the current time, an error is raised.

  • machine_id is the unique ID of a Sonyflake instance. If machine_id is not provided, the default machine_id is used, which is the lower 16 bits of the private IP address.

  • check_machine_id validates the uniqueness of a machine ID. If check_machine_id returns false, an error is raised. If check_machine_id is not provided, no validation is done.

The bit length of time is calculated by 63 - bits_sequence - bits_machine_id. If it is less than 32, an error is raised.

In order to get a new unique ID, you just have to call the method next_id.

Sync

import datetime

from sonyflake import Sonyflake

start_time = datetime.datetime(2025, 1, 1, 0, 0, 0, 0, datetime.UTC)
sf = Sonyflake(start_time=start_time)
next_id = sf.next_id()
print(next_id)

Async

import asyncio
import datetime

from sonyflake import AsyncSonyflake


async def main() -> None:
    start_time = datetime.datetime(2025, 1, 1, 0, 0, 0, 0, datetime.UTC)
    sf = AsyncSonyflake(start_time=start_time)
    next_id = await sf.next_id()
    print(next_id)

asyncio.run(main())

next_id can continue to generate IDs for about 174 years from start_time by default. But after the Sonyflake time is over the limit, next_id raises an error.

Examples

Examples can be found in the examples directory

Contact

Send a DM on discord at iyad8888.

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

sonyflake-1.0.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

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

sonyflake-1.0.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sonyflake-1.0.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.19

File hashes

Hashes for sonyflake-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8e8b92cd7702b3414d5560d4f4d88dd6141c6c1c549ee21be9b78b006a60c0e5
MD5 a50111e1437d5d013adf3082db65b110
BLAKE2b-256 9fef383ec5956a2ba605d8c041abe1deae1bc5710c53081899ec00b2c2d4309e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sonyflake-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.19

File hashes

Hashes for sonyflake-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2b6f566c3b950ce6afd59a1400c388b8fd5ac13f090655b50b3936628d6bbf4
MD5 10ab82bfe25ebcd40674cd192563ad83
BLAKE2b-256 28171607b52bbe9888accd64a40112aff19ca09cc7e85f0ca2eeb46995288db9

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