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

Escalando sua aplicação para milhões de visitas.

Zizaco
October 08, 2016

Escalando sua aplicação para milhões de visitas.

Escalando sua aplicação para milhões de visitas.

Vídeo da Apresentação (2016/10/08): https://youtu.be/jPbghDn0HLE?t=335

Luiz Fernando / Zizaco

- Software Developer
- Product Owner
- Leroy Merlin (Laravel)
- Keep learning

http://zizaco.net

"Investing in scaling before it is needed is generally not a smart business proposition; however, some forethought into the design can save substantial time and resources in the future." - Kate Matsudaira

1. Medição

Teste de carga
smartmeter.io

2. Cache

O que é Cache?
Guardar algo temporáriamente para uso posterior.

Por que usar Cache?
Encurtar o tempo de acesso, reduzir a latência e melhorar o input/output.

Caching é usado para melhorar a performance de uma aplicação.

Laravel Request Lifecycle
- HTTP
- WebServer
- HTTP Kernel
- Middlewares
- index.php
- autoload
- Routes
- Controller
- External Services
- File System
- Databases
- Models / Services
- View

autoload:

APC & Zend OpCache (OpCode Cache)
Até 3x mais rápido

Routes:

Laravel's Route Caching
"Using the route cache will drastically decrease the amount of time it takes to register all of your application's routes. In some cases, your route registration may even be up to 100x faster"
php artisan route:cache
Até 3x mais rápido

- Middleware (action caching)
- View
- Model / Services:

Reverse Proxy:

Reverse Proxy (ou CDN)
"A reverse proxy can reduce load on its origin servers by caching content [...] also known as web acceleration. Proxy caches of this sort can often satisfy a considerable number of website requests, greatly reducing the load on the origin server(s)."

- Varnish Cache
- Akamai
- Nginx

Standard HTTP Caching:

3. Concorrência

The Twelve-Factor App: https://12factor.net/

The twelve-factor methodology can be applied to apps written in any programming language, and which use any combination of backing services (database, queue, memory cache, etc).

"Twelve-factor processes are stateless and share-nothing. Never assume that anything on disk or cached in memory will be available on a future request or job."

Storing files (Object Storage / S3 / NAS)
Storing logs (Monolog Handlers)

"Sticky sessions should never be used or relied upon. Session state data is a good candidate for a datastore that offers time-expiration, such as Memcached or Redis.""

Centralizing sessions

"Store config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code"

"Build your application to be as generic and stateless as possible. If you must use a singleton, enable a voting protocol [...]" - Kyle Brown

Preventing singleton

"Lock" distribuído
"In computer science, a lock is a synchronization mechanism [...] A lock is designed to enforce a mutual exclusion concurrency control policy." - Wikipedia

Redis: rápido, transacional e consistente

4. Topologia

"DevOps: Developers partner with system administrators and operations staff to assure that software runs with a minimum of problems." - Neil Garnichaud

5. Indivíduos e interações

"Conway’s Law: The architecture of the system will reflect the organization that produced it. Parts of the organization that don’t talk together will likely produce software that doesn’t talk together."

"Investing in scaling before it is needed is generally not a smart business proposition; however, some forethought into the design can save substantial time and resources in the future." - Kate Matsudaira

Fontes:

Scalable Web Architecture and Distributed Systems - Kate Matsudaira
http://www.aosabook.org/en/distsys.html
Top 9 rules for cloud applications - Kyle Brown and Mike Capern
http://www.ibm.com/developerworks/websphere/techjournal/1404_brown/1404_brown.html
The Twelve Factors App - Adam Wiggins
https://12factor.net/
5 Common Server Setups For Your Web Application - Mitchell Anicas
https://www.digitalocean.com/community/tutorials/5-common-server-setups-for-your-web-application
Estratégias de Caching - Zizaco
https://www.youtube.com/watch?v=z8qkUITz5rU
Scaling Your Web App 101: Lessons in Architecture Under Load - Hartley Brody
https://blog.hartleybrody.com/scale-load/
Distributed locks with Redis - Redis.io
http://redis.io/topics/distlock
What Exactly is DevOps? - Neil Garnichaud
http://www.drdobbs.com/architecture-and-design/what-exactly-is-devops/240009147

Trabalhe conosco (:

Zizaco

October 08, 2016
Tweet

More Decks by Zizaco

Other Decks in Technology

Transcript

  1. Escalando sua aplicação
    para milhões de visitas.

    View Slide

  2. Who am I?
    ● Software Developer
    ● Product Owner
    ● Leroy Merlin (Laravel)
    ● Keep learning
    http://zizaco.net
    Luiz Fernando / Zizaco

    View Slide

  3. "Investing in scaling before it is needed
    is generally not a smart business
    proposition; however, some
    forethought into the design can
    save substantial time and
    resources in the future."
    - Kate Matsudaira

    View Slide

  4. View Slide

  5. View Slide

  6. View Slide

  7. View Slide

  8. View Slide

  9. View Slide

  10. Medição
    1.

    View Slide

  11. Medição

    View Slide

  12. View Slide

  13. Teste de carga
    smartmeter.io

    View Slide

  14. View Slide

  15. View Slide

  16. View Slide

  17. Cache
    2.

    View Slide

  18. O que é Cache?
    Guardar algo temporáriamente
    para uso posterior.

    View Slide

  19. Por que usar Cache?
    Encurtar o tempo de acesso, reduzir a
    latência e melhorar o input/output.
    Caching é usado para melhorar a
    performance de uma aplicação.

    View Slide

  20. Laravel Request Lifecycle
    HTTP://
    WebServer
    HTTP Kernel Middlewares
    index.php autoload
    Routes
    Controller
    External
    Services
    File System Databases
    Models / Services
    View

    View Slide

  21. Laravel Request Lifecycle
    HTTP://
    WebServer
    HTTP Kernel Middlewares
    index.php autoload
    Routes
    Controller
    External
    Services
    File System Databases
    Models / Services
    Cache
    View

    View Slide

  22. APC & Zend OpCache (OpCode Cache)

    View Slide

  23. Laravel Request Lifecycle
    HTTP://
    WebServer
    HTTP Kernel Middlewares
    index.php autoload
    Routes
    Controller
    External
    Services
    File System Databases
    Models / Services
    Cache
    View
    Cache

    View Slide

  24. Laravel's Route Caching
    "Using the route cache will drastically
    decrease the amount of time it takes to
    register all of your application's routes. In
    some cases, your route registration may
    even be up to 100x faster"
    php artisan route:cache

    View Slide

  25. Laravel Request Lifecycle
    HTTP://
    WebServer
    HTTP Kernel Middlewares
    index.php autoload
    Routes
    Controller
    External
    Services
    File System Databases
    Models / Services
    Cache
    View
    Cache
    Cache

    View Slide

  26. Caching Middleware (actionCaching)
    HTTP Kernel Middlewares
    Routes Controller
    Database
    View

    View Slide

  27. Caching Middleware (actionCaching)
    HTTP Kernel Middlewares
    Routes Controller
    Database
    View
    HTTP Kernel Middlewares
    Routes Controller
    Database
    View

    View Slide

  28. Caching Middleware (actionCaching)
    HTTP Kernel Middlewares
    Routes Controller
    Database
    View
    HTTP Kernel Middlewares
    Routes Controller
    Database
    View
    Hard
    work

    View Slide

  29. Caching Middleware (actionCaching)

    View Slide

  30. Caching Middleware (actionCaching)

    View Slide

  31. Laravel Request Lifecycle
    HTTP://
    WebServer
    HTTP Kernel Middlewares
    index.php autoload
    Routes
    Controller
    External
    Services
    File System Databases
    Models / Services
    Cache
    View
    Cache
    Cache
    Cache

    View Slide

  32. View Caching (fragment Caching)
    View Object View Object
    Render Render View Object Render ...
    @include @include

    View Slide

  33. View Caching (fragment Caching)
    View Object View Object
    Render Render View Object Render ...
    @include @include
    View Object View Object
    Render Render View Object
    @cached_include @include
    Render ...

    View Slide

  34. View Caching (fragment Caching)
    View Object View Object
    Render Render View Object Render ...
    @include @include
    View Object View Object
    Render Render View Object
    @include
    Render ...
    @cached_include

    View Slide

  35. View Caching (fragment Caching)

    View Slide

  36. View Caching (fragment Caching)

    View Slide

  37. Laravel Request Lifecycle
    HTTP://
    WebServer
    HTTP Kernel Middlewares
    index.php autoload
    Routes
    Controller
    External
    Services
    File System Databases
    Models / Services
    Cache
    View
    Cache
    Cache
    Cache
    Cache

    View Slide

  38. General Caching

    View Slide

  39. General Caching

    View Slide

  40. HTTP://
    WebServer
    Cache
    Laravel Request Lifecycle
    HTTP Kernel Middlewares
    index.php autoload
    Routes
    Controller
    External
    Services
    File System Databases
    Models / Services
    Cache
    View
    Cache
    Cache
    Cache
    Cache

    View Slide

  41. Internet Server(s) Application Database
    Reverse Proxy
    Internet Server(s)
    Proxy Application Database

    View Slide

  42. Reverse Proxy
    "A reverse proxy can reduce load on its
    origin servers by caching content [...] also
    known as web acceleration. Proxy caches of
    this sort can often satisfy a considerable
    number of website requests, greatly
    reducing the load on the origin server(s)."

    View Slide

  43. Reverse Proxy (ou CDN)
    Varnish Cache

    View Slide

  44. HTTP://
    WebServer
    Cache
    Laravel Request Lifecycle
    HTTP Kernel Middlewares
    index.php autoload
    Routes
    Controller
    External
    Services
    File System Databases
    Models / Services
    Cache
    View
    Cache
    Cache
    Cache
    Cache
    Cache

    View Slide

  45. Standard HTTP Caching

    View Slide

  46. Laravel Request Lifecycle
    HTTP://
    WebServer
    HTTP Kernel Middlewares
    index.php autoload
    Routes
    Controller
    External
    Services
    File System Databases
    Models / Services
    View

    View Slide

  47. Laravel Request Lifecycle
    HTTP://
    WebServer
    HTTP Kernel Middlewares
    index.php autoload
    Routes
    Controller
    External
    Services
    File System Databases
    Models / Services
    Cache
    Cache
    Cache
    Cache
    View
    Cache
    Cache
    Cache

    View Slide

  48. View Slide

  49. View Slide

  50. View Slide

  51. Concorrência
    3.

    View Slide

  52. View Slide

  53. View Slide

  54. View Slide

  55. View Slide

  56. "Twelve-factor processes are
    stateless and share-nothing.
    Never assume that anything on disk
    or cached in memory will be
    available on a future request or job."

    View Slide

  57. View Slide

  58. Storing files (Object Storage / S3 / NAS)

    View Slide

  59. View Slide

  60. Storing logs (Monolog Handlers)

    View Slide

  61. View Slide

  62. View Slide

  63. Sticky sessions should never be
    used or relied upon. Session state
    data is a good candidate for a
    datastore that offers time-expiration,
    such as Memcached or Redis.

    View Slide

  64. Centralizing sessions
    SESSION_DRIVER=redis

    View Slide

  65. Store config in environment
    variables (often shortened to env
    vars or env). Env vars are easy to
    change between deploys without
    changing any code

    View Slide

  66. View Slide

  67. "Build your application to be as
    generic and stateless as possible. If
    you must use a singleton, enable a
    voting protocol [...]"
    - Kyle Brown

    View Slide

  68. View Slide

  69. Preventing singleton

    View Slide

  70. Preventing singleton

    View Slide

  71. Preventing singleton

    View Slide

  72. "Lock" distribuído
    "In computer science, a lock is a
    synchronization mechanism [...] A
    lock is designed to enforce a mutual
    exclusion concurrency control
    policy." - Wikipedia

    View Slide

  73. "Lock" distribuído
    rápido, transacional e consistente

    View Slide

  74. Preventing singleton

    View Slide

  75. View Slide

  76. View Slide

  77. View Slide

  78. Topologia
    4.

    View Slide

  79. View Slide

  80. View Slide

  81. View Slide

  82. View Slide

  83. View Slide

  84. DevOps: Developers partner with
    system administrators and
    operations staff to assure that
    software runs with a minimum of
    problems.
    - Neil Garnichaud

    View Slide

  85. View Slide

  86. Uma tarde "boa"
    Uma semana

    View Slide

  87. 5410 requests / minuto
    ...

    View Slide

  88. 5410 requests / minuto
    90 requests / segundo
    324 mil req. / hora
    ...

    View Slide

  89. 5410 requests / minuto
    90 requests / segundo
    324 mil req. / hora
    7.7 milhões req. / dia
    233 milhões req. / mês

    View Slide

  90. Indivíduos e
    interações
    5.

    View Slide

  91. Conway’s Law: The architecture of
    the system will reflect the
    organization that produced it. Parts
    of the organization that don’t talk
    together will likely produce software
    that doesn’t talk together.

    View Slide

  92. Fontes
    Scalable Web Architecture and Distributed Systems - Kate Matsudaira
    http://www.aosabook.org/en/distsys.html
    Top 9 rules for cloud applications - Kyle Brown and Mike Capern
    http://www.ibm.com/developerworks/websphere/techjournal/1404_brown/1404_brown.html
    The Twelve Factors App - Adam Wiggins
    https://12factor.net/
    5 Common Server Setups For Your Web Application - Mitchell Anicas
    https://www.digitalocean.com/community/tutorials/5-common-server-setups-for-your-web-application
    Estratégias de Caching - Zizaco
    https://www.youtube.com/watch?v=z8qkUITz5rU
    Scaling Your Web App 101: Lessons in Architecture Under Load - Hartley Brody
    https://blog.hartleybrody.com/scale-load/
    Distributed locks with Redis - Redis.io
    http://redis.io/topics/distlock
    What Exactly is DevOps? - Neil Garnichaud
    http://www.drdobbs.com/architecture-and-design/what-exactly-is-devops/240009147

    View Slide

  93. Trabalhe conosco (:
    Back-end
    Front-end
    UX
    [email protected]
    assunto: [vaga]

    View Slide