Truly Madly Deeply Parallel
Ruby (Web)* Applications
@harikrishnan83
* Thanks @headius
How many of you are
building web applications?
How many of you have
more than 1 user using it at
a time?
How many of you use scaling out as the
way hit scale?
How many of you are scaling up to hit
scale?
What is a parallel
environment?
Parallelism
Concurrency
Thread A - load
Thread B - load
Thread A - increment
Thread B - increment
Thread A - save
Thread B - save
i = ...
Ruby web application
deployment
Process Parallelism
Reverse Proxy Unicorn Processes Database Layer
On my current project
We only use EC2 small
instances
Because it is very hard to
utilize a high spec machine
Process Context Switch is Expensive
Today...
● We have 4 small EC2 instances
● 2 Puma processes run on each
● Handles about 100,000 requests per hour
● And th...
We need to...
● Handle about 1 million requests per hour
● Which means about 40-45 EC2 small
instances
This is not trivial
● Costs a lot of money
● Lot of time required to maintain these boxes
● Being elastic will become very...
In general
It is easier to baby sit few boxes
Than a lot!
Ideally, we would like to
both scale up and scale out
i.e. we want to achieve the
same throughput with, say,
just 5 large instances
Enter thread based
parallelism
Why were we not doing this
till now?
Threads are hard*
They share memory and mutate thingsThey share memory and mutate things
* - Supposedly
And there is the ubiquitous
issue
‘Thread Safety’
Before we go there, first lets
look at some code
The real question is...
Are you
“Safe for Parallelization”
Understanding this will take
you a long way in “getting
parallel”
Things to remember while
moving to threaded
parallelism
#1 - Always identify the
shared resources
Shared Resource
● Objects
● DB rows
● Caches
● Log files!
#2 - Bank on thread safe
libraries
Libraries
● Data structures
● JSON, XML parsing, HTTP clients etc
● Generally, auditing all the gems you use for
thread sa...
If you only use thread safe
libraries are you ‘safe for
parallelism’?
Rails is thread safe right?
Why is everyone concerned
about thread safety in the
first place?
#3 - If two libraries are
thread safe, code that uses
both of them need not be
Rails thread safety model
● Instantiate everything for every request
● No shared state (global objects)
● Different from, ...
#4 - Try and stick to Rails’
way of handling requests
Are you ‘Safe for
parallelism’ if you follow
these steps?
Well, it depends...
Validating, say, through a
green bar is very hard.
Always give yourself some
time to stabilize.
The move is definitely not
overnight!
Speaking of the move,
move where?
Since Rubinius is mostly
MRI like, its simpler
I personally love JRuby
more because of my JVM
background
Lots of good things have
been spoken about JRuby
Some gotchas based on my
experience
JRuby impacts Developers
● The JRuby startup time (mostly because of
the JVM startup time) can sometimes kill red-
green c...
JRuby impacts OPs
● You no longer have a ruby app in prod, its a
Java app
● GC tuning, Process monitoring, Profiling etc.
...
Thread Parallelism
Reverse Proxy Puma Instance Database LayerThreads
Thank you!
@harikrishnan83
Truly madly deeply parallel ruby applications
Upcoming SlideShare
Loading in …5
×

Truly madly deeply parallel ruby applications

305 views
249 views

Published on

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
305
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Truly madly deeply parallel ruby applications

  1. 1. Truly Madly Deeply Parallel Ruby (Web)* Applications @harikrishnan83 * Thanks @headius
  2. 2. How many of you are building web applications?
  3. 3. How many of you have more than 1 user using it at a time?
  4. 4. How many of you use scaling out as the way hit scale?
  5. 5. How many of you are scaling up to hit scale?
  6. 6. What is a parallel environment?
  7. 7. Parallelism
  8. 8. Concurrency Thread A - load Thread B - load Thread A - increment Thread B - increment Thread A - save Thread B - save i = 0 i = 0 i = 1 i = 1
  9. 9. Ruby web application deployment
  10. 10. Process Parallelism Reverse Proxy Unicorn Processes Database Layer
  11. 11. On my current project
  12. 12. We only use EC2 small instances
  13. 13. Because it is very hard to utilize a high spec machine Process Context Switch is Expensive
  14. 14. Today... ● We have 4 small EC2 instances ● 2 Puma processes run on each ● Handles about 100,000 requests per hour ● And this is our Private alpha
  15. 15. We need to... ● Handle about 1 million requests per hour ● Which means about 40-45 EC2 small instances
  16. 16. This is not trivial ● Costs a lot of money ● Lot of time required to maintain these boxes ● Being elastic will become very important ● Cost also in terms of more Devops time
  17. 17. In general
  18. 18. It is easier to baby sit few boxes
  19. 19. Than a lot!
  20. 20. Ideally, we would like to both scale up and scale out
  21. 21. i.e. we want to achieve the same throughput with, say, just 5 large instances
  22. 22. Enter thread based parallelism
  23. 23. Why were we not doing this till now?
  24. 24. Threads are hard* They share memory and mutate thingsThey share memory and mutate things * - Supposedly
  25. 25. And there is the ubiquitous issue ‘Thread Safety’
  26. 26. Before we go there, first lets look at some code
  27. 27. The real question is...
  28. 28. Are you “Safe for Parallelization”
  29. 29. Understanding this will take you a long way in “getting parallel”
  30. 30. Things to remember while moving to threaded parallelism
  31. 31. #1 - Always identify the shared resources
  32. 32. Shared Resource ● Objects ● DB rows ● Caches ● Log files!
  33. 33. #2 - Bank on thread safe libraries
  34. 34. Libraries ● Data structures ● JSON, XML parsing, HTTP clients etc ● Generally, auditing all the gems you use for thread safety is a good idea
  35. 35. If you only use thread safe libraries are you ‘safe for parallelism’?
  36. 36. Rails is thread safe right? Why is everyone concerned about thread safety in the first place?
  37. 37. #3 - If two libraries are thread safe, code that uses both of them need not be
  38. 38. Rails thread safety model ● Instantiate everything for every request ● No shared state (global objects) ● Different from, say, Java (single servlet object per container, IOC with singletons etc.)
  39. 39. #4 - Try and stick to Rails’ way of handling requests
  40. 40. Are you ‘Safe for parallelism’ if you follow these steps?
  41. 41. Well, it depends...
  42. 42. Validating, say, through a green bar is very hard.
  43. 43. Always give yourself some time to stabilize. The move is definitely not overnight!
  44. 44. Speaking of the move, move where?
  45. 45. Since Rubinius is mostly MRI like, its simpler
  46. 46. I personally love JRuby more because of my JVM background
  47. 47. Lots of good things have been spoken about JRuby
  48. 48. Some gotchas based on my experience
  49. 49. JRuby impacts Developers ● The JRuby startup time (mostly because of the JVM startup time) can sometimes kill red- green cycle time ● Sometimes, you should be OK with stooping down to Java code to figure out why something is not working
  50. 50. JRuby impacts OPs ● You no longer have a ruby app in prod, its a Java app ● GC tuning, Process monitoring, Profiling etc. are very different on a JVM
  51. 51. Thread Parallelism Reverse Proxy Puma Instance Database LayerThreads
  52. 52. Thank you! @harikrishnan83

×