Marcos quesada caching_sf2
Upcoming SlideShare
Loading in...5

Marcos quesada caching_sf2



Presentación de Marcos Quesada sobre "Caching en Symfony2 y escalabilidad" para Symfony Barcelona

Presentación de Marcos Quesada sobre "Caching en Symfony2 y escalabilidad" para Symfony Barcelona



Total Views
Views on SlideShare
Embed Views



6 Embeds 243 190 40 9 2 1 1



Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • -dump routes to apache
  • analizando headers: curl -so /dev/null -D - GET -Used http://www . (trae Header completo)
  • Step 1: A gateway cache, or reverse proxy, is an independent layer that sits in front of your application. The reverse proxy caches responses as they're returned from your application and answers requests with cached responses before they hit your application. Symfony2 provides its own reverse proxy, but any reverse proxy can be used. Step 2: HTTP cache headers are used to communicate with the gateway cache and any other caches between your application and the client. Symfony2 provides sensible defaults and a powerful interface for interacting with the cache headers. Step 3: HTTP expiration and validation are the two models used for determining whether cached content is fresh (can be reused from the cache) or stale (should be regenerated by the application). Step 4: Edge Side Includes (ESI) allow HTTP cache to be used to cache page fragments (even nested fragments) independently. With ESI, you can even cache an entire page for 60 minutes, but an embedded sidebar for only 5 minutes.
  • Sub Request en renders (widgets)..., ESI,-Assetic,-Varnish-and-Twig.html
  • P 29
  • Cuando standalone es false (predeterminado), Symfony2 combina el contenido de la página incluida en la principal antes de enviar la respuesta al cliente. Pero cuando standalone es true, y si Symfony2 detecta que está hablando con una pasarela caché compatible con ESI, genera una etiqueta include ESI. Pero si no hay una pasarela caché o si no es compatible con ESI, Symfony2 termina fusionando el contenido de las páginas incluidas en la principal como lo habría hecho si standalone se hubiera establecido en false.
  • Basics Varnish - CPU Vs Memory - low cache hit rate What you can learn from this is actually simple: Do not focus on the CPU when you want to scale your Varnish setup. I know it’s tempting to buy the biggest baddest server around for a high-traffic site, but if your active data set can fit within physical memory and you have a 64-bit CPU, Varnish will thrive. And for the record: All CPU-usage graphs I’ve seen from Varnish installations confirm this. Most of the time, those sexy CPUs are just sitting idle regardless of traffic. About running all data set on mem & sync to disk as a rescue An otherimportant detail is that your shmlog shouldn’t trigger disk activity. On my setup, it didn’t sync to disk to begin with, but you may want to stick it on a tmpfs just to be sure. I suspect this has improved throughout the 2.0-series of Varnish, but it’s an easy insurance. Typically the shmlog is found in /usr/var/varnish, /usr/local/var/varnish or similar (“ls /proc/*/fd | grep _.vsl” is the lazy way to find it).
  • This command shows the most often-made requests to the backend: varnishtop -b -i TxURL It’s excellent for spotting often-requested items that are currently not being cached. The “-b” flag filters for requests made to the backend. “-i TxURL” filters for the request URL that triggered the request to the backend. Its output looks something like this:
  • This command displays all varnish traffic for a specific client. It’s helpful for seeing exactly what a particular page or request is doing. Set it to your workstation IP, load the page, see everything Varnish does with your connection including hit/miss/pass status. Varnishlog is really useful, but it puts out an overwhelmingly-large amount of data that isn’t easily filtered. The “-o” option groups all of the entries for a specific request together (without it all entries from all requests are displayed fifo) and it accepts a tag (“ReqStart” in this example) and regex (the IP address in this case) to filter for only requests associated with that tag & regex. It’s the only way I’ve found to filter down the firehose of log entries into something useful.
  • We did not want to use PHP sessions in this project as that would have been a major performance drawback. What we did instead: We implemented client side signed cookies as a C plugin in varnish (more about that in a separate post somewhen later). This way, varnish can cache the content for different user levels and distinguish between them without the need to invoke PHP
  • Conclusión Hay cosas que cuestan tan poco… 1. Aumentar el espacio de memoria para que el “Cache full count” sea 0. 2. Utilizad igbinary para acelerar el procese de serialización y deserialización (a parte lo podréis utilizar en código normal con igbinary_serialize y igbinary_unserialize, con sistemas de colas, por ejemplo). 3. Setead apc.stat a 0 en producción y forzar un reload o un flush del apc después de que algún fichero cambie (con una subida, por ejemplo) Material extra
  • When using the APC autoloader, if you add new classes, they will be found automatically and everything will work the same as before (i.e. no reason to "clear" the cache). However, if you change the location of a particular namespace or prefix, you'll need to flush your APC cache. Otherwise, the autoloader will still be looking at the old location for all classes inside that namespace.
  • Cache de DQL $config->setMetadataCacheImpl(newDoctrine\\Common\\Cache\\ApcCache()); Cache de resultados $config->setQueryCacheImpl(newDoctrine\\Common\\Cache\\ApcCache());
  • Cache de DQL $config->setMetadataCacheImpl(newDoctrine\\Common\\Cache\\ApcCache()); Cache de resultados $config->setQueryCacheImpl(newDoctrine\\Common\\Cache\\ApcCache());

Marcos quesada caching_sf2 Marcos quesada caching_sf2 Presentation Transcript

  • Caching en Symfony 2Escalando aplicaciones web Marcos Quesada @marcos_quesada
  • About meCode enthusiast & More ;)Symfony 2 desde agosto 2011Symfony BCN @marcos_quesada
  • Contenido de la presentación Introducción Cache Symfony 2 Varnish Caching contenido estático + CDN Transient Data Arquitectura de escalado Conclusiones
  • IntroducciónBackend developer:donde almaceno los datos Datos: strings, arrays, objects, HTML … En dB / Static files …. o en memoria!Cuando pensamos en cache expiration&validation : PERFORMANCE Piensa en cold start: cache Warmup & Low Hit RatesVolumen datos Vs tiempo consumido
  • Performance¿Cual va a ser nuestro volumen de páginas vistas totales?PlanificaciónOptimización “The fastest HTTP request is the one not made.” No resuelvas problemas que no tienes Reducción del contenido a entregar Análisis PageSpeedImpacto directo con el coste de explotación
  • Metricas: localiza cuellos de botella Monitoriza : Munin, Nagios ... Profiling: XHProf Pruebas de carga Jmeter , Apache Benchmark ,httperf … Identificando que cachear: Datos con alta demanda Caros de generar Grandes en tamaño Datos comunes Trucos para encontrar datos cacheables Monitor Queries Output queries Monitor Page Loads Monitor Web Analytics
  • Introduction to HTTP CachingReverse HTTP proxy, a veces llamdo HTTP accelerator o web acceleratorCache-control max-age stale_while_revalidate stale_if_errorMetricas Cache hit Vs Cache miss
  • Introduction to HTTP Caching
  • Analizando Headers
  • Analizando Headers
  • Symfony 2 cacheSymfony 2 es un framework con cache propia¿Qué cache?Definición de cache: "A cache is a collection of data duplicating original values stored elsewhere or computed earlier, where the original data is expensive to fetch or to compute, compared to the cost of reading the cache." (Wikipedia)¿Que tipo de cache ? Carpeta Cache ? APC ? Reverse Proxy Cache ?
  • Symfony 2 cache folder Generación de cache desactivable comentando $kernel->loadClassCache
  • Symfony 2 cache PROD
  • Symfony 2 cacheSymfony2 incluye un reverse proxy escrito en PHP.Otras denominaciones: Gateway caches ,reverse proxy caches, surrogate caches, delegado inverso o HTTP accelerators.El sistema de cache de Symfony2 está basado en la potencia y simplicidad del caching HTTP como define su normativa RFC 2616
  • Symfony 2 cache : headers
  • Symfony 2 cache: contents
  • Enabling AppCache
  • HttpFoundation ComponentObject Oriented wrapper Server Globals VariablesSymfonyComponentHttpFoundationResponse Colección de métodos para manipular las cabeceras HTTP del Objeto Response
  • AppCache default setings
  • Setting Response HTTP headers
  • Using Validation
  • PURGE La implementación del PURGE puede romper la compatibilidad con otros Reverse Proxy Caches Nunca debería ser necesario invalidar los datos almacenados en caché porque la invalidación ya se tiene en cuenta de forma nativa en los modelos de caché HTTP Proteger el método PURGE de HTTP : restringe el acceso
  • Ventajas & ProblemasTruco debugar cache : error_log($kernel->getLog());Cache:clear & cache:warmup Clear sin fin Imposible borrar app/cache Multiples regeneraciones de cache en paralelostale_if_errorDeploys en producción complicados si hay mucho tráfico Corte programado / Restricción de acceso Alternativa: apc.stat = 0 , Pull & Clear Opcode cachePurge total o parcial complicado
  • ESIHabilita ESI , setea secciones como standalone Esto permite varios TTLs en una misma página Setea shared max age en responses y sections Si se fija un max age como non-shared,toda la página queda cacheada, ESI no funcionará: $response->setSharedMaxAge(60)
  • ESIRenders: symfony2 will create a subrequest for you, call the widget and include the subresponse in your masterresponse. {% render MyBundle:Controller:widgetaction {param1:foo} %}Having every (sub)action represented as a request makes ESI {% render MyBundle:Controller:widgetaction {param1:foo} with {standalone:true} %}
  • ESI
  • ESIapp/config/config.yml framework: esi: { enabled: true }{% render FooBundle:Bar:index with {}, {standalone: true} %} Añadir la ruta (debe estar securizada) # app/config/routing.yml _internal: resource: "@FrameworkBundle/Resources/config/routing/internal.xml" prefix: /_internal
  • ProblemasDebuggar ESI se vuelve complicado!Enabling debugging with:$framework = new HttpCache($framework, new Store(__DIR__./../cache), new ESI(), array(debug => true) );The debug mode adds a X-Symfony-Cache header to each response that describes what the cache layer did:X-Symfony-Cache: GET /is_leap_year/2012: stale, invalid, storeX-Symfony-Cache: GET /is_leap_year/2012: fresh
  • VarnishReverse Proxy CacheEscrito on CSoporta ESIMuy flexible y configurable (Varnish Configuration Language)Almacena todo su contenido sobre la RAM: muy rápidoMuy altas prestaciones: Consumo de CPU relativamente bajoVarnish puede consumir toda la RAM que le pongas,asegúrate que configuras su max memory (por debajo de la RAM disponible)
  • VarnishVarnish puede trabajar como Balanceador de Carga Configurado contra multiples backends Realiza Health-Checks en los web ServersNúmero de Threads: Varnish puede crear nuevos threads bajo demanda, y eliminarlos cuando la carga total se reduce Es perfecto para picos de tráfico Una buena aproximación es mantener unos pocos threads idle durante el tráfico regular, anticipando el incremento de tráfico en lugar de levantar y destruir otros según cambia la demanda En sistemas de 64-bit , el coste de tener unos cientos de threads extras es muy limitado
  • Varnish Configuration LanguageConfigura cada requestDefine: Backends (web servers) ACLs Load balancing strategy Can be reloaded while runningTell Varnish to communicate with the content server (Nginx) on port 8080.backend default { .host = ""; .port = "8080";}Unsetting Cookies:sub vcl_recv { unset req.http.cookie;}sub vcl_fetch { unset beresp.http.set-cookie;}
  • VarnishVarnish- what can/cantt be cached?Can: Static Pages Images,js,css Static parts of pages that dont change often(ESI)Cant POST request Very large files Request with Set-Cookie !!!! User-specific content
  • Real-time statistics Varnishtopvarnishtop
  • Real-time statistics Varnishtopvarnishtop -b -i TxURL “-b” flag filters for requests made to the backend. “-i TxURL” filters for the request URL that triggered the request to the backend.
  • Real-time statistics : VarnishhistVarnishhist shows a histogram for the past 1000 requests
  • Real-time statistics: varnishlogvarnishlog -c -o ReqStart displays all varnish traffic for a specific client. It’s helpful for seeing exactly what a particular page or request is doing.
  • Real-time statistics :VarnishstatVarnishstat is the tool used to monitor the basic health of Varnish. Unlike all the other tools, it doesn’t read log entries, but counters that Varnish update in real-time. It can be used to determine your request rate, memory usage,thread usage, and just about anything that’s not related to a specific request.This command provides an overview of the stats for the current Varnish instance. It shows hit/miss/pass rates and ratios, lots of other gory internal details.
  • PURGE en VarnishVarnish se puede configurar para aceptar un método HTTP especial PURGE que invalida la caché para un determinado recurso: acl purge { "localhost"; ""; } sub vcl_recv { if (req.request == "PURGE") { if (!client.ip ~ purge) { error 405 "Not allowed."; } return (lookup); } } sub vcl_hit { if (req.request == "PURGE") { set obj.ttl = 0s; error 200 "Purged."; } } sub vcl_miss { if (req.request == "PURGE") { error 404 "Not purged"; } }
  • ESI on Varnish # /etc/varnish/default.vcl backend default { .host = ""; .port = "80"; } sub vcl_recv { set req.backend = default; # Tell Symfony2 that varnish is there, supporting ESI set req.http.Surrogate-Capability = "abc=ESI/1.0"; } sub vcl_fetch { # Enable ESI only if the backend responds with an ESI header if (beresp.http.Surrogate-Control ~ "ESI/1.0") { unset beresp.http.Surrogate-Control; // for Varnish >= 3.0 set beresp.do_esi = true; // for Varnish < 3.0 // esi; } }
  • Varnish : problemasBusca un setup estable: “ The system worked great for a while, and then took a nosedive as the Varnish cache ate up all the available RAM and pushed the system into a swap death spiral.” Cuidado con low hit rate (Usa ESI)Riesgos de purgadoCookies & sessions : planifica tu estrategia
  • Browser CacheUsing Expiration Last-Modified & is-modified-since response header
  • Browser CacheUsing Etag: If-None-Match request
  • CDNAkamai:worlds largest CDN serves about 20% of the web traffic using over 100,000 servers located in over 1000 networks and over 70 countriesAmazon S3 + CloudFrontAhorro de ancho de banda
  • CDNEnabling CDN on assets framework: secret: s3cr3t templating: assets_base_urls: engines: { engines: [twig], assets_version: v1 } packages: images: base_urls: $assets=new AssetCollection( array( new FileAsset(“web/Bundle/style.css”), new GlobAsset(“web/*.css”),)); $assets->load(); $writer=newAssetWriter(“s3://bucket”); $writer->writeManagerAssets($assets);External tools: S3cmd: s3cmd sync /var/www/symfony/web/images/ s3://cdn-image01 --acl-publicZend Library S3Associated path Twig Extension
  • Transient DataKey => Value APC Memcached Redis ...La base de datos suele ser siempre el mayor cuello de botella de performance en backendUna capa de caching sobre la base de datos resuelve de gran manera los problemas de performance y escalabilidad
  • APC: PHP compilationPHP Compilation -scanning / lexing: plain text code turned in tokens -parsing: tokens are collected into Expressions -compilation: Expressions are translated into Opcodes for execution -execution: Opcode stacks are processed, one at a time.APC caches the Opcodes for later execution
  • APC performance
  • APC.stat performance
  • APC.php
  • APC tweakingse recomienda: apc.stat=1 en desarrollo. Devolver a 0 en producción.apc.shm_size: cuánta memoria va a ser asignada a APC Como regla general,apc.shm_size debe ser el doble de la máxima memoria utilizada por APC’s para evitar por completo la fragmentaciónVigilar Full Count: Se purga por quedarse sin espacio, si sube de 0 es probable que surjan errores 500!apc.user_ttl = 0: Sin limite TTLEjemplo configuración en php.ini: extension=/usr/lib/php5/20090626/ apc.enabled = 1 apc.shm_size = 100M apc.ttl = 0 apc.user_ttl = 0 apc.gc_ttl = 600 apc.stat = 1
  • Simple API to store PHP variableson APC
  • Symfony 2 apc autoload UniversalClassLoader: the loader iterates over all configured namespaces to find a particular file, making file_exists calls until it finally finds the file its looking for. ApcUniversalClassLoader( namespace ) Solution:cache on APC the location of each class after its located the first time.
  • MemcachedSistema distribuido de cacheo de objetos en memoriaTecnicamente es un serverClient access sobre TCP or UDP: Fast asynchronous network I/OSistema escalable a multiples servers en forma de poolsServers are independent, managed by clientsNo redundacy or fail over! Its a cache!No replication , no authentication …No enumeration of keysSin mecanismo de clean-upNo tiene persistencia
  • MemcachedOffers built-in session handlerGiant “Hash table” or ArrayFlujo: Pool Memcached Servers Assign values to keys that are stored in cluster The client hashes the key to a particular machine in the cluster Subsequent request for that key retrtive the value from the memcached server on wich it was stored Values time out after the specified TTLLimites: Key max 250 chars Values Up to 1MB
  • Improving MemcachedSerialization Si se usa memcached para almacenar datos complejos (array&objects), necesitan ser serializados en strings Consume recursos Puede ser mejorado con igbinary 30% mas rápido Produce datos mas compactos hasta 45% menores con respecto al serializador nativo PHP
  • Distributed memcached
  • Memcached: OO API
  • Memcached app/config/config.yml acme_memcached: servers: server1: host: port: 11211 weight: 25 server2: Host: port: 11211 Weight: 75 server3: host: port: 11211 weight: 50
  • MemcachedService Definition class AcmeMemcachedExtension extends Extension { public function load(array $configs, ContainerBuilder $container) { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); $container->setParameter(acme_memcached.servers, $config[servers]); unset($config[servers]); $loader = new LoaderXmlFileLoader($container, new FileLocator(__DIR__./../Resources/config)); $loader->load(services.xml); } }Access to memcached Service:$memcachedSvc = $this->container->get(memcached.service.pool);//sets key=> value, with no compression & no expiration$memcachedSvc->set(hask_key,$dataValue, false, 0);$data = $memcachedSvc->get(hash_key);
  • Memcached : GUIphpMemcachedAdmin
  • Memcached GUI
  • APC Vs MemcachedAparte del Opcode, Cuando debemos usar APC ? Cuando los datos no sean distribuidos (único server) Op Code + User cache ambos en APC == “all eggs in one basket” Requests garantizados a una misma sesión ( sticly sessions) file upload progress&sessions ( sticky sessions) Es un componente PHP Typical uses: Application settings Almacenar configuraciones Caching de datos (db) Data comunes para los usuarios
  • APC Vs MemcachedCuando debemos usar memcached? Cuando no está garantizado que las request vayan a una misma máquina Datos específicos dirigidos al usuario user sessions (Memcached Session Handler) Habla diferentes lenguajes : PHP, Python, Ruby ,Java,Perl ... Las instancias pueden ser compartidas en múltiples servers del pool MAS LENTO APC, especialmente en array storage
  • Caching on DoctrineDoctrine cache drivers: Apc , memcache, XcacheuseResultCache & useQueryCache
  • Caching on DoctrineQueryBuilder cache que almacena la transformada SQL de la query DQL Usando APC:
  • Arquitectura de escalado
  • Arquitectura de escalado
  • Colas & LogsOperaciones asíncronas Emails, generación thumbs, procesos pesados RabbitMQ Beanstalk Amazon SQSLogs: Redis MongoDb...
  • NginxStack alternativo a Apache PHP-FPMSoporte nativo de MemcachedNginx maneja los rquest con una aproximación asynchronous event-driven Le hace mas predecible en tiempo de respuesta Vs ApacheAlto rendimiento, consumo de memoria óptimoReload GracelyReverse Proxy CacheSoporta ESI , aunque no es compatible al 100%Escala muy bien , menos configurable que Varnish
  • Varnish Vs NginxUsa Nginx donde no se utilizan las plenas capacidades de Varnish (ESI principalmente)Si necesitas servir contenidos estáticos lo mas rápido posible, usa Nginx como frontalSi necesitas servir contenido dinámico con ESI , usa VarnishSi usas CDN para contenido estático , la potencia de Nginx se vuelve menos relevantePor tanto Nginx es claramente mas rápido que Varnish Usamos Varnish por ESI
  • ConclusionesPhp 5.3 Vs php 5.4 :benchmarks & page-load 10-30% mas rápido.Intercambia Apache por NginxEstudia tus necesidades para buscar tu configuración ideal Soluciones Javascript (no bloqueantes) & Ajax ESI te ayuda en Low Hit Caching rate (HTTP) Soluciones de microcaching pueden ser suficientesCiclo de crecimiento Mide – Mejora – vuelve a medir Stress TestPlanifica tu deploy y tus subidas de códigoImplementa mecanismos de WarmupUsa tiempos de cache lo mas largo posible
  • Gracias !!
  • Enlaces de interésCaching on the Edge – Fabian Potencier : Cache: 2 API: SymfonyComponentHttpFoundationResponse: cache: with httperf: Basics: varnish: form Dummies: Varnish event further: APC on autoloader: Debugging phpmemcachedadmin : phpMemcacheAdmin: on Doctrine : at 99 designs: - Relaunch with symfony2, Assetic, Varnish and Twig,-Assetic,-Varnish-and-Twig.htmlAmazon Guidelines: