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:
+------+----------------------------+ | id | Attributes | +------+----------------------------+ | 205 | author="jeff", text="..." | | 206 | author="bob", text="..." | | 207 | author="alice", text="..." | +------+----------------------------+
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.
<dependency> <groupId>co.stateful</groupId> <artifactId>java-sdk</artifactId> <version>0.6</version> </dependency>
And, you can use stateful.co counters from Java code:
Sttc sttc = new RtSttc( new URN("urn:github:526301"), "9FF3-41E0-73FB-F900" ); Counters counters = sttc.counters(); Counter counter = counters.get("foo"); long value = counter.incrementAndGet(1L); System.out.println("new value: " + value);
You can review authentication parameters for
RtSttc constructor at stateful.co.
The service is absolutely free of charge.