# How to improve Dgraph mutate latency

**URL:** <https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422>\
**Category:** Dgraph\
**Tags:** kind:question, dgraph\
**Created:** [September 17, 2020, 2:12am UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422 "2020-09-17T02:12:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![xxm404](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/xxm404/32/4182_2.png) [@xxm404](https://discuss.dgraph.io/u/xxm404)\
**Post date:** [September 17, 2020, 2:12am UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/1 "2020-09-17T02:12:05Z")

</div>

When I use dgraph，I find the mutate it`s not effective immediately.  
The schema like this

```auto
<xid>: string @index(hash) @upsert .
<ww/attr/level>: int @index(int) .
type User {
    <xid>
    <ww/attr/level>
}

```

The pseudo code like this

```auto
startTransaction
update user level from 100 to 200
if sleep 10 milliseconds
    get user level it`s 200
if query immediately
    get user level it`s still 100

```

dgraph version: v20.03.4

---

<div class="post-metadata">

**Author:** ![rajas](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rajas/32/4393_2.png) [@rajas](https://discuss.dgraph.io/u/rajas)\
**Post date:** [September 17, 2020, 5:58am UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/2 "2020-09-17T05:58:45Z")

</div>

Hi @xxm404,

Can you provide more details ? How are you making these transactions and mutations ? Is the transaction getting committed before querying it ?

---

<div class="post-metadata">

**Author:** ![xxm404](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/xxm404/32/4182_2.png) [@xxm404](https://discuss.dgraph.io/u/xxm404)\
**Post date:** [September 17, 2020, 7:44am UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/3 "2020-09-17T07:44:22Z")

</div>

Hi @rajas thanks for the reply.  
We used the raw http client .  
update and query are within the same transaction, not committed.  
query like this

```auto
getUserNode(func: uid(0x1)) {
     uid
     level: <ww/attr/level>
}

```

mutate like this

```auto
set {
    <0x1> <ww/attr/level> 200 .
}

```

---

<div class="post-metadata">

**Author:** ![rajas](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rajas/32/4393_2.png) [@rajas](https://discuss.dgraph.io/u/rajas)\
**Post date:** [September 17, 2020, 10:32am UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/4 "2020-09-17T10:32:59Z")

</div>

Are you using `mutate` with `commitNow=true` ?  
I tried to reproduce this on v20.03.4, the behaviour I observed is that if mutate is done with `commitNow=true` , the query always returned the updated value while when `commitNow` option is not set, it always returned the old value.

Ideally, a query should be run only after a mutation is commited, so that the updated value gets written to disk.

---

<div class="post-metadata">

**Author:** ![xxm404](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/xxm404/32/4182_2.png) [@xxm404](https://discuss.dgraph.io/u/xxm404)\
**Post date:** [September 17, 2020, 1:22pm UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/5 "2020-09-17T13:22:53Z")

</div>

I have not use `commitNow=true`  
In my function I need to update(mutate) and the query in the same transaction, like my pseudo code

```auto
startTransaction
set {
    <0x1> <ww/attr/level> 200 .
}
getUserNode(func: uid(0x1)) {
     uid
     level: <ww/attr/level>
}
echo user level (In most cases it will output 200 but sometimes it will output 100)
commit

```

This problem may be related to the amount of data, we have 700 thousand node.

---

<div class="post-metadata">

**Author:** ![rajas](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rajas/32/4393_2.png) [@rajas](https://discuss.dgraph.io/u/rajas)\
**Post date:** [September 17, 2020, 1:56pm UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/6 "2020-09-17T13:56:05Z")

</div>

Is there any compulsion to mutate and query in the same transaction ?  
Adding `commitNow=true` with mutate should solve the issue and the query will then output 200 always.

Let us know if you still face the problem after adding `commitNow=true`.

---

<div class="post-metadata">

**Author:** ![xxm404](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/xxm404/32/4182_2.png) [@xxm404](https://discuss.dgraph.io/u/xxm404)\
**Post date:** [September 18, 2020, 9:43am UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/7 "2020-09-18T09:43:24Z")

</div>

Does it mean that the mutate delay is normal?

---

<div class="post-metadata">

**Author:** ![rajas](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rajas/32/4393_2.png) [@rajas](https://discuss.dgraph.io/u/rajas)\
**Post date:** [September 18, 2020, 10:33am UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/8 "2020-09-18T10:33:32Z")

</div>

Are you facing any delays for mutate with `commitNow=true` option ? Can you provide more details about the delay ?

Once a mutation is committed, there shouldn’t be any delay before the new value starts being shown with a query.

---

<div class="post-metadata">

**Author:** ![xxm404](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/xxm404/32/4182_2.png) [@xxm404](https://discuss.dgraph.io/u/xxm404)\
**Post date:** [September 21, 2020, 10:09am UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/9 "2020-09-21T10:09:46Z")

</div>

> Once a mutation is committed, there shouldn’t be any delay before the new value starts being shown with a query.

When mutation and query in the same transaction, mutation not committed, query has any delay is normal?

---

<div class="post-metadata">

**Author:** ![RJKeevil](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rjkeevil/32/2625_2.png) [@RJKeevil](https://discuss.dgraph.io/u/RJKeevil)\
**Post date:** [September 21, 2020, 1:29pm UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/10 "2020-09-21T13:29:54Z")

</div>

Its not really a delay, you simply wont see 200 in your query result unless you commit the mutation before subsequently querying it. Its a different paradigm to e.g. RDBMS, as I understand until you commit your transaction is just a list of commands pending to be run, it wont be reflected in a graph query.

---

<div class="post-metadata">

**Author:** ![xxm404](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/xxm404/32/4182_2.png) [@xxm404](https://discuss.dgraph.io/u/xxm404)\
**Post date:** [September 22, 2020, 3:07am UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/11 "2020-09-22T03:07:37Z")

</div>

Really？In my case，query result in most cases is 200, only in a few cases is 100.

---

<div class="post-metadata">

**Author:** ![rajas](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rajas/32/4393_2.png) [@rajas](https://discuss.dgraph.io/u/rajas)\
**Post date:** [September 22, 2020, 5:26am UTC](https://discuss.dgraph.io/t/how-to-improve-dgraph-mutate-latency/10422/12 "2020-09-22T05:26:31Z")

</div>

Hi @xxm404,

If query is done before committing the mutate transaction, it will show the old value.  
Can you share your code snippet and the way the code is run so that we can reproduce it ? In case there is inconsistency with the value in query result, this should be reported as a bug.
