How does Unicorn works out?
Unicorn works in cluster wherein it queues requests to multiple workers. Whenever the Unicorn master starts, it loads the app into its memory and handles to serve the requests to its workers.
Here is a link which will elaborate the architecture for Unicorn in a nutshell: [Unicorn Architecture(https://github.com/blog/517-unicorn)][1]
This is one of the good architecture that helps to distribute requests to provide load balancing. At the same time, there might occur an issue with Data Transaction (Concurrency). Would like to give a small scenario that created a issue in one of the web.
Scenario:
We had a tagging feature in the application which used to send a AJAX request for its creation.
On live application, it triggered multiple requests to server for creating tags in DB. It created multiple tags in backend though the Tags Model involved a Unique validation on itself.
As per the validation, the database model should handle all the possible validations declared on it. So this issue was unexpected. In rails, every changes to the database is locked under a transaction, so when any of the exceptions raised, should rollback the transaction. This behaviour wasnt following with Tags feature.
Basically, when multiple requests were fired from application to create tags, Unicorn queued this request to its workers which were internally in transaction. Unless one of the transaction was committed, other transaction couldn't trace the existence of data in DB. Hence, both the transaction were committed at the same time. At the end, we received tags twice in our DB.
This scenario may occur whenver there is multiple requests to server for same the data and Unicorn tries to queue them within its workers.
It would be a good approach to queue all requests to server once for the data rather hitting multiple times at once. This will boost performance of the application.
[1]: https://github.com/blog/517-unicorn