A unique ID generator that's a hundredfold faster than a UUID

Source: Internet
Author: User
Tags unique id generator
This is a creation in Article, where the information may have evolved or changed.

Objective

In game development, we often have to set a globally unique ID for a request, an object, or something else. The common choices are random numbers, timestamps, UUID, and Redis generation IDs, which can either be duplicated or too slow, so I developed wuid, a very fast unique ID generator.

The wuid is 100 times times faster than the UUID, 4,600 times times faster than the Redis generation ID.

Core Design

The wuid sequence generates a 64-bit integer whose high 24 bits are loaded from the external storage and automatically plus 1 per load.

Currently supported external storage includes: Redis, MySQL, MongoDB.

Benchmarks

Benchmarkwuid 200000000 9.38 ns/op 0 b/op 0 allocs/opbenchmarkwuid-4 20000000  0 9.19 ns/op 0 b/op 0 allocs/opbenchmarkrand 100000000 21.6 ns/op 0 B/op 0 allocs/opbenchmarkrand-4 100000000 21.9 ns/op 0 b/op 0 Allocs/opbenchm          Arktimestamp 2000000 669 ns/op 0 b/op 0 allocs/opbenchmarktimestamp-4 2000000          664 ns/op 0 b/op 0 allocs/opbenchmarkuuid_v1 2000000 888 ns/op 0 B/OP 0 allocs/opbenchmarkuuid_v1-4 2000000 871 ns/op 0 b/op 0 Allocs/opbenchmarkuui D_V2 2000000 904 ns/op 0 b/op 0 allocs/opbenchmarkuuid_v2-4 2000000 8          Ns/op 0 b/op 0 allocs/opbenchmarkuuid_v4 1000000 1325 ns/op 1 Allocs/opbenchmarkuUid_v4-4 1000000 1287 ns/op b/op 1 Allocs/opbenchmarkredis 30000 4          3970 ns/op 176 b/op 5 allocs/opbenchmarkredis-4 30000 42279 ns/op 176 b/op 5 allocs/op

Characteristics

    • Extremely fast speed
    • Thread Safety
    • Guaranteed to be unique within the same room
    • Guaranteed across time only
    • Low 40 bits are about to be exhausted when the new high 24 bit is automatically acquired

Using the example

import "github.com/edwingeng/wuid/redis"// Setupg := wuid.NewWUID("default", nil)g.LoadH24FromRedis("127.0.0.1:6379", "", "wuid")// Generatefor i := 0; i < 10; i++ {    fmt.Println(g.Next())}

Transmission Door

Https://github.com/edwingeng/wuid

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.