# Command like redis incr :atomic increment operations

**URL:** <https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248>\
**Category:** How Developers Are Using Aerospike\
**Created:** [December 10, 2015, 9:42am UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248 "2015-12-10T09:42:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincent](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/vincent/32/282_2.png) [@vincent](https://discuss.aerospike.com/u/vincent)\
**Post date:** [December 10, 2015, 9:42am UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/1 "2015-12-10T09:42:17Z")

</div>

` final Set set = new HashSet\<\>();

```
        for (int i = 0; i < number; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    Record operate = client.operate(policy, key, Operation.add(bin), Operation.get());
                    int value = operate.getInt(bin.name);
                    set.add(value);
                }
            });
            thread.start();
        }`

```

the set.size() less than number,it‘s mean will get duplicate values

I want to increase the value of atom,In multithreaded environment,the code above some problems!

redis command: incr

[http://redis.io/commands/incr](http://redis.io/commands/incr)

How I implement it in aerospike? use UDF?

---

<div class="post-metadata">

**Author:** ![rbotzer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/rbotzer/32/2114_2.png) [@rbotzer](https://discuss.aerospike.com/u/rbotzer)\
**Post date:** [December 10, 2015, 10:04am UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/2 "2015-12-10T10:04:32Z")

</div>

Every language client for Aerospike includes an increment operation which will initialize the record if it does not exist. Why would you use UDF for this?

For example, the Python client’s [`aerospike.Client.increment`](http://www.aerospike.com/apidocs/python/client.html#aerospike.Client.increment)

The docs for the various clients are linked to from here: [http://www.aerospike.com/docs/client/](http://www.aerospike.com/docs/client/)

---

<div class="post-metadata">

**Author:** ![vincent](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/vincent/32/282_2.png) [@vincent](https://discuss.aerospike.com/u/vincent)\
**Post date:** [December 10, 2015, 10:20am UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/3 "2015-12-10T10:20:58Z")

</div>

i use java and i don’t found native increment operation

I found the answer on github,use above code.

> <https://github.com/aerospike/aerospike-client-java/issues/6#issuecomment-52013852>
>
> Is a feature to Increment value of a particular bin by certain amount?

---

<div class="post-metadata">

**Author:** ![rbotzer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/rbotzer/32/2114_2.png) [@rbotzer](https://discuss.aerospike.com/u/rbotzer)\
**Post date:** [December 10, 2015, 2:35pm UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/4 "2015-12-10T14:35:20Z")

</div>

In the Java client documentation on our site there’s a link to the [API reference](https://www.aerospike.com/apidocs/java/) where you would find it. It’s the ‘add’ operation.

---

<div class="post-metadata">

**Author:** ![vincent](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/vincent/32/282_2.png) [@vincent](https://discuss.aerospike.com/u/vincent)\
**Post date:** [December 10, 2015, 4:13pm UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/5 "2015-12-10T16:13:53Z")

</div>

I think you misunderstood what I mean

I need to increase the value of multi-threading and get to the increased

JAVA API [add] method can increase the value and can not be returned,so i use operate method add and return .

if the bin base value is 1 add i use multiple threads to add and get ,the return value maybe dirty data .

i test my code,the final value is no problem.

i mean like this:

```
base bin value is 1

 multi-threading 
{
    add "key" 1
    read "key" #Multiple threads may be return 3
 }

```

i wan’t thead-1 return 2,thead-2 return 3. current may be thead-1 reuturn 3 and thead-3 reutrn 3;

---

<div class="post-metadata">

**Author:** ![vincent](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/vincent/32/282_2.png) [@vincent](https://discuss.aerospike.com/u/vincent)\
**Post date:** [December 10, 2015, 4:17pm UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/6 "2015-12-10T16:17:05Z")

</div>

I want to give the user a non-repetition of id

I do not want to use uuid,

I want to use auto-incremented long value

---

<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:** [December 10, 2015, 4:32pm UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/7 "2015-12-10T16:32:46Z")

</div>

Here is some example code that

- increments a counter
- decrements a counter
- increments a counter and returns the new value

```java
Key coounterKey = new Key(this.namespace, this.set, "my-counter");
/*
 * increment the counter by 1
 */
Bin counterBin = new Bin("some-counter", 1);
this.client.add(null, coounterKey, counterBin);
/*
 * increment the counter by 15
 */
counterBin = new Bin("some-counter", 15);
this.client.add(null, coounterKey, counterBin);
/*
* increment the counter by 1 and return the new value
*/
counterBin = new Bin("some-counter", 1);
Record record = this.client.operate(null, coounterKey, Operation.add(counterBin), Operation.get("some-counter"));
System.out.println("New value: " + record.getLong("some-counter"));
/*
 * decrement the counter by 5
 */
counterBin = new Bin("some-counter", -5);
this.client.add(null, coounterKey, counterBin);

```

I hope this helps

---

<div class="post-metadata">

**Author:** ![vincent](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/vincent/32/282_2.png) [@vincent](https://discuss.aerospike.com/u/vincent)\
**Post date:** [December 11, 2015, 2:58am UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/8 "2015-12-11T02:58:00Z")

</div>

Thank you for your answer,

But I think in multiple threads,increments a counter and returns the new value,This result will be problems

---

<div class="post-metadata">

**Author:** ![rbotzer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/rbotzer/32/2114_2.png) [@rbotzer](https://discuss.aerospike.com/u/rbotzer)\
**Post date:** [December 11, 2015, 4:11am UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/9 "2015-12-11T04:11:22Z")

</div>

It will not, because the increment operation is atomic. If you are using multi-ops you can tell it to both increment and read the value, so there is no time between those operations for something else to happen. You will perform both operations together. So you can increment a counter and get its value at that time, post-increment.

---

<div class="post-metadata">

**Author:** ![ankit.kothana](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@ankit.kothana](https://discuss.aerospike.com/u/ankit.kothana)\
**Post date:** [August 4, 2016, 7:01am UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/10 "2016-08-04T07:01:44Z")

</div>

Does increment operation in Aerospike updates TTL of record?

---

<div class="post-metadata">

**Author:** ![kporter](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/kporter/32/515_2.png) [@kporter](https://discuss.aerospike.com/u/kporter)\
**Post date:** [September 30, 2016, 8:44pm UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/11 "2016-09-30T20:44:12Z")

</div>

> [@ankit.kothana](#):
>
> Does increment operation in Aerospike updates TTL of record?

Yes, increment will update a record’s TTL.

---

<div class="post-metadata">

**Author:** ![nanokassa](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/nanokassa/32/1062_2.png) [@nanokassa](https://discuss.aerospike.com/u/nanokassa)\
**Post date:** [February 6, 2019, 8:04am UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/12 "2019-02-06T08:04:27Z")

</div>

Hello!

Can you show this code for php? A saw operation like “operate” and I want to know, if this is the same like INCR in Redis?

```
$key = $client->initKey("test", "users", 1234);
$operations = [
 ["op" => Aerospike::OPERATOR_INCR, "bin" => "age", "val" => 1],
 ["op" => Aerospike::OPERATOR_READ, "bin" => "age"],
];
$options = [Aerospike::OPT_TTL => 600];
$status = $client->operate($key, $operations, $returned, $options);
if ($status == Aerospike::OK) {
    var_dump($returned);
} else {
    echo "[{$client->errorno()}] ".$client->error();
}

```

Is the same like INCR in Redis? So is it atomic?

---

<div class="post-metadata">

**Author:** ![rmarks](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@rmarks](https://discuss.aerospike.com/u/rmarks)\
**Post date:** [February 6, 2019, 5:11pm UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/13 "2019-02-06T17:11:23Z")

</div>

> [@nanokassa](#):
>
> $operations = [[“op” =\> Aerospike::OPERATOR\_INCR, “bin” =\> “age”, “val” =\> 1], [“op” =\> Aerospike::OPERATOR\_READ, “bin” =\> “age”], ];

The code in your snippet looks correct for an increment of a bin, and a read of the new value.

The operate method in the PHP Client will cause all of the provided operations to be applied atomically. The value of the `"age"` bin will be incremented, and its new value will be returned to the client.

---

<div class="post-metadata">

**Author:** ![nanokassa](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/nanokassa/32/1062_2.png) [@nanokassa](https://discuss.aerospike.com/u/nanokassa)\
**Post date:** [February 11, 2019, 12:50pm UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/14 "2019-02-11T12:50:25Z")

</div>

Thanks!

So one more question - is in Aerospike function like bRPopLPush (in Redis)? I need to move element from List1 to List2. So it should be atomic. I mean there are 600 operations that will do the same, but I need that only one operation can do it and then return element back, and so on. Its like queue. Is this implementation in php language for this operation?

---

<div class="post-metadata">

**Author:** ![rmarks](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@rmarks](https://discuss.aerospike.com/u/rmarks)\
**Post date:** [February 11, 2019, 4:25pm UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/15 "2019-02-11T16:25:30Z")

</div>

Aerospike does not currently have an equivalent atomic operation for either `RPOPLPUSH` or `BRPOPLPUSH`.

---

<div class="post-metadata">

**Author:** ![nanokassa](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/nanokassa/32/1062_2.png) [@nanokassa](https://discuss.aerospike.com/u/nanokassa)\
**Post date:** [February 11, 2019, 11:10pm UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/16 "2019-02-11T23:10:39Z")

</div>

> [@rmarks](#):
>
> Aerospike does not currently have an equivalent atomic operation for either `RPOPLPUSH` or `BRPOPLPUSH` .

So, is there any other ways to do it?

---

<div class="post-metadata">

**Author:** ![rmarks](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@rmarks](https://discuss.aerospike.com/u/rmarks)\
**Post date:** [February 12, 2019, 1:07am UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/17 "2019-02-12T01:07:32Z")

</div>

Presently, there isn’t an ‘operation’ to do such. You can write a [UDF](https://www.aerospike.com/docs/guide/udf.html) in LUA to do this atomically. UDF is currently the only solution for atomic multi-bin inter-bin operations.

---

<div class="post-metadata">

**Author:** ![nanokassa](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/nanokassa/32/1062_2.png) [@nanokassa](https://discuss.aerospike.com/u/nanokassa)\
**Post date:** [February 12, 2019, 11:26am UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/18 "2019-02-12T11:26:13Z")

</div>

Even if I will make pop operation? When I make pop operation - so it must be atomic operation. In manual there is: **Atomic list operations only work against top level elements, not nested values** ([https://www.aerospike.com/docs/guide/cdt-list.html](https://www.aerospike.com/docs/guide/cdt-list.html)) So will it be atomic? And can I use it for queue?

---

<div class="post-metadata">

**Author:** ![rmarks](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@rmarks](https://discuss.aerospike.com/u/rmarks)\
**Post date:** [February 12, 2019, 4:13pm UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/19 "2019-02-12T16:13:31Z")

</div>

That is saying that an operation such as `list_pop` is able to remove an element from the list, but not able to remove an element from a sublist.

For example

```auto
list = [1, 2, 3, [4, 5, 6]]
remove_by_index(list, -1) ## List = [1,2,3] now

```

But if list is:

```auto
list = [1, 2, 3, [4, 5, 6]]

```

With our current operations, you can not do something like remove a single element from the list stored in the last position of the main list. So, with our current operations, you _cannot_ change list to:

```auto
[1, 2, 3, [4, 5]]

```

Using UDFs you _can_ do these types of operations, and operate on multiple bins in the same record atomically.

---

<div class="post-metadata">

**Author:** ![rbotzer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/rbotzer/32/2114_2.png) [@rbotzer](https://discuss.aerospike.com/u/rbotzer)\
**Post date:** [November 7, 2019, 5:07pm UTC](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248/20 "2019-11-07T17:07:16Z")

</div>

> [@Nested map read/get API](https://discuss.aerospike.com/t/nested-map-read-get-api/4030/2):
>
> As of [Aerospike version 4.6](https://www.aerospike.com/blog/aerospike-4-6-enhancing-security-developer-features/) you can apply [list](https://www.aerospike.com/docs/guide/cdt-list.html) and [map](https://www.aerospike.com/docs/guide/cdt-map.html) operations on nested structures at arbitrary depths by defining a [context](https://www.aerospike.com/docs/guide/cdt-context.html). The context is the logical/physical path to finding the nested element you want to operate on. I wrote a medium post on the topic

[Next page](https://discuss.aerospike.com/t/command-like-redis-incr-atomic-increment-operations/2248.md?page=2)
