Amazon DynamoDB is a great NoSQL cloud database. It is cheap, highly reliable and rather powerful. I'm using it in many web systems.
There is one feature that it lacks, though—auto-increment attributes.
Say that you have a table with a list of messages:
Every time you add a new item to the table, a new value of
id has to be set. And this has to be done with concurrency in mind. SQL databases like PostgreSQL, Oracle, MySQL and others support auto-increment features. When you add a new record to the table, the value of the primary key is omitted and the server retrieves the next one automatically. If a number of
INSERT requests arrive at the same time the server guarantees that the numbers won't be duplicated.
In case of an atomic counter, you should create a supplementary table and keep the latest value of
id in it.
In case of conditional updates, you should retry a few times in case of collisions.
To make life easier in a few of my applications, I created a simple web service—stateful.co. It provides a simple atomic counter feature through its RESTful API.
First, you create a counter with a unique name. Then, you set its initial value (it is zero by default). And, that's it. Every time you need to obtain a new value for
id column in DynamoDB table, you make an HTTP request to stateful.co asking to increment your counter by one and return its next value.
stateful.co guarantees that values returned will never duplicate each other—no matter how many clients are using a counter or how fast they request increments simultaneously.
And, you can use stateful.co counters from Java code:
You can review authentication parameters for
RtSttc constructor at stateful.co.
The service is absolutely free of charge.