1

I have this items node:

items
|
|----item_id_1
     -name:
     -type:
     -price:

The idea is when one user gets an item, 2 things happen:

  1. The item gets removed from the items node.
  2. The item gets added to his/her my_items node.

These are items that only one user can get, obviously the user that requested them first, and then they get removed.


The problem:

If multiple users requested the item at the same time, how do I handle where this item will go?

Question:

  1. How to make sure that if multiple users requested an item at the same time, only one will get it (No One Else)?

  2. Are Security rules able to solve this?

  3. I am aware of Firebase Transaction operations, but not sure if they help in my case.

Any advice is appreciated.

Thanks.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • 1
    @Mises Transactions only work on a single node as far as I know. I am trying to update multiple locations (nodes) at the same time. – Data overflow Jan 10 '23 at 16:34
  • @Mises I mean when a user gets an item, the item should get deleted from `items` and the item must be added to `my_items`, this is a multi path update and I don't think a transaction is useful here. – Data overflow Jan 10 '23 at 20:39
  • I would be happy to see your users trying to click an item to get it, and item disappears in milliseconds, and they click on a completely different item. MAGIC XD – Mises Jan 10 '23 at 22:26

1 Answers1

1

I am aware of Firebase Transaction operations, but not sure if they help in my case.

When it comes to updating a Realtime Database location (node) in a multi-user environment, then indeed a transaction is required. However, a transaction can only read and update a single location (node). There is no way you can perform multi-location transactions. On the other hand, Firestore does:

Using the Cloud Firestore client libraries, you can group multiple operations into a single transaction.

So when using Firestore, you can update documents no matter if they exist in a collection or a sub-collection. In the Realtime Database, you can only safely update children under the location (node) you are using for the transaction.

Are Security rules able to solve this?

Yes, there will be an alternative to secure your multi-location by writing security rules. Please see below an example:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Can you please give me an idea of what would a Security Rule look like in my case? Thanks a lot for your time. – Data overflow Jan 11 '23 at 13:17
  • You should make your own attempt given the information in the answer and ask another question if something else comes up. So please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Jan 11 '23 at 13:27
  • Do you think maybe I can update this question, so that I don't make a duplicate question? – Data overflow Jan 11 '23 at 13:31
  • No, asking a follow-up question is just a new post. – Alex Mamo Jan 11 '23 at 13:34
  • Ok then just to make sure. So what you suggested in your answer is that for the realtime - database case, I would have to do a multi-location update to my 2 nodes `items` and `my_items` and secure both nodes with `Security-Rules`, so that only one user gets the item even though many clicked at the same time? – Data overflow Jan 11 '23 at 13:38
  • I would upvote, but I don't have enough reputation. Thanks for your help and for your time. – Data overflow Jan 11 '23 at 14:09
  • Yes, that's pretty much what the linked answer says. You're very, welcome. – Alex Mamo Jan 11 '23 at 14:15
  • I posted another question about securing the nodes like you suggested, would you please check [here](https://stackoverflow.com/questions/75088972/firebase-how-would-i-secure-a-node-where-each-item-will-end-up-with-one-user-onl). – Data overflow Jan 13 '23 at 01:34
  • I'll take a look and if I'll know the answer, I'll write to you. – Alex Mamo Jan 13 '23 at 06:09
  • Thanks Alex and if you want me to update any thing in the other question or something is not clear, please let me know. – Data overflow Jan 13 '23 at 12:23