Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's "wrong" with Ruby threads?

What's "wrong" with Ruby threads?

Rostislav Zhuravsky

March 15, 2021
Tweet

More Decks by Rostislav Zhuravsky

Other Decks in Technology

Transcript

  1. OR

  2. All you need to know for answering the question “What’s

    WRONG with Ruby threads?” at technical interview
  3. Agenda • Missed computer science lectures • Concurrency vs parallelism

    • Are ruby threads real? • Ways to implement concurrency in Ruby • Are ruby threads even useful?
  4. What is a process? Process is an independent instance of

    a program in execution. It consist of state context(which is about allocated memory, opened file descriptors) and execution context(which is about sequence of instructions that should be performed). A run process has at least one thread.
  5. What is a thread? A thread is a basic unit

    of CPU utilization, consisting of a program counter, a stack, and a set of registers. Basically, it’s a set of commands that can be executed by a core. Threads share a process’ memory, opened files. One process can have several threads.
  6. • 4 cores • 398 processes a.k.a tasks • 1782

    threads - 1 running How many threads can be run at once on my laptop?
  7. What is a web server? A web server is a

    program which is working constantly. Since the moment you started a web server, it listens to a port or a socket, reads request data, processing it, and writes response data.
  8. Concurrency is about dealing with lots of things at once.

    Parallelism is about doing lots of things at once. Rob Pike.
  9. About concurrency management There are two types of languages: 1.

    That map language-level threads directly to OS threads(Ruby, Java). Basically, it means if you run 100 threads in your app it will create 100 threads in OS. And OS is responsible for their scheduling. You can only set priorities. 2. That have their own concurrency abstraction levels and their own schedulers(Go, Erlang, Elixir). If you create 100 threads it won’t create the same amount. It depends how your VM is configured.
  10. The answer is Ruby threads are real threads. But why

    does everybody tell that Ruby can’t do concurrency?
  11. You need to understand that Ruby is a program itself

    which written in C. It reads source files, tokenizes, parses them, compiles and puts into Ruby VM execution stack. It keeps info about everything existing in your program, variables, constants, classes.
  12. Ruby doesn’t control the context switching in OS. It can

    only set priorities for each thread but it can’t control the moment of actual switching.
  13. GIL helps Ruby to keep its internal state incorrupt between

    context switching. It makes Ruby thread-safe internally but it doesn’t guarantee that your programs will be thread safe. So, the only thread can work per a unit of time
  14. So, if the only thread can work at a unit

    of time, can threads even be useful?
  15. The possible reasons why thread-based gems work faster than process-based

    • Synchronization between threads are much easier because threads share the single memory • Design, it’s possible that sidekiq’s algorithms are written much better • Something else...
  16. What is it? Every time, you write/read files, sockets, ports,

    call external commands you do IO. Real world IO examples: • Working with files. • Listening to ports/sockets • Making HTTP calls • Querying DG(Postgres, MySQL, Redis, Elastic)
  17. Summary • Ruby threads are real threads of OS •

    Ruby can’t control the context switch, it can only provide hints for OS • GIL is a mechanism which helps Ruby VM not to be corrupted between context switches • GIL makes the Ruby itself thread-safe but it doesn’t make our code thread safe • Threads are beneficial when you do a lot of IO because Ruby VM uses async IO.