This document summarizes Kyle Drake's presentation on using EventMachine (EM) and EM::Synchrony to build fast, concurrent Ruby web applications without blocking I/O or callback hell. Key points include:
- EM implements the reactor pattern to handle blocking I/O without threads by using callbacks and kernel threads.
- EM::Synchrony avoids callback nesting by wrapping callbacks in fibers, allowing synchronous-looking code.
- Sinatra can be made concurrent by running each request in its own EM::Synchrony fiber with little code change.
- Ruby has strong options for building high-performance concurrent apps while maintaining productivity advantages over Node.js.
Ruby developers need to stop using EventMachine. It's the wrong direction.
Lost in the "Threads vs Event Driven vs Process Spawning" debate is that you can combine them! Learn how Celluloid is improving thread programming by abstracting them using a higher level framework called Celluloid, how you can use Celluloid::IO to throw a reactor pattern into a thread. Using this approach, you can take advantage of threading and use all CPU power on a machine with JRuby or Rubinius. I also discuss the future of distributed objects and computing, and where I think things are going.
No Callbacks, No Threads - RailsConf 2010Ilya Grigorik
Multi-threaded servers compete for the global interpreter lock (GIL) and incur the cost of continuous context switching, potential deadlocks, or plain wasted cycles. Asynchronous servers, on the other hand, create a mess of callbacks and errbacks, complicating the code. But, what if, you could get all the benefits of asynchronous programming, while preserving the synchronous look and feel of the code – no threads, no callbacks?
Real time applications are here and users expecting that Real time data is part of UX. What are your options for building RTA with Symfony2/PHP? Slides from Symfony Camp Ukraine.
Ruby developers need to stop using EventMachine. It's the wrong direction.
Lost in the "Threads vs Event Driven vs Process Spawning" debate is that you can combine them! Learn how Celluloid is improving thread programming by abstracting them using a higher level framework called Celluloid, how you can use Celluloid::IO to throw a reactor pattern into a thread. Using this approach, you can take advantage of threading and use all CPU power on a machine with JRuby or Rubinius. I also discuss the future of distributed objects and computing, and where I think things are going.
No Callbacks, No Threads - RailsConf 2010Ilya Grigorik
Multi-threaded servers compete for the global interpreter lock (GIL) and incur the cost of continuous context switching, potential deadlocks, or plain wasted cycles. Asynchronous servers, on the other hand, create a mess of callbacks and errbacks, complicating the code. But, what if, you could get all the benefits of asynchronous programming, while preserving the synchronous look and feel of the code – no threads, no callbacks?
Real time applications are here and users expecting that Real time data is part of UX. What are your options for building RTA with Symfony2/PHP? Slides from Symfony Camp Ukraine.
“Node's goal is to provide an easy way to build scalable Network programs”
Asynchronous i/o framework
Core in c++ on top of v8
Rest of it in javascript
Swiss army knife for network Related stuffs
Can handle thousands of Concurrent connections with Minimal overhead (cpu/memory) on a single process
It’s NOT a web framework, and it’s also NOT a language
• Created by Ryan Dahl in 2009
• Development && maintenance sponsored by Joyent
• License MIT
• Last release : 0.10.31
• Based on Google V8 Engine
• +99 000 packages
Grand Rapids PHP Meetup: Behavioral Driven Development with BehatRyan Weaver
Testing our applications is something we all do. Ahem, rather, it's something we all *wish* we did. In this chat, I'll introduce you to Behat (behat.org) (version 3!!!!): a behavior-driven-development (BDD) library that allows you to write functional tests against your application just by writing human-readable sentences/scenarios. To sweeten the deal these tests can be run in a real browser (via Selenium2) with just the flip of a switch. If you asked me to develop without Behat, I'd just retire. It's that sweet. By the end, you'll have everything you need to start functionally-testing with Behat in your new, or very old and ugly project.
Zepto and the rise of the JavaScript Micro-FrameworksThomas Fuchs
Here are my slides from JSConf 2011 in Portland, Oregon. I was talking about Zepto, my micro-framework, WebKit-only (works on Firefox too, though) clone of the jQuery API, and why I think that Micro-Frameworks and Micro-Libraries are better suited for the mobile Web than traditional, monolithic libraries, like script.aculo.us, Prototype or jQuery. (A video of the talk is forthcoming and will be announced on my blog, http://mir.aculo.us).
Slides from my workshop about node.js which I conducted in Girl Geek Dinner Bangalore. More details at http://sudarmuthu.com/blog/introduction-to-node-js-at-yahoo-girl-geek-dinner
How to run system administrator recruitment process? By creating platform based on open source parts in just 2 nights! I gave this talk in Poland / Kraków OWASP chapter meeting on 17th October 2013 at our local Google for Entrepreneurs site. It's focused on security and also shows how to create recruitment process in CTF / challenge way.
This story covers mostly security details of this whole platform. There's great chance, that I will give another talk about this system but this time focusing on technical details. Stay tuned ;)
This is a presentation for International Lisp Conference 2012 which was held in Kyoto, Japan.
Clack is a web application environment for Common Lisp to make your web applications be portable and reusable by abstracting HTTP into a simple API.
In this paper, I describe what are problems in web development and how Clack solves them.
Developing high-performance network servers in LispVladimir Sedach
Overview of current high-performance Common Lisp web servers and implementation techniques, and description of a new hybrid approach to asynchronous I/O based on separate racing accept() and epoll() thread pools.
EC2 AMI Factory with Chef, Berkshelf, and PackerGeorge Miranda
Presentation accompanying a Live Demo at the AWS Pop-Up Loft in San Francisco on using Chef + Berks + Packer to create an AWS EC2 AMI Factory.
Demo Repo available here -- https://github.com/gmiranda23/chef-ami-factory
Ansible: How to Get More Sleep and Require Less CoffeeSarah Z
Why you need automation, configuration management and remote execution in your life. An intro to Ansible and how it can make your life in Ops infinitely easier.
Drupal 8: Huge wins, a Bigger Community, and why you (and I) will Love itRyan Weaver
It's true: Drupal 8 includes big and exciting changes to its core and how Drupal code is written. These include using outside PHP libraries (Guzzle, Symfony, etc) as well as embracing PHP 5.4 and object-oriented code.
Scary, right? Definitely not! These changes give Drupal 8 so many "wins" and new possibilities that you're going to absolutely love it.
In this keynote, we'll explore the changes together and start to realize all of the wonderful things that each will bring. These include a (much) larger community, many more libraries you can use, higher quality tools, easier future upgrades, and some fantastic new features.
And because Drupal uses Symfony, you'll easily be able to use Symfony (or its little brother Silex) for any non-CMS projects. Being a great Drupal 8 developer means being a great PHP developer.
Of course, Drupal 8 has its criticisms: it will be too hard for new developers to learn, or it will not be useable for smaller sites. We'll take on these concerns directly and see why they are valid, but exaggerated.
An introduction about JavaScript web workers I gave at BerlinJS on the 18th of July 2013. It introduces the concept of web workers for simple parallel processing in client side JavaScript.
An absolute beginners guide to node.js . Done for a presentation at college. The presentation contains data from various sources ,sources are noted at the end slide. please inform me any mistakes ,since at that time i was in a bit of hurry :)
Patrick Chanezon and Guillaume Laforge are presenting Google App Engine Java and Gaelyk, the lightweight groovy toolkit on top of the GAE SDK, at the Devoxx conference
“Node's goal is to provide an easy way to build scalable Network programs”
Asynchronous i/o framework
Core in c++ on top of v8
Rest of it in javascript
Swiss army knife for network Related stuffs
Can handle thousands of Concurrent connections with Minimal overhead (cpu/memory) on a single process
It’s NOT a web framework, and it’s also NOT a language
• Created by Ryan Dahl in 2009
• Development && maintenance sponsored by Joyent
• License MIT
• Last release : 0.10.31
• Based on Google V8 Engine
• +99 000 packages
Grand Rapids PHP Meetup: Behavioral Driven Development with BehatRyan Weaver
Testing our applications is something we all do. Ahem, rather, it's something we all *wish* we did. In this chat, I'll introduce you to Behat (behat.org) (version 3!!!!): a behavior-driven-development (BDD) library that allows you to write functional tests against your application just by writing human-readable sentences/scenarios. To sweeten the deal these tests can be run in a real browser (via Selenium2) with just the flip of a switch. If you asked me to develop without Behat, I'd just retire. It's that sweet. By the end, you'll have everything you need to start functionally-testing with Behat in your new, or very old and ugly project.
Zepto and the rise of the JavaScript Micro-FrameworksThomas Fuchs
Here are my slides from JSConf 2011 in Portland, Oregon. I was talking about Zepto, my micro-framework, WebKit-only (works on Firefox too, though) clone of the jQuery API, and why I think that Micro-Frameworks and Micro-Libraries are better suited for the mobile Web than traditional, monolithic libraries, like script.aculo.us, Prototype or jQuery. (A video of the talk is forthcoming and will be announced on my blog, http://mir.aculo.us).
Slides from my workshop about node.js which I conducted in Girl Geek Dinner Bangalore. More details at http://sudarmuthu.com/blog/introduction-to-node-js-at-yahoo-girl-geek-dinner
How to run system administrator recruitment process? By creating platform based on open source parts in just 2 nights! I gave this talk in Poland / Kraków OWASP chapter meeting on 17th October 2013 at our local Google for Entrepreneurs site. It's focused on security and also shows how to create recruitment process in CTF / challenge way.
This story covers mostly security details of this whole platform. There's great chance, that I will give another talk about this system but this time focusing on technical details. Stay tuned ;)
This is a presentation for International Lisp Conference 2012 which was held in Kyoto, Japan.
Clack is a web application environment for Common Lisp to make your web applications be portable and reusable by abstracting HTTP into a simple API.
In this paper, I describe what are problems in web development and how Clack solves them.
Developing high-performance network servers in LispVladimir Sedach
Overview of current high-performance Common Lisp web servers and implementation techniques, and description of a new hybrid approach to asynchronous I/O based on separate racing accept() and epoll() thread pools.
EC2 AMI Factory with Chef, Berkshelf, and PackerGeorge Miranda
Presentation accompanying a Live Demo at the AWS Pop-Up Loft in San Francisco on using Chef + Berks + Packer to create an AWS EC2 AMI Factory.
Demo Repo available here -- https://github.com/gmiranda23/chef-ami-factory
Ansible: How to Get More Sleep and Require Less CoffeeSarah Z
Why you need automation, configuration management and remote execution in your life. An intro to Ansible and how it can make your life in Ops infinitely easier.
Drupal 8: Huge wins, a Bigger Community, and why you (and I) will Love itRyan Weaver
It's true: Drupal 8 includes big and exciting changes to its core and how Drupal code is written. These include using outside PHP libraries (Guzzle, Symfony, etc) as well as embracing PHP 5.4 and object-oriented code.
Scary, right? Definitely not! These changes give Drupal 8 so many "wins" and new possibilities that you're going to absolutely love it.
In this keynote, we'll explore the changes together and start to realize all of the wonderful things that each will bring. These include a (much) larger community, many more libraries you can use, higher quality tools, easier future upgrades, and some fantastic new features.
And because Drupal uses Symfony, you'll easily be able to use Symfony (or its little brother Silex) for any non-CMS projects. Being a great Drupal 8 developer means being a great PHP developer.
Of course, Drupal 8 has its criticisms: it will be too hard for new developers to learn, or it will not be useable for smaller sites. We'll take on these concerns directly and see why they are valid, but exaggerated.
An introduction about JavaScript web workers I gave at BerlinJS on the 18th of July 2013. It introduces the concept of web workers for simple parallel processing in client side JavaScript.
An absolute beginners guide to node.js . Done for a presentation at college. The presentation contains data from various sources ,sources are noted at the end slide. please inform me any mistakes ,since at that time i was in a bit of hurry :)
Patrick Chanezon and Guillaume Laforge are presenting Google App Engine Java and Gaelyk, the lightweight groovy toolkit on top of the GAE SDK, at the Devoxx conference
"WTF is Twisted? (or; owl amongst the ponies)" is a talk that introduces the Twisted asynchronous programming framework, how it works, and what uses it.
Developing realtime apps with Drupal and NodeJS drupalcampest
Based on Google's V8 JavaScript engine, NodeJS is a fairly new platform for creating scalable and real-time web applications. I will introduce you to NodeJS internals and ecosystem as well as exaplain why and how you can use Node in your Drupal based projects.
An overview of Ruby, jRuby, Rails, Torquebox, and PostgreSQL that was presented as a 3 hour class to other programmers at The Ironyard (http://theironyard.com) in Greenville, SC in July of 2013. The Rails specific sections are mostly code samples that were explained during the session so the real focus of the slides is Ruby, "the rails way" / workflow / differentiators and PostgreSQL.
Title: The JavaScript Delusion [long presentation]
Sub-title: And Why Java Will Continue to Rule The Business World
Abstract: JavaScript is the new hype. It is the coolest of cool technologies. Everyone from FaceBook to NASA is using it. Let us see what is there once we peel the thick layer of hype and mass delusion around it. How does it compare with the Java world?
Speaker:
Mojahedul Hoque Abul Hasanat
CTO, Dynamic Solution Innovators
Software Engineer, Backpack Technologies, Inc.
I was invited by the Hatchery+ to give a presentation and workshop on building products - a brief overview on modern web apps, tech stacks, languages, frameworks, services, APIs and more.
Similar to Fast, concurrent ruby web applications with EventMachine and EM::Synchrony (20)
2. A bit about me.
• Full time Facebook developer
• All FB apps: Sinatra + DM, MRI + Thin on EY, Heroku
• Lots and lots of slow API calls to Facebook
• Lots and lots of delayed timeouts, API errors
• Lots and lots of users
• Lots and lots of performance issues from slow API
calls!
4. MapAttack!
• Over 60 hits per second
• Lots of network API calls to Geoloqi platform
• Lots of scary thread exceptions (Rainbows! +
ThreadSpawn)
• Completely unsustainable as a conventional
ruby web application
5. What’s going on here?
• Blocking IO!
• Process spends 90% of time waiting, 10% actually
doing something
• Raw performance (Typheous vs Net::HTTP) is
almost irrelevant! It’s not your bottleneck.
• MRI: No real threads, so IO blocks. 1.8 has green
threads, 1.9 has kernel threads with GIL
• JRuby: Real kernel threads, much better. But slow
(before JIT warms up), special deploy stack, and...
6.
7.
8. Based on the Reactor pattern..
No threads (sortof)
No blocking IO at all
Uses callbacks as events
var app = express.createServer();
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(3000);
Is this the future of web development?
9. CALLBACK HELL
var useFile = function(filename,callback){
posix.stat(filename).addCallback(function (stats) {
posix.open(filename, process.O_RDONLY, 0666).addCallback(function (fd) {
posix.read(fd, stats.size, 0).addCallback(function(contents){
callback(contents);
});
});
});
};
I found far worse examples, but
they wouldn’t fit on this page.
10. New feature: Joyent Owns It
“Any sort of use in commerce, such as promoting a
Platform-as-a-Service or professional services
offering by using the Node.js mark, does require a
written license agreement.”
“If Joyent notifies you that your use of any trademark
is detrimental to any Joyent trademarks or is
otherwise unacceptable, you must immediately cease
using the marks, blah blah...”
11.
12. What does Node.js do?
It uses the Reactor pattern.
Can we copy it?
YES!
13. The Reactor Pattern
“The reactor design pattern is a concurrent
programming pattern for handling service
requests delivered concurrently to a service
handler by one or more inputs” - Wikipedia
What Node.js does: Takes your blocking IO
operation, shoves it into its own kernel thread
behind the scenes, uses Unix kernel magic to make
it rejoin the reactor queue when it’s ready.
• Linux: epoll(4)
• BSD: kqueue/kevent
14. • Blocking IO a UNIVERSAL problem
• All programming languages have trouble with it
• The Reactor pattern resolves it
• Most languages have the Reactor pattern!
15. • Blocking IO a UNIVERSAL problem
• All programming languages have trouble with it
• The Reactor pattern resolves it
• Most languages have the Reactor pattern!
JavaScript Node.js
Python Twisted
Ruby EventMachine (libem, C)
Java JBoss_Netty
(wait, what? I thought Java had good threading..)
PHP None yet (perhaps ever)
16. EventMachine
• Stable, fast, mature, works!
• Production tested
• Thin has EM built in
• Hosting Support (CloudFoundry, Heroku)
# Source: http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers/
EventMachine.run {
page = EventMachine::HttpRequest.new('http://google.ca/').get
page.errback { p "Google is down! terminate?" }
page.callback {
about = EventMachine::HttpRequest.new('http://google.ca/search?q=eventmachine').get
about.callback { # callback nesting, ad infinitum }
about.errback { # error-handling code }
}
}
17. EventMachine
The Bad
• Documentation is weak
• It’s weird for synchronous programmers
• Education problem - nobody understands it
• Like Node.js, it requires callback programming
• Callbacks don’t play nicely with web frameworks
without nasty hacks (async-sinatra, throw :async,
et cetera)
18. EventMachine
What if I told you you could do
concurrent asynchronous
programming, WITHOUT
CALLBACKS?
19. EM-Synchrony
• http://github.com/igrigorik/em-synchrony
• “Collection of convenience classes and
primitives to help untangle evented code,
plus a number of patched EM clients to make
them Fiber aware”
• Wraps callbacks in Ruby 1.9 Fibers
automatically via EM::Synchrony.sync. The
result: NO CALLBACKS!
• Anything with a callback method can be
patched instantly to support this.
20. EM-Synchrony
def http_get(url)
f = Fiber.current
http = EventMachine::HttpRequest.new(url).get
# resume fiber once http call is done
http.callback { f.resume(http) }
http.errback { f.resume(http) }
return Fiber.yield
end
EventMachine.run do
Fiber.new{
page = http_get('http://www.google.com/')
puts "Fetched page: #{page.response_header.status}"
if page
page = http_get('http://www.google.com/search?q=eventmachine')
puts "Fetched page 2: #{page.response_header.status}"
end
}.resume
end
21. EM-Synchrony
EventMachine.synchrony do
page = EventMachine::HttpRequest.new("http://www.google.com").get
p "No callbacks! Fetched page: #{page}"
EventMachine.stop
end
22. EM-Synchrony
Goliath
http://postrank-labs.github.com/goliath
require 'goliath'
class Hello < Goliath::API
# reload code on every request in dev environment
use ::Rack::Reloader, 0 if Goliath.dev?
def response(env)
[200, {}, "Hello World"]
end
end
# > ruby hello.rb -sv
# > [97570:INFO] 2011-02-15 00:33:51 :: Starting server on 0.0.0.0:9000
23. EM-Synchrony
Goliath
• Awesome! But...
• Designed for SOA work, not high-level web
development
• Doesn’t play nicely with Rack, Thin, Heroku
• It’s re-inventing the wheel (I love Sinatra!)
• Can we make Sinatra work with EM-Synchrony?
YES!
24. Sinatra-Synchrony
http://github.com/kyledrake/sinatra-synchrony
• Tiny glue extension, < 100 LOC
• Same old Sinatra, concurrency is (mostly) built in.
• EventMachine and EM-Synchrony, Rack, Thin, Rainbows!,
CloudFoundry, Heroku
• Wraps each request in its own Fiber
• Only coding change is to use non-blocking drivers/libraries
• Patches Rack::Test to make tests run within EM-Synchrony fiber
Wow, that was easy.
25. Conclusions
• You can bake strong concurrency support into Ruby
with almost zero changes to your code
• You can take advantage of this while programming
synchronously as usual.. Node.js can’t!
• This makes Ruby a real competitor here. Strong
performance, concurrency, maintainability, productivity,
testing
• RUBY IS NOT SLOW. This is a marketing failure, and
we need to fix it.
26. What You Can Do
• Try this stuff out! Play with it. Teach others how to use
it. Work on the code for it.
• Help me with this Sinatra-Synchrony idea. Perhaps we
can make the idea more general purpose? Rails
support?
• DEFEND RUBY FROM THE “RUBY IS SLOW”
PEOPLE. Sinatra-Synchrony gets 3000 hits per second
in benchmarks on my MacBook with OSX’s crappy
network stack, on one core. Productivity and
performance are not incompatible here.
• It’s my birthday today, get me drunk.
27. The Future
Rubinius - Hydra Branch
http://rubini.us/2011/02/17/rubinius-what-s-next
• The Smalltalk-80 Blue Book approach is working
• They are trashing their GIL with pure ruby!
• Supports EventMachine, Fibers coming soon
• Because of Evan (and friends), Alan Kay, Ezra,
RUBINIUS IS THE FUTURE