Giter Club home page Giter Club logo

meteor's Introduction

meteor

An implementation of distributed ID.

中文

Based on Snowflake,but resolve time back, and have higher performance(10 times faster), I call it Meteor.

Here is the benchmark Meteor compare to Snowflake

Machine:Lenovo xiaoxin pro13 Ryzen 5 3550H

go version:1.15

goos: windows
goarch: amd64
BenchmarkSnowflake-8
4917643	       244 ns/op	       0 B/op	       0 allocs/op
BenchmarkMeteor-8
52173231	   22.6 ns/op	       0 B/op	       0 allocs/op

How to use

go get -u github.com/TheFutureIsOurs/meteor

import "github.com/TheFutureIsOurs/meteor"

node, _ := meteor.NewNode(0)
id, _ := node.Generate()

I'd strongly recommend that keep nodeID uniq when you NewNode, include reload you service or add new machine. You can use mysql's increase id to keep you NodeID unique.

Meteor

int64

Explain:

0:the first bit do nothing(positive number)

Data section is the time difference when the current NodeID is created. So Meteor just rely on the system time when one NodeID is created. This can resolve time back.

NodeID section,for the result ID unique, we should keep the NodeID unique. For example, if we add new machine or one machine is reload, we should increase the NodeID, we can use mysql do this.

Serial section is 11 bits, increment when the generator is called. the Data section should +1 when the serial reach 2048, then the serial began from zero.

Rand section is 3 bits. Why we need this? Just because we want discrete IDs. For example, if the result id is 1,2,3,4 etc,which is easy to get next. Why Snowflake do not have rand num? Because Snowflake is based on millisecond, the qps should reach 4096000, the result ID can continuous.

Because we need rand section,what random algorithm we should choose? As we know, most random algorithm is based on the system time as seed. If we use this, we can't reach our goal:high performace than Snowflake and can't remove system time when generator. We need high performance random algorithm. I choose the Xorshift algorithm

Let's generator 10 IDs:

5016762319896585
5016762319896596
5016762319896600
5016762319896614
5016762319896616
5016762319896626
5016762319896635
5016762319896640
5016762319896651
5016762319896656

We got it.

meteor's People

Contributors

thefutureisours avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

peicheng

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.