how can we implement concurrency in ruby, where multiple thread are accessing same resource?

Viewed 31

I was asked to implement something like a movie seat booking with multiple concurrent request to book same seat. How can we do this in ruby without the use of database locks and only for in memory data structure? Are there other ways to have this type of check on application level?

1 Answers

Concurrency and parallelism aren't the same thing. You also can't do threading of writable shared resources without locking. It's pretty much axiomatic, because you don't want objects being mutated by multiple processes at once.

With Ruby, you have a few basic options:

  1. For thread concurrency:

  2. For parellel processing, use a Ractor if your version of Ruby supports it.

Solving these sorts of problems isn't impossible or hard. You simply have to account for the race conditions and possible mutability in your design.

Related