• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.gitignoreH A D29-Oct-201726

.travis.ymlH A D29-Oct-2017210

LICENSEH A D29-Oct-20171 KiB

README.mdH A D29-Oct-20174.5 KiB

shortid.goH A D29-Oct-201711.2 KiB

shortid_integration_test.goH A D29-Oct-20173.5 KiB

shortid_test.goH A D29-Oct-20179.6 KiB

README.md

1[![Build status][buildimage]][build] [![Coverage][codecovimage]][codecov] [![GoReportCard][cardimage]][card] [![API documentation][docsimage]][docs]
2
3# Generator of unique non-sequential short Ids
4
5The package `shortid`enables the generation of short, fully unique,
6non-sequential and by default URL friendly Ids at a rate of hundreds of thousand per second. It
7guarantees uniqueness during the time period until 2050!
8
9The package is heavily inspired by the node.js [shortid][nodeshortid] library (see more detail below).
10
11The easiest way to start generating Ids is:
12
13	fmt.Printf(shortid.Generate())
14	fmt.Printf(shortid.Generate())
15
16The recommended one is to initialise and reuse a generator specific to a given worker:
17
18	sid, err := shortid.New(1, shortid.DefaultABC, 2342)
19
20	// then either:
21	fmt.Printf(sid.Generate())
22	fmt.Printf(sid.Generate())
23
24	// or:
25	shortid.SetDefault(sid)
26	// followed by:
27	fmt.Printf(shortid.Generate())
28	fmt.Printf(shortid.Generate())
29
30
31### Id Length
32
33The standard Id length is 9 symbols when generated at a rate of 1 Id per millisecond,
34occasionally it reaches 11 (at the rate of a few thousand Ids per millisecond) and very-very
35rarely it can go beyond that during continuous generation at full throttle on high-performant
36hardware. A test generating 500k Ids at full throttle on conventional hardware generated the
37following Ids at the head and the tail (length > 9 is expected for this test):
38
39	-NDveu-9Q
40	iNove6iQ9J
41	NVDve6-9Q
42	VVDvc6i99J
43	NVovc6-QQy
44	VVoveui9QC
45	...
46	tFmGc6iQQs
47	KpTvcui99k
48	KFTGcuiQ9p
49	KFmGeu-Q9O
50	tFTvcu-QQt
51	tpTveu-99u
52
53### Life span
54
55The package guarantees the generation of unique Ids with no collisions for 34 years
56(1/1/2016-1/1/2050) using the same worker Id within a single (although can be concurrent)
57application provided application restarts take longer than 1 millisecond. The package supports
58up to 32 workers all providing unique sequences from each other.
59
60### Implementation details
61
62Although heavily inspired by the node.js [shortid][nodeshortid] library this is
63not just a Go port. This implementation
64
65* is safe to concurrency (test included);
66* does not require any yearly version/epoch resets (test included);
67* provides stable Id size over a the whole range of operation at the rate of 1ms (test included);
68* guarantees no collisions: due to guaranteed fixed size of Ids between milliseconds and because
69multiple requests within the same ms lead to longer Ids with the prefix unique to the ms (tests
70included);
71* supports 32 instead of 16 workers (test included)
72
73The algorithm uses less randomness than the original node.js implementation, which permits to extend
74the life span as well as reduce and guarantee the length. In general terms, each Id has the
75following 3 pieces of information encoded: the millisecond since epoch (first 8 symbols, epoch:
761/1/2016), the worker Id (9th symbol), the running concurrent counter within the millisecond (only
77if required, spanning over all remaining symbols).
78
79The element of randomness per symbol is 1/2 for the worker and the millisecond data and 0 for the
80counter. The original algorithm of the node.js library uses 1/4 throughout. Here 0 means no
81randomness, i.e. every value is encoded using a 64-base alphabet directly; 1/2 means one of two
82matching symbols of the supplied alphabet is used randomly, 1/4 one of four matching symbols. All
83methods accepting the parameters that govern the randomness are exported and can be used to directly
84implement an algorithm with e.g. more randomness, but with longer Ids and shorter life spans.
85
86### License and copyright
87
88	Copyright (c) 2016. Oleg Sklyar and teris.io. MIT license applies. All rights reserved.
89
90**[Original algorithm][nodeshortid]:** Copyright (c) 2015 Dylan Greene, contributors. The same MIT
91license applies. Many thanks to Dylan for putting together the original node.js library, which
92inspired this "port":
93
94**Seed computation:** based on The Central Randomizer 1.3. Copyright (c) 1997 Paul Houle (houle@msc.cornell.edu)
95
96[go]: https://golang.org
97[nodeshortid]: https://github.com/dylang/shortid
98
99[build]: https://travis-ci.org/teris-io/shortid
100[buildimage]: https://travis-ci.org/teris-io/shortid.svg?branch=master
101
102[codecov]: https://codecov.io/github/teris-io/shortid?branch=master
103[codecovimage]: https://codecov.io/github/teris-io/shortid/coverage.svg?branch=master
104
105[card]: http://goreportcard.com/report/teris-io/shortid
106[cardimage]: https://goreportcard.com/badge/github.com/teris-io/shortid
107
108[docs]: https://godoc.org/github.com/teris-io/shortid
109[docsimage]: http://img.shields.io/badge/godoc-reference-blue.svg?style=flat
110