Concurrency: How to limit the edge count to a given number?


I recently described a game scenario where a game is initiated by a player and then the first other player who subscribes to the game will play it.

The structure is basic : nodes for games and players, and a player predicate from a game to a player.

@MichelDiz suggested a method to do it, but it doesn’t seem to fit a great number of players, say 10, 50 or 100.

I don’t know how to use upserts in that scenario, as it only applies to indexes on literals, and it could generate a lot of aborted transactions as there will be (hopefully) a lot of game creations and subscriptions.

My current solution is to add a nano-timestamp as a facet to each player predicate and then only keep the N firsts to subscribe based on the timestamp.

But I can imagine a scenario where it fails, I’m just not sure it can happen.
Here is such a scenario with games allowing maximum 2 players:

  • Player A creates game G
  • Player B finds game G available
  • Player C finds game G available
  • Player B generates a timestamp T1
  • Player C generates a timestamp T2 (greater than T1)
  • Player C updates the game with his player predicate and the T2 timestamp
  • Player C queries the game to check if he’s the first to subscribe
  • Player C seems to be the first, the game start
  • Player B updates the game with his player predicate and the T1 timestamp
  • Player B queries the game to check if he’s the first to subscribe
  • Player B find himself and player C as subscribers, but as his timestamp T1 is lesser than T2, he thinks he is the first so he also plays the game

Does this can happen?

I found a solution that might work just fine and I cannot think about a scenario where it fails.

Instead of using timestamps, I will use dGraph new UIDs as tokens for subscribers to games and only keep the N first tokens based on the UID value.

Now, N players (generating N parallel requests) will each:

  • get an available game
  • create a token (connected to the game and themselves)
  • get the game node again with all tokens (and their connections to players)
  • sort all tokens based on their UID
  • check if their own token is at a position that fit into the game maximum capacity
  • if not they remove their connections to the game and try the whole process again


Of course this as to be well tested, but it is not easy to do so.
I will write massive parallel tests for my API and see if it works.

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