Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Off to the races

Off to the races

Race conditions are a natural hazard whenever working with shared resources. If you have multiple processes such as using a database, or a microservice architecture, there are likely some race conditions that are hiding in your code. As usage scales up, they will become more and more common. They are difficult to detect, challenging to reproduce and test, and sometimes downright hard to prove that it is fixed. In this talk, we will dive into what a race condition is, how it can show up in Rails applications, and some strategies on how to test them to give confidence that they are fixed.

Kyle d'Oliveira

April 24, 2023
Tweet

More Decks by Kyle d'Oliveira

Other Decks in Programming

Transcript

  1. P1 P2 P3 P4 P5 There is one chopstick between

    each philosopher Dining Philosophers
  2. P1 P2 P3 P4 P5 There is one chopstick between

    each philosopher A philosopher may eat if they can pick up both adjacent chopsticks Dining Philosophers
  3. P1 P2 P3 P4 P5 There is one chopstick between

    each philosopher A philosopher may eat if they can pick up both adjacent chopsticks Each chopstick can only be picked up by one philosopher Dining Philosophers
  4. P1 P2 P3 P4 P5 There is one chopstick between

    each philosopher A philosopher may eat if they can pick up both adjacent chopsticks Each chopstick can only be picked up by one philosopher Dining Philosophers What is a process all philosophers can agree to such that no one will starve?
  5. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  6. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  7. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  8. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  9. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  10. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  11. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  12. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  13. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  14. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  15. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  16. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  17. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available.
  18. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available.
  19. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 1. Pick up the left chopstick when it is available.
  20. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 1. Pick up the left chopstick when it is available.
  21. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available.
  22. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available.
  23. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available.
  24. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available.
  25. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available.
  26. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available.
  27. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available.
  28. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available. 1. Pick up the left chopstick when it is available.
  29. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat
  30. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat Multiple processes
  31. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat Shared Resources Multiple processes
  32. P1 P2 P3 P4 P5 Dining Philosophers 1. Pick up

    the left chopstick when it is available. 2. Pick up right chopstick when it is available 3. Eat for some amount of time 4. Put left chopstick down 5. Put right chopstick down 6. Repeat Shared Resources Critical section Multiple processes
  33. 1. How many times will retry? 2.What will you do

    if the retries aren’t successful?
  34. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? No Yes
  35. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? No No Yes
  36. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? No No Yes Yes
  37. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? No No Yes Yes • Read-modify-write • Check-then-act • Others
  38. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? No No No Yes Yes • Read-modify-write • Check-then-act • Others
  39. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? Can you remove the critical section? No No No Yes Yes Yes • Read-modify-write • Check-then-act • Others
  40. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? Can you remove the critical section? No No No Yes Yes Yes • Can you remove/refactor the code? • Can you make the operation atomic? • Read-modify-write • Check-then-act • Others
  41. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? Can you remove the critical section? No No No Yes Yes Yes Yes • Can you remove/refactor the code? • Can you make the operation atomic? • Read-modify-write • Check-then-act • Others
  42. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? Can you remove the critical section? Can you detect the race condition and recover from it? No No No No Yes Yes Yes Yes • Can you remove/refactor the code? • Can you make the operation atomic? • Read-modify-write • Check-then-act • Others
  43. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? Can you remove the critical section? Can you detect the race condition and recover from it? No No No No Yes Yes Yes Yes • Can you remove/refactor the code? • Can you make the operation atomic? • How many times can you retry? • What do you do when it exceeds that? • Read-modify-write • Check-then-act • Others
  44. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? Can you remove the critical section? Can you detect the race condition and recover from it? No No No No Yes Yes Yes Yes Yes • Can you remove/refactor the code? • Can you make the operation atomic? • How many times can you retry? • What do you do when it exceeds that? • Read-modify-write • Check-then-act • Others
  45. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? Can you remove the critical section? Can you detect the race condition and recover from it? Can you protect the critical section? No No No No No Yes Yes Yes Yes Yes • Can you remove/refactor the code? • Can you make the operation atomic? • How many times can you retry? • What do you do when it exceeds that? • Read-modify-write • Check-then-act • Others
  46. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? Can you remove the critical section? Can you detect the race condition and recover from it? Can you protect the critical section? No No No No No Yes Yes Yes Yes Yes • Can you remove/refactor the code? • Can you make the operation atomic? • How many times can you retry? • What do you do when it exceeds that? • How long can a process wait? • What do you do when it exceeds that? • Read-modify-write • Check-then-act • Others
  47. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? Can you remove the critical section? Can you detect the race condition and recover from it? Can you protect the critical section? No No No No No Yes Yes Yes Yes Yes • Can you remove/refactor the code? • Can you make the operation atomic? • How many times can you retry? • What do you do when it exceeds that? • How long can a process wait? • What do you do when it exceeds that? Yes • Read-modify-write • Check-then-act • Others
  48. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? Can you remove the critical section? Can you detect the race condition and recover from it? Can you protect the critical section? No No No No No Yes Yes Yes Yes Yes • Can you remove/refactor the code? • Can you make the operation atomic? • How many times can you retry? • What do you do when it exceeds that? Application speci f ic solution • How long can a process wait? • What do you do when it exceeds that? Yes No • Read-modify-write • Check-then-act • Others
  49. Start Done Working with shared resources? Can multiple processes work

    on those resources at the same time? Is there a race condition? Can you remove the critical section? Can you detect the race condition and recover from it? Can you protect the critical section? No No No No No Yes Yes Yes Yes Yes • Can you remove/refactor the code? • Can you make the operation atomic? • How many times can you retry? • What do you do when it exceeds that? Application speci f ic solution • How long can a process wait? • What do you do when it exceeds that? Yes No • Read-modify-write • Check-then-act • Others