Race Conditions in Nodejs

27 Jul 2023

Recently I’ve implemented a backend API project by FreeCodeCamp, which is url shortener. You can see the project here

The thing is even when we know Javascript in this case Nodejs is single-threaded we could have ‘Race condition’ happening in your project. The reason for this is because we are using a database to store our data, ( well for this project we were using mongodb and redis ), and the database is not single-threaded. So, we could have several simultaneous requests to our application.

I found two articles about race condition on Nodejs that I found very interesting and I wanted to share with you. One of them is the Luciano Mammino article, which is very well explained, giving us an idea about race condition on nodejs could be posible, picturing the way that 2 transactions could go, a beautiful example about olives and grapes which one solution could be forcing to the second process to wait for the frist one to finish.

// source  -> https://www.nodejsdesignpatterns.com/blog/node-js-race-conditions/
async function main () {
  await sellGrapes() // <- schedule the first transaction and wait for completion
  await sellOlives() // <- when it's completed, we start the second transaction 
                     //    and wait for completion
  const balance = await loadBalance()
  console.log(`Final balance: ${balance}`)
}

The second one is about a real case described Matilde Duboille here, talking how she solved a race condition problem using mutex. In the article is defining race condition, and putting us in context about the problem that she was facing.

You can find the code that she used to solve the problem here

I hope you find this articles interesting and useful.