SlideShare une entreprise Scribd logo
1  sur  55
Accelerating Rails 
with edge caching 
Michael May | @ohaimmay | SFRails | 10/16/2014
Topic 
• Rails caching best practices 
• Dynamic content/caching 
• Edge caching 
• Edge caching dynamic content with Rails
Rails caching 
• Query/SQL 
• Page/Action (removed from Rails 4 core) 
• Asset 
• Fragment
Rails caching 
config.action_controller.perform_caching = true
Query caching 
• Automagically done by rails when 
perform_caching = true 
• Not cached between requests! 
• Could just store the query result in a variable
Manual Query Caching 
class Product < MyModel 
def self.out_of_stock 
Rails.cache.fetch("out_of_stock", expires_in: 1.hour) do 
Product.where("inventory.quantity = 0") 
end 
end 
end
Asset Caching 
• Serve static assets from a proxy 
• config.serve_static_assets = false 
• Enable Compression* 
• config.assets.compress = true 
• # In Rails 4 
• config.assets.css_compressor = :yui 
• config.assets.js_compressor = :uglifier 
• Asset Digests 
• config.assets.digest = true
mmay.rocks/assets/catzlol-75408509152249b79b818b252da51bc4.
Enable Compression* 
Compress HTML, JSON responses at runtime 
module FastestAppEver 
class Application < Rails::Application 
config.middleware.use Rack::Deflater 
end 
* http://robots.thoughtbot.com/content-compression-with-rack-deflater 
end
Asset Caching 
• Configure an asset host if needed 
• config.action_controller.asset_host = 
ENV[‘FASTLY_CDN_URL'] 
• Cache-Control like a pro 
• config.static_cache_control = 'public, s-maxage= 
15552000, maxage=2592000'
Cache-Control 
public, s-maxage=15552000, maxage=2592000 
public 
“please cache me” 
maxage=2592000 
“keep me for 30 days” 
s-maxage=15552000 
“PROXIES ONLY! - Keep me for 180 days”
Keepin’ it fresh 
• stale-while-revalidate 
• Serve the current (stale) version for n seconds while it re-fetches the 
latest version in the background 
• Cache-Control: max-age=604800, stale-while-revalidate=86400 
• stale-if-error 
• If the re-fetch fails within n seconds of the response becoming stale, 
serve the cached response 
• Cache-Control: max-age=604800, stale-while-revalidate=86400, stale-if-error= 
259200
The Vary HTTP Header 
• In general, never Vary on anything other than 
Content-Encoding 
• Varying makes it impossible to serve the same 
response more than once and limits caching 
benefits 
• NEVER Vary on User-Agent! 
• There are THOUSANDS of these!
Dynamic Content
Dynamic Content 
• Changes are unpredictable! 
• user driven events 
• Can’t just set a Time To Live (TTL)
Dynamic Content 
• Changes are unpredictable! 
• user driven events 
• Can’t just set a Time To Live (TTL) 
• Frequently, but not continuously changing 
• Actually static for short periods of time (we can 
cache static things)!
Dynamic Content Caching 
• Usually don’t 
• Edge Side Includes (ESI) 
• Dynamic Site Acceleration (DSA)
Fragment Caching 
The rails answer to caching dynamic HTML 
# products/index.html.erb 
<% cache(cache_key_for_products) do %> 
<% Product.all.each do |p| %> 
<%= link_to p.name, product_url(p) %> 
<% end %> 
<% end %> 
# products_controller.rb 
def update 
… 
expire_fragment(cache_key_for_products) 
… 
end
Nested Fragment Caching 
<% cache(cache_key_for_products) do %> 
All available products: 
<% Product.all.each do |p| %> 
<% cache(p) do %> 
<%= link_to p.name, product_url(p) %> 
<% end %> 
<% end %> 
<% end %>
Nested Fragment Issues 
• Tedious 
• Comb through (probably terrible) view code 
• Cache keys are weird 
• “A given key should always return the same content.” - DHH 
• products/15-20110218104500 
• “A given key should always return the most up-to-date content.” - Me 
• products/15 
• Hacks around cache limitations 
• Memcache has no wildcard purging!
Nested Fragment Issues 
• Garbage left in the cache 
• Defaults writing to disk 
• Memcached, Redis, etc 
• Probably lives in the same DC as your app server 
• Distributing, replication takes effort 
• What about dynamic API caching? 
• “The caching itself happens in the views based on partials rendering 
the objects in question” 
• Take control over your cached data!
Edge Caching
Edge Caching 
aka content delivery network 
aka CDN
Edge Cache 
• Geographically distributed 
• Highly optimized storage and network 
(nanoseconds count) 
• Move content physically closer to the end-users 
• End goal - DECREASE LATENCY!
The more content we 
can offload, the better 
performance we get
#cachemoney 
• App servers cost real cash money (not cache 
money) 
• Less requests back to your application server 
• Avoid complex or less efficient strategies 
• Edge Side Includes (ESI) 
• Fragment caching
Edge caching the 
dynamic content
Our approach to dynamic 
content 
• Tag content with Surrogate-Key HTTP headers 
• Programmatically purge (~150ms globally) 
• By Surrogate-Key 
• By resource path 
• Real-time analytics and log streaming 
• Optimize the hell out of the pieces of the network we 
can control
Tagging responses 
with Surrogate-Key
class ProductsController < ApplicationController 
# set Cache-Control, strip Set-Cookie 
before_filter :set_cache_control_headers,only [:index,:show] 
def index 
@products = Product.last(10) 
# set Surrogate-Key: products 
set_surrogate_key_header @products.table_key 
respond_with @products 
end 
def show 
@product = Products.find(params[:id]) 
# set Surrogate-Key: product/666 
set_surrogate_key_header @product.record_key 
respond_with @product 
end 
end
class ProductsController < ApplicationController 
# set Cache-Control, strip Set-Cookie 
before_filter :set_cache_control_headers,only [:index,:show] 
def index 
@products = Product.last(10) 
# set Surrogate-Key: products 
set_surrogate_key_header @products.table_key 
respond_with @products 
end 
def show 
@product = Products.find(params[:id]) 
# set Surrogate-Key: product/666 
set_surrogate_key_header @product.record_key 
respond_with @product 
end 
end
class ProductsController < ApplicationController 
# set Cache-Control, strip Set-Cookie 
before_filter :set_cache_control_headers,only [:index,:show] 
def index 
@products = Product.last(10) 
# set Surrogate-Key: products 
set_surrogate_key_header @products.table_key 
respond_with @products 
end 
def show 
@product = Products.find(params[:id]) 
# set Surrogate-Key: product/666 
set_surrogate_key_header @product.record_key 
respond_with @product 
end 
end
Purge on updates
class ProductsController < ApplicationController 
def create 
@product = Product.new(params) 
if @product.save 
# purge Surrogate-Key: products 
@product.purge_all 
render @product 
end 
end 
...
def update 
@product = Product.find(params[:id]) 
if @product.update(params) 
# purge Surrogate-Key: product/666 
@product.purge 
render @product 
end 
end
fastly-rails 
github.com/fastly/fastly-rails
Edge caching in 
practice
Be aware of cookies 
• Nothing with a Set-Cookie header is cached (by 
default) 
• Authentication frameworks/middleware might 
inject Set-Cookie after the rails stack removes it 
• Avoid caching pains by knowing when, where, 
and how you use Set-Cookie
edge scripting
URL Rewriting 
• Apache, nginx, etc support URL Rewriting 
• Filter bad requests 
• Normalize paths
URL Rewrite at the edge 
• Varnish HTTP cache 
• VCL 
• Requests never hit origin!
#winning 
meh 
yay
What can we do better? 
• Add better caching defaults? 
• Cache-Control, stale-while-revalidate, stale-if-error 
• Re-use existing rails cache interfaces for edge 
caching? 
• ActiveSupport::Cache::EdgeStore 
• More fine-grained integration with HTTP accelerators 
like Varnish?
Takeaways 
• Rails has tons of built-in caching options 
• Get fancy with Cache-Control directives 
• Use Google PageSpeed Insights (chrome plugin 
adds it to dev tools) 
• Dynamic edge caching is all about the power of 
purge! 
• Similar school of thought to rails action caching
Questions? 
Contact: Michael May | @ohaimmay | michael@fastly.com 
cool links: 
fastly-rails - github.com/fastly/fastly-rails 
surrogate keys - fastly.com/blog/surrogate-keys-part-1 
cache-control tutorial - docs.fastly.com/guides/tutorials/cache-control-tutorial 
serve stale cache-control - fastly.com/blog/stale-while-revalidate 
vary header best practices - fastly.com/blog/best-practices-for-using-the-vary-header 
caching like & share buttons - fastly.com/blog/caching-like-and-share-buttons 
pagespeed insights - developers.google.com/speed/pagespeed

Contenu connexe

Tendances

Next Generation DevOps in Drupal: DrupalCamp London 2014
Next Generation DevOps in Drupal: DrupalCamp London 2014Next Generation DevOps in Drupal: DrupalCamp London 2014
Next Generation DevOps in Drupal: DrupalCamp London 2014Barney Hanlon
 
Aem dispatcher – tips & tricks
Aem dispatcher – tips & tricksAem dispatcher – tips & tricks
Aem dispatcher – tips & tricksAshokkumar T A
 
Configuring Apache Servers for Better Web Perormance
Configuring Apache Servers for Better Web PerormanceConfiguring Apache Servers for Better Web Perormance
Configuring Apache Servers for Better Web PerormanceSpark::red
 
DrupalCampLA 2014 - Drupal backend performance and scalability
DrupalCampLA 2014 - Drupal backend performance and scalabilityDrupalCampLA 2014 - Drupal backend performance and scalability
DrupalCampLA 2014 - Drupal backend performance and scalabilitycherryhillco
 
JSR107 Come, Code, Cache, Compute!
JSR107 Come, Code, Cache, Compute! JSR107 Come, Code, Cache, Compute!
JSR107 Come, Code, Cache, Compute! Payara
 
Drupal High Availability High Performance 2012
Drupal High Availability High Performance 2012Drupal High Availability High Performance 2012
Drupal High Availability High Performance 2012Amazee Labs
 
A Tale of 2 Systems
A Tale of 2 SystemsA Tale of 2 Systems
A Tale of 2 SystemsDavid Newman
 
Silverstripe at scale - design & architecture for silverstripe applications
Silverstripe at scale - design & architecture for silverstripe applicationsSilverstripe at scale - design & architecture for silverstripe applications
Silverstripe at scale - design & architecture for silverstripe applicationsBrettTasker
 
Magento performance feat. core Hacks
Magento performance feat. core HacksMagento performance feat. core Hacks
Magento performance feat. core HacksDaniel Niedergesäß
 
Supercharge JavaEE applications using JCache
Supercharge JavaEE applications using JCacheSupercharge JavaEE applications using JCache
Supercharge JavaEE applications using JCachePayara
 
Performance and scalability with drupal
Performance and scalability with drupalPerformance and scalability with drupal
Performance and scalability with drupalRonan Berder
 
Caching: A Guided Tour - 10/12/2010
Caching: A Guided Tour - 10/12/2010Caching: A Guided Tour - 10/12/2010
Caching: A Guided Tour - 10/12/2010Jason Ragsdale
 
Load Balancing and Scaling with NGINX
Load Balancing and Scaling with NGINXLoad Balancing and Scaling with NGINX
Load Balancing and Scaling with NGINXNGINX, Inc.
 
Ansible for large scale deployment
Ansible for large scale deploymentAnsible for large scale deployment
Ansible for large scale deploymentKarthik .P.R
 
Leveraging Docker and CoreOS to provide always available Cassandra at Instacl...
Leveraging Docker and CoreOS to provide always available Cassandra at Instacl...Leveraging Docker and CoreOS to provide always available Cassandra at Instacl...
Leveraging Docker and CoreOS to provide always available Cassandra at Instacl...DataStax
 
Ruby Driver Explained: DataStax Webinar May 5th 2015
Ruby Driver Explained: DataStax Webinar May 5th 2015Ruby Driver Explained: DataStax Webinar May 5th 2015
Ruby Driver Explained: DataStax Webinar May 5th 2015DataStax
 
Squeezing Performance out of Hazelcast
Squeezing Performance out of HazelcastSqueezing Performance out of Hazelcast
Squeezing Performance out of HazelcastHazelcast
 

Tendances (20)

Improve Magento Performance
Improve Magento PerformanceImprove Magento Performance
Improve Magento Performance
 
Next Generation DevOps in Drupal: DrupalCamp London 2014
Next Generation DevOps in Drupal: DrupalCamp London 2014Next Generation DevOps in Drupal: DrupalCamp London 2014
Next Generation DevOps in Drupal: DrupalCamp London 2014
 
Aem dispatcher – tips & tricks
Aem dispatcher – tips & tricksAem dispatcher – tips & tricks
Aem dispatcher – tips & tricks
 
Configuring Apache Servers for Better Web Perormance
Configuring Apache Servers for Better Web PerormanceConfiguring Apache Servers for Better Web Perormance
Configuring Apache Servers for Better Web Perormance
 
DrupalCampLA 2014 - Drupal backend performance and scalability
DrupalCampLA 2014 - Drupal backend performance and scalabilityDrupalCampLA 2014 - Drupal backend performance and scalability
DrupalCampLA 2014 - Drupal backend performance and scalability
 
JSR107 Come, Code, Cache, Compute!
JSR107 Come, Code, Cache, Compute! JSR107 Come, Code, Cache, Compute!
JSR107 Come, Code, Cache, Compute!
 
Drupal High Availability High Performance 2012
Drupal High Availability High Performance 2012Drupal High Availability High Performance 2012
Drupal High Availability High Performance 2012
 
A Tale of 2 Systems
A Tale of 2 SystemsA Tale of 2 Systems
A Tale of 2 Systems
 
Silverstripe at scale - design & architecture for silverstripe applications
Silverstripe at scale - design & architecture for silverstripe applicationsSilverstripe at scale - design & architecture for silverstripe applications
Silverstripe at scale - design & architecture for silverstripe applications
 
Magento performance feat. core Hacks
Magento performance feat. core HacksMagento performance feat. core Hacks
Magento performance feat. core Hacks
 
Supercharge JavaEE applications using JCache
Supercharge JavaEE applications using JCacheSupercharge JavaEE applications using JCache
Supercharge JavaEE applications using JCache
 
Performance and scalability with drupal
Performance and scalability with drupalPerformance and scalability with drupal
Performance and scalability with drupal
 
Caching: A Guided Tour - 10/12/2010
Caching: A Guided Tour - 10/12/2010Caching: A Guided Tour - 10/12/2010
Caching: A Guided Tour - 10/12/2010
 
Load Balancing and Scaling with NGINX
Load Balancing and Scaling with NGINXLoad Balancing and Scaling with NGINX
Load Balancing and Scaling with NGINX
 
Ansible for large scale deployment
Ansible for large scale deploymentAnsible for large scale deployment
Ansible for large scale deployment
 
Leveraging Docker and CoreOS to provide always available Cassandra at Instacl...
Leveraging Docker and CoreOS to provide always available Cassandra at Instacl...Leveraging Docker and CoreOS to provide always available Cassandra at Instacl...
Leveraging Docker and CoreOS to provide always available Cassandra at Instacl...
 
Ruby Driver Explained: DataStax Webinar May 5th 2015
Ruby Driver Explained: DataStax Webinar May 5th 2015Ruby Driver Explained: DataStax Webinar May 5th 2015
Ruby Driver Explained: DataStax Webinar May 5th 2015
 
Performance out
Performance outPerformance out
Performance out
 
Squeezing Performance out of Hazelcast
Squeezing Performance out of HazelcastSqueezing Performance out of Hazelcast
Squeezing Performance out of Hazelcast
 
Caching on the web
Caching on the webCaching on the web
Caching on the web
 

Similaire à Accelerating Rails with edge caching

Rails Caching: Secrets From the Edge
Rails Caching: Secrets From the EdgeRails Caching: Secrets From the Edge
Rails Caching: Secrets From the EdgeFastly
 
Rails Caching Secrets from the Edge
Rails Caching Secrets from the EdgeRails Caching Secrets from the Edge
Rails Caching Secrets from the EdgeMichael May
 
Caching your rails application
Caching your rails applicationCaching your rails application
Caching your rails applicationArrrrCamp
 
Day 7 - Make it Fast
Day 7 - Make it FastDay 7 - Make it Fast
Day 7 - Make it FastBarry Jones
 
Advanced Apache Cayenne
Advanced Apache CayenneAdvanced Apache Cayenne
Advanced Apache CayenneWO Community
 
Improving Performance on Magento 1*
Improving Performance on Magento 1*Improving Performance on Magento 1*
Improving Performance on Magento 1*David Z. Lerner
 
In-browser storage and me
In-browser storage and meIn-browser storage and me
In-browser storage and meJason Casden
 
More Cache for Less Cash (DevLink 2014)
More Cache for Less Cash (DevLink 2014)More Cache for Less Cash (DevLink 2014)
More Cache for Less Cash (DevLink 2014)Michael Collier
 
Play Framework and Activator
Play Framework and ActivatorPlay Framework and Activator
Play Framework and ActivatorKevin Webber
 
06 integrate elasticsearch
06 integrate elasticsearch06 integrate elasticsearch
06 integrate elasticsearchErhwen Kuo
 
High performance website
High performance websiteHigh performance website
High performance websiteChamnap Chhorn
 
DrupalSouth 2015 - Performance: Not an Afterthought
DrupalSouth 2015 - Performance: Not an AfterthoughtDrupalSouth 2015 - Performance: Not an Afterthought
DrupalSouth 2015 - Performance: Not an AfterthoughtNick Santamaria
 
How_To_Soup_Up_Your_Farm
How_To_Soup_Up_Your_FarmHow_To_Soup_Up_Your_Farm
How_To_Soup_Up_Your_FarmNigel Price
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcachedJurriaan Persyn
 
The Need for Speed - EpiCenter 2010
The Need for Speed - EpiCenter 2010The Need for Speed - EpiCenter 2010
The Need for Speed - EpiCenter 2010Phil Pursglove
 

Similaire à Accelerating Rails with edge caching (20)

Rails Caching: Secrets From the Edge
Rails Caching: Secrets From the EdgeRails Caching: Secrets From the Edge
Rails Caching: Secrets From the Edge
 
Rails Caching Secrets from the Edge
Rails Caching Secrets from the EdgeRails Caching Secrets from the Edge
Rails Caching Secrets from the Edge
 
Caching your rails application
Caching your rails applicationCaching your rails application
Caching your rails application
 
Mini-Training: To cache or not to cache
Mini-Training: To cache or not to cacheMini-Training: To cache or not to cache
Mini-Training: To cache or not to cache
 
Day 7 - Make it Fast
Day 7 - Make it FastDay 7 - Make it Fast
Day 7 - Make it Fast
 
Advanced Apache Cayenne
Advanced Apache CayenneAdvanced Apache Cayenne
Advanced Apache Cayenne
 
Improving Performance on Magento 1*
Improving Performance on Magento 1*Improving Performance on Magento 1*
Improving Performance on Magento 1*
 
In-browser storage and me
In-browser storage and meIn-browser storage and me
In-browser storage and me
 
More Cache for Less Cash (DevLink 2014)
More Cache for Less Cash (DevLink 2014)More Cache for Less Cash (DevLink 2014)
More Cache for Less Cash (DevLink 2014)
 
Play Framework and Activator
Play Framework and ActivatorPlay Framework and Activator
Play Framework and Activator
 
06 integrate elasticsearch
06 integrate elasticsearch06 integrate elasticsearch
06 integrate elasticsearch
 
High performance website
High performance websiteHigh performance website
High performance website
 
Caching in Kentico 11
Caching in Kentico 11Caching in Kentico 11
Caching in Kentico 11
 
DrupalSouth 2015 - Performance: Not an Afterthought
DrupalSouth 2015 - Performance: Not an AfterthoughtDrupalSouth 2015 - Performance: Not an Afterthought
DrupalSouth 2015 - Performance: Not an Afterthought
 
Where to save my data, for devs!
Where to save my data, for devs!Where to save my data, for devs!
Where to save my data, for devs!
 
Velocity - Edge UG
Velocity - Edge UGVelocity - Edge UG
Velocity - Edge UG
 
How_To_Soup_Up_Your_Farm
How_To_Soup_Up_Your_FarmHow_To_Soup_Up_Your_Farm
How_To_Soup_Up_Your_Farm
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcached
 
Azure appfabric caching intro and tips
Azure appfabric caching intro and tipsAzure appfabric caching intro and tips
Azure appfabric caching intro and tips
 
The Need for Speed - EpiCenter 2010
The Need for Speed - EpiCenter 2010The Need for Speed - EpiCenter 2010
The Need for Speed - EpiCenter 2010
 

Dernier

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 

Dernier (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 

Accelerating Rails with edge caching

  • 1. Accelerating Rails with edge caching Michael May | @ohaimmay | SFRails | 10/16/2014
  • 2.
  • 3. Topic • Rails caching best practices • Dynamic content/caching • Edge caching • Edge caching dynamic content with Rails
  • 4. Rails caching • Query/SQL • Page/Action (removed from Rails 4 core) • Asset • Fragment
  • 6. Query caching • Automagically done by rails when perform_caching = true • Not cached between requests! • Could just store the query result in a variable
  • 7. Manual Query Caching class Product < MyModel def self.out_of_stock Rails.cache.fetch("out_of_stock", expires_in: 1.hour) do Product.where("inventory.quantity = 0") end end end
  • 8. Asset Caching • Serve static assets from a proxy • config.serve_static_assets = false • Enable Compression* • config.assets.compress = true • # In Rails 4 • config.assets.css_compressor = :yui • config.assets.js_compressor = :uglifier • Asset Digests • config.assets.digest = true
  • 10. Enable Compression* Compress HTML, JSON responses at runtime module FastestAppEver class Application < Rails::Application config.middleware.use Rack::Deflater end * http://robots.thoughtbot.com/content-compression-with-rack-deflater end
  • 11.
  • 12.
  • 13. Asset Caching • Configure an asset host if needed • config.action_controller.asset_host = ENV[‘FASTLY_CDN_URL'] • Cache-Control like a pro • config.static_cache_control = 'public, s-maxage= 15552000, maxage=2592000'
  • 14. Cache-Control public, s-maxage=15552000, maxage=2592000 public “please cache me” maxage=2592000 “keep me for 30 days” s-maxage=15552000 “PROXIES ONLY! - Keep me for 180 days”
  • 15. Keepin’ it fresh • stale-while-revalidate • Serve the current (stale) version for n seconds while it re-fetches the latest version in the background • Cache-Control: max-age=604800, stale-while-revalidate=86400 • stale-if-error • If the re-fetch fails within n seconds of the response becoming stale, serve the cached response • Cache-Control: max-age=604800, stale-while-revalidate=86400, stale-if-error= 259200
  • 16. The Vary HTTP Header • In general, never Vary on anything other than Content-Encoding • Varying makes it impossible to serve the same response more than once and limits caching benefits • NEVER Vary on User-Agent! • There are THOUSANDS of these!
  • 17.
  • 19. Dynamic Content • Changes are unpredictable! • user driven events • Can’t just set a Time To Live (TTL)
  • 20. Dynamic Content • Changes are unpredictable! • user driven events • Can’t just set a Time To Live (TTL) • Frequently, but not continuously changing • Actually static for short periods of time (we can cache static things)!
  • 21. Dynamic Content Caching • Usually don’t • Edge Side Includes (ESI) • Dynamic Site Acceleration (DSA)
  • 22. Fragment Caching The rails answer to caching dynamic HTML # products/index.html.erb <% cache(cache_key_for_products) do %> <% Product.all.each do |p| %> <%= link_to p.name, product_url(p) %> <% end %> <% end %> # products_controller.rb def update … expire_fragment(cache_key_for_products) … end
  • 23. Nested Fragment Caching <% cache(cache_key_for_products) do %> All available products: <% Product.all.each do |p| %> <% cache(p) do %> <%= link_to p.name, product_url(p) %> <% end %> <% end %> <% end %>
  • 24. Nested Fragment Issues • Tedious • Comb through (probably terrible) view code • Cache keys are weird • “A given key should always return the same content.” - DHH • products/15-20110218104500 • “A given key should always return the most up-to-date content.” - Me • products/15 • Hacks around cache limitations • Memcache has no wildcard purging!
  • 25. Nested Fragment Issues • Garbage left in the cache • Defaults writing to disk • Memcached, Redis, etc • Probably lives in the same DC as your app server • Distributing, replication takes effort • What about dynamic API caching? • “The caching itself happens in the views based on partials rendering the objects in question” • Take control over your cached data!
  • 27. Edge Caching aka content delivery network aka CDN
  • 28. Edge Cache • Geographically distributed • Highly optimized storage and network (nanoseconds count) • Move content physically closer to the end-users • End goal - DECREASE LATENCY!
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34. The more content we can offload, the better performance we get
  • 35. #cachemoney • App servers cost real cash money (not cache money) • Less requests back to your application server • Avoid complex or less efficient strategies • Edge Side Includes (ESI) • Fragment caching
  • 36. Edge caching the dynamic content
  • 37.
  • 38. Our approach to dynamic content • Tag content with Surrogate-Key HTTP headers • Programmatically purge (~150ms globally) • By Surrogate-Key • By resource path • Real-time analytics and log streaming • Optimize the hell out of the pieces of the network we can control
  • 39. Tagging responses with Surrogate-Key
  • 40. class ProductsController < ApplicationController # set Cache-Control, strip Set-Cookie before_filter :set_cache_control_headers,only [:index,:show] def index @products = Product.last(10) # set Surrogate-Key: products set_surrogate_key_header @products.table_key respond_with @products end def show @product = Products.find(params[:id]) # set Surrogate-Key: product/666 set_surrogate_key_header @product.record_key respond_with @product end end
  • 41. class ProductsController < ApplicationController # set Cache-Control, strip Set-Cookie before_filter :set_cache_control_headers,only [:index,:show] def index @products = Product.last(10) # set Surrogate-Key: products set_surrogate_key_header @products.table_key respond_with @products end def show @product = Products.find(params[:id]) # set Surrogate-Key: product/666 set_surrogate_key_header @product.record_key respond_with @product end end
  • 42. class ProductsController < ApplicationController # set Cache-Control, strip Set-Cookie before_filter :set_cache_control_headers,only [:index,:show] def index @products = Product.last(10) # set Surrogate-Key: products set_surrogate_key_header @products.table_key respond_with @products end def show @product = Products.find(params[:id]) # set Surrogate-Key: product/666 set_surrogate_key_header @product.record_key respond_with @product end end
  • 44. class ProductsController < ApplicationController def create @product = Product.new(params) if @product.save # purge Surrogate-Key: products @product.purge_all render @product end end ...
  • 45. def update @product = Product.find(params[:id]) if @product.update(params) # purge Surrogate-Key: product/666 @product.purge render @product end end
  • 47. Edge caching in practice
  • 48. Be aware of cookies • Nothing with a Set-Cookie header is cached (by default) • Authentication frameworks/middleware might inject Set-Cookie after the rails stack removes it • Avoid caching pains by knowing when, where, and how you use Set-Cookie
  • 50. URL Rewriting • Apache, nginx, etc support URL Rewriting • Filter bad requests • Normalize paths
  • 51. URL Rewrite at the edge • Varnish HTTP cache • VCL • Requests never hit origin!
  • 53. What can we do better? • Add better caching defaults? • Cache-Control, stale-while-revalidate, stale-if-error • Re-use existing rails cache interfaces for edge caching? • ActiveSupport::Cache::EdgeStore • More fine-grained integration with HTTP accelerators like Varnish?
  • 54. Takeaways • Rails has tons of built-in caching options • Get fancy with Cache-Control directives • Use Google PageSpeed Insights (chrome plugin adds it to dev tools) • Dynamic edge caching is all about the power of purge! • Similar school of thought to rails action caching
  • 55. Questions? Contact: Michael May | @ohaimmay | michael@fastly.com cool links: fastly-rails - github.com/fastly/fastly-rails surrogate keys - fastly.com/blog/surrogate-keys-part-1 cache-control tutorial - docs.fastly.com/guides/tutorials/cache-control-tutorial serve stale cache-control - fastly.com/blog/stale-while-revalidate vary header best practices - fastly.com/blog/best-practices-for-using-the-vary-header caching like & share buttons - fastly.com/blog/caching-like-and-share-buttons pagespeed insights - developers.google.com/speed/pagespeed