
Ruby Concurrency Final Report - pvsukale3
https://www.codeotaku.com/journal/2020-04/ruby-concurrency-final-report/index
======
thibaut_barrere
ioquatix (Samuel Williams, the author) is doing tons of great work for Ruby at
the moment. You'll want to check out, for instance:

\- [https://github.com/socketry/async](https://github.com/socketry/async) "An
awesome asynchronous event-driven reactor for Ruby"

\- [https://github.com/socketry/falcon](https://github.com/socketry/falcon) "A
high-performance web server for Ruby, supporting HTTP/1, HTTP/2 and TLS"

I can recommend following him on twitter too:
[http://twitter.com/ioquatix](http://twitter.com/ioquatix)

~~~
giu
In the linked article, the author also included some videos of himself
implementing some of the presented parts e.g. exclusive Fibers [0], including
writing the necessary C code using the Ruby C API.

Recording yourself writing code and then publishing it takes quite a lot of
courage, even moreso if it's a complex topic such as the one presented.
Additionally, it's such a valuable resource: You can follow the whole
implementation step by step.

That's just awesome.

[0]
[https://www.youtube.com/watch?v=cNaqbeagqUw](https://www.youtube.com/watch?v=cNaqbeagqUw)

~~~
djohnston
This is really cool. Do you have any recs for similar vid of someone hacking
on c python API? I want to gain a better understanding of how these langs
interface w their implementation, but Ruby isn't really in my wheelhouse.

~~~
ioquatix
The internals aren't THAT different, so while I can't really answer your
question (I'm not working on CPython), you might still find it interesting
from the POV as to how interpreters work.

------
pantulis
This is a great read and also provides useful links to get up to date to the
state of MT and concurrency in Ruby-world.

------
ketamine__
I've run into an issue where a Ruby library is technically thread-safe but
there is some C library code that blocks (i.e. connect with TinyTds). This
makes it practically unusable with threading under special scenarios where
your SQL servers are not guaranteed to be available.

~~~
matharmin
Thread-safe just means it won't result in data corruption, race conditions or
other similar issues when used from multiple threads. That's still quite
different from being able to execute concurrently.

~~~
k__
haha, in fact "not being able to execute concurrently" is the easiest way "to
ensure thread-safety" :D

~~~
ioquatix
Actually, completely sequential programs can have parallelism bugs due to the
underlying hardware being out of order and other such features. I know, maybe
a bit of a stretch by your definitions, but I'm also not sure that "not being
able to execute concurrently" implies "thread safety" in practice (even though
theoretically pretty sound).

~~~
BubRoss
That would not be a bug in the software, that would be a bug in the CPU.
Still, I haven't ever heard of this actually happening, do you have a link?

~~~
ioquatix
Yes, agreed, all I'm trying to say is, as software engineers we have some
assumptions about processors (and compilers/interpreters) that they do things
in the order we specify... but that's not always the case, and sometimes that
introduces non-determinism.

Processors (and compilers) are not supposed to leak the implications of their
non-determinism into user code... but unfortunately it does happen. Rewriting
the order of instructions sometimes happens incorrectly (which is why we need
memory barriers).

It's not my area, but two things that come to mind, are out-of-order execution
where the CPU will re-order micro-ops, and speculative execution, where the
CPU will execute one or more branches before the result of a conditional is
known.

Both of these issues have lead to bugs in the past and will continue to be a
source of bugs in the future.

[https://en.wikipedia.org/wiki/Spectre_(security_vulnerabilit...](https://en.wikipedia.org/wiki/Spectre_\(security_vulnerability\))

~~~
BubRoss
None of that has anything to do with software bugs, it doesn't change the
behavior of a program. People can understand that or not, it doesn't affect
the correctness of the software.

------
vadasambar
I get connection timeout when I try to load the page :(

~~~
ioquatix
As you can imagine, deploying a new technology to production can have issues
:p

I've been dogfooding falcon
([https://github.com/socketry/falcon](https://github.com/socketry/falcon)) for
the past week, which is built on top of async/Ruby. The HN hug of death +
Reddit hug of death is a really great traffic test.

I think actually it's been pretty solid, but something caused the instance to
run out of swap space, even though it had plenty of free memory. It's
something I'll have to try and reproduce so I can understand how it's
happening.

------
NickGerleman
I haven't touched Ruby in a while, but are there any common multithreaded use
cases? It seemed like the direction was to go multi-process for web workloads
(E.g. with Unicorn).

~~~
ioquatix
Those are both solid choices for servers. However neither of them have a
scalability model suitable for HTTP/2 or WebSockets. That's something I wanted
to try and address.

~~~
timdorr
And thank you for addressing it, Samuel!

I converted the streaming and Autopark clients in the Tesla API gem from EM to
Async and has been great: [https://github.com/timdorr/tesla-
api/blob/master/lib/tesla_a...](https://github.com/timdorr/tesla-
api/blob/master/lib/tesla_api/stream.rb)

No more globals, which makes it much safer to use inside of threaded job
runners like Sidekiq.

~~~
ioquatix
It would be awesome to talk about your work in a blog post, do you think you
can reach out to me to explain your work in more detail?

------
bepvte
This thin font is very difficult to read

~~~
ioquatix
Welcome suggestions for alternatives.

~~~
bloudermilk
Bootstrap adopted a native font stack that looks great and doesn't require any
web fonts.

[https://getbootstrap.com/docs/4.0/content/reboot/#native-
fon...](https://getbootstrap.com/docs/4.0/content/reboot/#native-font-stack)

~~~
ptx
Is "adopting a native font stack" different in some way from simply not using
web fonts and doing it the old-fashioned way instead?

Edit: Other than using the magic font names "-apple-system" and
"BlinkMacSystemFont" it looks like it's just specifying the Windows, Android
and Mac fonts in order. This is not going to use the native system font on
other platforms or if the user happens to have Segoe UI installed on Android.

------
nickbauman
Working complex systems develop from working simple systems. This is trying to
get a working complex system by creating another initial working complex
system. If only ruby didn't have that darned assignment operator...

~~~
ioquatix
I definitely agree with your first statement. However, I think the Fiber +
Reactor approach is about as simple as it gets, taking into consideration
actual practical, scalable, concurrency. Every approach has trade offs, but I
think this design is pretty good. My goal was to build enough of the stack to
prove that.

