I'm trying to create a game where users are matched with each other randomly using Firebase.
My idea to match them is like this:
- User A comes, reads list of games. If there aren't any games waiting start game. Sets himself as initiator, opponent as null.
- User B comes, reads list of games. Finds User A's game (opponent is null) and joins it (he becomes the opponent)
- What happens if User C comes between the moment where User B read the games list and the moment User B wrote himself as the opponent (for him opponent would still be null). Basically the concurrency problem.
I've read about transactions but I'm not 100% sure they help in this case (Read games with no Opponent, Write opponent to one of the games) because I've seen them used a lot in increasing values rather than reading/writing data.