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

Estratégias de Caching - Escalando sua aplicação para milhões de visitas

Zizaco
June 07, 2016

Estratégias de Caching - Escalando sua aplicação para milhões de visitas

Escalando sua aplicação para milhões de visitas: Estratégias de Caching

Vídeo da Apresentação (2016/06/07): https://youtu.be/z8qkUITz5rU

Source-code de exemplo: https://gist.github.com/Zizaco/1777fe7566f334dc04d83b17f7832007

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):

public function handle($request, Closure $next)
{
$path = $request->path();

if ($this->cacheManager->isCached($path) && $this->canBeCached($request)) {
return $this->cacheManager->get($path);
}

return $next($request);
}

...

public function terminate($request, $response)
{
$path = $request->path();

if ($this->cacheManager->isCached($path) || !$this->canBeCached($request)) {
return;
}

$this->cleanCookies($response);
$this->cacheManager->cache($path, $response);
}

View:

@include
@cached_include

function cached_include($view, $vars = null, $time = 60)
{
$cacheKey = $view . md5(serialize($vars));

return Cache::remember(
$cacheKey,
$time,
function () use ($view) {
return view(
$view,
is_array($vars) ? $vars : array_except(get_defined_vars(), ['__data', '__path'])
)->render();
}
);
}

Blade::extend(
function ($view) {
$pattern = '/@cached_include\((.*)\)/';

return preg_replace($pattern, "", $view);
}
);

Model / Services:

General Caching

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:

Lições Aprendidas para tirar o máximo do Caching:

- Feature flipping ("switch" central para desativar cache)
- Criar serviços "CacheManager" para cachear e invalidar quando necessário.
- Comece o quanto antes
- Compactar antes de cachear pode ser uma boa ideia.
- Cache local VS Cache compartilhado
- CDN

Fontes:

Speed up PHP with APC - Jacob Nicholson
http://www.inmotionhosting.com/support/website/what-is/speed-up-php-with-apc
PHP Performance I: Everything You Need To Know About OpCode Caches - Davey Shafik
https://support.cloud.engineyard.com/hc/en-us/articles/205411888-PHP-Performance-I-Everything-You-Need-to-Know-About-OpCode-Caches
Laravel Documentation: Route Caching
https://laravel.com/docs/master/controllers#route-caching
Caching Routes Using Filters - Mark van Eijk
http://markvaneijk.com/caching-routes-using-filters-in-laravel-4
Google Developers: HTTP Caching
https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching
Varnish Cache
https://varnish-cache.org/
Nginx: Reverse Proxy with Caching
https://www.nginx.com/resources/wiki/start/topics/examples/reverseproxycachingexample/

Zizaco

June 07, 2016
Tweet

More Decks by Zizaco

Other Decks in Technology

Transcript

  1. Escalando sua aplicação para
    milhões de visitas .
    Estratégias de Caching

    View full-size slide

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

    View full-size slide

  3. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  6. APC & Zend OpCache (OpCode Cache)

    View full-size slide

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

    View full-size slide

  8. 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 full-size slide

  9. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  13. Caching Middleware (actionCaching)

    View full-size slide

  14. Caching Middleware (actionCaching)

    View full-size slide

  15. 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 full-size slide

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

    View full-size slide

  17. 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 full-size slide

  18. 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 full-size slide

  19. View Caching (fragment Caching)

    View full-size slide

  20. View Caching (fragment Caching)

    View full-size slide

  21. 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 full-size slide

  22. General Caching

    View full-size slide

  23. General Caching

    View full-size slide

  24. 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 full-size slide

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

    View full-size slide

  26. 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 full-size slide

  27. Reverse Proxy (ou CDN)
    Varnish Cache

    View full-size slide

  28. 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 full-size slide

  29. Standard HTTP Caching

    View full-size slide

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

    View full-size slide

  31. 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 full-size slide

  32. Lições Aprendidas
    Para tirar o máximo do Caching

    View full-size slide

  33. Lições Aprendidas
    ● Feature flipping ("switch" central para
    desativar cache)

    View full-size slide

  34. Lições Aprendidas
    ● Feature flipping ("switch" central para
    desativar cache)
    ● Criar serviços "CacheManager" para
    cachear e invalidar quando necessário.

    View full-size slide

  35. Lições Aprendidas
    ● Feature flipping ("switch" central para
    desativar cache)
    ● Criar serviços "CacheManager" para
    cachear e invalidar quando necessário.
    ● Comece o quanto antes

    View full-size slide

  36. Lições Aprendidas
    ● Compactar antes de cachear pode ser uma
    boa ideia.

    View full-size slide

  37. Lições Aprendidas
    ● Compactar antes de cachear pode ser uma
    boa ideia.
    ● Cache local VS Cache compartilhado.

    View full-size slide

  38. Fontes
    Speed up PHP with APC - Jacob Nicholson
    http://www.inmotionhosting.com/support/website/what-is/speed-up-php-with-apc
    PHP Performance I: Everything You Need To Know About OpCode Caches - Davey Shafik
    https://support.cloud.engineyard.com/hc/en-us/articles/205411888-PHP-Performance-I-Everything-You-Need-to-
    Know-About-OpCode-Caches
    Laravel Documentation: Route Caching
    https://laravel.com/docs/master/controllers#route-caching
    Caching Routes Using Filters - Mark van Eijk
    http://markvaneijk.com/caching-routes-using-filters-in-laravel-4
    Google Developers: HTTP Caching
    https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching
    Varnish Cache
    https://varnish-cache.org/
    Nginx: Reverse Proxy with Caching
    https://www.nginx.com/resources/wiki/start/topics/examples/reverseproxycachingexample/

    View full-size slide