61
Using Queues and Offline Processing David Stockton Madison PHP - Oct 1, 2016

Using queues and offline processing to help speed up your application

Embed Size (px)

Citation preview

Page 1: Using queues and offline processing to help speed up your application

Using Queues and Offline Processing

David Stockton Madison PHP - Oct 1, 2016

Page 2: Using queues and offline processing to help speed up your application

Synchronous Processing

Page 3: Using queues and offline processing to help speed up your application

Shopping Trip

Page 4: Using queues and offline processing to help speed up your application

Car Wash

Page 5: Using queues and offline processing to help speed up your application

DMV

Page 6: Using queues and offline processing to help speed up your application

The Web Should Be Fast

Page 7: Using queues and offline processing to help speed up your application

Customers Want It To Be Fast

Page 8: Using queues and offline processing to help speed up your application

Think Work Order

Page 9: Using queues and offline processing to help speed up your application

Asynchronous Processing

Page 10: Using queues and offline processing to help speed up your application

Real-World Async

Page 11: Using queues and offline processing to help speed up your application

Online Shopping

Page 12: Using queues and offline processing to help speed up your application

Personal Assistants

Page 13: Using queues and offline processing to help speed up your application

Personal Assistants

Page 14: Using queues and offline processing to help speed up your application

Web Pages

Page 15: Using queues and offline processing to help speed up your application

Infinite Scrollers

Page 16: Using queues and offline processing to help speed up your application

Magical Parallelization

Page 17: Using queues and offline processing to help speed up your application

Fake It Until You Make It

Page 18: Using queues and offline processing to help speed up your application

Time Is Not On Our Side

Page 19: Using queues and offline processing to help speed up your application

User Input

Page 20: Using queues and offline processing to help speed up your application

Do The Needful Now Everything Else Can Wait

Page 21: Using queues and offline processing to help speed up your application

Database Queue

Page 22: Using queues and offline processing to help speed up your application

Data Synchronization

Page 23: Using queues and offline processing to help speed up your application

Message Queues

Page 24: Using queues and offline processing to help speed up your application

Work Producer

Page 25: Using queues and offline processing to help speed up your application

Work Producer

Page 26: Using queues and offline processing to help speed up your application

How does that help?

Page 27: Using queues and offline processing to help speed up your application

Workers

Page 28: Using queues and offline processing to help speed up your application

Workers

Page 29: Using queues and offline processing to help speed up your application

Multiplicity

Page 30: Using queues and offline processing to help speed up your application

Control Server Load

Page 31: Using queues and offline processing to help speed up your application

Handle Load Spikes

Page 32: Using queues and offline processing to help speed up your application

Workers as Assistants

Page 33: Using queues and offline processing to help speed up your application

Ballottrax

Page 34: Using queues and offline processing to help speed up your application

Data Loads

Page 35: Using queues and offline processing to help speed up your application

Communication Rates

• Email - 250 connections, 10 per second

• SMS - 30 per second

• Voice - 1 per second, per phone number

Page 36: Using queues and offline processing to help speed up your application

Splitting Work By Type

Page 37: Using queues and offline processing to help speed up your application

Rate Limiting

Page 38: Using queues and offline processing to help speed up your application

Queues to Queue Work

Page 39: Using queues and offline processing to help speed up your application

Acceptable for Work

Page 40: Using queues and offline processing to help speed up your application

Notification Filters

Page 41: Using queues and offline processing to help speed up your application

Geocoding

Page 42: Using queues and offline processing to help speed up your application

Producers Consumers

Page 43: Using queues and offline processing to help speed up your application

DB Queue vs Pure MQ

Page 44: Using queues and offline processing to help speed up your application

Feeding Queue from Database - Cron

Page 45: Using queues and offline processing to help speed up your application

Feeding Queues from Database - Polling

Page 46: Using queues and offline processing to help speed up your application

Polling - The Worst

Page 47: Using queues and offline processing to help speed up your application

Events and Webhooks

Page 48: Using queues and offline processing to help speed up your application

Magical Webhooks

Page 49: Using queues and offline processing to help speed up your application

Automated Pull Requests

Page 50: Using queues and offline processing to help speed up your application

Queues For Webhooks

Page 51: Using queues and offline processing to help speed up your application

Install AMQPLib

composer require php-amqplib/php-amqplib

Page 52: Using queues and offline processing to help speed up your application

Create Queue/Channel$connection = new \PhpAmqpLib\Connection\AMQPStreamConnection($server, $port, $user, $password, $vhost);$channel = $connection->channel(); $channel->queue_declare( $queueName, false, // Passive true, // Durable false, // Exclusive false // Auto Delete);

Page 53: Using queues and offline processing to help speed up your application

Setting Channel Options

$channel->basic_qos(null, 1, null);$channel->basic_consume( $queueName, '', // Consumer tag false, // No local false, // No ACK false, // Exclusive false, // No wait $callback);

Page 54: Using queues and offline processing to help speed up your application

Do Some Workwhile (count($channel->callbacks)) { $channel->wait();}$channel->close();$connection->close();

Page 55: Using queues and offline processing to help speed up your application

The Callback$callback = function ($msg) { $message = (array) json_decode($msg->body, false); // Do work $msg->delivery_info['channel'] ->basic_ack( $msg->delivery_info['delivery_tag'] );};

Page 56: Using queues and offline processing to help speed up your application

Creating a Message$message = new \PhpAmqpLib\Message\AMQPMessage( json_encode($data), [ 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT ]);

Page 57: Using queues and offline processing to help speed up your application

Putting Message in the Queuetry { $channel->basic_publish($message, '', $queueName);} catch (AMQPExceptionInterface $e) { $this->getLogger()->err( 'Error putting message into RabbitMQ', $e->getMessage() ); return false;}

Page 58: Using queues and offline processing to help speed up your application

Work Priority

Page 59: Using queues and offline processing to help speed up your application

Recap

Page 60: Using queues and offline processing to help speed up your application

Questions?

Page 61: Using queues and offline processing to help speed up your application

David Stockton @dstockto

https://davidstockton.com https://tddftw.com