Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use auto increment for primary key id in dynamodb

I am new to dynamodb. I want to auto increment id value when I use putitem with dynamodb.

Is possible to do that?

like image 618
pranay Avatar asked May 06 '16 12:05

pranay


People also ask

Does DynamoDB generate ID?

DynamoDB does not support auto-incrementing IDs for items. They can be a performance bottleneck at scale, and most workloads are better off with UUIDs anyway. However, there are use cases where auto-incrementing integers are required.

Can we update primary key in DynamoDB?

You cannot update the primary key attributes using UpdateItem. Instead, delete the item and use PutItem to create a new item with new attributes. The UpdateItem operation includes an Action parameter, which defines how to perform the update. You can put, delete, or add attribute values.

Can DynamoDB have 2 primary keys?

DynamoDB supports two types of primary keys: Partition key: A simple primary key, composed of one attribute known as the partition key.

Does DynamoDB primary key need to be unique?

Primary key. When you create a table, in addition to the table name, you must specify the primary key of the table. The primary key uniquely identifies each item in the table, so that no two items can have the same key.


1 Answers

This is anti-pattern in DynamoDB which is build to scale across many partitions/shards/servers. DynamoDB does not support auto-increment primary keys due to scaling limitations and cannot be guaranteed across multiple servers.

Better option is to assemble primary key from multiple indices. Primary key can be up to 2048 bytes. There are few options:

  1. Use UUID as your key - possibly time based UUID which makes it unique, evenly distributed and carries time value
  2. Use randomly generated number or timestamp + random (possibly bit-shifting) like: ts << 12 + random_number
  3. Use another service or DynamoDB itself to generate incremental unique id (requires extra call)

Following code will auto-increment counter in DynamoDB and then you can use it as primary key.

var documentClient = new AWS.DynamoDB.DocumentClient(); var params = {   TableName: 'sampletable',   Key: { HashKey : 'counters' },   UpdateExpression: 'ADD #a :x',   ExpressionAttributeNames: {'#a' : "counter_field"},   ExpressionAttributeValues: {':x' : 1},   ReturnValues: "UPDATED_NEW" // ensures you get value back }; documentClient.update(params, function(err, data) {}); // once you get new value, use it as your primary key 

My personal favorite is using timestamp + random inspired by Instagram's Sharding ID generation at http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram

Following function will generate id for a specific shard (provided as parameter). This way you can have unique key, which is assembled from timestamp, shard no. and some randomness (0-512).

var CUSTOMEPOCH = 1300000000000; // artificial epoch function generateRowId(shardId /* range 0-64 for shard/slot */) {   var ts = new Date().getTime() - CUSTOMEPOCH; // limit to recent   var randid = Math.floor(Math.random() * 512);   ts = (ts * 64);   // bit-shift << 6   ts = ts + shardId;   return (ts * 512) + randid; } var newPrimaryHashKey = "obj_name:" + generateRowId(4); // output is: "obj_name:8055517407349240" 
like image 86
vladaman Avatar answered Sep 19 '22 17:09

vladaman