I post my findings here after I posted my question above. Hoping that someone else will benefit from these results when he finds himself in a situation like the one above.
Discussing the Rack :: Lock issue with one of my senior Juha Litola employees , below were his first thoughts (quoting his own words as they are):
Could it be possible that you see the dimension of the artifact in the sense that you simply see Rack :: Lock as time-consuming, but this is only because it completes the actual call? Thus, Rack :: Lock time is a cumulative time from everything that happens when processing a request. Cm.
https://github.com/rack/rack/blob/master/lib/rack/lock.rb .
As for performance issues, could you clarify what problems you have so that I can help?
What I thought this might be an opportunity. However, I could not convince myself of this possibility because of the following doubts:
Rack::Lock is in second position in the Middlewares chain with the Rails application (see the list of middleware mentioned above at https://gist.github.com/JigneshGohel-BoTreeConsulting/91c004686de21bd6ebc1 ). And each middleware is processed sequentially in a chain. Thus, Rack::Lock will be the second to process the request and then the others in the chain will be able to jump.
In this case, as I understand it, the timestamps recorded for the Rack :: Lock middleware are cumulative time from everything that happens when processing the request. And that should be the time taken by Rack :: Lock middleware.
Later Juha, having spent several minutes viewing the server (see the note below), provided the following data:
With quick browsing, I think there is a pretty obvious problem with how the application is configured. The application does not have config.threadsafe !, which means that Rack :: Lock is enabled and request processing is limited to one thread / process. Now puma is configured for only one process, but on threads 16-32. This means that puma is currently processing only one request at a time.
The best solution, of course, would be if you could enable thread safe mode, but that would require rigorous testing. If this does not work or is not an option, puma should be configured with multiple employees with 1 thread each.
Note. I forgot to add any configuration information for the web server on which my application is deployed. We use the Puma web server ( https://github.com/puma/puma )
With this, I got a hint to dig more into config.threadsafe! . Searching the Internet, I landed on the following articles.
http://www.sitepoint.com/config-threadsafe/
http://tenderlovemaking.com/2012/06/18/removing-config-threadsafe.html
sheds great hope on how to enable or disable the config.threadsafe option ! affects the performance of an application deployed on a multi-threaded or multi-processor web server during production.
A brief overview of what is presented above the articles
What is Rack :: Lock?
Rack :: Lock is a middleware that is pushed onto the Rails middleware stack to protect our applications from multithreaded Bogeyman. This middleware is supposed to protect us from unpleasant race conditions and dead ends by wrapping our queries with a mutex. The binder locks the mutex at the start of the request and unlocks the mutex when the request completes.
Suppose there is a program that executes and sends 5 requests at the same time 100 times for an application whose code (for example, Controller) is NOT thread safe.
Now consider the effects of the middleware combination Rack :: Lock, config.threadsafe! the option is enabled or disabled, insecure code in the application and a multithreaded or multiprocessor web server, after the program is terminated or destroyed
Multithreaded Web Server (Puma)
Note. Runtime examples and statistics are provided at http://tenderlovemaking.com/2012/06/18/removing-config-threadsafe.html
Multiprocessor Web Server (Unicorn)
Conclusion:
In a multiprocessor environment, Rack :: Lock becomes redundant if we save config.threadsafe! option is disabled. This is because in a multiprocessor environment, the socket is our lock, and we do not need additional lock. Thus, it is useful to include config.threadsafe! and remove Rack :: Lock overhead in the production environment.
In a multi-threaded environment, if we save config.threadsafe! developers need to ensure that application code is thread safe. And the advantage of saving config.threadsafe! lies in the fact that it takes less time to process incoming requests.
In my application context, we configured the Puma server configuration, increasing the number of workers. I hope the performance improves.