Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works
Anything · Lazarus or Delphi
Random projectRandom project

SnowFlakeID-Delphi

Snowflake's unique identifier generation system for objects within a distributed system or database.

View on GitHub

Member rating

No ratings yet.

Sign in to rate this project.

README

Snowflake IDs for Delphi

Snowflake's unique identifier generation system is a method of generating unique identifiers for objects within a distributed system or database.

Snowflake IDs for Delphi

Usage | Examples

Why you need it!?

In distributed systems, generating unique identifiers is crucial for tracking and managing entities across multiple machines. Using a snowflake ID approach ensures that IDs are system unique rather than globally unique, minimizing conflicts in high-volume environments, AND it's sortable.

Snowflake Components Usage

  • Timestamp: Represents the time when the ID was generated, facilitating chronological ordering.
  • MachineID: Serves as a sender ID, identifying the specific machine or instance that generated the ID, ensuring system uniqueness.
  • Sequence: Functions as an acknowledgment counter, incrementing with each ID generated, allowing multiple IDs to be created without collision.

This design fits within a 64-bit integer (Int64).

Companies Using Snowflake IDs

Several companies have adopted Snowflake IDs for efficient ID generation:

  • Twitter: Utilized Snowflake IDs for unique identifiers in their tweet and user systems, enabling scalability and efficient data indexing.
  • Instagram: Employed Snowflake IDs for photos and user accounts, ensuring unique content identification and efficient database operations.
  • Uber: Implemented Snowflake IDs for trips and drivers, supporting global scalability and efficient data tracking across their systems.
  • Airbnb: Used Snowflake IDs for listings and bookings, achieving global uniqueness and reliable inventory management.
  • GitHub: Leveraged Snowflake IDs for repositories and issues, ensuring unique identification and smooth navigation for developers.

Usage

  1. Clone or simply download the unit: Clone the repository or download the SFID.pas unit to your Delphi project.
  2. Start using it: Assign new variable from TSFID, create new ID TSFID.NewSfid.
  3. Please give it a 🌟 and share it with others

Examples

Example: Simple generate SnowFlake ID

delphi
uses
    SFID;

var
    Sfid: TSFID;
begin
    // Create a new Snowflake ID with the current timestamp, default machine ID, and a new sequence
    Sfid := TSFID.NewSfid;

    // Get Unique ID as int64 base
    WriteLn('Snowflake ID as Integer: ', Sfid.AsInt);

    // Access components of the Snowflake ID
    WriteLn('Timestamp: ', Sfid.Timestamp);
    WriteLn('Machine ID: ', Sfid.MachineID);
    WriteLn('Sequence: ', Sfid.Sequence);
end;

Author

  • Shadi Ajam

Useful Unit, Right?

YES! We’d love your support! Please give it a 🌟 and share it with others.

Share on social media: