RailsConf 2023 - Off to the races by Kyle d'Oliveira

Learn how to identify and mitigate sailboat-flying code problems by understanding race conditions, optimistic and pessimistic locking, and advisory locks in Ruby on Rails, ensuring robust and stable systems.

Key takeaways
  • It’s crucial to consider the concept of race conditions in code, defining it as an unanticipated behavior that arises from concurrent access to shared resources.
  • The speaker uses an analogy to explain race conditions, comparing them to the “dining philosophers” problem, where multiple philosophers share chopsticks and may lead to unpredictable outcomes.
  • To mitigate race conditions, Ruby on Rails offers pessimistic locking, which ensures that only one process can access the database at a time, and optimistic locking, which attempts to update the database without locking.
  • Ruby on Rails also provides advisory locks, which can be used to prevent conflicts between concurrent requests.
  • The speaker emphasizes the importance of testing for race conditions, as they can be difficult to reproduce and may not be immediately apparent.
  • The speaker suggests that, when dealing with race conditions, it’s essential to consider the possibility of failures and be prepared to handle them, ensuring that the system remains robust and stable.
  • He also recommends that developers consider using libraries or frameworks that can help simplify and handle race conditions, such as Redis’s increment command.
  • The speaker concludes that, by understanding and working with race conditions, developers can create more reliable and efficient systems.