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

Rack-AMQP: Ditch HTTP inside SOA!

Rack-AMQP: Ditch HTTP inside SOA!

We’re comfortable with HTTP, but using it for communication in your SOA isn’t simple. You need load balancers, URL discovery, SSL, and there’s a cost to set up and tear down that HTTP window.

Rack-AMQP lets you serve your rack (and rails) apps over AMQP, a robust, open protocol for messaging. You get simple queues, load balancing, security, and persistent connections for free (plus pub-sub!)—without changing your app.

Learn the pitfalls of HTTP and why you should use AMQP in your SOA!

A video is available at https://www.youtube.com/watch?v=cINCWpn-LQM

Jess Szmajda

April 22, 2014
Tweet

More Decks by Jess Szmajda

Other Decks in Technology

Transcript

  1. RACK-AMQP IS Jackalope AMQP-HTTP RFC Rack-AMQP-client AMQParty amqurl rails-demo Image

    Credit http://www.deviantart.com/art/Jackalope-76202872
  2. A new ruby web application server Like Passenger, Unicorn, Puma,

    or Thin Special Purpose - SOA No HTTP JACKALOPE Image Credit http://fliegertruppen.deviantart.com/art/Jackalope-For-Luck-146298053
  3. WHY

  4. SOA

  5. –Wikipedia “Service-oriented architecture (SOA) is a software design and software

    architecture design pattern based on discrete pieces of software providing application functionality as services to other applications”
  6. WHAT WE DON'T NEED More Load balancers More SSL certificates

    Distributed management Service discovery
  7. HTTP Blind pipe Fast Open Distributed Great for RPC Image

    Credit Chua Hock Chuan http://www3.ntu.edu.sg/home/ehchua/programming/webprogramming/http_basics.html
  8. RACK-AMQP Jackalope: A new server for Rack-based applications Transparently serves

    Rack (HTTP) applications over AMQP As few opinions as necessary Allows for Pub/Sub and other messaging patterns Queues everywhere - No Load Balancers Fewer Components
  9. BUILDING WHAT WE KNOW We are web developers, we understand

    web development Jackalope is just another rack application server We can still build and debug what we know We deploy with Jackalope and it just works
  10. HOW

  11. AMQP MESSAGES Headers and Bodies Well-known fields routing key persistent

    mandatory content type content encoding priority Content Length Content Type Content Encoding Body Headers Status
  12. CONVENTIONS HTTP has a straightforward structure that's easy to emulate

    AMQP has conventions that map easily to HTTP concepts AMQP supports RPC messaging pattern
  13. AMQP-HTTP HTTP AMQP Protocol n/a Hostname Routing Key Port n/a

    Headers Headers URI Headers[‘path’] Request Method Type Content Length Headers[‘Content-Length’] Content Type Content Type Content Encoding Content Encoding Body Body Headers Headers Status X-AMQP-HTTP-Status
  14. AMQP-HTTP 1 def request(hostname, uri, params)! 2 callback_queue = create_response_queue!

    3 amqp.publish('', {! 4 type: 'GET',! 5 headers: { path: ‘/doc/test.html’ },! 6 app_id: ‘Mozilla/4.0’,! 7 routing_key: ‘my.awesome.service',! 8 reply_to: callback_queue.name,! 9 timestamp: Time.now.to_i,! 10 message_id: request_id,! 11 })! 12 wait_for_response(callback_queue)! 13 end
  15. RACK API All ruby web applications run on Rack (Rails,

    Sinatra, etc) Doesn't actually care about HTTP 1 require 'rubygems'! 2 require 'rack'! 3 ! 4 class HelloWorld! 5 def call(env)! 6 [200, {"Content-Type" => "text/html"}, "Hello Rack!"]! 7 end! 8 end
  16. HTTP EMULATION 1 def server! 2 amqp.subscribe(queue) do |meta, body|!

    3 ... unpack stuff ...! 4 env.update({! 5 'REQUEST_METHOD' => http_method,! 6 'PATH_INFO' => uri,! 7 'QUERY_STRING' => query,! 8 'REQUEST_PATH' => uri,! 9 "rack.input" => StringIO.new(body)! 10 })! 11 response_code, headers, body = app.call(env)! 12 publish_response(response_code, headers, body)! 13 end! 14 end! 15
  17. RABBITMQ Open-source AMQP broker Easy to use All major operating

    systems Secure Distributed, Fault-Tolerant, Highly Available
  18. ARCHITECTURE Service Service Service Service Service Rabbit API API APIs

    Service Service Service Service Service Service Service
  19. CODE - CLIENT AMQParty - API-compatible with HTTParty 1 require

    'amqparty'! 2 AMQParty.get('amqp://users.service/users.json') 1 def get(uri, options={})! 2 transport.get(target(uri), options)! 3 end HTTParty or AMQParty
  20. CODE - CLIENT 1 require 'rack/amqp/client'! 2 client = Rack::AMQP::Client.client({host:

    'rabbitmqhost'})! 3 response = client.request("some.queue/users.json", {! 4 body: "",! 5 http_method: "GET",! 6 headers: {},! 7 timeout: 5! 8 })! 9 puts "Response Code: #{response.response_code}"! 10 puts "Headers:"! 11 response.headers.each_pair do |key, value|! 12 puts " #{key}: #{value}"! 13 end! 14 puts "Body:\n\n#{response.body}"
  21. SAMPLE SOA WITH RAILS interface - “API” service userland -

    users service userland_client - client to userland
  22. (MICRO)BENCHMARKS 3 AMQParty.configure { |c| c.amqp_host = 'localhost' }! 4

    ! 5 RUNS = 500! 6 Benchmark.bm do |x|! 7 x.report('amqp') do! 8 RUNS.times do! 9 response = AMQParty.get('amqp://test.simple/users.json')! 10 raise unless response.code == 200! 11 end! 12 end! 13 x.report('http') do! 14 RUNS.times do! 15 response = HTTParty.get('http://localhost:8080/users.json')! 16 raise unless response.code == 200! 17 end! 18 end! 19 end
  23. REFERENCES Architecture: The Lost Years http://www.confreaks.com/videos/759- rubymidwest2011-keynote-architecture-the-lost- years Hexagonal Rails

    by Matt Wynne http://blog.mattwynne.net/category/hexagonal-rails/ Microservices by Martin Fowler and James Lewis http://martinfowler.com/articles/microservices.html Ruby AMQP Gem Docs http://rubyamqp.info/articles/guides.html RabbitMQ AMQP Concepts https://www.rabbitmq.com/tutorials/amqp- concepts.html HTTP Basics http://www3.ntu.edu.sg/home/ehchua/ programming/webprogramming/http_basics.html
  24. Josh Szmajda - @jszmajda Ruby Hangout - @rubyhangout DCRUG Optoro,

    BLINQ.com http://github.com/rack-amqp/rack-amqp THANKS! Thanks John Nestoriak!