Skip to main content

A SonyFlake ID generator tailored for high-volume ID generation.

Installation

pip install sonyflake-turbo

Usage

Easy mode:

from sonyflake_turbo import SonyFlake

sf = SonyFlake(0x1337, 0xCAFE, start_time=1749081600)

print("one", next(sf))
print("n", sf(5))

for id_ in sf:
    print("iter", id_)
    break

Turbo mode:

from time import time_ns
from timeit import timeit

from sonyflake_turbo import MachineIDLCG, SonyFlake

get_machine_id = MachineIDLCG(time_ns())
EPOCH = 1749081600  # 2025-06-05T00:00:00Z

for count in [32, 16, 8, 4, 2, 1]:
    machine_ids = [get_machine_id() for _ in range(count)]
    sf = SonyFlake(*machine_ids, start_time=EPOCH)
    t = timeit(lambda: sf(1000), number=1000)
    print(f"Speed: 1M ids / {t:.2f}sec with {count} machine IDs")

Async:

import anyio
from sonyflake_turbo import AsyncSonyFlake, SonyFlake

sf = SonyFlake(0x1337, 0xCAFE, start_time=1749081600)
asf = AsyncSonyFlake(sf, sleep=anyio.sleep)  # defaults to asyncio.sleep

print("one", await asf)
print("n", await asf(5))

async for id_ in asf:
    print("aiter", id_)
    break

Important Notes

Vanilla SonyFlake Difference

In vanilla SonyFlake, whenever counter overflows, it simply waits for the next 10ms window. Which severely limits the throughput. I.e. single generator produces 256ids/10ms.

Turbo version is basically the same as vanilla SonyFlake, except it accepts more than one Machine ID in constructor args. On counter overflow, it advances to the next “unexhausted” Machine ID and resumes the generation. Waiting for the next 10ms window happens only when all of the Machine IDs were exhausted.

This behavior is not much different from having multiple vanilla ID generators in parallel, but by doing so we ensure produced IDs are always monotonically increasing (per generator instance) and avoid potential concurrency issues (by not doing concurrency).

Few other features in comparison to other SonyFlake implementations found in the wild:

  • Optional C extension module, for extra performance in CPython.

  • Async-framework-agnostic wrapper.

  • Thread-safe. Also has free-threading/nogil support [1].

Machine IDs

Machine ID is a 16 bit integer in range 0x0000 to 0xFFFF. Machine IDs are encoded as part of the SonyFlake ID:

Time

Machine ID

Counter

0x

0874AD4993 [2]

CAFE

04

SonyFlake IDs, in spirit, are UUIDv6, but compressed down to 64 bit. But unfortunately, we do not have luxury of having 48 bits for encoding node id (UUID equivalent of SonyFlake’s Machine ID). UUID standard proposes to use pseudo-random value for this field, which is sub-optimal for our case due to high risk of collisions.

Vanilla SonyFlake, on the other hand, used lower 16 bits of the private IP address. Which is sort of works, but has two major drawbacks:

  1. It assumes you have exactly one ID generator per machine in your network.

  2. You’re leaking some of your infrastructure info.

In the modern world (k8s, “lambdas”, etc…), both of these fall apart:

  1. Single machine often runs multiple different processes and/or threads. More often than not they’re isolated enough to be able to successfully coordinate ID generation.

  2. Security aspect aside, container IPs within cluster network are not something globally unique, especially if trimmed down to 16 bit.

Solving this issue is up to you, as a developer. This particular library does not include Machine ID management logic, so you are responsible for coordinating Machine IDs in your deployment.

Task is not trivial, but neither is impossible. Here are a few ideas:

  • Coordinate ID assignment via something like etcd or ZooKeeper using lease pattern. Optimal, but a bit bothersome to implement.

  • Reinvent Twitter’s SnowFlake by having a centralized service/sidecar. Extra round-trips SonyFlake intended to avoid.

  • Assign Machine IDs manually. DevOps team will hate you.

  • Use random Machine IDs. If I ignore it, maybe it will go away.jpg

But nevertheless, it has one helper class: MachineIDLCG. This is a primitive LCG-based 16 bit PRNG. It is intended to be used in tests, or in situations where concurrency is not a problem (e.g. desktop or CLI apps). You can also reuse it for generating IDs for a lease to avoid congestion when going etcd/ZooKeeper route.

How many Machine IDs you want to allocate per generator is something you should figure out on your own. Here’s some numbers for you to start (generating 1 million SonyFlake IDs):

Time

Machine IDs

1.22s

32

2.44s

16

4.88s

8

9.76s

4

19.53s

2

39.06s

1

Clock Rollback

There is no logic to handle clock rollbacks or drift at the moment. If clock moves backward, it will sleep() (await sleep() in async wrapper) until time catches up to last timestamp.

Start Time

SonyFlake ID has 39 bits dedicated for the time component with a resolution of 10ms. The time is stored relative to start_time. By default it is 1409529600 (2014-09-01T00:00:00Z), but you may want to define your own “epoch”.

Motivation

Sometimes you have to bear with consequences of decisions you’ve made long time ago. On a project I was leading, I made a decision to utilize SonyFlake. Everything was fine until we needed to ingest a lot of data, very quickly.

A flame graph showed we were sleeping way too much. The culprit was SonyFlake library we were using at that time. Some RTFM later, it was revealed that the problem was somewhere between the chair and keyboard.

Solution was found rather quickly: just instantiate more generators and cycle through them about every 256 IDs. Nothing could go wrong, right? Aside from fact that hack was of questionable quality, it did work.

Except, we’ve got hit by Hyrum’s Law. Unintentional side effect of the hack above was that IDs lost its “monotonically increasing” property [3]. Ofc, some of our and other team’s code were dependent on this SonyFlake’s feature. Duh.

Adding even more workarounds like pre-generate IDs, sort them and ingest was a compelling idea, but I did not feel right. Hence, this library was born.

Why should I use it?

If you’re starting a new project, please use UUIDv7. It is superior to SonyFlake in almost every way. It is an internet standard (RFC 9562), it is already available in various languages’ standard libraries and is supported by popular databases (PostgreSQL, MariaDB, etc…).

Otherwise you might want to use it for one of the following reasons:

  • You already use it and encountered similar problems mentioned in Motivation section.

  • You want to avoid extra round-trips to fetch IDs.

  • Usage of UUIDs is not feasible (legacy codebase, db indexes limited to 64 bit integers, etc…) but you still want to benefit from index locality/strict global ordering.

  • As a cheap way to reduce predicability of IDOR attacks.

  • Architecture lunatism is still strong within you and you want your code to be DDD-like (e.g. being able to reference an entity before it is stored in DB).

Development

Install:

python3 -m venv env
. env/bin/activate
pip install -r requirements-dev.txt
pip install -e .

Run tests:

pytest

Build wheels:

pip install cibuildwheel
cibuildwheel

Build a py3-none-any wheel (without the C extension):

SONYFLAKE_TURBO_BUILD=0 python -m build --wheel

Metadata

Release files for sonyflake-turbo 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sonyflake-turbo 1.0.1
File Size Uploaded
sonyflake_turbo-1.0.1.tar.gz 22.3 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for sonyflake-turbo 1.0.1
File
sonyflake_turbo-1.0.1-py3-none-any.whl Python 3 none any Details
sonyflake_turbo-1.0.1-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
sonyflake_turbo-1.0.1-cp310-abi3-win32.whl CPython 3.10 abi3 Windows x86-32 Details
sonyflake_turbo-1.0.1-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details
sonyflake_turbo-1.0.1-cp310-abi3-musllinux_1_2_aarch64.whl CPython 3.10 abi3 Linux musl 1.2+ ARM64 Details
sonyflake_turbo-1.0.1-cp310-abi3-manylinux_2_28_x86_64.whl CPython 3.10 abi3 Linux glibc 2.28+ x86-64 Details
sonyflake_turbo-1.0.1-cp310-abi3-manylinux_2_28_aarch64.whl CPython 3.10 abi3 Linux glibc 2.28+ ARM64 Details
sonyflake_turbo-1.0.1-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details

Total release size: 182.4 kB

Release files / sonyflake_turbo-1.0.1.tar.gz

Download URL sonyflake_turbo-1.0.1.tar.gz
Size 22.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5706680739505c46ee5566de1464f95d1d842fabc7c14fd0b473f7fb47a04afc
BLAKE2b-256 checksum
How to use checksums
c4867b93ae087cec89e382bf63a5ff02de0faa4c9450f0df64485e1b00ffa800
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 5, 2026.

Transparency log

Release files / sonyflake_turbo-1.0.1-py3-none-any.whl

Download URL sonyflake_turbo-1.0.1-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
81e16941baefbac034aade282f911f5dcd39d8cd39261fabc68c96a18d6170e5
BLAKE2b-256 checksum
How to use checksums
fff6da68b6a37c56f92c96bb5f0242ffd4e94ee1abe935b9fcc3d5099bce3063
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 5, 2026.

Transparency log

Release files / sonyflake_turbo-1.0.1-cp310-abi3-win_amd64.whl

Download URL sonyflake_turbo-1.0.1-cp310-abi3-win_amd64.whl
Size 21.2 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
103795905312f14a2cd505ca6c7c990cfaa1607c2e20aaa9bf7a9bca17b2918b
BLAKE2b-256 checksum
How to use checksums
33859f9aad20fe89102c32fdb409458211d5af981fe2c8ef762881ee00166e74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 5, 2026.

Transparency log

Release files / sonyflake_turbo-1.0.1-cp310-abi3-win32.whl

Download URL sonyflake_turbo-1.0.1-cp310-abi3-win32.whl
Size 20.9 kB
Tags CPython 3.10 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
15885e0f71d9bba5755335250be50daf3fc2f6aa27b155f38df0269edf565207
BLAKE2b-256 checksum
How to use checksums
7591e461053a06eca8ebe4cab1233ac9293471125e84619deb73fc31293d2e30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 5, 2026.

Transparency log

Release files / sonyflake_turbo-1.0.1-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL sonyflake_turbo-1.0.1-cp310-abi3-musllinux_1_2_x86_64.whl
Size 21.4 kB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
c994091f3aff85ccca56e1c3e5c3b39c87bed5f3796736593e7ac220d74dbb49
BLAKE2b-256 checksum
How to use checksums
2b8e74b0ec93678f8457e4b2325a9e8bade87c8c7c79e169586cd68f0b4efd27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 5, 2026.

Transparency log

Release files / sonyflake_turbo-1.0.1-cp310-abi3-musllinux_1_2_aarch64.whl

Download URL sonyflake_turbo-1.0.1-cp310-abi3-musllinux_1_2_aarch64.whl
Size 22.3 kB
Tags CPython 3.10 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
55e87353c7829b79f715956cdf61777dae8365b36c35f75aafa4626ab8311fc8
BLAKE2b-256 checksum
How to use checksums
31db729495da475d5c7acae0a49161e45ad9322f24e1fc1633f49c2d2d47155e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 5, 2026.

Transparency log

Release files / sonyflake_turbo-1.0.1-cp310-abi3-manylinux_2_28_x86_64.whl

Download URL sonyflake_turbo-1.0.1-cp310-abi3-manylinux_2_28_x86_64.whl
Size 21.0 kB
Tags CPython 3.10 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
baceb7e5fe99a9abb3f5baec63855fab1de6923a3ce3f6d01be183d37a2c5edf
BLAKE2b-256 checksum
How to use checksums
84d2aa868160a660daf40c7db67606278d7688ecee31dc9d46235d15d4030230
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 5, 2026.

Transparency log

Release files / sonyflake_turbo-1.0.1-cp310-abi3-manylinux_2_28_aarch64.whl

Download URL sonyflake_turbo-1.0.1-cp310-abi3-manylinux_2_28_aarch64.whl
Size 22.5 kB
Tags CPython 3.10 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
a7dea477ea83bd4e5c9145d7f5268c98d2f4a04815d795269610674b837b449e
BLAKE2b-256 checksum
How to use checksums
0075a271bbb65a3bcacdfbfcc0c1aeb6de23ba98d5b2c1fd11955fee92f6878a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 5, 2026.

Transparency log

Release files / sonyflake_turbo-1.0.1-cp310-abi3-macosx_11_0_arm64.whl

Download URL sonyflake_turbo-1.0.1-cp310-abi3-macosx_11_0_arm64.whl
Size 19.4 kB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
bb7cf2d55313f77ec57d4cfed2466fe574c07cfb5afb290e2234ba81ab1379af
BLAKE2b-256 checksum
How to use checksums
35fa64a7d191d778ad621a8b23e5ccd3931c47d55c75771285b35194659b74d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 5, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.1 This release

9 release files

0.1.0

8 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page