Distributed sequence number generation?

后端 未结 13 1957
小鲜肉
小鲜肉 2020-11-29 14:32

I\'ve generally implemented sequence number generation using database sequences in the past.

e.g. Using Postgres SERIAL type http://www.neilconway.o

13条回答
  •  清歌不尽
    2020-11-29 15:05

    There are a few strategies; but none that i know can be really distributed and give a real sequence.

    1. have a central number generator. it doesn't have to be a big database. memcached has a fast atomic counter, in the vast majority of cases it's fast enough for your entire cluster.
    2. separate an integer range for each node (like Steven Schlanskter's answer)
    3. use random numbers or UUIDs
    4. use some piece of data, together with the node's ID, and hash it all (or hmac it)

    personally, i'd lean to UUIDs, or memcached if i want to have a mostly-contiguous space.

提交回复
热议问题