Monday, June 5, 2017

JRuby saved my project

Nobody here ignores I am creating my own full-stack web development framework, the Emerald. And this week, during the first tests, I faced a serious problem.

Emerald is ReactJS-based and uses fetch API to get data from the back-end to the front-end. Of course, there are lots of web requests when you are creating a big (and meaningful) page. And it was then that I started getting lots of Error 500.

My logic was flawless! I tested the backed exhaustively, using both RSpec and command line with curl. 100% of requests working fine in both, but lots of failures when testing with the browser.

After one whole day fighting these errors I remembered a discussiong in a StackOverflow question, when a fellow developer with the name Myst pointed me a big problem when serving static files with Ruby: the GIL.

I won't try to explain the GIL here. Better giving you this link, where you may find more information.

Just to summarize, this is a problem related with threads. Threads in Ruby are an illusion, as this article correctly points out. And this problem turns out to be very obvious when a piece of code is receiving lots of web requests per second. Things will break!

But do not despair, 'cause JRuby is here to help!

As you all know, JRuby is a Ruby implementation written in Java. And Java does multithreading in an easy (not really, but hey... it works!) way.

Then, when I installed JRuby and moved my project to it, everything worked really fine!

This is another example of how discussing things with many people may enlighten your work and save your sanity.