Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous PHP and Why you should care as a PHP Developer

Asynchronous PHP and Why you should care as a PHP Developer

In this talk, we will examine how traditional PHP Applications works and their limitations. We'll look into how Asynchronous PHP proffer solutions to some of these problems.

'Bosun Egberinde

November 16, 2019
Tweet

More Decks by 'Bosun Egberinde

Other Decks in Programming

Transcript

  1. Asynchronous PHP and Why you should care as a PHP

    Developer Olatunbosun Egberinde
  2. About Me... • PHP/JS/Typescript Developer • In a Love Relationship

    with PHP • EEE Student • UNEMPLOYED @bosunski
  3. About The Talk... • Aggregated talks from experts in this

    field • Not a one size fits all talk.
  4. Our Journey • PHP Now • Traditional PHP Apps •

    Async Apps (Why you should care) • Getting Started • The Building Blocks • Applying all these
  5. — Rasmus Lerdorf - PHP’S DAD I saw PHP as

    a thin Templating system … [but] the world disagreed with me. ” “ — Rasmus Lerdorf (PHP’S DAD) @WeAreDevelopers Conference
  6. PHP IN 2019 PHP HAS COME A LONG WAY TO

    EVOLVE INTO SOMETHING REALLY USEFUL AS COMPARED TO ITS INCEPTION • Better Typing System • Robust CLI SAPI • Long Running Processes • FPM • JIT • Preloading • 2x Speed on PHP 7+ All these improvements are paving ways for more possibilities in the aspect of what PHP can be used for.
  7. Even though there’s been a major improvements in this setup,

    PHP Still isn’t what some developers would want on the performance side.
  8. Hence... Some common saying about PHP around performance area. Examples...

    • You can’t really do Concurrent stuffs with PHP • Can I do Async in PHP? • Don’t even do Async with PHP just use X or Y or Z • PHP is too Slow for Modern Apps • Just use another Language for concurrent stuffs.
  9. We are generally used to seeing PHP as SLOW or

    not fitted for some of the demands of this ERA ‍♂
  10. Common solutions Some solutions over time for • Caching (Redis,

    Memcache, SPAs) • Load Balancing/ Auto Scaling • More Hardware
  11. PHP can be better in performance that it currently is

    But… PHP already has what it takes to be better in Performance
  12. SYNCHRONOUS APP ASYNCHRONOUS APP TIME OF REQUEST RESPONSE REQUEST PROCESS

    READ FROM DB RENDER VIEW RESPONSE REQ. B PROCESS READ FROM DB RENDER VIEW RESPONSE REQ. A PROCESS READ FROM DB RENDER VIEW A Request style Sample NOTE: The Asynchronous part was Animated
  13. I/Os Stings Performance... I/Os are very common and they can

    be seen around Apps in different forms. • External API Calls • Reading / Writing to Files • Database Calls • Spawning Child Processes • Add Yours!
  14. The idea of this problem is that CPU Calculations are

    Fast while I/Os are Slow. Check this out ➡
  15. ⬅ Deductions • Time is wasted at I/O • We’re

    wasting TIME! ‍♂ • Latencies Grew at I/Os • While waiting CPU is doing nothing! • Other operations are BLOCKED during waiting.
  16. — Rasmus Lerdorf - PHP’S DAD The trouble is you

    think you have time. ” “ — Budha
  17. • A performant app reduces this waiting time … How?

    • By NOT HAVING to Wait in the First place. • This is CONCURRENCY! • This is an important advantage we get in Asynchronous Applications. • Since NO WAITING is involved, we have an I/O process that is NON-BLOCKING
  18. Some Options we have to do Async Stuffs: • NodeJS

    • GoLang • PHP You don’t have to use other languages if you don’t want to.
  19. PHP doesn’t have constructs within the core for Async, but

    it does have what it takes to do async stuffs within itself.
  20. React PHP • Based on Reactor Pattern • Event Loop

    • Streams • Promises • No extension Required • Written in PURE PHP React PHP is a low level library for Event Driven programming in PHP • PHP 5.3 Up! • React PHP before React JS • https://reactphp.org
  21. AMP PHP • Streams • Synchronous like • No extension

    Required • Written in PURE PHP • PHP 5.3 Up! Amp is a Non-Blocking concurrency framework for PHP. • Coroutines Based • Event Loop • Promises • Async Iterators
  22. Swoole PHP • NO PROMISES • https://swoole.co.uk Coroutine Asynchronous Programming

    framework for PHP • Coroutine Based • Extension Based • Written in C! • Channels
  23. Event Loop - The CORE • Runs till: • App

    has nothing to do • Stopped • Errors Out • The Scheduler • Created ONCE • Run ONCE • Based on Events • It’s a Task Scheduler • Handles SIGNALS
  24. Streams • On Demand Data Access • Processing of Data

    in Chunks • Provides better wrapping for streams better than native PHP implementation for Non-Blocking I/O • Readable Stream • Writable Streams • Through Sreams • Duplex Streams
  25. Promises • 3 States ◦ Rejected ◦ Resolved ◦ Pending

    • Like Promise/A+ in JavaScript The eventual result of a deferred computation or an Asynchronous Operation
  26. Using Async in Traditional Apps Some of these Async tools

    are quite suitable for use in a Synchronous app Promise based tools can be used in synchronous apps using clue/react-block
  27. Caution 1: Not all of these async Tools can be

    used directly in a synchronous environment.
  28. Caution 3: This is not a new thing in town

    that must be used, if your app is better off without Async, just stick with it.
  29. Building out some services on Async PHP • HTTP Clients

    • WebSocket Servers • Message Queues • PHP PM (Load Balancer)
  30. Going full blown on Async PHP • Async HTTP Server

    • Async Database Connectors • Stream based Filesystem Abstraction • Streams • STD I/O • Promises for Deferred Executions • WebSocket Server • TCP Server • Load Balancer • Streams • STD I/O
  31. Not possible... • No PDO or MySQLi • File_get_contents, …

    etc • NO Guzzle (Or use with React Adapter) • Not all Profilers are USEFUL
  32. The Async community is still growing, hence, you won’t see

    much tool around as you’re used to in Synchronous apps.
  33. Some Takeaways • You don’t have to use other languages

    if you need some Async in your Apps • PHP is Here to Stay • You can reduce Blocking Operations by replacing Synchronous Implementations with Async Implementations when Performance is a concern. • I/Os forms a major part of our Applications and MOST Times they constitutes what makes our apps slow because of blocking operations.