SoFunction
Updated on 2025-03-05

The field of action and implementation method of Go snowflake algorithm

What is the snowflake algorithm

Snowflake algorithm is a distributed system unique ID generation strategy open source on Twitter. Its core idea is to use 41 bits as milliseconds, 5 bits as the ID of the data center, 5 bits as the machine ID, and 12 bits as the serial number within milliseconds. This ensures that up to 4096 IDs can be generated in each millisecond. The entire structure is as follows: 1-bit identification bit + 41 bits of timestamp + 5 bits of data center + 5 bits of machine + 12 bits of serial number. This algorithm can ensure global uniqueness.

Field of action of snowflake algorithm

The snowflake algorithm is mainly used in distributed systems or large concurrent systems to solve the problem of generating global unique identifiers (IDs). These areas include, but are not limited to:

  • Order number generation system
  • Primary key generation of database
  • Key generation in distributed cache
  • Confirmation of uniqueness of data in distributed systems
  • In large Internet or medium-sized software systems, scenarios such as generating global unique IDs.

How to implement the snowflake algorithm of Go

Method 1: Introduce a three-party library

Introduce package

go get -u /bwmarrin/snowflake

Implement code

package main
import (
  "fmt"
  "/bwmarrin/snowflake"
)
func main() {
  node, err := (1)
  if err != nil {
    (err)
    return
  }
  id := ()
  (id)
}

This example creates a snowflake node first and then generates an ID.

Note: The snowflake algorithm used in this library is somewhat different from the original algorithm's allocation of bit width. The Twitter version is 1-bit identifier + 41-bit timestamp + 5-bit data center + 5-bit machine + 12-bit serial number. And the bwmarrin library version is=10,=12, that is, the data center and machine ID account for 10 digits, and the serial number account for 12 digits.

Method 2: Implement source code

package main
import (
  "fmt"
  "sync"
  "time"
)
const (
  epoch     int64 = 1526285084378           // Set the start time (this is usually the project launch time)  timestampBits = uint(41)                  // Time stamp occupies 41 bits  machineBits   = uint(5)                   // The machine position occupies 5  sequenceBits  = uint(12)                  // Serial number occupies 12 digits  machineMax    = int64(-1) ^ (int64(-1) << machineBits)  // Maximum value of machine identification  sequenceMask  = int64(-1) ^ (int64(-1) << sequenceBits) // Maximum serial number  machineShift  = sequenceBits              // The left shift number of machine code  timestampShift = machineBits + sequenceBits // The number of timestamp shifts left)
var (
  machineID  int64
  sequence   int64
  lastTimestamp int64
  lock 
)
func NextID() int64 {
  ()
  defer ()
  timestamp := ().UnixNano() / int64()
  if timestamp < lastTimestamp {
    panic("invalid timestamp")
  }
  if timestamp == lastTimestamp {
    sequence = (sequence + 1) & sequenceMask
    if sequence == 0 {
      timestamp = waitNextMillisecond(lastTimestamp)
    }
  } else {
    sequence = 0
  }
  lastTimestamp = timestamp
  return ((timestamp - epoch) << timestampShift) | (machineID << machineShift) | sequence
}
func waitNextMillisecond(last int64) int64 {
  timestamp := ().UnixNano() / int64()
  for timestamp <= last {
    timestamp = ().UnixNano() / int64()
  }
  return timestamp
}
func main() {
  (NextID())
}

Note that this is just a simplified version of the snowflake algorithm. In production environments, you need to deal with out-of-order and clock callback issues, as well as considering the generation of data center and machine identity. In addition, you need to set the epoch time yourself, and it is generally set as the time when the system is online.

The above is the detailed content of the Go snowflake algorithm's field of function and implementation method examples. For more information about the Go snowflake algorithm, please pay attention to my other related articles!