Pick random node

Hello Dgraph team,

Picking a random has already been mentioned here and on github, but no feature request ticket seems to have been created.

I’m still in the need of such a feature as it would greatly improve:

  • Performances because I currently need to do random queries twice (in a transaction): First to count nodes, second to set one or more random offsets. My biggest and slowest query right now is to do matchmaking in my game, and I have to do it twice each time.
  • Code readability because my current solution requires a big function.

The syntax could be similar to first and offset keywords: random: 3 which would mean we want to pick 3 different random nodes from the current result set. It would perfectly work with offset and first to select the range before picking random nodes.

Please consider my request, I’m sure many projects would benefit from it :slightly_smiling_face:

1 Like

This may be a side effect of fixing Inconsistent response of sort as a result of indexing of a predicate when we get round to it.

@chewxy it would be very helpful for me to pick random nodes.

You know what, let’s make this a proper feature request. Accepted.

1 Like

Once we have a count, we can pick random nodes from the client side. Right now there is no way to know what the maximum node is to provide a random offset to a maximum value.

This of course would be limited to picking a random group of N nodes and not N random nodes. The use case needing N random nodes could be worked around by doing N requests which could be within a single query.

Indeed that’s pretty much what I’m doing right now:

  • A first request to count available nodes according to my filters
  • A second request, in the same transaction, with same filters, and split into query blocks, as many as required random nodes, each having a random offset.

oh, yes, sorry this is DQL tagged and not GQL. I was in the GQL mindset not having count.

Be aware that doing the second read query in the same transaction may fail when there have been a hand full of mutations in the meanwhile. After some mutations a rollup happens which makes the startTs of your transaction disappear.

Sure, you can retry the two steps until you succeed, but just so you know that this situation can occur with concurrent writes.

Indeed, I do this way because this is currently the only solution I found out.
That’s precisely why I’m requesting for a random nodes picker.

Duplicate: Is there a rand option for dgraph? - #2 by amaster507

Hello the team, and Happy New Year to you :slight_smile:
Is there any update on this feature request?

At the moment we’re pretty busy on shoring up some older features. Sorry. I’ll try my best to get this in

1 Like

Hello, did you find time to look into this?

Another good reason for having random selectors is to reduce transaction conflicts when returned nodes might be used by parallel threads for mutations.

In my scenario, random nodes are games opened by single players (creators). Opponent players try to pick a random game to play with game creators. But if 2 opponent pick the same game, one will be rejected thanks to a transaction conflict.

If MANY players play together, I get LOTS of transaction conflicts, because all opponents try to get the same open game, then all but one fail and finally they all try to get the same other open game, and so on.


I would like to work on this issue. I’m guessing this will have to go in the query/query.go file, but other than that I’m a little lost in the codebase. I’ve read the docs page on pagination (since first and offset were mentioned here).

Could anyone give me a rough idea of what needs to be done (in terms of the source code)?

I hope this is appropriate :slight_smile:

Opened a draft PR for this: feat(Query): Add random keyword in DQL by Samyak2 · Pull Request #7693 · dgraph-io/dgraph · GitHub

It’s my first major PR in Go, please let me know if there’s any feedback :slightly_smiling_face:


Thanks @samyak , we will review it :slight_smile:

1 Like