2016 Ruby Hero, top 50 Rails contributor.
On the eve of RailsClub 2017, on which Richard will be one of the speaks, we questioned him about his job and his views on Ruby development.
Why you will talk about threads in Ruby?
A lot of people don’t use threads because it’s difficult. Inside of Ruby, there are many other complicated factors, like global variable, global virtual machine lock called the GVL. A lot of developers have misconception that threads are hard to use due to the fact that GVL make them slightly less useful. We can’t use them inside of Ruby. I’m trying to provide the arguments that there still is a very good usecases for threads in Ruby. Using threads is hard in any language, not only in Ruby. I had some experience with multi threads in C and it is so much harder than in Ruby. Ruby isn’t easiest language to use threads with, but not the hardest one. As app developers, we can see real performance benefits with threads. There is something you can do only with threads. For example, a background task. I have a library called "Puma Worker Killer". You can use it, for example, for every 4 hours recycle. You can't do it without threads. It uses threads as a basis of its work. So, i’ve decided to make a talk about threads.
Where do you use threads already?
Currently, I’m maintaining:
- Puma Worker Killer
- Threaded (a thread pool implementation)
- Concurrent Ruby
Using threads, we can run checks in parallel and all checks are happening in the same time and it will run 6 times faster.
How many threads can we start at the same time and how much memory it will take?
This is one of the hardest questions in the world! haha! I even asked an university professor about it and she couldn’t give me the good answer. In general, you can run as many processes as there are CPUs assuming your program was totally CPU bound and did not use any IO. So, figure out how many CPUs you have and run that number of processes. Make sure you’re not going over your memory limits though. Threads are good at handling I/O, so the real question is how many threads per process to add.
Adding more threads per process will increase memory but also increase throughput. When the program is doing I/O like making a database call, it can switch to a new thread.
Alternative is to add even more processes. But they take even more memory than threads. Ruby takes a lot of memory already, so using only processes will take giant amount of memory. The number 1 reason for using threads is for memory sharing. The second reason is I/O. When there is an I/O, it’s really good candidate for using threads. Almost all Ruby and Rails apps use a ton of I/O so there is lots of room to add more threads for performance. For a Rails server running on Puma a good number for of threads to start with is is 5. From there you can increase or decrease it. You want to maximize your throughput. If you add way too many threads, like hundreds then your application performance will be worse because there is lots of thread contention. You want to add threads until your throughput stops improving.
Telling exact number of threads is really hard, it depends on your exact application and the machine you are running on.
How have you become a programmer?
I started learning Ruby in my free time in the university. At that time, websites and webapps were very popular, everyone was talking about it. I heard about the success of Bill Gates and said “wow! I wanna be like him! Wanna make billion dollars!”. So I asked my roommate how to create a website, he was studying computer science. He said that one of the popular websites of that time uses Ruby on Rails. So I bought “Ruby on Rails for dummies” book. At university, I was studying for an engineer, to design refrigerators. It was kinda boring for me. So all my free time I was building websites. It was so much fun to me! I started to go to a developer meetups and user groups. So I became a professional programmer ever since.
What advice can you give for a newbie?
Try to find a thing that gonna make you a million dollars! haha! Sometimes people ask me “How to become a programmer, how to learn it?” I ask them to step back and figure out what is the thing they wanna build? Can you imagine it? It can be anything, but having a project in your head is very important. The hardest part of programming isn’t a technical aspect. The hardest is about getting and staying excited, being able to understand how things work. Find what makes you excited and stay with it!
What’s your hobby? How do you spend your free time?
Toolworking. I enjoy woodworking. But now I don't have free time, I go to school for master’s program. It takes a 100% of my free time. Also, in December I will have another hobby! My second child will be born. I'm excited about that! Kids are the full time job! I will have a two month work break and for that time everything else will be a hobby.