Message Queues : A Primer - International PHP Conference Fall 2012

77
Mike Willbanks | Barnes & Noble Message Queues : A Primer

description

Message Queues - A Primer is an introduction to message queues and how they work with PHP.

Transcript of Message Queues : A Primer - International PHP Conference Fall 2012

Page 1: Message Queues : A Primer - International PHP Conference Fall 2012

Mike Willbanks | Barnes & Noble

Message Queues : A Primer

Page 2: Message Queues : A Primer - International PHP Conference Fall 2012

Housekeeping… •  Talk

– Slides will be posted after the talk. •  Me

– Sr. Web Architect Manager at NOOK Developer

– Prior MNPHP Organizer – Open Source Contributor – Where you can find me:

•  Twitter: mwillbanks G+: Mike Willbanks •  IRC (freenode): mwillbanks Blog:

http://blog.digitalstruct.com •  GitHub: https://github.com/mwillbanks

Page 3: Message Queues : A Primer - International PHP Conference Fall 2012

Agenda •  Message Queues? •  Protocols and Software •  Picking a Message Queue •  Best Practices

Page 4: Message Queues : A Primer - International PHP Conference Fall 2012

A Definition “Message queues and mailboxes are software-engineering components used for interprocess communication, or for inter-thread communication within the same process. They use a queue for messaging – the passing of control or of content.”

Page 5: Message Queues : A Primer - International PHP Conference Fall 2012

What is messaging? “Messaging describes the sending and receiving of data (in the form of messages) between systems. Messages are exchanged between programs or applications, similar to the way people communicate by email but with guarantees on delivery, speed, security and the absence of spam.”

Page 6: Message Queues : A Primer - International PHP Conference Fall 2012

General Anatomy

Task

Producer ConsumerMessages

Messages

Messages

Producer creates a message and pushes it to the queue; the consumer reads from the queue and processes the message.

Page 7: Message Queues : A Primer - International PHP Conference Fall 2012

l  Pub/Sub l  FIFO buffer l  Push / Pull l  A way to communicate between

applications / systems. l  A way to decouple components. l  A way to offload work.

Describing Message Queues

Page 8: Message Queues : A Primer - International PHP Conference Fall 2012

l  Offload Heavy Work l  Integration with Legacy Systems l  Asynchronous Processing l  Parallel Processing l  Process consistency l  Scalability

Why to use a Message Queue

Page 9: Message Queues : A Primer - International PHP Conference Fall 2012

Unix Foundations l  “Write programs that work together.” l  “Do it in the background.”

Page 10: Message Queues : A Primer - International PHP Conference Fall 2012

l  Web systems need to be geared to run things asynchronously.

l  Distribution of load l  System integrity

Why it matters

Page 11: Message Queues : A Primer - International PHP Conference Fall 2012

MESSAGE QUEUE EXAMPLES

You’ve seen them before; they are used in most applications to help them scale.

Page 12: Message Queues : A Primer - International PHP Conference Fall 2012
Page 13: Message Queues : A Primer - International PHP Conference Fall 2012
Page 14: Message Queues : A Primer - International PHP Conference Fall 2012
Page 15: Message Queues : A Primer - International PHP Conference Fall 2012

GENERIC USE CASES When to make use of message queues in case you’re wondering.

Page 16: Message Queues : A Primer - International PHP Conference Fall 2012

Notifications Email, SMS, Push Messaging….

Page 17: Message Queues : A Primer - International PHP Conference Fall 2012

Photo Processing Thumbnails, Resizing, Watermarking, Converting…

Page 18: Message Queues : A Primer - International PHP Conference Fall 2012

Video Processing Resampling, Audio Overlay, Type Conversion…

Page 19: Message Queues : A Primer - International PHP Conference Fall 2012

Analytics Web Server Logs, Log Aggregation, PHP Errors, etc.

Page 20: Message Queues : A Primer - International PHP Conference Fall 2012

Integrations Save local first; push second.

Page 21: Message Queues : A Primer - International PHP Conference Fall 2012

PROTOCOLS

AMQP STOMP XMPP Vendor Specific

Page 22: Message Queues : A Primer - International PHP Conference Fall 2012

AMQP Advanced Message Queuing Protocol

Page 23: Message Queues : A Primer - International PHP Conference Fall 2012

l  AMQP Working Group (Community and Vendor)

l  Platform agnostic protocol. l  Completely open, interoperable and broadly

applicable. l  Many severs available and many client

libraries.

Overview of AMQP

Page 24: Message Queues : A Primer - International PHP Conference Fall 2012

How it Works Producer, Exchange, Queue, Consumer

Page 25: Message Queues : A Primer - International PHP Conference Fall 2012

l  AMQP utilizes exchanges, queues and bindings.

l  An exchange are routers with routing tables. l  A binding defines the routing rules. l  A queue is where the messages wait for a

consumer.

How it Works

Page 26: Message Queues : A Primer - International PHP Conference Fall 2012

Exchanges

Page 27: Message Queues : A Primer - International PHP Conference Fall 2012

l  Fanout Exchange l  No routing keys involved. Any message that is

sent to the exchange is sent to all queues bound to that exchange.

l  Direct Exchange l  Routing keys involved. A queue binds to the

exchange to request messages that match a routing key exactly.

l  Topic Exchange l  Routing keys involved. A queue binds to the

exchange to request messages that match a routing key pattern.

Understanding Exchanges

Page 28: Message Queues : A Primer - International PHP Conference Fall 2012

Implementations www.rabbitmq.com Very popular and common message queue owned by VMware.

qpid.apache.org Long standing project; apache foundation.

www.openamq.org Long standing project; ZeroMQ partner, no news since 2009.

Page 29: Message Queues : A Primer - International PHP Conference Fall 2012

l  An exchange, queue and bindings must be defined first. Publishing can then commence after. l  Create the queue l  Create the exchange l  Bind to the queue.

Building a Queue

Page 30: Message Queues : A Primer - International PHP Conference Fall 2012

l  Default behavior is no persistence. l  How important are the messages? l  Just about all items have a level of persistence if you

would like them to survive on reboot. l  Mark exchanges, queues and messages as DURABLE.

Persistence?

Page 31: Message Queues : A Primer - International PHP Conference Fall 2012

l  Extension compatible with AMQP specification 0-9-1.

l  pecl install amqp

PECL AMQP

Page 32: Message Queues : A Primer - International PHP Conference Fall 2012

AMQP Client

Page 33: Message Queues : A Primer - International PHP Conference Fall 2012

AMQP Worker

Page 34: Message Queues : A Primer - International PHP Conference Fall 2012

STOMP Simple (or Streaming) Text Orientated Messaging Protocol

Page 35: Message Queues : A Primer - International PHP Conference Fall 2012

Overview l  Simple protocol

l  Behaviors follow very simple commands.

l  Most message queues can communicate over STOMP.

Page 36: Message Queues : A Primer - International PHP Conference Fall 2012

How It Works l  When you send in a message, you tell it which

queue to go to. l  When you subscribe you request a queue.

Connect Send Disconnect

/queue/msg

PHP

STOMP

SERVER

Connect Subscribe Disconnect

/queue/msg

Read

Ack

Page 37: Message Queues : A Primer - International PHP Conference Fall 2012

Sever Implementations activemq.apache.org One of the oldest message queues existing; a apache foundation project

activemq.apache.org/apollo Next generation ActiveMQ

www.rabbitmq.com Very popular and common message queue owned by VMware.

www.jboss.org/hornetq Supported by Red Hat Middle Ware division, picking up steam.

Page 38: Message Queues : A Primer - International PHP Conference Fall 2012

l  pecl install stomp l  That was easy J

PECL Stomp

Page 39: Message Queues : A Primer - International PHP Conference Fall 2012

STOMP Client

Page 40: Message Queues : A Primer - International PHP Conference Fall 2012

STOMP Worker

Page 41: Message Queues : A Primer - International PHP Conference Fall 2012

XMPP Extensible Messaging and Presence Protocol (Although not really a “Message Queue”)

Page 42: Message Queues : A Primer - International PHP Conference Fall 2012

Overview l  Best for real-time data. l  Leveraging pub/sub can turn it into more of a generic message

system. l  Multiple libraries

l  JAXL - https://github.com/abhinavsingh/JAXL l  Xmpp - https://github.com/alexmace/Xmpp

Page 43: Message Queues : A Primer - International PHP Conference Fall 2012

XEP-0060: Publish-Subscribe l  Specification for implementing Publish Subscribe

models. l  Extension to the original XMPP specification.

Publish

Subscribe

Sub1 Sub2 Sub3

to, id, message

from, to, id, message

Page 44: Message Queues : A Primer - International PHP Conference Fall 2012

Publish

Page 45: Message Queues : A Primer - International PHP Conference Fall 2012

Message

Page 46: Message Queues : A Primer - International PHP Conference Fall 2012

SPECIAL MESSAGE QUEUES Various others; including Gearman, ZeroMQ, etc.

Page 47: Message Queues : A Primer - International PHP Conference Fall 2012

l  There are job servers available that are more flexible or more specific. l  Extreme Flexibility l  Job Severs l  Cloud Messaging

Overview

Page 48: Message Queues : A Primer - International PHP Conference Fall 2012

ZeroMQ The ultimate in message queue flexibility. Socket library that acts as a concurrency framework. Contains a PHP extension.

Page 49: Message Queues : A Primer - International PHP Conference Fall 2012

Several Types of Queues Request / Reply Publish / Subscribe Parallel Pipeline Fair Queuing And more…

Page 50: Message Queues : A Primer - International PHP Conference Fall 2012

ZeroMQ Client Example

Page 51: Message Queues : A Primer - International PHP Conference Fall 2012

ZeroMQ Worker Example

Page 52: Message Queues : A Primer - International PHP Conference Fall 2012

l  Application framework for farming out work. l  Job sever for asynchronous or synchronous

messages.

Gearman

Page 53: Message Queues : A Primer - International PHP Conference Fall 2012

l  Pass a job to the job server l  Worker receives the job and processes l  Ability to persist if enabled; default is in-memory.

Gearman

Page 54: Message Queues : A Primer - International PHP Conference Fall 2012

Gearman Client Example

Page 55: Message Queues : A Primer - International PHP Conference Fall 2012

Gearman Worker Example

Page 56: Message Queues : A Primer - International PHP Conference Fall 2012

l  Asynchronous Job Queue l  Good scheduling system (aka delays). l  User land PHP clients.

Beanstalkd

Page 57: Message Queues : A Primer - International PHP Conference Fall 2012

l  Pass a job to the job server l  Worker receives the job and processes l  Ability to persist to binlog; default is in-memory

Beanstalkd

Application Code

Beanstalkd Client API(Pheanstalk)

Beanstalkd Server

Beanstalkd Worker API(Pheanstalk)

Worker Application Code

Page 58: Message Queues : A Primer - International PHP Conference Fall 2012

Pheanstalk Client

Page 59: Message Queues : A Primer - International PHP Conference Fall 2012

Pheanstalk Worker

Page 60: Message Queues : A Primer - International PHP Conference Fall 2012

PICKING A MESSAGE QUEUE What you want to look for.

Page 61: Message Queues : A Primer - International PHP Conference Fall 2012

The Trifecta

Scale

Performance Durability

Messages

Horizontal?Vertical?

In-Memory?

Persistence?

Replication?Raw Speed?

# Messages?

Messages Messages

Messages

MessagesMessages

MessagesMessagesMessages

Page 62: Message Queues : A Primer - International PHP Conference Fall 2012

Standards •  A recognized standard?

– AMQP? STOMP? XMPP? •  How many developers?

– Will an unfortunate event kill off the product?

•  Undocumented protocol? – Forget about it!

Page 63: Message Queues : A Primer - International PHP Conference Fall 2012

Delivery Policies

Store Message Saved?

Process Message ACK?

Receive Message TTL?

Page 64: Message Queues : A Primer - International PHP Conference Fall 2012

Message Policies •  Handling in-active messages? •  Messages that failed processing? •  Time to live? •  Retry? •  Ability to check message status?

Page 65: Message Queues : A Primer - International PHP Conference Fall 2012

Routing Policies •  Fanout? •  Direct? •  Topic? •  Broadcast? •  Etc?

Page 66: Message Queues : A Primer - International PHP Conference Fall 2012

Security Policies •  Require authentication?

– LDAP/AD integration? •  Connection Restrictions? •  Require logging?

– Compliance –  Intrusion Detection

Page 67: Message Queues : A Primer - International PHP Conference Fall 2012

BEST PRACTICES Rules of the road my friends…

Page 68: Message Queues : A Primer - International PHP Conference Fall 2012

Messages •  Formatting

– JSON or XML are great options. •  Please no serialized PHP objects.

•  Message Size – Only as large as necessary.

•  Don’t send a binary object through the queue.

Page 69: Message Queues : A Primer - International PHP Conference Fall 2012

Workers •  Dumb as possible!

– Single message type •  Easier to scale.

– Single operation •  Easy to debug and far more flexible.

Page 70: Message Queues : A Primer - International PHP Conference Fall 2012

PHP Daemons •  Prevent Memory Leaks

– Detection – Cycle Workers

•  Handle Signals – Properly shutdown! – Watch out for OS service kills

•  Sleeping is good J

Page 71: Message Queues : A Primer - International PHP Conference Fall 2012
Page 72: Message Queues : A Primer - International PHP Conference Fall 2012

Environment Integration

Web Server

Database

Worker Server

Message Queue

Web Server -> Message Queue Worker Server -> Message Queue

Page 73: Message Queues : A Primer - International PHP Conference Fall 2012

High Availability

Web Server

Database

Worker Server

Message Queue

Message Queue

Worker Server

Send to 1

Insert multiple message queue servers + multiple worker nodes. Each worker node can connect to as many message queue servers as necessary.

Page 74: Message Queues : A Primer - International PHP Conference Fall 2012

Supervisord Process monitoring, logging and more!

Page 75: Message Queues : A Primer - International PHP Conference Fall 2012

Installing Supervisord •  Requires: python-setuptools

sudo easy_install supervisor!

•  Generate Configuration sudo echo_supervisord_conf > /etc/

supervisord.conf!!

Page 76: Message Queues : A Primer - International PHP Conference Fall 2012
Page 77: Message Queues : A Primer - International PHP Conference Fall 2012

QUESTIONS?

These slides will be posted to SlideShare & SpeakerDeck. SpeakerDeck: http://speakerdeck.com/u/mwillbanks Slideshare: http://www.slideshare.net/mwillbanks Twitter: mwillbanks G+: Mike Willbanks IRC (freenode): mwillbanks Blog: http://blog.digitalstruct.com GitHub: https://github.com/mwillbanks