Welcome to Python Local SnowFlake’s documentation!

A library that provides snowflake features to local thread. You will not need to setup any server to serve this service.

  • _how-it-build-unique-id
  • _how-to-use

How does it build Unique ID?

I have referenced the algorithm from:

to build this library.

The main idea is, each ID is built from 3 parts:

  1. The timestamp part: 41 bit (gives us 41 years of IDs with a custom epoch)
  2. The logical shard part: 15 bit (gives us 128*256 logical shards)
  3. Auto-incrementing sequence part: 8 bit (so we can generates 256 IDs per shard per millisecond)

How does it detect logical shard?

The idea is: each logical shard is a combination of the server/node and the thread where the code run in.

Server/Node ID

This is the number to identify the server/node, this will get from environment variable named: PYLOCALFLAKE_NODE_ID

It will take 7 bits, gives us capacity to handle 128 nodes.

Thread ID

This will identify each thread/instance of the ID generator pernode. This will be an anto-increment number that be shared all over the server/node.

It will takes 8 bits, give uses capacity to handle 256 threads per node.

I used a file to share this: /tmp/pylocalflake.tid.

This file is a simple database (CSV formatted). Each row has 3 fields in order: thread_id, real_process_id, real_thread_id.

Whenever a localflake instance is created, it will check this file with a lock, take a comfortable thread-id, save its information (real process-id, real thread-id) to claim its thread-id, then release the lock.

Whenever a localflake instance is deleted or the process exit, it unclaims the thread-id.

How to use?

# just import and use it
from localflake import get_guid

>>> print(get_guid())
12342432143