queueing at the checkout
TRANSCRIPT
★Chief Technical Architect at Reward Gateway
★ Interested in scalability and engineering
★ “PHP addict!”
Who am I?
<html> <head> <title>Tutorial</title> </head> <body> <?php echo "<p>Hello World</p>"; ?> </body> </html>
“Do not wait; the time will never be 'just right.' Start where you stand, and work with whatever tools you may have at your command, and
better tools will be found as you go along.”
American author, Napoleon Hill (1883-1970)
Modern applications are complex
★Modern web applications often do not work in isolation.
★Gone are the days of a single database powering everything.
★ Integrate with Facebook, Stripe, etc.
Your application
Stripe
Amazon S3Zendesk
Adding an API
★Let’s add a new SOAP service to our application.
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-‐envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-‐encoding">
<soap:Body>
<m:GetName xmlns:m="http://www.example.com/name">
<m:Item>John</m:Item>
</m:GetPrice>
</soap:Body>
</soap:Envelope>
Adding an API
★Let’s add a new SOAP service to our application.
★PHP makes this really easy!<?php
$client = new SoapClient("http://www.example.com/some.wsdl");
echo "Hi " . $client-‐>name() . "\n";
Adding an API
★Let’s add a new SOAP service to our application.
★PHP makes this really easy!
★We can be in production in two minutes.
$ php client.php Hi John
$ php client.php Hi Dave
$ php client.php Hi Colin
Poor response times lead to loss of revenue
★The slower your site, the more likely people are to give up (“exit”)
★Over 75% of people in an Akami survey said they would stop using a site if it took longer than 4 seconds.
★The more people who give up, the less money you’ll make.
Exit
Rat
e
0%
5%
10%
15%
20%
Load Time (secs)0.1 1 2 3
TL;DR
★Split your application into bits.
★Keep the predictable bit on the request path.
★Move the unpredictable bit somewhere else.
★Use a message queue to communicate between the two halves.
Message Queue Servers
★There are lots of message queue implementations:
★ RabbitMQ
★ Amazon SQS
★ OpenAMQ
★ ….
★Written in different languages with different tradeoffs.
★Almost all support a protocol called AMQP.
Message Queue Basics
★AMQP has three concepts.
★Exchanges route messages using rules
★Queues are where messages wait for processing
★Bindings define the routing rules
Publisher
Publisher
Exchange
Exchange
Queue
Queue
Consumer
Consumer
Consumer
Exchange Rules
★The exchanges understand three types of rule:
★ Fanout
★ Direct
★ Topic
★You can create almost any structure with this set.
Publisher Exchange Queue Consumer
Consumer
Fanout
Queue
Publisher Exchange Queue Consumer
Direct
Exchange Rules Continued
★The exchanges understand three types of rule:
★ Fanout
★ Direct
★ Topic
★You can create almost any structure with this set.
Publisher Exchange
Queue Consumer
Consumer
Topic
Queue
lazy.#
*.orange.*
*.*.rabbit
Basic Publisher
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection; use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPConnection('localhost', 5672, 'guest', ‘guest');
$channel = $connection-‐>channel(); $channel-‐>queue_declare('hello', false, false, false, false);
$msg = new AMQPMessage('Hello World!'); $channel-‐>basic_publish($msg, '', 'hello');
echo " [x] Sent 'Hello World!’\n”;
$channel-‐>close(); $connection-‐>close();
Basic Consumer
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost')) channel = connection.channel()
channel.queue_declare(queue='hello')
print ' [*] Waiting for messages. To exit press CTRL+C'
def callback(ch, method, properties, body): print " [x] Received %r" % (body,)
channel.basic_consume(callback, queue='hello', no_ack=True)
channel.start_consuming()
Still need to handle problems…
★You still have to monitor your queues.
★Easy to see when things go wrong.
★More advanced features let you cope with this better (e.g. timeouts on delivery)
Que
ue S
ize
(mes
sage
s)
0
250
500
750
1,000
Requests (/secs)
010
020
030
0
Still need to handle crashes…
★What happens when the message queue server crashes?
★ You also still need to worry about your data.
★By default, nothing is saved by RabbitMQ (i.e you lose everything.)
★Pay attention to durability settings when configuring the queues.
“Bad API”
★http://badapi.trip.tv
★Allows you to send a request and get a known response back.
★Lets you specify how long to delay the response
★Lets you specify a HTTP error code
★Good for simulating a poorly performing API
“RabbitMQ Simulator”
★http://tryrabbitmq.com
★Single page simulator for RabbitMQ
★ Interactive user interface
★Lets you try out all the concepts in this talk and get comfortable with exchanges, bindings etc.
Conclusion
★Keep your application predictable - move the unpredictable bit out.
★Your integration tests should consider API failures and performance problems.
★Command-Query Response Separation can really help in this area (another talk!)
★We’re hiring! http://rg.co/careers