I have found some thread below which talk about this scenario.
- Firebase: How do I update multiple resources atomically
- Firebase data consistency across multiple nodes
- How to store users and groups for a chat using Firebase
However still struggling to find, if there are any recommended data structure design for this. I see a Multi-write library firebase-multi-write which says you probably don't need this in most cases.
But I think I do need this, my scenario is : 3 users
/users/A : {credit:20}
/users/B : {credit:3}
/users/C : {credit:10}
And each user can steal credits from each other all at the same time.,
- A steals from B credits look like {21, 2}
- B steals from C credits look like {3, 9}
- C steals from A credits look like {11, 20}
Now I need to update the credits for each user to maintain this consistency, so that each steal operation is atomic in nature.
What will be the best way to handle such scenario, while maintaining data integrity in Java?