Architecting queueslsp15

27
Architecting with Queues for Scale, Speed and Separation Sandy Smith Lone Star PHP 2015

Transcript of Architecting queueslsp15

Page 1: Architecting queueslsp15

Architecting with Queues for Scale, Speed and Separation

Sandy Smith Lone Star PHP 2015

Page 2: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

The Challenge

2

Page 3: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Social Contest

•Show off Azure + PHP •People submit tweets to enter contest •Pull specified keywords from Twitter queue (prefiltered by Node.js app) •Human admins filter out inappropriate content •Humans or computer pulls out winner from approved entries, on timer or arbitrarily •Display latest entries and latest winners to public

3

Page 4: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Stretch goals

•Allow any size contest •Assume global contest with distributed moderators

4

Page 5: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Initial design

5

Page 6: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

The real bottleneck

What’s the slowest and most variable part of any application?

6

Page 7: Architecting queueslsp15

Insert Text Here

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

The real bottleneck

7

Page 8: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Quick refresherPerformance vs. ScalingVertical vs. Horizontal Scaling

8

Page 9: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Traditional database design

•Group by kind •Keep metadata with object or in metadata tables •Objects (Document, Person, Account) are most important •Reinforced by TableGateway, ActiveRecord patterns, and ORM and framework module generator defaults •Works for 80% of cases

9

Page 10: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Tradeoffs of traditional design

•Everything’s in one table, even when you routinely only need a subset •Typically one master writes, replicants read •Design is focused around what something is, not its state or other attribute •Requires creative solutions for horizontal scaling •Encourages (but does not require) centralizing logic for a given type of data

10

Page 11: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Worst of all…

•This design really didn’t show off all the stuff in Azure

11

Page 12: Architecting queueslsp15

(We had a week left)

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Redesign time

12

Page 13: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Redesign goals

• Include as much Azure stuff as is reasonable • Implement the stretch goals of scalability

13

Page 14: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Queues to the rescue!(Plus some other cool stuff)

14

Page 15: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

New approach

•Keep long term but fast storage •Central concern is not the Thing (Entry) but Status

– Unapproved– Approved– Denied– Winner

•Separate updates to long term storage from status changes •Minimal impact to working code

15

Page 16: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Azure queuing options

Azure Queues (duh) •Simple •“Short”-lived (<7 days) •Used within Azure •Uses REST •Can track message processing

16

Azure Service Bus •Enterprisey •Long-lived • In Azure or private cloud •Can use AMQP, REST, or API •Can publish/subscribe •Can batch requests •Can guarantee FIFO •etc.

See https://msdn.microsoft.com/en-us/library/azure/hh767287.aspx

Page 17: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

More options

•Anything you can install on Linux or Windows (RabbitMQ, ZeroMQ, Kafka, Kestrel, ActiveMQ, etc.) •Any relational or NoSQL database •Azure Tables - Simple REST NoSQL store with a twist

17

Page 18: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Solutions

•Long term storage and display retrieval: Azure Table •Since Node.js app already used it, use Service Bus to store changes in status for consistency •Have daemons pull incoming status changes out and write them to the Table

18

Page 19: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

New design

19

Page 20: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Azure Table basics

20

require_once 'vendor\autoload.php'; use WindowsAzure\Common\ServicesBuilder; use WindowsAzure\Common\ServiceException; use WindowsAzure\Table\Models\Entity; use WindowsAzure\Table\Models\EdmType; // Create table REST proxy. $tableRestProxy = ServicesBuilder::getInstance()->createTableService($connectionString); try { // Create table. $tableRestProxy->createTable("mytable"); } catch(ServiceException $e){ // Handle exception based on error codes and messages.} $entity = new Entity(); $entity->setPartitionKey("pk"); $entity->setRowKey("1"); $entity->addProperty("PropertyName", EdmType::STRING, "Sample"); try { $tableRestProxy->insertEntity("mytable", $entity); } catch(ServiceException $e){ // Handle exception based on error codes and messages.}

Page 21: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Create and send with Service Bus

21

require_once 'vendor\autoload.php'; use WindowsAzure\ServiceBus\Models\QueueInfo; use WindowsAzure\Common\ServiceException; use WindowsAzure\Common\ServicesBuilder; $serviceBusRestProxy = ServicesBuilder::getInstance()->createServiceBusService($connectionString); try { $queueInfo = new QueueInfo("myqueue"); $serviceBusRestProxy->createQueue($queueInfo); } catch(ServiceException $e) { // handle error } try { // Create message. $message = new BrokeredMessage(); $message->setBody("my message"); // Send message. $serviceBusRestProxy->sendQueueMessage("myqueue", $message); } catch(ServiceException $e) { // handle error }

Page 22: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Receive with Service Bus

22

require_once 'vendor\autoload.php'; use WindowsAzure\ServiceBus\Models\QueueInfo; use WindowsAzure\Common\ServiceException; use WindowsAzure\Common\ServicesBuilder; $serviceBusRestProxy = ServicesBuilder::getInstance()->createServiceBusService($connectionString); try { // Set the receive mode to PeekLock (default is ReceiveAndDelete). $options = new ReceiveMessageOptions(); $options->setPeekLock(true); // Receive message. $message = $serviceBusRestProxy->receiveQueueMessage("myqueue", $options); echo "Body: ".$message->getBody()."<br />"; echo "MessageID: ".$message->getMessageId()."<br />"; // *** Process message here *** // Delete message. $serviceBusRestProxy->deleteMessage($message); } catch(ServiceException $e){ // handle error }

Page 23: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Benefits

•Queues add safety: if processing fails, main store is unchanged •App doesn’t wait for process-update-delete cycle

– Concerns more separated•Can move queue processing to separate machines •Trivial to move to different stores for each status •Very performant with up-to-second data

23

Page 24: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Challenges

•No full ACID •Safety is largely in the application layer •Potential for race conditions

– Humans suck

24

Page 25: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Takeaways

•Look for more than just long processes •Use queues to decouple functionality •Look for status changes, e.g. workflow • Is the type of data the most important aspect of your data?

– It usually is!•Design for replacement of components

– Makes changes easier (not easy)

25

Page 26: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Links

•Azure: http://azure.microsoft.com/en-us/ •Social Contest:https://github.com/MusketeersMe/SocialContest •Me

– @SandyS1– http://phparch.com/

•Feedback! https://joind.in/13543 •php[tek] (May 18–22) http://tek.phparch.com •Slides will be at: http://www.slideshare.net/SandySmith

26

Page 27: Architecting queueslsp15

Architecting with Queues - Sandy Smith - Lone Star PHP 2015

Image credits

•Questions? by Valerie Everetthttps://flic.kr/p/5zEjFG •Expanded coke zero can! by Audin Malminhttps://flic.kr/p/5Ldjx8

27