Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
How to handle users simultaneously trying to fill limited "slots" in Firebase realtime db

I have a situation where users are watching for an "item" to have "slots" become available. Users will try to fill a slot if possible. I'm new to firebase and am pretty certain that the way I have things set up will not scale. It works with just me testing, but if there are many users I'm thinking it will break.

Currently it's implemented like so:

The "item" is an object on firebase realtime db with an observer set on it. When the observer fires, it checks to see if the "slots" object within that item is full of users or not. If it's full, nothing should happen. If it's not full then the user's id is put within the "slots" object.

Fullness is determined by comparing the current length of the "slots" object with a "maxLength" property/integer attached to the item.

Unfortunately, I'm fairly certain that what will happen is that all users will try to fill the slots at the same time, and they will all detect that the item is not full before any of them actually fill slots. Therefore I will accidentally have more users in the "slots" object than the number in "maxLength". How can I guarantee that won't happen?

Pseudocode of what I have (react native):

const item = firebase.database().ref(`item/`);          //the item
const slots = firebase.database().ref(`item/slots`);    //slots within the item

item.on('value', (snapshot) => {
    let data = snapshot.val();
    let keys = data.slots ? Object.keys(data.slots) : [];
    let full = keys.length >= data.maxLength ? true : false;

    if (!full) {
        slots.update({
            [user.uid]: true
        });
    }
});

Realtime DB example looks roughly like:

item {
    maxLength: 10,
    slots: {
        rj6h5f7sd88s7f6sdfs7f7: true,  //user
        d466df8dfj8dfjd3nkvmdd: true,  //another user
        // etc.
    }
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda