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

211
Visualizações
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);
    }
});
over 4 years ago · Santiago Trujillo
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