Git Product home page Git Product logo

longid's Introduction

LongId - Smart and simple Java UUID generator

Replacement for auto-incrementing ID, especially in multi-server multi-datacenter environment.

Created in response to complexity of Snowflake and Snowizard, it's only a single class.

Advantages:

  • SQL inserts will always be at bottom of table when used as primary key

    • ID always larger than previous
    • algorithm: currentTimeMillis & intraMilliCounter & serverId
  • Thread-safe

  • Single class

  • 8byte Java 'long' result, perfect for primary key

  • Single or Multiple Servers

    • Does not require server coordination

    • Instantiate with serverId (0-4095) for 100% assurance of uniqueness

  • 256,000 unique IDs per second per server

  • Good for years 1970 to 2557

  • longId contains timestamp -> getDate(longId)

  • longId contains serverId -> getServerId(longId)

Installation

  • copy the source into your code
  • or
  • compile 'com.communicate:longid:1.1'

Use Example: Single Server

  LongId longId = new LongId()
  longId.getNewId();

Use Example: Multi Server

  int serverId = 4095;
  LongId longId = new LongId(serverId)
  longId.getNewId();

Use Example: Grails GORM ID Generator

You can use LongId with GORM to create a new LongId for each record as it's created. To do so you'll need to create a Groovy IdentifierGenerator and use it in the object mapping. You can store the static LongId in a service or another object if you prefer.

class LongIdGenerator implements IdentifierGenerator {
    static private LongId longId = new LongId()
    Serializable generate(SessionImplementor session, Object object) {
        return longId.getNewId()
    }
}

And then in your mapping for the Grails class:

    static mapping = {
        id generator: 'com.LongIdGenerator'
    }

Disclosures:

  • Not technically a UUID generator as it's not Universal, that would require 16 instead of 8 bytes

    • Uniqueness is guaranteed within a single server
    • Uniqueness is guaranteed within multi-server network when distinct serverId is used
  • 256,000 ID's per second is theoretical, really it's a max of 256 per millisecond, when exceeded will sleep for 1ms

    • during multi-threaded testing on quad-core machine, we rarely exceeded 256 per millisecond
  • Multi-server / Multi-datacenter may result in SQL inserts not exactly at the bottom of the table, depending on delay of insertion and quantity of servers. But it will be in the bottom pages, and almost guaranteed insert into pages that are in memory.

  • If you need more than 4096 servers, you must adjust the code to decrease timeEpoch to stay within 8byte/long

longid's People

Contributors

ktvtff avatar

Watchers

James Cloos 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.