72
Developing highly scalable applications with Symfony and RabbitMQ Alexey Petrov [email protected]

Developing highly scalable applications with Symfony and RabbitMQ

Embed Size (px)

DESCRIPTION

Developing highly scalable applications with Symfony and RabbitMQ

Citation preview

Page 1: Developing highly scalable applications with  Symfony and RabbitMQ

Developing highly scalable

applications with

Symfony and RabbitMQ

Alexey Petrov

[email protected]

Page 2: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Agenda

• Story 1: For those who not used queueing before

• Story 2: For those who started using queueing

• Story 3: For those who have to scale and distribute

• Story 4: Symfony2 Integration

Page 3: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Story 1

For those who not used queueing before

Page 4: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Request

ResponseTime

P

R

O

C

E

S

S

I

N

G

Page 5: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Time min

Muscules

(better hardware)

Brains

(better software)

Page 6: Developing highly scalable applications with  Symfony and RabbitMQ

Prepare

Response

Symfony and RabbitMQ

Request

ResponseTime

Parsing

Request

P

R

O

C

E

S

S

I

N

G

Page 7: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Request

Response

Time

Parsing

Request

Prepare

Response

P

R

O

C

E

S

S

I

N

G

Page 8: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Request

Response

Time

Parsing

Request

Prepare

Response Task 1 Task 2

Page 9: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

What is asynchronous?

Page 10: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Page 11: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

How to run asynchronous

process from PHP

Page 12: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

• popen

• proc_open

• shell_exec

• system

• exec

Page 13: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Symfony Process

Page 14: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Page 15: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Page 16: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Page 17: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Problems?

Page 18: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

1 Request = X tasks

N Requests = N*X tasks

Page 19: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

Worker

Page 20: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

Worker

Database Cron Job

Page 21: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Problems?

Page 22: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

Worker

Producer

Producer

Producer

Page 23: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

T

A

S

K

WorkerProducer

Producer

Producer

Worker

Worker

Page 24: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Solution:

USE RIGHT TOOLS!

Page 25: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Use RabbitMQ

Page 26: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Page 27: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Story 2

For those who started using queueing

Page 28: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Basic Examples

http://www.rabbitmq.com/getstarted.html

Page 29: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Connection and Channels

Page 30: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

COOL STORY BRO

But we’re in PHP :(

Page 31: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Exchanges To Exchanges

Page 32: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Exchanges and Queues

FanoutDB1

DB2

Msg Topic Stats

Email

SMSTopic

Page 33: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Libraries

• PhpAmqpLibhttps://github.com/videlalvaro/php-amqplib

• PECL amqphttp://php.net/manual/pl/book.amqp.php

https://github.com/alanxz/rabbitmq-c

https://github.com/bkw/pecl-amqp-official/tree/master/stubs

Page 34: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

PECL amqp

https://bugs.php.net/bug.php?id=66661

Page 35: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Durability

Page 36: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Durability

• Exchanges

• Queues

• Messages

Page 37: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Durability

Page 38: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Transactions

Page 39: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Transactions

Page 40: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

4 minutes to publish

10 000 messages

Page 41: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Publisher Confirms

Page 42: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Publisher Confirms

Page 43: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Publisher Confirms

Page 44: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Dead Letter Exchanges

Page 45: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Dead Letter Exchanges

• Delayed Messages (Events)

• Retries

• Errors processing

• etc.

Page 46: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Dead Letter Exchanges

• The message is rejected

(basic.reject or basic.nack)

with requeue=false

• The TTL for the message expires;

• The queue length limit is exceeded.

Page 47: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Dead Letter Exchanges

Page 48: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Dead Letter Messages

x-death header:

queue – name of the queue the message was in before

reason – rejected | expired | maxlen

time – timestamp the message was dead lettered

exchange – the exchange the message was published to

routing-keys – the routing keys the message was published with.

original-expiration – the original expiration message property

Page 49: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Delayed Pattern

MSG ExchangeQ1

TTL = 60000

DLX Q2

Page 50: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

More Queues = Better

Queue lives in 1 Eralng process

Page 51: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Per-Message TTL

vs

Per-Queue TTL

Page 52: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Page 53: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Story 3

For those who have to scale and distribute

Page 54: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Clustering

Page 55: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Clustering

Page 56: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

1 client = 1 socket

Cluster helps to scale!

Page 57: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Mirrored queues

Page 58: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Mirrored queues

Page 59: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Network Partitions

Page 60: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Network Partitions

• Ignore mode

• Pause-minority mode

• Autoheal mode

Page 61: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Federation

Page 62: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Federation

Page 63: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Federated Exchanges

Page 64: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Federated Queues

Page 65: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Shovel

Page 66: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

The Symfony

Page 67: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Story 4

Symfony2 Integration

Page 68: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Installing Bundle

Page 69: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Configuration

Page 70: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Producer

Page 71: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Consumer

Page 72: Developing highly scalable applications with  Symfony and RabbitMQ

Symfony and RabbitMQ

Questions?

Alexey Petrov

Senior PHP Developer / System Architect

Email: [email protected]

Twitter: @alexhelkar