Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RubyConf 2012: Custom Reverse Proxies

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.

Avatar for Nick Muerdter

Nick Muerdter

November 01, 2012

More Decks by Nick Muerdter

Other Decks in Programming

Transcript

  1. Abstracting Features Into Custom Reverse Proxies! Or: Making Better Lemonade

    From Chaos! Photo by Lori Greig! http://www.flickr.com/photos/lori_greig/4906180111! Nick Muerdter • RubyConf 2012 • November 1, 2012!
  2.  ? ? ? ?   ? Photo by

    Brian Lane Winfield Moore! http://www.flickr.com/photos/doctabu/342220423!
  3.  ? ? ? ?   ? Photo by

    Brian Lane Winfield Moore! http://www.flickr.com/photos/doctabu/342220423!
  4. •  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! What We Wanted!
  5. Reverse Proxy Internet Internal Network • Authentication • Rate Limiting

    • Analytics Department A Department B Department C Department D
  6. •  1 API key for all our services! •  Shielded

    from our internal complexities! For API Users!
  7. •  Old APIs: Do absolutely nothing! •  New APIs: Do

    absolutely nothing! •  Get for free:! – Authentication! – Rate Limiting! – Analytics! For Our Developers!
  8. •  Reduced implementation code! •  Standardization is enforced! •  New

    features in the reverse proxy benefit everyone! •  Reverse proxies can also be used for scaling!
  9. Proxy.start(:host => "0.0.0.0", :port => 80) do |conn|! conn.server :srv,

    :host => "127.0.0.1", :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...! end! end!
  10. conn.on_data do |data|! # Modify the User-Agent on the incoming!

    # request! data.gsub(/User-Agent: .*?\r\n/,! "User-Agent: em-proxy/0.1\r\n")! end!
  11. redis = Redis.new(:host => "127.0.0.1")! ! conn.on_data do |data|! #

    Fun things with Ruby!! ip = peer[0]! redis.incr(ip)! ! data! end!
  12. parser = Http::Parser.new! parser.on_headers_complete = proc do |h|! # Hello,

    friendlier HTTP headers...! puts h["User-Agent"]! end! ! conn.on_data do |data|! parser << data! data! end!
  13. 0   0.5   1   1.5   2  

    2.5   3   3.5   4   Direct   em-­‐proxy   rack-­‐reverse-­‐proxy   Time  (milliseconds)   (Terribly unscientific benchmarks)!
  14. 1400   1500   1600   1700   1800  

    1900   2000   2100   2200   2300   2400   Direct   em-­‐proxy   rack-­‐reverse-­‐proxy   Time  (milliseconds)   (Terribly unscientific benchmarks)!
  15. •  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?!
  16. •  Make it easier for users to find and consume

    federal APIs! •  Make it easier for federal agencies to develop & deploy more APIs! Main Objectives!
  17. Reverse Proxy Internet Internal Network • Authentication • Rate Limiting

    • Analytics Agency A Agency B Agency C Agency D Same Solution?!
  18. •  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. !
  19. •  Our full API management solution! – Includes custom Event Machine

    based proxy! •  Open source! API Umbrella! https://github.com/NREL/api-umbrella! ! (Just recently open sourced, so pardon the current state of things)! !
  20. •  em-proxy! –  https://github.com/igrigorik/em-proxy! –  Simple and very capable! • 

    ProxyMachine! –  https://github.com/mojombo/proxymachine! –  Simpler, but can only act on requests, not responses! •  Goliath! –  https://github.com/postrank-labs/goliath! –  More of a framework, uses em-synchrony (Fibers)! Ruby & Event Machine!
  21. •  HAProxy! –  http://haproxy.1wt.eu/! –  General proxy and load balancing

    awesomeness! •  Varnish Cache! –  https://www.varnish-cache.org/! –  Proxy caching layer coolness! •  nginx! –  http://nginx.org/! –  Web server powerhouse and nice proxy! Other Reverse Proxies!