Skip to content

coder-clan/edelweiss-parent

Repository files navigation

Edelweiss

The Edelweiss is a Snowflake ID generator. It has following 2 features:

  • Automatic assign instance id of Snowflake
  • Pause to generate ID if the system clock has been changed backward.

ChangeLog

  • 1.0.1 support spring boot 3.x

Design

The Edelweiss introduce an interface org.coderclan.edelweiss.InstanceIdAssigner. It calls InstanceIdAssigner.assignAnInstanceId(String key, long expiringTime) to get a Machine ID when system startup. The Instance ID will expire at expiringTime to avoid exhaust all IDs. The Edelweiss will renew the Instance ID before expiring periodically by calling InstanceIdAssigner.renewInstanceId(int instanceId, String key, long expiringTime).

The org.coderclan.edelweiss.JbdcInstanceIdAssigner is an implementation of InstanceIdAssigner. It uses Relational Database to store the information of Machine ID assignation. More implementation for MongoDB, Zookeeper, etc. could be added in the future.

How to Use

The module edelweiss-demo demonstrate how to use the Edelweiss.

  • Add maven dependency.
        <dependency>
            <groupId>org.coderclan</groupId>
            <artifactId>spring-boot-starter-edelweiss-jdbc</artifactId>
        </dependency>
  • Inject IdGenerator and use it to generate ID, just as org.coderclan.edelweiss.demo.Demo does.
  • configure the expiringTime of machine ID, coderclan.edelweiss.machineIdTtl=600, unit: second.

Limitation

The Snowflake ID algorithm is highly depending on System Clock. The System Clock should be accurate enough. Using a time-server to synchronize the System Clock is strong recommended.

Different System Clock between nodes may cause two or more nodes use the same Machine ID. we could prevent this by increasing the System Clock accuracy or Increasing the life of machine ID(increasing the value of coderclan.edelweiss.machineIdTtl). The max difference of System Clocks allowed by The Edelweiss is ( coderclan.edelweiss.machineIdTtl * Constants.DEFAULT_INSTANCE_ID_TTL_MARGIN_RATE + 1), unit: second. the default configuration of the Edelweiss will allow 2 minutes of System Clock difference.