Mongrel Handlers
-
Upload
vishnu -
Category
Technology
-
view
2.953 -
download
2
description
Transcript of Mongrel Handlers
![Page 1: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/1.jpg)
Who am I?
• Ezra Zygmuntowicz
• Rubyist for 4 years
• Engine Yard Founder and Architect
• Blog: http://brainspl.at
![Page 2: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/2.jpg)
MongrelLearning how to walk the dog
![Page 3: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/3.jpg)
What is Mongrel?
![Page 4: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/4.jpg)
Mongrel is an HTTP Server Library written by Zed Shaw
• Fast HTTP Parser written in Ragel + C
• Fast URI Classifier written in C
• Stackable Request Handlers
• Flexible Configuration
• Secure and RFC Compliant HTTP Parser
![Page 5: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/5.jpg)
Ragel State Machine Defined HTTP Parser
![Page 6: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/6.jpg)
Why?
![Page 7: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/7.jpg)
FastCGI is Poop
![Page 8: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/8.jpg)
• HTTP is a well known and well tooled protocol
• Mongrel is way easier to setup and use
• Transparent wire protocol
![Page 9: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/9.jpg)
But Rails isn’t Thread Safe!
• Giant Mutex Lock around Rails Dispatch
• Only one request served at a time by one mongrel
• Use mongrel_cluster to scale with multiple processes
![Page 10: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/10.jpg)
• Request comes into gateway server
• Rewrite rules are evaluated and request gets served directly if it’s a static asset
• Dynamic requests are proxied to one Mongrel in the Mongrel Cluster
• Mongrel dispatches request through Rails and returns response to client
Full Stack Request/Response Life-Cycle
![Page 11: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/11.jpg)
• Mongrel Locks Mutex
• Rails Dispatcher is invoked with request/response objects
• Routing is invoked and returns the proper Controller object or 404 if no route found
• Filter chain is invoked
• Controller’s Action is called, manipulates Models
• View is rendered and any after filters are called
• Mongrel Unlocks Mutex
• Final response or error page returned to client
Rails Internal Request/Response Life-Cycle
![Page 12: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/12.jpg)
Mongrel != Rails
• Mongrel *is* Thread Safe
• Mongrel is capable of much more then just running Rails
• Rails is beautiful for the 80% part of the 80/20 rule
• What to do when your app needs the other 20%?
![Page 13: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/13.jpg)
Handle It!
• Building Mongrel Handlers is easier then you think
• Mongrel is *very* high performance
• Ruby not so slow after all? Maybe it’s just Rails that is slow?
![Page 14: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/14.jpg)
Rails vs Mongrel Handler in a Hello World Battle
Rails:
Mongrel Handler:
![Page 15: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/15.jpg)
Naive Benchmarks
Mongrel Handler:
Rails:
7Mb RAM
35Mb RAM
![Page 16: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/16.jpg)
Why the Huge Difference?
• Of course Rails provides much more out of the box
• Our HelloHandler runs in a multi-threaded way, hence the 100 concurrent users in our benchmark
• Rails has to run requests in serial, hence the 1 concurrent user.
![Page 17: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/17.jpg)
What can Custom Mongrel Handlers do for me?
• More concurrent users on fewer processes
• Higher throughput with less resources
• Less convenience for developers means you don’t need it until you *need* it
![Page 18: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/18.jpg)
Standalone or Integrated with Rails?• Mongrel Handlers can ‘Stack’
• You can use :in_front => true to put a custom Mongrel Handler in process with your Rails app and have it intercept and serve certain urls
• Access to your Rails models and other loaded classes
![Page 19: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/19.jpg)
Integrating HelloHandler into our Rails app
![Page 20: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/20.jpg)
Another Useless Benchmark
![Page 21: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/21.jpg)
Real World ExampleSecureFile
![Page 22: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/22.jpg)
The next logical step?
• Gee, Mongrel without Rails is hella fast
• I’ll start writing more and more of my apps as Handlers
• I’ll start implementing the parts of Rails I need in my handlers...
![Page 23: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/23.jpg)
MerbStarted as a hack, Merb == Mongrel + Erb
• No CGI.rb !!
• Clean room implementation of ActionPack
• Thread Safe with configurable Mutex Locks
• Rails compatible REST routing
• No Magic( well less anyway ;)
• It’s what you will end up with if you keep writing custom Mongrel Handlers
![Page 24: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/24.jpg)
Dispatching Rails vs Merb
Request comes in
**Mutex gets locked**
Parse CGI + Mime(expensive)
Route recognition(expensive)
Before Filter Chain
Call Controller Action
Render Template
**Mutex Unlocked**
Results returned to client
Request comes in
Parse CGI + Mime
Route recognition
**Mutex gets locked**
Before Filter Chain
Call Controller Action
Render Template
**Mutex Unlocked**
Results returned to client
Rails Merb(with ActiveRecord)
![Page 25: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/25.jpg)
Merb Hello World
![Page 26: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/26.jpg)
Routing• Rails routing is 1800 lines of *very
complex* non thread-safe code
• Merb’s router is 200 lines of complex but *thread safe* code and much more efficient for route matching
![Page 27: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/27.jpg)
Why should you care?
• Rails has gotten 10-20% slower with each recent release
• Resident RAM usage per process gets larger as well
• Premature Optimization is blah, blah
• At this point it is not premature anymore ;)
• There is a point where optimization Matters
![Page 28: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/28.jpg)
Merb’s Mongrel Handler
![Page 29: Mongrel Handlers](https://reader034.fdocuments.in/reader034/viewer/2022052504/55492f71b4c90547498c2a95/html5/thumbnails/29.jpg)
Questions?