Introducing Atomic Counters and Batch Sets for Serverless Data

Atomically increment numeric values and set up to 25 data items at a time!

Jeremy Daly
Serverless Cloud
3 min readMar 8, 2022

--

Serverless Data is a super fast, automatically scalable datastore that’s built directly into your Serverless Cloud apps. Today, we’re happy to announce the addition of atomic counters and batch set operations, giving users even more capabilities to rapidly build modern applications in the cloud.

Atomic Counters

Atomic counters allow numeric items or numeric item object values to be atomically incremented. Atomic updates ensure that addition and subtraction operations are processed atomically, giving users the ability to maintain the integrity of counters even if there are multiple simultaneous requests. Atomic counters are great for safely updating aggregations and other running totals.

Updating a single value atomically

Serverless Data now provides an add method that lets you atomically increment numeric values. This method works on both keys with a simple numeric value, or objects with nested attributes.

For simple values, you pass the full key name as the first parameter and the numeric value you want to add to the existing value as the second parameter. Numbers can be positive or negative, and atomic counters support both integers and float values.

For nested objects, pass the key as the first parameter, the nested attribute name as the second parameter, and the numeric value as the third parameter.

The “add” method will return the updated object by default. You can specify an optional final parameter of true to return the item’s metadata, or pass in an options object like {meta: true}.

Updating multiple values atomically

Updating a single value is useful, but what if you want to update several atomic counters at the same time? You can achieve this using the standard set method along with the new special $add keyword. You set an item like you normally would, but for any numeric value that you’d like to atomically increment, you specify a value of {$add: [value]} , where [value] is whatever numeric value you wish to add. For example:

In the example above, “nestedCounter” will be atomically incremented by 1 on every call and “anotherCounter” will be atomically incremented by 5. Please note that regular values like “someOtherValue” above will not be updated atomically and the last write wins.

Setting multiple items

Until now, you could only set one item at a time with Serverless Data. Today, we are introducing batch sets that let you set up to 25 items in one request. Using the same simple set method, you can now specify an array of objects that each contain a key and value as well as any additional metadata (e.g. labels and a ttl value) as the first argument. The second parameter must be an options object with the overwrite flag set to true. This is for future compatibility to support batch updates. You can also add a meta: true flag to return the metadata of your items.

At this time, batch set operations must have the {overwrite: true} flag set. We are working to add support for batch updates in a future release.

What will you build?

Atomic counters and batch set operations were some of our most requested features. They open up a number of powerful use cases (like aggregations in your data.on() handlers). We’re excited to see what you’ll build with these new capabilities.

Be sure to check out the full documentation for more information, join our community slack channel, and sign up for your free Serverless Cloud account to get started now!‍

Originally published at https://www.serverless.com.

--

--

Jeremy Daly
Serverless Cloud

CEO/Founder @ getAmpt.com, AWS Serverless Hero, host of the ServerlessChats.com podcast. I build web & open source stuff, blog, speak, and publish OffByNone.io.