Code Monkey home page Code Monkey logo

idgen's Introduction

GenId

Global ID generator base on MySQL

Inspired the projects flike/idgo

genId is a sequential id generator which can generate batch ids through MySQL transcation way. Its features as follows:

  • The ID generated by genId is by order.
  • generate batch ids through MySQL transcation way, dose not increase the load of MySQL.
  • When process crashed, admin can restart server, does not worry about generating repetitive ids.
  • GenId talks with clients using redis protocol. developer can connect the server using redis sdk.
  • The server also supports Http protocol to provide services

Someone knows the resolution of generating id with MySQL:

REPLACE INTO Tickets64 (stub) VALUES ('a');
SELECT LAST_INSERT_ID();

The disadvantage of this resolution is that generates one id need query MySQL once. When generating id too quickly, leading MySQL overload. This is why I build this project to generating ids.

2. The architecture

GenId talks with clients using redis protocol, developer can connect to genid using redis sdk. Every Key will mapped to a table storing in MySQL, and the table only has one row data to record the id.

GenId only supports four commands of redis as follows:

  • SET key value, set the initial value of id generator.
  • GET key, get the value of key.
  • EXISTS key, check the key if exist.
  • DEL key, delete the key from server.
  • SELECT index, just a mock select command, prevent the select command error.

3. Install

Install following these steps:

  1. Install Go environment, the version of Go is greater than 1.12.
  2. git clone https://github.com/inherelab/genid
  3. cd genid
  4. go mod tidy
  5. set the config file.
  6. run genid: ./bin/genid -config=config/config.toml

Set the config file(config/config.toml):

# the address of genid server
addr="127.0.0.1:6389"
# log_path: /Users/inhere/go/dev 
log_level="debug"

[db]
mysql_host="127.0.0.1"
mysql_port=3306
db_name="idgen_test"
user="root"
password=""
max_idle_conns=64

Quick start

Examples:

# start server
➜  genid git:(master) ✗ ./bin/genid redis -config=config/config.toml

#start a redis client to connecting genid server, and set/get the key.
➜  ~  redis-cli -p 6389
redis 127.0.0.1:6389> get abc
(integer) 0
redis 127.0.0.1:6389> set abc 100
OK
redis 127.0.0.1:6389> get abc
(integer) 101
redis 127.0.0.1:6389> get abc
(integer) 102
redis 127.0.0.1:6389> get abc
(integer) 103

4. HA

When the server crashed, you can restart genid and reset the key by increasing a fixed offset.

5. License

MIT

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.