Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a UUID in DynamoDB?

In my db scheme, I need a autoincrement primary key. How I can realize this feature?

PS For access to DynamoDB, I use dynode, module for Node.js.

like image 719
NiLL Avatar asked Jul 30 '12 12:07

NiLL


People also ask

Can DynamoDB generate unique ID?

Because DynamoDB already guarantees that the pk attribute is unique, you need a mechanism to ensure that the userName and email attributes are also unique. Every time that you insert a new item into the table, you also must insert the other two items. This guarantees uniqueness for the userName and email attributes.

How do I create a unique constraint in DynamoDB?

To define a unique constraint for the username too, just add items with the type “username”. Also, by defining the rarer of the two parts of the key as the partition, you'll end up with more partitions, which is a best practice when working with DynamoDB.

What is UUID generator?

A UUID (Universal Unique Identifier) is a 128-bit value used to uniquely identify an object or entity on the internet. Depending on the specific mechanisms used, a UUID is either guaranteed to be different or is, at least, extremely likely to be different from any other UUID generated until A.D. 3400.

Does GSI need to be unique?

However, the key values in a global secondary index do not need to be unique.


1 Answers

Disclaimer: I am the maintainer of the Dynamodb-mapper project

Intuitive workflow of an auto-increment key:

  1. get the last counter position
  2. add 1
  3. use the new number as the index of the object
  4. save the new counter value
  5. save the object

This is just to explain the underlying idea. Never do it this way because it's not atomic. Under certain workload, you may allocate the same ID to 2+ different objects because it's not atomic. This would result in a data loss.

The solution is to use the atomic ADD operation along with ALL_NEW of UpdateItem:

  1. atomically generate an ID
  2. use the new number as the index of the object
  3. save the object

In the worst case scenario, the application crashes before the object is saved but never risk to allocate the same ID twice.

There is one remaining problem: where to store the last ID value ? We chose:

{     "hash_key"=-1, #0 was judged too risky as it is the default value for integers.     "__max_hash_key__y"=N } 

Of course, to work reliably, all applications inserting data MUST be aware of this system otherwise you might (again) overwrite data.

the last step is to automate the process. For example:

When hash_key is 0:     atomically_allocate_ID() actual_save() 

For implementation details (Python, sorry), see https://bitbucket.org/Ludia/dynamodb-mapper/src/8173d0e8b55d/dynamodb_mapper/model.py#cl-67

To tell you the truth, my company does not use it in production because, most of the time it is better to find another key like, for the user, an ID, for a transaction, a datetime, ...

I wrote some examples in dynamodb-mapper's documentation and it can easily be extrapolate to Node.JS

If you have any question, feel free to ask.

like image 199
yadutaf Avatar answered Sep 23 '22 06:09

yadutaf