php conference 2016: daemons, workers e bots com o reactphp

Post on 06-Jan-2017

108 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Daemons, workers e bots com o

Aryel Tupinambá - PHP Conference 2016

Sobre o palestrante

Co-fundador e CTO da LQDI DigitalProjetos para empresas como Porto Seguro, Nestlé, Ticket, Editora FTD, Tishman Speyer e Ambev

13 anos trabalhando com PHPDesde a época que o PHPClasses era a onda :)

Aryel Tupinambá

Asynchronous? Event-driven?

Sincronismo vs. Assíncronismo

Request blocking I/O operation (file read) Response1x Thread

Blocking I/O

Sincronismo vs. Assíncronismo

Request blocking I/O operation (file read) Response1x Thread

Request blocking I/O operation (file read) Response1x Thread

Request blocking I/O operation (file read) Response1x Thread

Blocking I/O

Sincronismo vs. Assíncronismo

Request blocking I/O operation (file read) Response

1x Thread

Non-Blocking I/O

Request pool

Sincronismo vs. Assíncronismo

Request blocking I/O operation (file read) Response

1x Thread

Non-Blocking I/O

Request pool

blocking I/O operation (DB read)

blocking I/O operation (file read)

Response

Response

Sincronismo vs. Assíncronismo

Assíncrono != ParaleloAssíncrono = Execução sob demanda

Assíncrono = Menos tempo perdido aguardando retorno

Paralelismo só é possível com uma aplicação multi-thread (como seu browser)Nem NodeJS, nem PHP são multi-thread :/

Sincronismo vs. Assíncronismo

Sincronismo vs. Assíncronismo

O PHP tradicional

Apache + mod_php Nginx + PHP-FPM

O PHP tradicional - mod_php

Apache Create thread Load mod_php.dll Execute ResponseRequest

O PHP tradicional - mod_php

Apache Create thread Load mod_php.dll Execute ResponseRequest

Create thread Load mod_php.dll Execute ResponseRequest

Create thread Load mod_php.dll Execute ResponseRequest

Create thread Load mod_php.dll Execute ResponseRequest

O PHP tradicional - mod_php

Apache Create thread Load mod_php.dll Execute ResponseRequest

Create thread Load mod_php.dll Execute ResponseRequest

Create thread Load mod_php.dll Execute ResponseRequest

Create thread Load mod_php.dll Execute ResponseRequest

4 requests, 4 threads

O PHP tradicional - mod_php

Apache Create thread Load mod_php.dll Execute ResponseRequest

Create thread Load mod_php.dll Execute ResponseRequest

Create thread Load mod_php.dll Execute ResponseRequest

100 requests, 100 threads

Create thread Load mod_php.dll Execute ResponseRequest … x100

O PHP tradicional - PHP-FPM

Nginx PHP-FPM FPM Pool Execute ResponseRequest

FPM Pool

O PHP tradicional - PHP-FPM

Nginx PHP-FPM FPM Pool Execute ResponseRequest

FPM PoolRequest Execute Response

O PHP tradicional - PHP-FPM

Nginx PHP-FPM FPM Pool Execute ResponseRequest

FPM PoolRequest Execute Response

Request Execute Response

Execute ResponseRequest

O PHP tradicional - PHP-FPM

Nginx PHP-FPM FPM Pool Execute ResponseRequest

FPM PoolRequest Execute Response

Request Execute Response

Execute ResponseRequest

4 requests, 2 threads

O PHP tradicional - PHP-FPM

Nginx PHP-FPM FPM Pool Execute ResponseRequest

FPM PoolRequest Execute Response

Request Execute Response

Execute ResponseRequest

… x100

100 requests, n threads (~4 a 8 threads)(n = qtd. configurada de pools, geralmente qtd. de cores na CPU)

O problema C10K10 mil requests concorrentes

A era do async

- Programação reativa a eventos- Microservices- Queuing (enfileiramento)

Code shamelessly copied from Igor's PHPNW 2012 talk :)

I/O blocking

Code shamelessly copied from Igor's PHPNW 2012 talk :)

while(true)

O ReactPHP

- Baseado em streams (stream_select) ou libevent (PECL)- Arquitetura em camadas- Event Loop: enfileiramento para eventos assíncronos

O ReactPHP

EventLoop

Stream

Socket

HttpServer

32

EventLoop

Stream

Socket

HttpServer

33

EventLoop

Stream

Socket

HttpServer

Server

Client

34

EventLoop

Stream

Socket

HttpServer

cat cool_stuff | grep php

cat

stdin

stdout

stderr

grep

stdin

stdout

stderr

35

EventLoop

Stream

Socket

HttpServer

36

EventLoop

Stream

Socket

HttpServer

37

EventLoop

Stream

Socket

HttpServer

Problemas

- Grande maioria das funções do PHP é I/O-blocking- PDO

- File I/O

- cURL

- Como resolver:- Usar alternativas assíncronas (mysqli / mysql-ng)

- Reimplementação de forma assíncrona

- O React faz isso com o React\HttpClient e o React\DNS

- Delegar a resolução para outros workers e aguardar resposta

(modelo Promises)

Quando usar o ReactPHP

- Microservices- Serviços que precisem de throughput alto

- Serviços com chamadas assíncronas

- Serviços em tempo real (chats, notificações, etc)

- Daemons e workers- Execução de jobs enfileirados

- Interface HTTP para sistemas stateless (microcontroladores,

Raspberry PI, drones, porta serial, etc)

- Chat bots- Se encaixa perfeitamente ao modelo assíncrono de

Request/Response

Gostei, quero ver mais!

ReactPHP

http://reactphp.org/

Pushing the limits of PHP with React PHP por Christian Lückhttps://speakerdeck.com/clue/pushing-the-limits-of-php-with-react-php

How I (ab)used php with the help of ReactPHP por Stijn Vannieuwenhuysehttps://speakerdeck.com/stijnvnh/how-i-ab-used-php-with-the-help-of-reactphp

Obrigado!Dúvidas?

Telegram: @DfKimeraE-mail / Hangouts: aryel.tupinamba@lqdi.netFacebook: http://facebook.com/aryel.tupinambaTwitter: http://twitter.com/DfKimeraLinkedIn: http://linkedin.com/in/aryeltupinamba

Slides da palestra: http://slideshare.net/aryeltupinamba

http://lqdi.net

top related