A look at some of the problems faced in modern web applications dealing with multiple APIs, what problems this creates, and an introduction to the techniques to solve them (message queues.)
5. “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)
7. 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
Facebook
Stripe
Amazon S3Zendesk
8. 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>
9. 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";
10. 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
15. 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.
ExitRate
0%
5%
10%
15%
20%
Load Time (secs)
0
.1
1
2
3
17. 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.
18. 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.
19. 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
20. 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
21. 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
22. Basic Publisher
<?php
require_once
__DIR__
.
'/vendor/autoload.php';
use
PhpAmqpLibConnectionAMQPConnection;
use
PhpAmqpLibMessageAMQPMessage;
$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();
24. 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)
QueueSize(messages)
0
250
500
750
1,000
Requests (/secs)
0
10
0
20
0
30
0
25. 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.
27. “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
29. 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