Distributed sequence number generation?

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

    Using a database you can reach 1.000+ increments per second with a single core. It is pretty easy. You can use its own database as backend to generate that number (as it should be its own aggregate, in DDD terms).

    I had what seems a similar problem. I had several partitions and I wanted to get an offset counter for each one. I implemented something like this:

    CREATE DATABASE example;
    USE example;
    CREATE TABLE offsets (partition INTEGER, offset LONG, PRIMARY KEY (partition));
    INSERT offsets VALUES (1,0);
    

    Then executed the following statement:

    SELECT @offset := offset from offsets WHERE partition=1 FOR UPDATE;
    UPDATE offsets set offset=@offset+1 WHERE partition=1;
    

    If your application allows you, you can allocate a block at once (that was my case).

    SELECT @offset := offset from offsets WHERE partition=1 FOR UPDATE;
    UPDATE offsets set offset=@offset+100 WHERE partition=1;
    

    If you need further throughput an cannot allocate offsets in advance you can implement your own service using Flink for real time processing. I was able to get around 100K increments per partition.

    Hope it helps!

    0 讨论(0)
提交回复
热议问题