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

High Concurrency Web Architecutre and Laravel Performance Tuning

Albert Chen
January 07, 2023

High Concurrency Web Architecutre and Laravel Performance Tuning

LaravelConf Taiwan 2019

Albert Chen

January 07, 2023
Tweet

More Decks by Albert Chen

Other Decks in Technology

Transcript

  1. High Concurrency Web Architecture
    and Laravel Performance Tuning
    @LaravelConf Taiwan 2019
    By Albert Chen

    View Slide

  2. About Me
    • Albert Chen
    • Software Engineer
    • M17 HandsUp
    • Open Source Maintainer

    View Slide

  3. What Is High Concurrency?

    View Slide

  4. What Is High Concurrency?
    PV = 2,000,000 (200w)
    QPS = (200w x 0.8) / (24 x 3600 x 0.2)
    93 QPS 😀

    View Slide

  5. What Is High Concurrency?
    2,000,000 (200w) in 1 minute
    QPS = 200w / 60
    33333 QPS😭

    View Slide

  6. What Is High Concurrency?
    Read in High Concurrency
    • Vertical Scaling
    • Horizontal Scaling
    • Content Delivery Network
    • Data Caching
    • Single Point Optimization
    Write in High Concurrency
    • Message Queue
    • Rate Limit
    • Maximum Serving Limit
    • Wait in Line
    • Rules Optimization

    View Slide

  7. Performance Tuning
    in Laravel

    View Slide

  8. Performance Tuning in Laravel
    • Route and Config Files Cache
    • Cache (Not Limited to Database)
    • Async Event (Delayed Processing)
    • Database Read/Write Separation
    • OPCache
    • Preloading in PHP 7.4

    View Slide

  9. Route and Config Files Cache
    php artisan route:cache

    View Slide

  10. Route and Config Files Cache
    Register
    Route
    Route
    Register
    Route
    Route
    Register
    Route
    Route
    Route
    Collection
    • Lifecycle for Registering Routes

    View Slide

  11. Route and Config Files Cache
    Unserialize
    Cached
    Serialized
    Route Collection
    Route
    Collection
    • Lifecycle for Routes Caching

    View Slide

  12. Route and Config Files Cache
    Route Numbers No cache (ms) Cache (ms) Speed-up
    1 2.5 1.9 1.3x
    10 5.2 2.6 2.0x
    100 22.5 4.3 5.3x
    1,000 166 32 5.1x
    10,000 1,513 334 4.5x
    https://voltagead.com/laravel-route-caching-for-improved-performance/

    View Slide

  13. Route and Config Files Cache
    https://voltagead.com/laravel-route-caching-for-improved-performance/

    View Slide

  14. Route and Config Files Cache
    php artisan config:cache

    View Slide

  15. Route and Config Files Cache
    Merged
    Config File
    • Lifecycle for Config Caching
    Config

    Files

    View Slide

  16. Benchmark Comparison
    • Provider: Google Cloud Platform
    • Instance Type: n1-standard-8
    • CPUs: 8 vCPU Cores (Skylake)
    • Memory: 30 G
    • Disk: 20 G SSD
    • OS: Ubuntu 18.04 LTS
    • PHP Version: 7.3

    View Slide

  17. Benchmark Comparison
    • Benchmark without Cache

    View Slide

  18. Benchmark Comparison
    • Benchmark with Routes and Config Cache

    View Slide

  19. Cache (Not Limited to Database)
    • Discover Hotspot Data (Hit Rate)
    • Proper TTL for Cached Data
    • Avoid Expiring Cache at The Same Time
    • Revoke Cache when Data Changes
    • Avoid Cache Missing
    • Hierarchical Cache Design
    • High Available Cache System
    • Warm Up the Cache

    View Slide

  20. Cache (Not Limited to Database)
    • Revoke Cache when Data Changes

    View Slide

  21. Cache (Not Limited to Database)
    • Avoid Cache Missing

    View Slide

  22. Cache (Not Limited to Database)
    • Avoid Cache Missing
    Client
    Database
    Cache Layer
    Client Client

    View Slide

  23. Cache (Not Limited to Database)
    • Hierarchical Cache Design
    Database
    Local
    Memory
    Local
    Memory
    Local
    Memory
    Cache Layer Cache
    Revoke

    View Slide

  24. Cache (Not Limited to Database)
    • High Available Cache System
    https://rancher.com/blog/2019/deploying-redis-cluster/

    View Slide

  25. Cache (Not Limited to Database)
    • Warm Up the Cache
    Request
    Request
    Request
    Request
    Cache
    Layer Database
    Time Consuming Query
    (Warm Up)

    View Slide

  26. Async Event (Delayed Processing)
    Producer
    Kafka
    Rabbit MQ
    Queue Driver
    Consumer
    Producer
    Producer
    Producer
    Consumer
    Consumer
    Consumer
    (Laravel Event) (Laravel Queue Worker)

    View Slide

  27. Database Read/Write Separation
    20% on Write
    80% on Read
    Master
    Slave Slave Slave
    Read Replicas

    View Slide

  28. Database Read/Write Separation

    View Slide

  29. Database Read/Write Separation
    App
    Master
    Slave Slave Slave
    Read Replicas
    Write
    Read

    View Slide

  30. Database Read/Write Separation

    View Slide

  31. Database Read/Write Separation

    View Slide

  32. Database Read/Write Separation
    Master Slave Slave Slave
    Read Replicas
    SQL Proxy
    Slave
    Write
    Read Read Read Read

    View Slide

  33. OPCache
    PHP

    Files
    Tokenizing
    Semantic
    Parsing
    AST
    Generate
    Bytecode
    Execute
    Bytecode
    Output
    • Lifecycle in PHP

    View Slide

  34. OPCache
    PHP

    Files
    Tokenizing
    Semantic
    Parsing
    AST
    Generate
    Bytecode
    Execute
    Bytecode
    Output
    • Lifecycle in PHP with OPCache
    Load Bytecode
    From Memory
    Check
    Bytecode Cache

    View Slide

  35. Benchmark Comparison
    • Benchmark with OPCache

    View Slide

  36. Benchmark Comparison
    • Benchmark without OPCache

    View Slide

  37. Preloading in PHP 7.4

    View Slide

  38. Preloading in PHP 7.4
    PHP

    Files
    Tokenizing
    Semantic
    Parsing
    AST
    Generate
    Bytecode
    Execute
    Bytecode
    Output
    Load Bytecode
    From Memory
    Check
    Bytecode Cache
    Preload
    in Memory
    • Lifecycle in PHP with Preloading

    View Slide

  39. Preloading in PHP 7.4
    composer require ayesh/composer-preload

    View Slide

  40. Benchmark Comparison
    • Benchmark with Preloading
    Benchmark
    Preloaded
    files
    Server
    startup time
    Opcache
    memory used
    Per request
    memory used
    QPS
    No
    preloading
    0 0.06 s
    16 MB after
    warmup
    1,825 KB 596 rq/s
    Preload hot
    classes
    878 0.26 s 21 MB 869 KB 695 rq/s
    Preload
    everything
    14541 1.56 s 105 MB 881 KB 675 rq/s
    https://github.com/composer/composer/issues/7777

    View Slide

  41. Use Case Analysis

    View Slide

  42. Banana News
    • PV is around 200w
    • Large amount of content resource
    • Fuzzy search support
    • Changeable news rank by topics
    • 99% read, 1% write

    View Slide

  43. Banana News
    Server Database
    Images
    Server
    (CDN)

    View Slide

  44. Banana News
    Database
    Images
    Server
    (CDN)
    Server
    Server
    Server
    Load
    Balancer

    View Slide

  45. Banana News
    Images
    Server
    (CDN)
    Load
    Balancer
    Master
    Slave Slave Slave
    Read
    Server
    Server
    Server

    View Slide

  46. Banana News
    Images
    Server
    (CDN)
    Load
    Balancer
    Master
    Slave Slave Slave
    Read
    Redis
    Cache
    Server
    Server
    Server

    View Slide

  47. Banana News
    Images
    Server
    Load
    Balancer
    Master
    Slave Slave Slave
    Read
    Redis
    Cache
    CDN
    Elastic Search
    Server
    Server
    Server
    eg. Cloudflare

    View Slide

  48. AATIX
    • High concurrency in short period
    • Limited tickets for selling
    • High available service

    View Slide

  49. AATIX
    Images
    Server
    Load
    Balancer
    Master
    Slave Slave Slave
    Read
    Redis
    Cache
    CDN
    Server
    Server
    Server

    View Slide

  50. AATIX
    Images
    Server
    Load
    Balancer
    Master
    Slave Slave Slave
    Read
    Redis
    Cache
    CDN
    Server
    Server
    Server
    😱

    View Slide

  51. AATIX
    Read
    HTTP Request
    CDN
    Queue
    Write
    DB

    View Slide

  52. AATIX
    Images
    Server
    Load
    Balancer
    Master
    Slave Slave Slave
    Read
    Redis
    Cache
    CDN
    Server
    Server
    Server
    Queue

    View Slide

  53. AATIX
    Master
    Slave Slave Slave
    Read
    Redis
    Cache
    Server
    Server
    Server
    Queue
    Local Cache
    Atomic Counter
    Rate Limit
    Circuit Breaking

    View Slide

  54. AATIX
    • Avoid large amount of write requests at the same time
    • Queue
    • Rate Limit
    • Reduce repeated requests from clients
    • Service isolation for hotspot data
    • Circuit breaking for high availability

    View Slide

  55. • Large amount of webhook calls
    • High availability for webhooks
    • Instant response time
    • Chat service for live streaming

    View Slide

  56. HandsUp

    View Slide

  57. HandsUp
    Facebook
    Webhooks
    Message
    Load
    Balancer
    Server
    Server
    Server
    Queue
    PubNub

    Service
    Analysis
    Service
    Swoole Service
    PubSub
    Many to Many
    Queue Workers

    View Slide

  58. The Last
    But Not the Least

    View Slide

  59. Q&A

    View Slide