๐—ฅ๐—ฎ๐—ฐ๐—ฒ ๐—–๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐˜€: ๐—ง๐—ต๐—ฒ ๐—Ÿ๐—ฎ๐˜€๐˜ ๐—–๐—ผ๐—ผ๐—ธ๐—ถ๐—ฒ

One cookie sits on a plate. You and your sibling see it. You both grab it at once.

Computers do this too. It is a race condition.

This bug happens when two tasks run at the same time. The result depends on who wins the race.

Example: Two people buy the last item in stock.

You shipped two items. You only had one.

The pattern is always the same. Read. Decide. Write.

There is a gap in the middle. Someone sneaks in.

Fix it by making the check and change one locked step. Let the database handle the subtraction.

Race conditions happen here:

Avoid these mistakes:

Look for "read, check, write" in your code. Think about two users acting at the same millisecond. Do both win?

If yes, you found a race.

Source: https://dev.to/edriso/race-condition-when-two-things-reach-for-the-same-cookie-36do