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

118
Vistas
Node / Express race condition on DB operations

I have an express route that needs to retrieve a row from a db, check if a field has a value set and if not set it. The row data then gets sent back to the client.

I know that node runs on a single thread but I/O operations do actually run asynchronously so I think I may have a problem if whilst the first client is waiting to write to db, a second client comes and reads a null value and performs the write a second time.

I can't have this happen as the value written is a shared value that there can only be one of.

Am I correct that this could happen and if so what is a recommended way to handle this?

Thanks.

let express = require('express');
let router = express.Router();

router.post('/getRoomStateByRoomUrl', async (req, res, next) => {
    const roomUrl = req.body.room_url;
    try {
        //READ FROM DB
        const roomState = await RoomStateModel.getRoomStateByRoomUrl(roomUrl);

        if(!roomState.tokboxSessionID) {
            const newSessionID = await TokboxService.createSession();
            
            //WRITE TO DB
            await RoomStateModel.setTokboxSessionID(newSessionID);
            
            roomState.tokboxSessionID = newSessionID;
        }
        res.status(200).json(roomState);
    } catch (error) {
        next(error);
    }
});
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