Check and Set in Dgraph?


I was wondering if there is CAS operation available in Dgraph. Here is one use case for CAS -

  1. Let’s say I look up the UID for a certain node in the database
  2. After lookup I use the UID to update/delete edges or literal values of the node

I want to be able to do this operation using multiple threads to achieve higher throughput. Now, while I am performing the second step, the UID may get deleted or values may be updated. Is there way to abort the transaction if for example, the version of the node is updated?


You can use transactions so that update conflicts result in an aborted transaction.

Let’s say I am trying to keep highest timestamp value. I have to read the value first, compute the higher value and then write it. Can I do it in single transaction? What about the case when my logic is more complex than just computing higher value?


Yes, you can do that in a single transaction. Transactions can contain any number of queries/mutations. That’s the purpose of ACID transactions. See this Wikipedia page for more info:

Increment transaction example

You can see transactions at work in our increment example that increments a counter stored in Dgraph. If multiple clients are trying to increment the counter value concurrently, they will not be able to successfully increment to the same value. Because transactions are ACID, the counter always increases.

The increment example uses a transaction by:

  1. Starting a transaction
  2. Querying for the current counter value.
  3. Incrementing the current counter value.
  4. Mutating Dgraph with the increment counter value.
  5. Committing the transaction.

This example explains the solution. Thank you so much.
One more question, are there way to reduce transaction aborts? When I was running the live loader, I observed decent number of transaction aborts. It directly takes away the throughput while still consuming resources.

For live loader specifically, you can decrease the number of concurrent requests with -c. By default the live loader does 100 concurrent requests. You can set it to around 10 or so to reduce the number of aborts.

sure, though I don’t want to lose on performance. Thanks for your reply.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.