Distributed sequence number generation?

后端 未结 13 1967
小鲜肉
小鲜肉 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:09

    Why not use a (thread safe) UUID generator?

    I should probably expand on this.

    UUIDs are guaranteed to be globally unique (if you avoid the ones based on random numbers, where the uniqueness is just highly probable).

    Your "distributed" requirement is met, regardless of how many UUID generators you use, by the global uniqueness of each UUID.

    Your "thread safe" requirement can be met by choosing "thread safe" UUID generators.

    Your "sequence number" requirement is assumed to be met by the guaranteed global uniqueness of each UUID.

    Note that many database sequence number implementations (e.g. Oracle) do not guarantee either monotonically increasing, or (even) increasing sequence numbers (on a per "connection" basis). This is because a consecutive batch of sequence numbers gets allocated in "cached" blocks on a per connection basis. This guarantees global uniqueness and maintains adequate speed. But the sequence numbers actually allocated (over time) can be jumbled when there are being allocated by multiple connections!

提交回复
热议问题