Saturday, 31 August 2013

Firebase how to prevent duplicate entries atomically

Firebase how to prevent duplicate entries atomically

I'm looking at using firebase as a data store for user data for a web app.
My current thought is to store each user's data using the timestamp of
when they joined as the key referencing that user's data. The advantage of
this scheme being that it's a simple way to assign unique integer ids to
users, and makes chronological sorting of users simple.
A downside, however, is that if two "add user" requests are submitted with
identical data, the app will happily add two separate entries, which is
unideal. I could shuffle things around (I'm starting to think I should use
email as the key and proritize by join data, rather than my current
scheme), but suppose I don't want to. Is there a way to prevent duplicate
data?
The naive approach would probably be just to do something like:
if(!searchFirebaseForUser(data)) {
addUser(data);
}
But this is definitely a race condition; it'd be easy for two requests to
both query and find no user in the database, and both add. I'd like to do
this in a transaction, but it doesn't seem like the Firebase transaction
support covers this case. Is there a way to handle this?

1 comment:

  1. Keep reading to ensure that you choose the strongest case possible. Ask for referrals from family members and friends to find a lawyer.This will help you find a reliable lawyer worthy of your case.
    solotravelapp.com |

    southshoregreenhomes.com |

    tabletpresseducation.com |

    techhelperusa.com |

    theschoolringstore.com |

    ReplyDelete