Coder Social home page Coder Social logo

cruftflake's Introduction

CruftFlake

A stab at a version of Twitter Snowflake but in PHP with a simple ZeroMQ interface (rather than Thrift).

Implementation

This project was motivated by personal curiosity and also my inability to get Twitter's project to build.

The implementation copies Twitter - generating 64 bit IDs.

  • time - 41 bits
  • configured machine ID - 10 bits
  • sequence number - 12 bits

Has a custom epoch that means it can generate IDs until 2081-09-06 (not the same epoch as Snowflake).

ZooKeeper for config coordination (optional)

Each machine must have a unique 10 bit machine ID. ZooKeeper can be used to keep a list of all registered machine IDs, allowing new machines to self-assign a new unique machine ID.

When using ZooKeeper, when a new node starts up for the first time it must be able to contact the ZooKeeper cluster and create a new node. It will look at all the existing nodes and then (if it can't find its own Mac Address) attempt to claim a free one.

I was using Ephemeral nodes for this - similar(ish) to a lock pattern but this had the issue that the node needed to remain connected to ZK throughout its lifetime -- this way it doesn't.

The downside is that potentially the 1024 possible machine IDs will "fill up" and need to be manually pruned.

Running

Git clone and then remember to git submodule init. You should run the tests to verify things are OK:

phpunit --bootstrap test/bootstrap.php test/

There are two scripts provided for playing about with.

  1. The generator (the server)

    ./scripts/cruftflake.php

Or to specify a manually configured machine ID:

./scripts/cruftflake.php -m 1
  1. A client that will generate N IDs and dump to STDOUT

    ./scripts/client.php -n 100

cruftflake's People

Watchers

James Cloos avatar Fabrizio Manunta avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.