+ All Categories
Home > Software > PHP Conference 2016: Daemons, workers e bots com o ReactPHP

PHP Conference 2016: Daemons, workers e bots com o ReactPHP

Date post: 06-Jan-2017
Category:
Upload: aryel-tupinamba
View: 108 times
Download: 1 times
Share this document with a friend
41
Daemons, workers e bots com o Aryel Tupinambá - PHP Conference 2016
Transcript
Page 1: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

Daemons, workers e bots com o

Aryel Tupinambá - PHP Conference 2016

Page 2: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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á

Page 3: PHP Conference 2016: Daemons, workers e bots com o ReactPHP
Page 4: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

Asynchronous? Event-driven?

Page 5: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

Sincronismo vs. Assíncronismo

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

Blocking I/O

Page 6: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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

Page 7: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

Sincronismo vs. Assíncronismo

Request blocking I/O operation (file read) Response

1x Thread

Non-Blocking I/O

Request pool

Page 8: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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

Page 9: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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 :/

Page 10: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

Sincronismo vs. Assíncronismo

Page 11: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

Sincronismo vs. Assíncronismo

Page 12: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

O PHP tradicional

Apache + mod_php Nginx + PHP-FPM

Page 13: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

O PHP tradicional - mod_php

Apache Create thread Load mod_php.dll Execute ResponseRequest

Page 14: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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

Page 15: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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

Page 16: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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

Page 17: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

O PHP tradicional - PHP-FPM

Nginx PHP-FPM FPM Pool Execute ResponseRequest

FPM Pool

Page 18: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

O PHP tradicional - PHP-FPM

Nginx PHP-FPM FPM Pool Execute ResponseRequest

FPM PoolRequest Execute Response

Page 19: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

O PHP tradicional - PHP-FPM

Nginx PHP-FPM FPM Pool Execute ResponseRequest

FPM PoolRequest Execute Response

Request Execute Response

Execute ResponseRequest

Page 20: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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

Page 21: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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)

Page 22: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

O problema C10K10 mil requests concorrentes

Page 23: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

A era do async

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

Page 24: PHP Conference 2016: Daemons, workers e bots com o ReactPHP
Page 25: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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

Page 26: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

I/O blocking

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

Page 27: PHP Conference 2016: Daemons, workers e bots com o ReactPHP
Page 28: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

while(true)

Page 29: PHP Conference 2016: Daemons, workers e bots com o ReactPHP
Page 30: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

O ReactPHP

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

Page 31: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

O ReactPHP

EventLoop

Stream

Socket

HttpServer

Page 32: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

32

EventLoop

Stream

Socket

HttpServer

Page 33: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

33

EventLoop

Stream

Socket

HttpServer

Server

Client

Page 34: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

34

EventLoop

Stream

Socket

HttpServer

cat cool_stuff | grep php

cat

stdin

stdout

stderr

grep

stdin

stdout

stderr

Page 35: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

35

EventLoop

Stream

Socket

HttpServer

Page 36: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

36

EventLoop

Stream

Socket

HttpServer

Page 37: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

37

EventLoop

Stream

Socket

HttpServer

Page 38: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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)

Page 39: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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

Page 40: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

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

Page 41: PHP Conference 2016: Daemons, workers e bots com o ReactPHP

Obrigado!Dúvidas?

Telegram: @DfKimeraE-mail / Hangouts: [email protected]: http://facebook.com/aryel.tupinambaTwitter: http://twitter.com/DfKimeraLinkedIn: http://linkedin.com/in/aryeltupinamba

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

http://lqdi.net


Recommended