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.

  • How to return the inserted item in dynamoDB
  • Strategy for sharing and reusing of schema parts in mongoose.js
  • How to get node.js to connect to mongolab using mongoose
  • Mongo/Mongoose Invalid atomic update value error
  • Multilanguage interface and content with mongo / node
  • How to insert json in dynamodb
  • Sequelize findOrCreate giving me a SequelizeUniqueConstraintError: Validation error
  • what's the equivalent of jpa or jdbc for node?
  • How can I use node-orm2 to model two or more databases with Express?
  • Multilanguage interface and content with mongo / node
  • In Mongoose (mongodb node.js), how to update existing values and insert missing, without removing unspecified values?
  • Recursive Fetch All Items In DynamoDB Query using Node JS
  • 7 Solutions collect form web for “How to make a UUID in DynamoDB?”

    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.

    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:

    For implementation details (Python, sorry), see

    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.

    If you’re okay with gaps in your incrementing id, and you’re okay with it only roughly corresponding to the order in which the rows were added, you can roll your own: Create a separate table called NextIdTable, with one primary key (numeric), call it Counter.

    Each time you want to generate a new id, you would do the following:

    • Do a GetItem on NextIdTable to read the current value of Counter –> curValue
    • Do a PutItem on NextIdTable to set the value of Counter to curValue + 1. Make this a conditional PutItem so that it will fail if the value of Counter has changed.
    • If that conditional PutItem failed, it means someone else was doing this at the same time as you were. Start over.
    • If it succeeded, then curValue is your new unique ID.

    Of course, if your process crashes before actually applying that ID anywhere, you’ll “leak” it and have a gap in your sequence of IDs. And if you’re doing this concurrently with some other process, one of you will get value 39 and one of you will get value 40, and there are no guarantees about which order they will actually be applied in your data table; the guy who got 40 might write it before the guy who got 39. But it does give you a rough ordering.

    Parameters for a conditional PutItem in node.js are detailed here.!AWS/DynamoDB.html. If you had previously read a value of 38 from Counter, your conditional PutItem request might look like this.

    var conditionalPutParams = {
        TableName: 'NextIdTable',
        Item: {
            Counter: {
                N: '39'
        Expected: {
            Counter: {
                AttributeValueList: [
                        N: '38'
                ComparisonOperator: 'EQ'

    I don’t believe it is possible to to a SQL style auto-increment because the tables are partitioned across multiple machines. I generate my own UUID in PHP which does the job, I’m sure you could come up with something similar like this in javascript.

    I’ve had the same problem and created a small web service just for this purpose. See this blog post, that explains how I’m using with DynamoDB in order to simulate auto-increment functionality:

    Basically, you register an atomic counter at and increment it every time you need a new value, through RESTful API. The service is free.

    Create the new file.js and put this code:

    exports.guid = function () {
        function _p8(s) {
            var p = (Math.random().toString(16)+"000000000").substr(2,8);
            return s ? "-" + p.substr(0,4) + "-" + p.substr(4,4) : p ;
        return (_p8() + _p8(true) + _p8(true)+new Date().toISOString().slice(0,10)).replace(/-/g,"");

    Then you can apply this function to the primary key id. It will generate the UUID.

    Addition to @yadutaf’s answer

    AWS supports Atomic Counters.

    Creating a separate table (order_id) with one row holding last id(order_number):

    | id | order_number |
    |  0 |         5000 |

    will allow to increment by 1 order_number and get the incremented result in a callback:

      region: 'us-east-1',
      endpoint: "http://localhost:8000"
    const docClient = new AWS.DynamoDB.DocumentClient(config); 
    let param = {
                TableName: 'order_id',
                Key: {
                    "id": 0
                UpdateExpression: "set order_number = order_number + :val",
                    ":val": 1
                ReturnValues: "UPDATED_NEW"
    docClient.update(params, function(err, data) {
       if (err) {
                    console.log("Unable to update the table. Error JSON:", JSON.stringify(err, null, 2));
       } else {
                    console.log(data.Attributes.order_number); // <= here is our incremented result

    Be aware that in some rare cases their might be problems with the connection between your caller point and AWS API. This will result in the dynamodb row increment, while you will get a connection error and will not be aware of a new value.
    So there might appear small amount of unused incremented values.

    Then you can use incremented data.Attributes.order_number in your table, e.g. to insert {id: data.Attributes.order_number, otherfields:{}} into order table.

    Another approach is to use a UUID generator for primary keys, as these are highly unlikely to clash.

    IMO you are more likely to experience errors consolidating primary key counters across highly available DynamoDB tables than from clashes in generated UUIDs.

    For example, in Node:

    npm install uuid

    var uuid = require('uuid');
    // Generate a v1 (time-based) id
    uuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'
    // Generate a v4 (random) id
    uuid.v4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'

    Taken from SO answer.

    Node.js is the Best Javascript runtime in the world.