views:

366

answers:

3

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.

+5  A: 

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

MrTelly
Use the GUID and don't worry about the ranges and composite-key. You'll inevitably at some point add another shard or need to reorganize your shards and your numbering scheme will need to be refactored.
Jeff Fritz
@Jeff: GUIDs do have a serious downside, they are large. Depending on the reason for divying up the DB it could be a significant factor. Using ID ranges allows for a small (32 bit) single field PK with no collisions across the various DBs. So I would agree that if volume isn't an issue use GUIDs but they're not always appropriate. It helps if one doesn't link the identity of the creating DB with the ID. That way DBs can "lease" ID ranges which eliminates issues in "refactoring".
AnthonyWJones
Personally I loath GUIDs as keys, as you can't yell - look at record 123456, GUIDs are anti-human
MrTelly
+1  A: 

1) You can two rows (one indicates the ID and the second the database id)

2) Use Guids

Yassir
+1  A: 

The two approaches I've used to this sort of problem:

  • GUID: Easy to implement, creates larger tables and indexes though.
  • ID Domain: I made that term up but basically it means dividing the 32 (or 64) bits of an integer type into two parts, the top part is represents a domain. The number of bits to use for the domain depends on how many domains you want to support verses the number of records you expect a single domain to introduce. In this approach you allocate a domain to each shard. The down side is DBs (that I know of) do not support this approach directly you need to code ID allocation yourself.
AnthonyWJones