NoSQL : Getting latest item from DynamoDB table I'm implementing chat server currently, with dynamoDB.

Here is keys from ChatData table


Primary partition key : roomNo
Primary sort key : seq


Chat server, can created many instant rooms, approximated to 100~120 daily.
So I choosed primary partition key as room Number(roomNo), and sort by data created time(or sequential value) on seq.

I want to increase seq automatically, however, I know that DynamoDB hasn't support such as auto_increment or identity. So, I'm going to try using Timestamp or AtomicLong from my client application(JAVA 1.8).

But, each has problems.
first, Timestamp, may inserted duplicated value in table.

and AtomicLong, can increased atomic value but when I restart my application, it starts from 1 again.

Maybe if I can get maximum seq value from DynamoDB, what should I use for?
or how to change keys on table?
I think you're on the right track with AtomicLong, but you'd have to run a scan operation each time your app starts to determine which value to start at. You'd want to test this to ensure it doesn't degrade performance too much.

Another option is a metadata item. You could create an additional table for this, or you could add an item to your existing table using a hash attribute value that will never otherwise exist (so it won't clash with your application, and can be easily retrieved using its well-known value). Store name/value pairs that your app needs to know about (e.g. lastSeq) and use conditional writes to change this as necessary (e.g. increment lastSeq but only if the current value is 234).

If there are multiple instances of the client application, you'll want to consider contention and how this might affect things; using conditional writes should alleviate this (but you'll need to query and update the metadata item every time an item is created).

You can read more about conditional writes in the AWS DynamoDB Developer's Guide.