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

Parallel Processing with Daemons in PHP - Split...

Lucas Mendes
November 02, 2017

Parallel Processing with Daemons in PHP - Splitting Big Problems Into Smaller Pieces

Sometimes we need to process large amounts of data and using the conventional architectural style used by PHP is not a simple thing because the language is synchronous by its nature, so this talk aims to get new insights about the applications architectures giving you new ways to handle this kind of task.

Presented at Campus Party Minas Gerais 2017.

Lucas Mendes

November 02, 2017
Tweet

More Decks by Lucas Mendes

Other Decks in Programming

Transcript

  1. Lucas Mendes | Software Architect | @devsdmf PARALLEL PROCESSING 


    WITH DAEMONS IN PHP SPLITTING BIG PROBLEMS INTO SMALLER PIECES
  2. PARALLEL PROCESSING WITH DAEMONS IN PHP AGENDA ▸ Parallel Computing

    ▸ Daemons ▸ A bit of PHP ▸ The Problem to Solve ▸ The Implementation ▸ Implementing a Daemon in PHP ▸ Code time
  3. PARALLEL COMPUTING IS A TYPE OF COMPUTATION IN WHICH MANY

    CALCULATIONS OR THE EXECUTION OF PROCESS ARE CARRIED SIMULTANEOUSLY. Wikipedia. PARALLEL PROCESSING WITH DAEMONS IN PHP
  4. A DAEMON IS A LONG-RUNNING BACKGROUND PROCESS THAT ANSWERS REQUESTS

    FOR SERVICES. Indiana University - Knowledge Base PARALLEL PROCESSING WITH DAEMONS IN PHP
  5. PARALLEL PROCESSING WITH DAEMONS IN PHP A DAEMON SHOULD… ▸

    Always acts as a background process ▸ Not allow direct user interaction ▸ Postfix the process name with the letter “d” (i.e. httpd, syslogd…) ▸ Respond to signals sent by other process or by the operating system ▸ Exit nicely ▸ Restart if needed
  6. PHP

  7. PARALLEL PROCESSING WITH DAEMONS IN PHP PHP OVERVIEW ▸ PHP:

    Hypertext Preprocessor ▸ Created in 1994 by Rasmus Lerdof ▸ Interpreted, imperative, procedural, object-oriented ▸ Dynamic type ▸ Synchronous by nature ▸ Single process and single threaded
  8. PARALLEL PROCESSING WITH DAEMONS IN PHP THE PROBLEM ▸ Every

    month Correios updates its CEP database ▸ Every month we need to update our database too ▸ There are more than 1M CEPs until now ▸ The provide this database in the CSV format ▸ The CSV file has 94.4MB of plain text
  9. PARALLEL PROCESSING WITH DAEMONS IN PHP CONVENTIONAL PHP - CONS

    ▸ Long process time ▸ Blocks the UI ▸ Blocks the I/O ▸ Possible memory leaks ▸ If fails, needs user retry ▸ Browser timeout can abort the process
  10. PARALLEL PROCESSING WITH DAEMONS IN PHP USING ASYNC PROGRAMMING -

    PROS ▸ Async Implementation ▸ Don’t block the I/O
  11. PARALLEL PROCESSING WITH DAEMONS IN PHP USING ASYNC PROGRAMMING -

    CONS ▸ Long process time ▸ Blocks the UI ▸ Possible memory leaks ▸ If fails, needs user retry ▸ Browser timeout
  12. PARALLEL PROCESSING WITH DAEMONS IN PHP MULTI-THREADED PHP - PROS

    ▸ Faster than procedural implementation ▸ Better usage of serve resources
  13. PARALLEL PROCESSING WITH DAEMONS IN PHP MULTI-THREADED PHP - CONS

    ▸ Requires PHP 7.2+ w/ ZTS ▸ Extension dependent ▸ Blocks the UI ▸ Blocks the I/O ▸ If fails, needs user retry
  14. PARALLEL PROCESSING WITH DAEMONS IN PHP USING CRONTAB - PROS

    ▸ Don’t block the UI ▸ If fails, can retry in next batch
  15. PARALLEL PROCESSING WITH DAEMONS IN PHP USING CRONTAB - CONS

    ▸ Time based ▸ Blocks the I/O ▸ Bad server resource usage ▸ Possible memory leaks
  16. PARALLEL PROCESSING WITH DAEMONS IN PHP USING GEARMAN - PROS

    ▸ Abstracts the architecture ▸ Faster than single-process implementation ▸ Don’t block the UI ▸ Don’t block the I/O
  17. PARALLEL PROCESSING WITH DAEMONS IN PHP USING GEARMAN - CONS

    ▸ Not too easy to implement ▸ Needs to configure and deploy a Gearman server ▸ Extension dependent ▸ External libraries dependent (libgearman, libevent, uuid) ▸ Doesn’t support newer versions of PHP (only 5.3 to 5.6) ▸ Not currently maintained
  18. PROCESS CONTROL SUPPORT IN PHP IMPLEMENTS THE UNIX STYLE OF

    PROCESS CREATION, PROGRAM EXECUTION, SIGNAL HANDLING AND PROCESS TERMINATION. PHP Official Documentation PARALLEL PROCESSING WITH DAEMONS IN PHP
  19. PARALLEL PROCESSING WITH DAEMONS IN PHP USING PCNTL - PROS

    ▸ Easy to implement ▸ Architecture control ▸ Faster than single-process implementation ▸ Don’t block the UI ▸ Don’t block the I/O ▸ Native in PHP, needs to be compiled with, but still native
  20. PARALLEL PROCESSING WITH DAEMONS IN PHP ZOMBIES VS ORPHANS ▸

    Zombies are dead ▸ Orphans are children whose parents has died
  21. SIGNALS ARE A LIMITED FORM OF IPC, TYPICALLY USED IN

    UNIX, UNIX-LIKE AND OTHER POSIX-COMPLIANT OPERATING SYSTEMS. A SIGNAL IS AN ASYNCHRONOUS NOTIFICATION SENT TO A PROCESS TO NOTIFY IT OF AN EVENT THAT OCCURRED. Wikipedia. PARALLEL PROCESSING WITH DAEMONS IN PHP
  22. PARALLEL PROCESSING WITH DAEMONS IN PHP SOME SIGNALS AVAILABLE… ▸

    SIGCHLD ▸ SIGHUP ▸ SIGINT ▸ SIGKILL ▸ SIGTERM ▸ So many others…
  23. THANK YOU! Lucas Mendes
 Software Architect at Tienda Nube
 about.me/devsdmf

    We're hiring, join the crew! 
 bit.ly/work-at-tiendanube