Search code examples
databasesharding

How to create unique row ID in sharded databases?


In a non-sharded DB, I could just use auto-increment to generate a unique ID to reference a specific row.

I want to shard my DB, say into 12 shards. Now when I insert into a specific shard, the auto-increment ID is no longer unique.

Would like to hear anyone's experience in dealing with this problem.


Solution

  • A few approaches

    1) Give each shard it's own ID, and use a composite key

    2) Give each shard it's own ID and set ID ranges for each shard

    3) Use a globally unique ID - GUID