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

Scaling Laravel - Laracon.net 2018

Chris Fidao
February 07, 2018

Scaling Laravel - Laracon.net 2018

Chris Fidao

February 07, 2018
Tweet

More Decks by Chris Fidao

Other Decks in Technology

Transcript

  1. Scaling Laravel the talk!

  2. What is Scaling? Optimization Scaling Out

  3. • Increase requests per second • Increase speed of response

    (reduce response time) • Decrease resource usage • Secondary Goal: Less Servers! What is Scaling Optimization Goal: Efficiency
  4. • Consistent requests per second • Consistent speed of response

    • Consistent despite increasing traffic What is Scaling Scaling Out Goal: Consistency
  5. Optimization

  6. Optimization • Eager Loading (n+1 problem) • Object Caching (decorator

    pattern) • PHP-FPM (process management) • OpCache • Server Specialization (mysql optimization)
  7. Optimization Eager Loading “n+1 problem” • Initial query returning a

    Collection • + 1 Additional query per object in Collection
  8. Optimization Eager Loading 3 Topics 6 Video Series

  9. Optimization Eager Loading +1 queries Lazy Loading

  10. Optimization Eager Loading +6 queries +3 queries Total Queries: 10

  11. Optimization Eager Loading • First Query: Grab all topics (+1

    query) • Foreach Topic: Grab Series (+3 queries) • Foreach Series: Grab Content (+6 queries)
  12. Optimization Eager Loading -2 queries

  13. Optimization Eager Loading -5 queries nested!

  14. Optimization Object Caching Reduce DB Queries + Processing

  15. Optimization Object Caching

  16. Optimization Object Caching Decorators • Add behavior w/out modifying code

    • Interface • Modifier(s) • Root Object (gross)
  17. Optimization Object Caching An Interface Repository!

  18. Optimization Object Caching Build Our Original Behavior

  19. Optimization Object Caching Get it Working

  20. Optimization Object Caching A cache decorator! magic!

  21. Optimization Object Caching TopicCache::topics( TopicQuery::topics( Topic::all(); ); )

  22. Optimization Object Caching Update Service Provider We “wrap” TopicQuery with

    decorator TopicCache.
  23. Optimization Object Caching You’re empowered to do fun things!

  24. Optimization Review: Code Optimization “n+1 problem”: eager loading object caching:

    decorators
  25. Optimization PHP-FPM Optimizing PHP-FPM Process Management

  26. Optimization PHP-FPM • Enable more requests per second • Trade-off

    between more requests and CPU/RAM usage • Monitor your server, even if just looking at `top` or `htop` commands (preferably under load) Process Management pm.max_children
  27. Optimization PHP-FPM Rule of Thumb (RAM - Overhead) / Memory

    per Request (2048mb - 512mb) / 50mb = ~30 max processes (2048mb - 1024mb) / 50mb = ~20 max processes *check CPU usage after making changes!
  28. Optimization PHP-FPM /etc/php/7.[1|2]/fpm/pool.d/www.conf Limits your req/sec directly (but w/ RAM/CPU

    trade off) Speeds up “startup time” Keep spares around But not too many Limit potential problems
  29. Optimization PHP-FPM apt-get install htop

  30. Optimization Opcache Opcache enabled

  31. Optimization Opcache • Reduce CPU and file I/O per request

    • Small trade off in RAM usage • Really a no-brainer Cache PHP File Bytecode
  32. Optimization Opcache /etc/php/7.[1|2]/fpm/php.ini

  33. Optimization Opcache /etc/php/7.[1|2]/fpm/php.ini

  34. Optimization Opcache • Must reload PHP-FPM after any code change

    • (e.g. after any deploy) • (you’re not editing code files in production, right?) Caveat: sudo service php7.2-fpm reload
  35. Optimization Review: Server Optimization PHP-FPM: Process Management Opcache: Free Performance

    Boost
  36. Optimization Specialization • Give MySQL it’s own server*** • Give

    Redis it’s own server • Give workers their own server • etc Split Out Services:
  37. Optimization MySQL Optimization tools.percona.com/wizard

  38. Optimization MySQL Optimization tools.percona.com/wizard Caches + InnoDB: The interesting stuff

  39. Optimization Review: Optimization PHP-FPM: Process Management Split Servers: Esp MySQL

    “n+1 problem”: Eager Loading Object Caching: Decorators
  40. Scaling Out

  41. • Load Balancing (Considerations) • Laravel + Proxies (Trusted Proxy)

    • Queues (tricks!) Scaling Out
  42. Scaling Out Load Balancing • Spread traffic across multiple servers

    • Remember: This is about consistency, not speed! • Also, caveats!
  43. Scaling Out Caveats • More Servers! • LB + App

    Servers • … And everything else… Load Balancing
  44. Scaling Out Caveats Load Balancing • Decentralizing Services • Redis

    (session/object cache) • MySQL* • Any other services (search, workers, cron) • Decentralizing Files • Sessions • User uploads: S3, NFS
  45. Scaling Out Load Balancing Example: HAProxy Load Balancing

  46. Scaling Out HAProxy /etc/haproxy/haproxy.cfg

  47. Scaling Out HAProxy /etc/haproxy/haproxy.cfg

  48. Scaling Out Example: Laravel + Proxies (e.g. load balancers) Load

    Balancing
  49. Scaling Out Trusted Proxy

  50. Scaling Out Trusted Proxy

  51. Scaling Out Trusted Proxy X-Forwarded-* headers

  52. Scaling Out Laravel app/Http/Middleware/TrustProxies.php Load balancer’s IP address here 172.31.0.238

  53. Scaling Out Laravel 172.31.0.238

  54. Scaling Out Trusted Proxy X-Forwarded-* header values used!

  55. Scaling Out Needed For: • Correct client detection • cookies,

    CSRF token Trusted Proxy • Correct URL & form URI generation • url(), action() and similar helpers
  56. Queues

  57. Queues Scaling Out • Don’t make users wait • Remove

    artificial timeout and memory constraints • Scale work across many servers Work outside of the HTTP cycle
  58. Scaling Out Queues Even if you’re on one server! .env

  59. Scaling Out Queues Personal Favorite: SQS Driver *CHEAP*

  60. Scaling Out Queues Personal Favorite: SQS Driver **VERY CHEAP**

  61. Scaling Out Queues Trick 1: Priority

  62. Scaling Out Queues Trick 2: Segmentation Separate Queues

  63. Scaling Out Queues Trick 2: Segmentation: Separate Workers

  64. Queues Scaling Out • Put workers on a server sized

    for their purpose • Scale workers separately Segmentation
  65. • Optimization vs Scale Out • Optimization • Eager Loading

    + n+1 • Caching • PHP-FPM • Opcache • Specialization • MySQL Optimization • Scale Out • LB Configuration • Headers/Trusted Proxy • Queues What We Covered Scaling Laravel
  66. More! Scaling Laravel • scalinglaravel.com • serversforhackers.com • @fideloper