2. Applica@on
Concurrency
• Event-‐Driven
I/O
(Ex.
Eventmachine,
goliath)
– Everything
need
to
be
non-‐blocking.
• Worker
Threads
(Ex.
Rainbows
with
proper
config)
– Everything
need
to
be
thread-‐safe.
• Worker
Processes
(Ex.
unicorn)
– OS
process
is
expensive
– Reality:
You
are
using
a
lot
of
3rd
party
libraries,
some
of
them
are
blocking
and
not
thread-‐safe
– S@ll
mainstream
today
– I
don’t
like
it
very
much,
but
it
just
works
4. Worker
Processes
Memory
Usage
80MB
80MB
80MB
80MB
80MB
80MB
80MB
80MB
Process
Process
Process
Process
Process
Process
Process
Process
1
2
3
4
5
6
7
8
5. Forked
Worker
Processes
(Ex.
Unicorn)
Memory
Usage:
Copy
on
Write
God
Monit
Master
Process
Process
Process
Process
Process
Process
Process
Process
Process
1
2
3
4
5
6
7
8
Normal
Memory
Page
(CoW)Read-‐Only
Page
6. Worker
Processes
Startup
Time
Process
1
Process
2
Process
3
Process
4
Process
5
Process
6
Process
7
Process
8
Time
• Assuming
2
cores
• The
8
processes
could
be
started
simultaneously
and
the
startup
will
be
execute
concurrently
with
content
switching.
7. Forked
Worker
Processes
(Ex.
Unicorn)
Startup
Time
Master
Process
Loading
environment
Process
1
Process
2
Process
3
Process
4
Forking
Process
5
Process
6
Process
7
Process
8
Time
• Load
the
applica@on
ONCE
• Fork
is
FAST
8. Resque
• The
master
process
loads
the
environment.
• Whenever
get
a
new
job,
fork
a
worker
process.
• Whenever
the
job
is
done,
the
worker
process
exits.
• For
isola@on:
Resque
assumes
chaos
Worker
Got
a
job!
Process
Job
done
Fork
Process
Worker
process
terminates
Master
Process
9. 50
resque
workers?
• Don’t
do
this:
– COUNT=50
QUEUE=*
rake
resque:workers
• 80MB*50
=
4GB
Worker
Worker
Worker
Process
Process
Process
Master
Master
Master
Master
Master
Master
…
Process
Process
Process
Process
Process
Process
80
MB
80
MB
80
MB
80
MB
80
MB
80
MB
11. Summary
• Process
forking
in
Ruby
– For
isola@on
• Prevent
process
status
corrup@on
• See
also:
spork-‐rails
– For
concurrency
• Faster
startup
• Smaller
memory
footprint