Asynchronous PHP Myth? Reality! Alexander Lisachenko, Head of Sowtware Architecture at Alpari (RU) About me About me Alexander Lisachenko • Software Architect at Alpari • Passionate developer • Author of Go! Aspect-Oriented framework for PHP • Symfony2 prophet (SOA, DI, IoC, Varnish) Coordinating over 30 instances of Symfony2 in Alpari, including www.alpari.ru Twitter: @lisachenko Github: https://github.com/lisachenko Technology Evolution Technology Evolution Personal Home Pages Simple blogs E-Commerce markets Social applications Highload What should we expect in the nearest future? Technology trends More Complex systems More data (Big Data) More Faster processing More Scalable systems More Real-Time (OLTP) PHP World Simple example What if sharding? What if unavailable? Highload? Blocking IO IO (TCP) buyAction IO (DB) Withdrawal Process IO (Mail) What is the solution? Asynchronous PHP • Soap one-way calls • DB: reap_async_query, • CLI processes: exec, mysqli_poll, popen, proc_open pg_send_query • CLI signals: • Selecting: pcnt_signal_dispatch, curl_multi_select, ticks, socket_select, register_tick_function stream_select, • Message Queues stream_set_blocking • Threads (pthreads extension) One-way SOAP call One-way SOAP call No output is defined for the operation One-way SOAP call Works only in WSDL-mode, so need an WSDL (Zend_AutoDiscover): Only “input” part is defined for one-way call One-way SOAP call – client side Positive • Immediately returns • Service architecture • Asynchronous Negative • Can not handle errors • Unreliable CLI tools Processes Processes. Elementary Positive • Asynchronous • Can be used inside web requests Negative • No return values • No callbacks • Detect completion? Processes. Intermediate Non-blocking stream, we can query it later asynchronously with stream_select() Processes. Intermediate Positive stream_select() Asynchronous goodness • • Full control over IO • Non-blocking mode Negative • A bit complex POSIX signals • No callbacks • Sync loops Processes. POSIX signals Processes. POSIX signals Process 1 • posix_kill($pid2, SIGHUP) UNIX Kernel Process 2 • pcnt_signal() • pcnt_signal_dispatch() Signal handling issue in the code PHP will hang until loop is finished and will ignore signals Asynchronous signal handling Enable support for tick handler and async signal handling. Asynchronous PHP. Enterprise way. Asynchronous PHP. Enterprise way. Publishers Subscribers PHP App1 PHP App2 PHP App4 M e s s a g e Q u e u e PHP App3 PHP App5 PHP App6 STOMP, WebSocket, AMQP Asynchronous PHP. Enterprise way. Asynchronous PHP. Enterprise way. Positive • • • • Negative • Require SOA • Complex solution Scalability Automatic failover Cross-platform Low coupling Asynchronous data access in PHP Asynchronous data access in PHP Like typical stream_select() call Asynchronous PHP. Cutting-edge. Asynchronous PHP. Cutting-edge. True threads in PHP! Just call a function in the separate thread Thread syncrhronization Asynchronous PHP. Cutting-edge. yields a threadcoroutine Asynchronous PHP. Cutting-edge. Threaded asynchronous socket server Non-blocking server Our threaded server Effective epoll() loop in libevent Threaded asynchronous socket server Non-blocking connection Threaded asynchronous socket server Each connection is handled in separate thread Separate loop for each connection Thanks! Questions? Alexander Lisachenko Twitter: https://twitter.com/lisachenko Github: https://github.com/lisachenko