Semaphore in Ruby on Rails

·

1 min read

While working on an image editing software application. I encountered a problem. The problem is that if the backend server (Ruby on Rails) receives many requests simultaneously, it causes memory overflow. To prevent this, I implement a concurrent semaphore in puma.rb file.

If I create a concurrent semaphore, you can limit the number of access from the image editing method on multiple threads. The easiest way to understand Semaphore is an access token box. Whenever, an application want to use a certain method, it must go to semaphore to get a token. If all tokens are taken from other applications, it has to wait. If this access token box has multiple tokens, this is a semaphore. if this access token box has only one token, this is a mutex.

Semaphore and Mutex are two concepts used in concurrent programming to synchronize access to shared resources among multiple threads or processes. Semaphore is used to control access to shared resources that can be accessed by multiple threads or processes simultaneously, while Mutex is used to prevent multiple threads or processes from simultaneously accessing a shared resource. Both Semaphore and Mutex are important tools for preventing race conditions and deadlocks in concurrent programs, and they are commonly used in a variety of applications.