Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda