lets build-ruby-app-server: vineet tyagi
DESCRIPTION
Would you like to know how to build an application server from scratch? This talk would provide an insight to the thought process and the key decisions made while building WebROaR from grounds up using C & Ruby. What enables this server to deliver high performance and also offer a rich bouquet of integrated features like Analytics, Exception Notifications etc? If gaining knowledge about design of a good software product interests you, do join us for this interactive session.TRANSCRIPT
How do ‘you’ visualize an ‘Application Server’?
Or it could be like this …
Let’s define the specs
Run all Ruby Web Applications
Rack Compliant
And also ..
Provide run time performance numbers of
the deployed applications
Report exceptions
Love to have …
Maximum performance
Simplicity
Let’s start building it …
What does the server need to do?
Accept client connection
Parse HTTP request
Hand it over to the ruby web framework
Send its response back to the client
Network Server Design
Evented vs Threaded
Event Based I/O Libraries
libev (Written in C)
EventMachine (Ruby Module + Extension
Library written in C++)
HTTP Parser
Mongrel’s Ragel Based Parser (written in C)
is ‘awesome’
Accept client connection
Parse HTTP request
Hand it over to the ruby web
framework
Send its response back to the
client
libev/EventMachine
Mongrel’s Ragel based Parser
?
libev/EventMachine
OptionsServer Tasks
More awesomeness - Libebb
Lightweight HTTP Library for C
libev + Mongrel’s Ragel based Parser
Implements HTTP/1.1 Grammar
Supports persistent, pipelined & chunked requests
Rejects invalid and malicious requests
Accept client connection
Parse HTTP request
Hand it over to the ruby web
framework
Send its response back to the
client
libebb
libebb
?
libebb
OptionsServer Tasks
Embedded Ruby Interpreter
#include <ruby.h>
int main(int argc, char **argv) {int state = 0;
ruby_init();
ruby_script("c-executable-name");
ruby_init_loadpath();
//Can use rb_gv_set() or rb_iv_set() methods to pass parameters to ruby land
rb_protect(RUBY_METHOD_FUNC(rb_require), (VALUE)"path-to-ruby-script", &state);
if(state != 0) {
// Exception Handling Code
return -1;
}
//Can use rub_funcall() to invoke methods in the ruby land
return 0;
}
Accept client connection
Parse HTTP request
Hand it over to the ruby web
framework
Send its response back to the
client
libebb
libebb
Embedded Ruby Interpreter
libebb
OptionsServer Tasks
Hola, we got a basic application server ready!
The Server – First Cut
libebb
Ruby Interpreter
Rack Web Application
Response
HTTP Request
But it only processes one request at a time ..
Let’s split up the server code and create more
ruby processing instances & static file
renderers (‘workers’)
The more powerful server version …
Response
HTTP Request
libebb libev
Head
Ruby Interpreter
Rack Web Applicationlibev
Worker #1
Ruby Interpreter
Rack Web Applicationlibev
Worker #2
Static Files Rendererlibev
Worker #n
Asynchronous Non-blocking I/O
SCGI
Server can handle (very) high load with this
architecture
Let’s capture data for each request being
processed …
The Server – Current Architecture
Response
HTTP Request
Head
Worker #n
Worker #1
DB
Message Queue
Request Analytics Data &
Exception Notifications
Analyzer DaemonAdmin Panel
Conf File
Hurray, we can write all the new advanced
features in our favorite language ..
Ruby makes life easier for …
Instrumentation
Messaging
Analyzer
Interacting with the Database
Admin Panel (It’s a Rails Application!)
Takeaways
Use the best tool for the job
C – Network I/O, Ruby – Advanced Features
Do not reinvent the wheel. Use tried and
tested libraries
Check it out
WebROaR – http://webroar.in
Photo Credits
All photographs used in this presentation are licensed under Creative Commons
Slide 3 – Stone House By Jsome1
Slide 5, 34 – Our House By Lee Coursey
Slide 11 – Willow Lane Construction By Lee Coursey
Slide 22 – Construction of our House – Framing By Lee Coursey
Slide 27 – Dead Lift By scarndp
Slide 29 – Willow Lane Construction By Lee Coursey
Slide 35 – Very Happy By Chris Winters
Slide 38 – Lion By auburnxc
More Questions?