Messages queues don't need to be scary.

Messages queues don't need to be scary.

An introduction tutorial to AMQP with RabbitMQ and Ruby.

02cd14759ba3327f68f737131947781f?s=128

Glen Mailer

October 14, 2013
Tweet

Transcript

  1. Message queues don't need to be scary An intro workshop

    to AMQP
  2. AMQP

  3. Advanced Message
 Queuing Protocol

  4. None
  5. Why?

  6. Job Queue
 Event Stream
 Buffered RPC

  7. Job Queue

  8. Producer Consumer

  9. Producer Consumer Producer

  10. Producer Consumer Producer Consumer

  11. Producer Consumer Producer Consumer

  12. Slow Operations
 Expensive Operations
 Brittle Operations

  13. Producer c = create_channel q = c.queue("task") x = c.default_exchange

    x.publish( "task", "payload" ) Consumer c = create_channel q = c.queue("task") q.subscribe(worker) worker = fn(*message) { // do stuff }
  14. Acknowledging Messages q = c.queue("task") q.subscribe(worker, ack = TRUE) worker

    = fn(info, metadata, body) { // do work c.acknowledge(info.delivery_tag) }
  15. Publishing Options Mandatory Will error if not queued Immediate Will

    error if no consumer available Confirm Server acknowledges receipt
  16. Queue Declare Options Passive Don't create, get reference to existing

    queue Durable Queue still exists after a broker restart
  17. Event Stream

  18. Producer Consumer Producer Consumer Consumer Consumer Producer

  19. Topic Exchange routing.key Exact match routing.*.key routing.#.key Wildcard matching routing.key

    routing.a.key routing.b.key routing.a.b.key
  20. (soft) Real-time updates
 User notifications
 Work distribution

  21. Binding a Queue x = c.exchange("activity”) q = c.queue("alert") q.bind(x,

    "balance.*") q.bind(x, "transfer.out") x.publish("balance.low") x.publish("transfer.in") x.publish("transfer.out")
  22. Queue Declare Options Exclusive Only one consumer on this queue

    at any time Auto Delete Queue is deleted after consumer disconnects
  23. Simulator Demo

  24. Buffered RPC

  25. Producer Consumer

  26. Message Header
 Reply To
 Correlation ID

  27. Reply To reply = c.queue("abcdef") x.publish("request", :headers => { :replyTo

    => "abcdef" }) reply.subscribe(handleResponse)
  28. Correlation ID x.publish("request1", :headers => { :replyTo => "abcdef", :correlationID

    => "request1" }) x.publish("request2", :headers => { :replyTo => "abcdef", :correlationID => "request2" })
  29. Smooth out Spikes
 Transparently scale
 Deferred Response

  30. Why not just use … ?

  31. Delayed Job
 Resque / Sidekiq
 Beanstalkd

  32. JMS
 Stomp
 MQTT

  33. ØMQ

  34. Ruby Quickstart

  35. gem install bunny

  36. require 'bunny'
 
 url = 'amqp://un:pw@host:5672/vhost'
 conn = Bunny.new(url)
 conn.start


    ch = conn.create_channel
 ch.prefetch 1
  37. declare_options = {:no_declare => true}
 q = ch.queue(name, options)
 


    options = {:block => true, :ack => true}
 q.subscribe(options) do |info, meta, body|
 # do stuff
 ch.ack(info.delivery_tag)
 end
  38. declare_options = {:no_declare => true}
 x = ch.topic("name", declare_options)
 


    x.publish("message", :routing_key => "key")