Amazon DynamoDB Conditional Writes and Atomic Counters
Asked Answered
M

2

11

The application im working on currently requires me to increment an attribute belonging to an item in DynamoDB many times in a 20 to 30 minute period. I've been doing some additional reading about DynamoDBs conditional writes and atomic counters

Atomic Counters in dynamo seems like a logical choice for what I need but I do worry about the consistency of the data especially across a distributed database like dynamo and issues accuracy of my data. I'm expecting the API to get hammered at peak times but I want to avoid the performance issues related to conditional updates. I guess I want to know how reliable the Atomic Counters are with DynamoDB and how to implement them correctly using dynamo. Other suggestions are also welcome.

Moleskin answered 24/3, 2014 at 16:50 Comment(0)
F
9

Yes, these are the features you would want to use. Using them via Dynamo API is the way.

Now, between these two features, use conditional write if the counter you need to modify if very critical to business (you tell the API to update the value to say x + 10 "IF and only IF" the existing value is x)

The same document which you referred explains Atomic Counter:

"You might retry this operation if you suspect that a previous request was unsuccessful. However, you would risk applying the same update twice. This might be acceptable for a web site counter, because you can tolerate with slightly over- or under-counting the visitors. However, in a banking application, it would be safer to use a conditional update."

So if it is business critical operation use Conditional write, otherwise atomic counter. Hope it clarifies.

Flagship answered 24/3, 2014 at 17:6 Comment(2)
I was also thinking about using caching of some kind. Write values to a cache then flush the cache every minute of two minutes.Moleskin
Cache will work ok again only if the counter is not business-critical, since the caches might go down and may not be very reliable etc. That way cache helps to reduce the latency and save on dynamo-throughputs.Flagship
F
0

This code is an atomic counter written in AWS V3 SDK. And you can get updated Counts in the same time. It also has the ability to give a default value, if the item doesn't exist in the table.

import {
    DynamoDBClient,
    UpdateItemCommand,
} from "@aws-sdk/client-dynamodb";
const client = new DynamoDBClient({});

export const handler = async (event, context) => {
    let statusCode = 200;
    let body={};
    const headers = {
        "Content-Type": "application/json",
    };
    let eventBody = JSON.parse(event.body);
    try{
        const input = {
            "TableName": "yourTableName",
            "Key": {
                "primaryKey": {
                    "S": eventBody.primaryKey,
                },
            },
            "UpdateExpression": 'set Counts = if_not_exists(Counts, :start) + :num',
            "ExpressionAttributeValues": {
                ':start': {
                    "N": "0",
                },
                ':num': {
                    "N": "1",
                },
            },
            "ReturnValues": 'ALL_NEW',
        };
        const command = new UpdateItemCommand(input);
        const response = await client.send(command);
        body=response.Attributes;//you can get the updated Counts
    }
    catch(error){
        statusCode = 500;
        body.error = error;
        body.message='Internal server error';
    }

    return {
        statusCode,
        body: JSON.stringify(body),
        headers,
    };
};

Fungus answered 21/5 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.