# How to handle concurrent increments for the same key?

**URL:** <https://discuss.aerospike.com/t/how-to-handle-concurrent-increments-for-the-same-key/1549>\
**Category:** Java Client\
**Created:** [July 29, 2015, 7:13am UTC](https://discuss.aerospike.com/t/how-to-handle-concurrent-increments-for-the-same-key/1549 "2015-07-29T07:13:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Assen\_Todorov](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Assen\_Todorov](https://discuss.aerospike.com/u/Assen_Todorov)\
**Post date:** [July 29, 2015, 7:13am UTC](https://discuss.aerospike.com/t/how-to-handle-concurrent-increments-for-the-same-key/1549/1 "2015-07-29T07:13:22Z")

</div>

Hi,

I would like to use **add** -operation in my application, so that concurrent increments for the same key are possible (my application runs on several servers). I already read about the **generation** -property of **WritePolicy** , but cannot figure out how to use it in the context of this operation. How do I have to implement this?

Cheers,

Assen

---

<div class="post-metadata">

**Author:** ![ManuelSchmidt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/manuelschmidt/32/424_2.png) [@ManuelSchmidt](https://discuss.aerospike.com/u/ManuelSchmidt)\
**Post date:** [July 30, 2015, 6:10pm UTC](https://discuss.aerospike.com/t/how-to-handle-concurrent-increments-for-the-same-key/1549/2 "2015-07-30T18:10:35Z")

</div>

One should avoid using something like incremented id’s, if possible. Can’t you use something like an user’s email as an unique part of your primary key? That’s how it’s typically solved. Synchronization of counters across multiple machine’s is a complex and not really ‘real time’ thing.

Cheers, Manuel

---

<div class="post-metadata">

**Author:** ![helipilot50](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/helipilot50/32/59_2.png) [@helipilot50](https://discuss.aerospike.com/u/helipilot50)\
**Post date:** [July 30, 2015, 11:06pm UTC](https://discuss.aerospike.com/t/how-to-handle-concurrent-increments-for-the-same-key/1549/3 "2015-07-30T23:06:44Z")

</div>

Hi Assen,

Every key-value operation is atomic. The record, relating to the key, is locked at the start of the operation and unlocked at the end of the operation. This means that the `add()` operation is atomic.

I suspect that you want to increment the counter and get the new value. If so, this can be done in a single operation atomically by using `operate()`. Here is an example that increments a counter and gets the new value, similar to `INCR` in Redis:

```auto
String binName = "my_counter";
Key key = new Key("test", "demo", "my-record-that-has-a-counter");
Record record = aerospikeClient.operate(null, key, Operation.add(new Bin(binName, 1)), Operation.get(binName));
long counterValue = record.getLong(binName);

```

I hope this helps

Peter

---

<div class="post-metadata">

**Author:** ![Assen\_Todorov](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Assen\_Todorov](https://discuss.aerospike.com/u/Assen_Todorov)\
**Post date:** [August 7, 2015, 8:12am UTC](https://discuss.aerospike.com/t/how-to-handle-concurrent-increments-for-the-same-key/1549/4 "2015-08-07T08:12:02Z")

</div>

Hi Peter,

Thanks for your explanation and the example. That was, what I was looking for.

Cheers,

Assen

---

<div class="post-metadata">

**Author:** ![Dipanshu\_Verma](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@Dipanshu\_Verma](https://discuss.aerospike.com/u/Dipanshu_Verma)\
**Post date:** [September 19, 2017, 7:22am UTC](https://discuss.aerospike.com/t/how-to-handle-concurrent-increments-for-the-same-key/1549/5 "2017-09-19T07:22:52Z")

</div>

One Question though, What if the key is not present, and I call Operation.add(),

Currently it is just adding a new bin with no key.

Is there a way possible that it creates the Key which is sent if not found??
