Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

RubyConf 2012: Custom Reverse Proxies

Abstracting Features Into Custom Reverse Proxies (Or: Making Better Lemonade From Chaos)

Life isn't always simple. We often have to deal with a mishmash of applications, languages, and servers. How can we begin to standardize functionality across this chaos? Custom reverse proxies to the rescue! Using Ruby and EventMachine, learn how you can abstract high-level features and functionality into fast reverse proxies that can improve scalability, save time, and make the world happy.

- See how we've applied this across a diverse set of web service APIs to standardize the implementation of authentication, request throttling, analytics, and more.
- See how this can save development time, eliminate code duplication, make your team happy, make the public happy, and make you a hero.
- See how this can be applied to any TCP-based application for a wide-variety of use cases.
- Still think your situation is complicated? Learn about the U.S. Government's plans to standardize API access across the entire federal government. With some reverse proxy magic, this isn't quite as difficult or as foolhardy as it may first sound. It also comes with some nice benefits for both the public audience and government developers.

  • Login to see the comments

RubyConf 2012: Custom Reverse Proxies

  1. 1. Abstracting Features IntoCustom Reverse Proxies Or: Making Better Lemonade From Chaos Photo by Lori Greig Muerdter • RubyConf 2012 • November 1, 2012
  2. 2. Photo by Brian Lane Winfield Moore
  3. 3. Internet Reverse Proxy Web Server Internal Network
  4. 4. Internet Reverse Proxy Web Server Internal Network
  5. 5. RUBY! CUSTOM FEATURES! EVENTMACHINE!Internet Reverse Proxy Web Server Internal Network
  6. 6. OKAY, BUT…
  7. 7. Photo by Brian Lane Winfield Moore
  8. 8. WHY & HOW
  9. 9. A Story… Photo by Elgin County Archives
  10. 10. Photo by rocketlass
  11. 11. Photo by alandberning
  12. 12. Analysis Solar services DeploymentTransportation services … Commercial Buildings Buildings services … … …
  13. 13. Users Don’t Care Photo from National Media Museum
  14. 14. What We Wanted• 1 entry point to all our APIs• 1 user account for all our APIs• API key access to all our APIs• Rate limiting for all our APIs• Analytics for all our APIs
  15. 15. What We Didn’t Want• Changes required to each API
  17. 17. • Authentication • Rate Limiting • Analytics Department A Department BInternet Reverse Proxy Department C Department D Internal Network
  18. 18. Better Lemonade? Photo by Lara604
  19. 19. For API Users• 1 API key for all our services• Shielded from our internal complexities
  20. 20. For Our Developers• Old APIs: Do absolutely nothing• New APIs: Do absolutely nothing• Get for free: –Authentication –Rate Limiting –Analytics
  21. 21. • Reduced implementation code• Standardization is enforced• New features in the reverse proxy benefit everyone• Reverse proxies can also be used for scaling
  22. 22. Building these things… Photo from The Library of Congress
  23. 23. em-proxy• Ruby & EventMachine• Blazing fast• Flexible• Low-level
  24. 24. Proxy.start(:host => "", :port => 80) do |conn| conn.server :srv, :host => "", :port => 81 conn.on_data do |data| # Do something with the incoming data... data end conn.on_response do |backend, resp| # Do something with the response... resp end conn.on_finish do |backend, name| # Do something when finished... endend
  25. 25. conn.on_data do |data| # Modify the User-Agent on the incoming # request data.gsub(/User-Agent: .*?rn/, "User-Agent: em-proxy/0.1rn")end
  26. 26. redis = => "")conn.on_data do |data| # Fun things with Ruby! ip = peer[0] redis.incr(ip) dataend
  27. 27. parser = Http::Parser.newparser.on_headers_complete = proc do |h| # Hello, friendlier HTTP headers... puts h["User-Agent"]endconn.on_data do |data| parser << data dataend
  28. 28. Photo by Madison Guy
  29. 29. Transparency Photo by Brett Jordan
  30. 30. Speed & Efficiency Photo by jamesjustin
  31. 31. 4 3.5 3Time (milliseconds) 2.5 2 1.5 1 0.5 0 Direct em-proxy rack-reverse-proxy (Terribly unscientific benchmarks)
  32. 32. 2400 2300 2200Time (milliseconds) 2100 2000 1900 1800 1700 1600 1500 1400 Direct em-proxy rack-reverse-proxy (Terribly unscientific benchmarks)
  33. 33. Flexibility Photo from The Library of Congress
  34. 34. What Else Can You Do? Photo by
  35. 35. • Error handling?• Web page manipulation? – Insert standard analytics JavaScript snippet? – Add a standard header and footer?• Add JSONP callbacks for all JSON APIs?• Security checks?• More than HTTP… – Intercept & manipulate e-mail? – Intercept & manipulate database calls?
  36. 36. Photo by Keoki Seu
  37. 37. Buffering Photo from The Library of Congress
  38. 38. Content-Length Photo by Sterlic
  39. 39. gzip Photo by Kaptain Kobold
  40. 40. Want Bigger? Photo by elviskennedy
  42. 42. Main Objectives• Make it easier for users to find and consume federal APIs• Make it easier for federal agencies to develop & deploy more APIs
  43. 43. Same Problem Photo by alandberning
  44. 44. Same Solution? • Authentication • Rate Limiting • Analytics Agency A Agency BInternet Reverse Proxy Agency C Agency D Internal Network
  45. 45. Stay Tuned… Photo by Lord Jerome
  46. 46. SO………
  47. 47. Photo by judepics
  48. 48. • Reverse Proxies: Fun for the whole family!• Custom Reverse Proxies: You might be able to implement more functionality at this layer than you realize.• Think Different: They can provide a different way to architect some features of your apps.
  49. 49. Resources & Support Photo by Musée McCord Museum
  50. 50. API Umbrella• Our full API management solution – Includes custom Event Machine based proxy• Open source (Just recently open sourced, so pardon the current state of things)
  51. 51. Ruby & Event Machine• em-proxy – – Simple and very capable• ProxyMachine – – Simpler, but can only act on requests, not responses• Goliath – – More of a framework, uses em-synchrony (Fibers)
  52. 52. Other Reverse Proxies• HAProxy – – General proxy and load balancing awesomeness• Varnish Cache – – Proxy caching layer coolness• nginx – – Web server powerhouse and nice proxy
  53. 53. Renewable Energy APIs• (Lots more APIs coming soon)
  54. 54.
  55. 55. @nickblahEnjoyed this presentation? Enjoyed this ‘stache? Enjoy charity?