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. • Increase requests per second • Increase speed of response

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

    • Consistent despite increasing traffic What is Scaling Scaling Out Goal: Consistency
  3. Optimization • Eager Loading (n+1 problem) • Object Caching (decorator

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

    Collection • + 1 Additional query per object in Collection
  5. Optimization Eager Loading • First Query: Grab all topics (+1

    query) • Foreach Topic: Grab Series (+3 queries) • Foreach Series: Grab Content (+6 queries)
  6. Optimization Object Caching Decorators • Add behavior w/out modifying code

    • Interface • Modifier(s) • Root Object (gross)
  7. 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
  8. 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!
  9. 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
  10. Optimization Opcache • Reduce CPU and file I/O per request

    • Small trade off in RAM usage • Really a no-brainer Cache PHP File Bytecode
  11. 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
  12. Optimization Specialization • Give MySQL it’s own server*** • Give

    Redis it’s own server • Give workers their own server • etc Split Out Services:
  13. Optimization Review: Optimization PHP-FPM: Process Management Split Servers: Esp MySQL

    “n+1 problem”: Eager Loading Object Caching: Decorators
  14. Scaling Out Load Balancing • Spread traffic across multiple servers

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

    Servers • … And everything else… Load Balancing
  16. 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
  17. Scaling Out Needed For: • Correct client detection • cookies,

    CSRF token Trusted Proxy • Correct URL & form URI generation • url(), action() and similar helpers
  18. 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
  19. Queues Scaling Out • Put workers on a server sized

    for their purpose • Scale workers separately Segmentation
  20. • 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