SlideShare a Scribd company logo
1 of 25
Download to read offline
TO CACHE OR NOT TO CACHE
MAXIME LEMAITRE - 06/02/2013

Private and Confidential
Agenda

•
•
•
•
•

Introduction
Definition, Concepts, … and a quiz
Caching in Web Applications
Caching for WebFarms and IS
Conclusion
???

Database
Cache

CPU Cache

Browser
Cache

Can you give me
an example
of cache system?

Web Cache

???

Disk Cache

Memory
Cache

Output
Cache

DNS Cache

???

Memoization

Distributed
Cache

???
3
Caching
introduction

A cache transparently stores data “somewhere” so that
future requests can be served faster
If requested data is in the cache (cache hit), this
request can be served by simply reading the cache,
which is comparatively faster.
Otherwise (cache miss), the data has to be recomputed
or fetched from its original storage location, which is
comparatively slower
4
Caching
why ? how ? when ? who ?

• Why ?
–
–
–
–

Better performance
Better scalability
Better robustness
Reduce costs

system loads faster/better responsiveness
limit bottlenecks in a system
can support more load
reduce round trips, servers and hardware

• How ?
– Many APIs/Frameworks available to meet all of your needs
– Many types of caching

always easy to use
“think cache” !

• When ?
– Not too late

“Designed with cache” is better than “Designed for being cached”

• Who ?
– You (The Devs)

you will never have a business request to use caching

Do you remember « Quality of Service » ? Caching is typically implicit and
you will have to put it in your projects by yourself
5
Caching
short quiz
• Does Caching always improve performance ?
No : even if performance is the target, an unhealthy cache will hurt performance.
« Cache everything » is also counterproductive.

• Is Caching small data items useless ?
No : ideal caching candidates are items frequently accessed, long to compute or
that change infrequently.

• Is Caching mandatory for web apps ?

Yes : because of high traffic, dynamic page content, … and because our business
involves frequently updates (lives, odds, …)

• Is Caching only for data access layer ?

No : you can cache everything. Caching raw data is only a small part of the job.

• Caching == indexes ?
No : Indexes are a way to get data faster ; cache is a copy of data (stale)
6
Caching
terminology
•
•
•
•

•
•
•
•
•
•
•
•

Cache Hit : when requested data is contained in the cache
Cache Miss : when requested data in not in the cache.Has to be recomputed or fetched
from its original storage
Cache Key : unique identifier for a data item in the cache

Expiration
– Absolute : item expires at a specific date, regardless of how often it is accessed
– Sliding : specifies how long after an item was last accessed that it expires
Backing store : persist cached data on disk
Cache Scavenging : deleting items from the cache when memory is scarce
MRU/LRU/LFU : algorithms for removing objects from the cache when scavenging
Memoization : avoid repeating the calculation of results for previously processed inputs
Cache Dependancy : item’s lifetime in the cache to be dependent on other application
elements such as files or databases
Local Cache : caching data on clients rather than on servers
Distributed Cache : extension of the traditional concept of cache that may span multiple
servers

…

7
Caching in Web Applications
in the next slides

• CDN (forward proxy)
– Serve content to end-users with high availability and high
performance (Images, Scripts, Medias, …)

• User-scoped caching (Session)
– allows us to store data that persists between multiple
requests on a per-user basis

•
•
•
•

Memory cache
Browser cache
HTML5 cache features
ASP.NET caching techniques
8
System.Runtime.Caching.MemoryCache
who does not know this cache, does not know very well BetClic
•
•
•

•

Previously called asp.net cache or Web Cache
Built-in, supported and recommended by Microsoft
Basically a key-value store (dictionary) that lives
throughout the lifetime of the application but
ASP.NET automatically manages removal of cached
items (Expiration, Eviction, Dependancy, …)
Be aware of double-checked locking (see below)

9
Browser Cache
part of HTTP protocol
Resources that are cached locally by the browser are controlled by
three basic mechanisms, defined by HTTP Headers.
• Freshness allows a response to be used without re-checking it on the
origin server, and can be controlled by both the server and the client.
–
–

•

Validation is used to check whether a cached response is still good after it
becomes stale.
–
–

•

“Expires” response header gives a date when the document becomes stale
“Cache-Control: max-age” tells the cache how many seconds the response is
fresh for.

For example, if the response has a ”Last-Modified” header, a cache can make a
conditional request using the ”If-Modified-Since” header to see if it has changed.
The ”Etag” mechanism allows for both strong validation rather than the IfModified-Since header which is often referred as weak validation.

Invalidation is usually a side effect of another request that passes through
the cache. For example, if URL associated with a cached response
subsequently gets a POST, PUT or DELETE request, the cached response will
be invalidated.

What’s the difference between a response 200 (from cache) and 304 (Not Modified) ?

10
Output Cache
an efficient cache for dynamic content
• Caching the HTML that is generated as a result of a request
• Simple add an attribute to a Controller or an action
• Able to cache multiple versions of the same controller action based on the
request parameters used to call the action
• By default content is cached in three locations: the web server, any proxy
servers, and the user’s web browser, but you can have fine-grained control
Warning : do not add output cache to something which is bound to user
like session

11
Output Cache
Donut Caching Vs Donut Hole Caching
• Donut caching
Server-side caching technique in which the entire page gets cached except for
a small portions which remain dynamic. It’s not a native feature of MVC (why
?) but there are Nuget Packages.

• Donut Hole Caching
Donut hole caching is where you cache one or more parts of a page, but not
the entire page. It is handled by using the built-in OutputCache attribute on
one or more child actions (called using Html.Action or Html.RenderAction from
the parent view)

12
HTML 5 Web Storage
Local Storage /Session Storage
• A "super cookie“ that allow us to persist
data (up to 5 MB) in the browser
(shared between tabs and keep after
restart)
• Only strings (thanks to JSON)
• JS API to store, get, remove, clear, get
remaining space, …
• Many frameworks available such as
http://www.jstorage.info/ ( Caching &
TTL Support for all browsers)
• Used at betClic for Live/Multiplex (Keep
static translations)

13
HTML Cache Manifest
for offline web applications
•

HTML5 feature to access a web application even without a
network connection
–
–

•

Cache Manifiest File : Allows a dev to specify which files the
browser should cache and make available to offline users.
–
–
–

•

Avoid the classic « 404 Not Found » Page, but not only …
In the Top 5 supported HTML5 features in mobiles devices

CACHE files will be explicitly cached after they're downloaded
NETWORK files are white-listed resources that require a
connection ; resources bypass the cache,
FALLBACK fallback file if a resource is inaccessible

Also some APIs to check the cache state and switch to online
too (work offline)

14
Caching for Information Systems and Web Farms
On the Road to another type of cache
All previous techniques are good but suffer from majors inconvenients :
– Cached Data reside in the same process/server
If we have 30 front end servers, they will have to set up their own cache.
– No High availability
If the web site is restarted/the pool recycled, all cached items are lost.
– Cached Data is limited to the server capacity
If all cached data become important, we need additional memory on the server
– Does not fit very well in WebFarms/Information Systems
imagine a company with databases in Gib and frontend server in Paris … 

Couldn't we find a way to solve all these problems?

Distributed Cache
15
Distributed Cache
introduction
Quite recent in web applications because
•
•

Memory is now very cheap and network cards are very fast
Works well on lower cost machines usually employed for web servers as opposed
to database servers which require expensive hardware

Currently two main approaches
•
•

Use a real distributed cache
Use a NoSql Database

Memcached
16
Typical Web Architecture
without distributed cache

Users

• Need to route users to same
machine (i.e. sticky sessions)

Web Tier

• Each machine round trips for data
• Some data might be expensive to
retrieve
• Cached data is typically stored in
the memory of one (each) server

Database

• CPU and disk can get saturated due
with an increase traffic

17
Scalable Web Architecture
with distributed cache

Users
Web Tier
Caching
Tier

Database

• No stick load balancing needed – all
servers have copy of cached data

• Easy access to cache cluster

• Multiple machines means scale and
potential for high-availability
• More machines == more memory for
cache objects

• Reduces load on database

18
Windows Server AppFabric
an example of Distributed Caching
•
•
•

A Distributed in-memory cache for “data”
.Net Client Api (Nuget Package)
Two Patterns :
– Cache Aside
– Read-Through and Write-Behind

•

Main Features
–
–
–
–
–
–
–
–
–
–

Logical Containers (Cache/Region)
Local Cache
Expiration/Eviction
Notifications
Secure API
High Availability
Concurrency Model
Tags
Monitoring API
…

//create DataCacheFactory based on config file
var dcf = new DataCacheFactory();
//get the cache named "TestCache"
var cache = dcf.GetCache(“MyCache");
//Add an item called "Test" - throws if exists
cache.Add("Test", new Data { TheData = "Test" });
//Get "Test" - add if not in cache (cache-aside)
var test = cache.Get("Test") as Data;
if (test == null)
{
test = new Data {TheData = "Test" };
cache.Add("Test", test);
}

19
Windows Service AppFabric Caching
Bonuses
•

Additional ASPNET Providers:
– Session State Provider

– Output Cache Provider

20
Caching challenges
remember this !

• Adding caching to a system is always easy
• Items in the cache may can become out-of-date or stale
– Find to best expiration duration

• Be careful of multiple cache level
– Db Cache + Service Cache + API Cache + Browser Cache = ???

• Be careful of Cache Health
– Always monitor your cache

• Be careful of Cache Context
– Do not include User-Specific data

• Find the best granularity for your usage
– Large list Vs many small items ?

• …
21
Questions ?

22
Appendices
•
•
•
•
•
•
•
•

http://www.mnot.net/cache_docs/
MvcDonutCaching Package : http://nuget.org/packages/MvcDonutCaching
AppFabric.Client Package : http://nuget.org/packages/ServerAppFabric.Client/
http://html5demos.com/
http://www.html5rocks.com
Caching Architecture Guide for .NET Framework Applications :
http://msdn.microsoft.com/en-us/library/ee817646.aspx
http://redis.io/
http://www.infoq.com/news/2011/11/distributed-cache-nosql-data-sto

23
Find out more
•

On https://techblog.betclicgroup.com/
About Betclic
•
•

•

Betclic Everest Group, one of the world leaders in online gaming, has a unique portfolio
comprising various complementary international brands: Betclic, Everest Gaming, bet-athome.com, Expekt…
Active in 100 countries with more than 12 million customers worldwide, the Group is
committed to promoting secure and responsible gaming and is a member of several
international professional associations including the EGBA (European Gaming and Betting
Association) and the ESSA (European Sports Security Association).
Through our brands, Betclic Everest Group places expertise, technological know-how and
security at the heart of our strategy to deliver an on-line gaming offer attuned to the passion
of our players.

More Related Content

What's hot

Drupal feature proposal: two new stream-wrappers
Drupal feature proposal: two new stream-wrappersDrupal feature proposal: two new stream-wrappers
Drupal feature proposal: two new stream-wrappersMarcus Deglos
 
Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slidesmkherlakian
 
Caching Data For Performance
Caching Data For PerformanceCaching Data For Performance
Caching Data For PerformanceDave Ross
 
Evolution of MongoDB Replicaset and Its Best Practices
Evolution of MongoDB Replicaset and Its Best PracticesEvolution of MongoDB Replicaset and Its Best Practices
Evolution of MongoDB Replicaset and Its Best PracticesMydbops
 
How to reduce database load using Memcache
How to reduce database load using MemcacheHow to reduce database load using Memcache
How to reduce database load using Memcachevaluebound
 
Integration with hdfs using WebDFS and NFS
Integration with hdfs using WebDFS and NFSIntegration with hdfs using WebDFS and NFS
Integration with hdfs using WebDFS and NFSChristophe Marchal
 
WebHDFS at King - May 2014 Hadoop MeetUp
WebHDFS at King - May 2014 Hadoop MeetUpWebHDFS at King - May 2014 Hadoop MeetUp
WebHDFS at King - May 2014 Hadoop MeetUphuguk
 
Drupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance SitesDrupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance Sitesdrupalcampest
 
Performance Optimization using Caching | Swatantra Kumar
Performance Optimization using Caching | Swatantra KumarPerformance Optimization using Caching | Swatantra Kumar
Performance Optimization using Caching | Swatantra KumarSwatantra Kumar
 
Clug 2011 March web server optimisation
Clug 2011 March  web server optimisationClug 2011 March  web server optimisation
Clug 2011 March web server optimisationgrooverdan
 
Drupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hoodDrupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hoodPiyuesh Kumar
 
Cache all the things - A guide to caching Drupal
Cache all the things - A guide to caching DrupalCache all the things - A guide to caching Drupal
Cache all the things - A guide to caching Drupaldigital006
 
Sofia WP User Group Presentation
Sofia WP User Group PresentationSofia WP User Group Presentation
Sofia WP User Group PresentationDaniel Kanchev
 
Hosting huge amount of binaries in JCR
Hosting huge amount of binaries in JCRHosting huge amount of binaries in JCR
Hosting huge amount of binaries in JCRWoonsan Ko
 
Caching Enhancement in ASP.NET 4.0
Caching Enhancement in ASP.NET 4.0Caching Enhancement in ASP.NET 4.0
Caching Enhancement in ASP.NET 4.0Abhijit Jana
 

What's hot (20)

Drupal feature proposal: two new stream-wrappers
Drupal feature proposal: two new stream-wrappersDrupal feature proposal: two new stream-wrappers
Drupal feature proposal: two new stream-wrappers
 
Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slides
 
Caching Data For Performance
Caching Data For PerformanceCaching Data For Performance
Caching Data For Performance
 
Evolution of MongoDB Replicaset and Its Best Practices
Evolution of MongoDB Replicaset and Its Best PracticesEvolution of MongoDB Replicaset and Its Best Practices
Evolution of MongoDB Replicaset and Its Best Practices
 
How to reduce database load using Memcache
How to reduce database load using MemcacheHow to reduce database load using Memcache
How to reduce database load using Memcache
 
Integration with hdfs using WebDFS and NFS
Integration with hdfs using WebDFS and NFSIntegration with hdfs using WebDFS and NFS
Integration with hdfs using WebDFS and NFS
 
WebHDFS at King - May 2014 Hadoop MeetUp
WebHDFS at King - May 2014 Hadoop MeetUpWebHDFS at King - May 2014 Hadoop MeetUp
WebHDFS at King - May 2014 Hadoop MeetUp
 
Drupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance SitesDrupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance Sites
 
Performance Optimization using Caching | Swatantra Kumar
Performance Optimization using Caching | Swatantra KumarPerformance Optimization using Caching | Swatantra Kumar
Performance Optimization using Caching | Swatantra Kumar
 
Clug 2011 March web server optimisation
Clug 2011 March  web server optimisationClug 2011 March  web server optimisation
Clug 2011 March web server optimisation
 
Caching in asp.net mvc
Caching in asp.net mvcCaching in asp.net mvc
Caching in asp.net mvc
 
Asp.net caching
Asp.net cachingAsp.net caching
Asp.net caching
 
04 web optimization
04 web optimization04 web optimization
04 web optimization
 
Drupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hoodDrupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hood
 
Cache all the things - A guide to caching Drupal
Cache all the things - A guide to caching DrupalCache all the things - A guide to caching Drupal
Cache all the things - A guide to caching Drupal
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
Sofia WP User Group Presentation
Sofia WP User Group PresentationSofia WP User Group Presentation
Sofia WP User Group Presentation
 
Aspnet Caching
Aspnet CachingAspnet Caching
Aspnet Caching
 
Hosting huge amount of binaries in JCR
Hosting huge amount of binaries in JCRHosting huge amount of binaries in JCR
Hosting huge amount of binaries in JCR
 
Caching Enhancement in ASP.NET 4.0
Caching Enhancement in ASP.NET 4.0Caching Enhancement in ASP.NET 4.0
Caching Enhancement in ASP.NET 4.0
 

Viewers also liked

High Performance Sites with Drupal and Cache Control Module
High Performance Sites with Drupal and Cache Control ModuleHigh Performance Sites with Drupal and Cache Control Module
High Performance Sites with Drupal and Cache Control ModuleExove
 
Caching Techniquesfor Content Delivery
Caching Techniquesfor Content DeliveryCaching Techniquesfor Content Delivery
Caching Techniquesfor Content Deliverysanjoysanyal
 
World Wide Web Caching
World Wide Web CachingWorld Wide Web Caching
World Wide Web Cachingersanbilik
 
Caching technology comparison
Caching technology comparisonCaching technology comparison
Caching technology comparisonRohit Kelapure
 
HTTP's Best-Kept Secret: Caching
HTTP's Best-Kept Secret: CachingHTTP's Best-Kept Secret: Caching
HTTP's Best-Kept Secret: Cachingrtomayko
 

Viewers also liked (6)

High Performance Sites with Drupal and Cache Control Module
High Performance Sites with Drupal and Cache Control ModuleHigh Performance Sites with Drupal and Cache Control Module
High Performance Sites with Drupal and Cache Control Module
 
Caching
CachingCaching
Caching
 
Caching Techniquesfor Content Delivery
Caching Techniquesfor Content DeliveryCaching Techniquesfor Content Delivery
Caching Techniquesfor Content Delivery
 
World Wide Web Caching
World Wide Web CachingWorld Wide Web Caching
World Wide Web Caching
 
Caching technology comparison
Caching technology comparisonCaching technology comparison
Caching technology comparison
 
HTTP's Best-Kept Secret: Caching
HTTP's Best-Kept Secret: CachingHTTP's Best-Kept Secret: Caching
HTTP's Best-Kept Secret: Caching
 

Similar to Mini-Training: To cache or not to cache

Selecting the right cache framework
Selecting the right cache frameworkSelecting the right cache framework
Selecting the right cache frameworkMohammed Fazuluddin
 
[Hanoi-August 13] Tech Talk on Caching Solutions
[Hanoi-August 13] Tech Talk on Caching Solutions[Hanoi-August 13] Tech Talk on Caching Solutions
[Hanoi-August 13] Tech Talk on Caching SolutionsITviec
 
Training Webinar: Enterprise application performance with distributed caching
Training Webinar: Enterprise application performance with distributed cachingTraining Webinar: Enterprise application performance with distributed caching
Training Webinar: Enterprise application performance with distributed cachingOutSystems
 
Caching your rails application
Caching your rails applicationCaching your rails application
Caching your rails applicationArrrrCamp
 
From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.Taras Matyashovsky
 
Developing High Performance and Scalable ColdFusion Application Using Terraco...
Developing High Performance and Scalable ColdFusion Application Using Terraco...Developing High Performance and Scalable ColdFusion Application Using Terraco...
Developing High Performance and Scalable ColdFusion Application Using Terraco...ColdFusionConference
 
Developing High Performance and Scalable ColdFusion Applications Using Terrac...
Developing High Performance and Scalable ColdFusion Applications Using Terrac...Developing High Performance and Scalable ColdFusion Applications Using Terrac...
Developing High Performance and Scalable ColdFusion Applications Using Terrac...Shailendra Prasad
 
Advanced caching techniques with ehcache, big memory, terracotta, and coldfusion
Advanced caching techniques with ehcache, big memory, terracotta, and coldfusionAdvanced caching techniques with ehcache, big memory, terracotta, and coldfusion
Advanced caching techniques with ehcache, big memory, terracotta, and coldfusionColdFusionConference
 
Caching and Its Main Types
Caching and Its Main TypesCaching and Its Main Types
Caching and Its Main TypesHTS Hosting
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcachedJurriaan Persyn
 
Tuning Your SharePoint Environment
Tuning Your SharePoint EnvironmentTuning Your SharePoint Environment
Tuning Your SharePoint Environmentvmaximiuk
 
Caching for J2ee Enterprise Applications
Caching for J2ee Enterprise ApplicationsCaching for J2ee Enterprise Applications
Caching for J2ee Enterprise ApplicationsDebajani Mohanty
 
Overview of the ehcache
Overview of the ehcacheOverview of the ehcache
Overview of the ehcacheHyeonSeok Choi
 
Share point 2013 distributed cache
Share point 2013 distributed cacheShare point 2013 distributed cache
Share point 2013 distributed cacheMichael Nokhamzon
 
Day 7 - Make it Fast
Day 7 - Make it FastDay 7 - Make it Fast
Day 7 - Make it FastBarry Jones
 
Accelerating Rails with edge caching
Accelerating Rails with edge cachingAccelerating Rails with edge caching
Accelerating Rails with edge cachingMichael May
 
VMworld 2013: Extreme Performance Series: Storage in a Flash
VMworld 2013: Extreme Performance Series: Storage in a Flash VMworld 2013: Extreme Performance Series: Storage in a Flash
VMworld 2013: Extreme Performance Series: Storage in a Flash VMworld
 

Similar to Mini-Training: To cache or not to cache (20)

Selecting the right cache framework
Selecting the right cache frameworkSelecting the right cache framework
Selecting the right cache framework
 
[Hanoi-August 13] Tech Talk on Caching Solutions
[Hanoi-August 13] Tech Talk on Caching Solutions[Hanoi-August 13] Tech Talk on Caching Solutions
[Hanoi-August 13] Tech Talk on Caching Solutions
 
Training Webinar: Enterprise application performance with distributed caching
Training Webinar: Enterprise application performance with distributed cachingTraining Webinar: Enterprise application performance with distributed caching
Training Webinar: Enterprise application performance with distributed caching
 
Caching your rails application
Caching your rails applicationCaching your rails application
Caching your rails application
 
From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.
 
Developing High Performance and Scalable ColdFusion Application Using Terraco...
Developing High Performance and Scalable ColdFusion Application Using Terraco...Developing High Performance and Scalable ColdFusion Application Using Terraco...
Developing High Performance and Scalable ColdFusion Application Using Terraco...
 
Developing High Performance and Scalable ColdFusion Applications Using Terrac...
Developing High Performance and Scalable ColdFusion Applications Using Terrac...Developing High Performance and Scalable ColdFusion Applications Using Terrac...
Developing High Performance and Scalable ColdFusion Applications Using Terrac...
 
Advanced caching techniques with ehcache, big memory, terracotta, and coldfusion
Advanced caching techniques with ehcache, big memory, terracotta, and coldfusionAdvanced caching techniques with ehcache, big memory, terracotta, and coldfusion
Advanced caching techniques with ehcache, big memory, terracotta, and coldfusion
 
Caching and Its Main Types
Caching and Its Main TypesCaching and Its Main Types
Caching and Its Main Types
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcached
 
AppFabric Velocity
AppFabric VelocityAppFabric Velocity
AppFabric Velocity
 
Caching in Kentico 11
Caching in Kentico 11Caching in Kentico 11
Caching in Kentico 11
 
Tuning Your SharePoint Environment
Tuning Your SharePoint EnvironmentTuning Your SharePoint Environment
Tuning Your SharePoint Environment
 
Drupal performance
Drupal performanceDrupal performance
Drupal performance
 
Caching for J2ee Enterprise Applications
Caching for J2ee Enterprise ApplicationsCaching for J2ee Enterprise Applications
Caching for J2ee Enterprise Applications
 
Overview of the ehcache
Overview of the ehcacheOverview of the ehcache
Overview of the ehcache
 
Share point 2013 distributed cache
Share point 2013 distributed cacheShare point 2013 distributed cache
Share point 2013 distributed cache
 
Day 7 - Make it Fast
Day 7 - Make it FastDay 7 - Make it Fast
Day 7 - Make it Fast
 
Accelerating Rails with edge caching
Accelerating Rails with edge cachingAccelerating Rails with edge caching
Accelerating Rails with edge caching
 
VMworld 2013: Extreme Performance Series: Storage in a Flash
VMworld 2013: Extreme Performance Series: Storage in a Flash VMworld 2013: Extreme Performance Series: Storage in a Flash
VMworld 2013: Extreme Performance Series: Storage in a Flash
 

More from Betclic Everest Group Tech Team

Mini-training: Personalization & Recommendation Demystified
Mini-training: Personalization & Recommendation DemystifiedMini-training: Personalization & Recommendation Demystified
Mini-training: Personalization & Recommendation DemystifiedBetclic Everest Group Tech Team
 

More from Betclic Everest Group Tech Team (20)

Mini training - Reactive Extensions (Rx)
Mini training - Reactive Extensions (Rx)Mini training - Reactive Extensions (Rx)
Mini training - Reactive Extensions (Rx)
 
Mini training - Moving to xUnit.net
Mini training - Moving to xUnit.netMini training - Moving to xUnit.net
Mini training - Moving to xUnit.net
 
Mini training - Introduction to Microsoft Azure Storage
Mini training - Introduction to Microsoft Azure StorageMini training - Introduction to Microsoft Azure Storage
Mini training - Introduction to Microsoft Azure Storage
 
Akka.Net
Akka.NetAkka.Net
Akka.Net
 
Mini training- Scenario Driven Design
Mini training- Scenario Driven DesignMini training- Scenario Driven Design
Mini training- Scenario Driven Design
 
Email Management in Outlook
Email Management in OutlookEmail Management in Outlook
Email Management in Outlook
 
Mini-Training: SSO with Windows Identity Foundation
Mini-Training: SSO with Windows Identity FoundationMini-Training: SSO with Windows Identity Foundation
Mini-Training: SSO with Windows Identity Foundation
 
Training - What is Performance ?
Training  - What is Performance ?Training  - What is Performance ?
Training - What is Performance ?
 
Mini-Training: Docker
Mini-Training: DockerMini-Training: Docker
Mini-Training: Docker
 
Mini Training Flyway
Mini Training FlywayMini Training Flyway
Mini Training Flyway
 
Mini-Training: NDepend
Mini-Training: NDependMini-Training: NDepend
Mini-Training: NDepend
 
Management 3.0 Workout
Management 3.0 WorkoutManagement 3.0 Workout
Management 3.0 Workout
 
Lean for Business
Lean for BusinessLean for Business
Lean for Business
 
Short-Training asp.net vNext
Short-Training asp.net vNextShort-Training asp.net vNext
Short-Training asp.net vNext
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
 
Mini-Training: Mobile UX Trends
Mini-Training: Mobile UX TrendsMini-Training: Mobile UX Trends
Mini-Training: Mobile UX Trends
 
Training: MVVM Pattern
Training: MVVM PatternTraining: MVVM Pattern
Training: MVVM Pattern
 
Mini-training: Personalization & Recommendation Demystified
Mini-training: Personalization & Recommendation DemystifiedMini-training: Personalization & Recommendation Demystified
Mini-training: Personalization & Recommendation Demystified
 
Mini-training: Let’s Git It!
Mini-training: Let’s Git It!Mini-training: Let’s Git It!
Mini-training: Let’s Git It!
 
AngularJS Best Practices
AngularJS Best PracticesAngularJS Best Practices
AngularJS Best Practices
 

Recently uploaded

March Patch Tuesday
March Patch TuesdayMarch Patch Tuesday
March Patch TuesdayIvanti
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInThousandEyes
 
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdfQ4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdfTejal81
 
Where developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingWhere developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingFrancesco Corti
 
LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0DanBrown980551
 
Planetek Italia Srl - Corporate Profile Brochure
Planetek Italia Srl - Corporate Profile BrochurePlanetek Italia Srl - Corporate Profile Brochure
Planetek Italia Srl - Corporate Profile BrochurePlanetek Italia Srl
 
How to release an Open Source Dataweave Library
How to release an Open Source Dataweave LibraryHow to release an Open Source Dataweave Library
How to release an Open Source Dataweave Libraryshyamraj55
 
My key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIMy key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIVijayananda Mohire
 
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechWebinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechProduct School
 
20140402 - Smart house demo kit
20140402 - Smart house demo kit20140402 - Smart house demo kit
20140402 - Smart house demo kitJamie (Taka) Wang
 
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series  - Day 1UiPath Studio Web workshop series  - Day 1
UiPath Studio Web workshop series - Day 1DianaGray10
 
EMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarEMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarThousandEyes
 
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTxtailishbaloch
 
AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024Brian Pichman
 
Oracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxOracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxSatishbabu Gunukula
 
Introduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationIntroduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationKnoldus Inc.
 
IT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingIT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingMAGNIntelligence
 
How to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxHow to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxKaustubhBhavsar6
 
Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.IPLOOK Networks
 
UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2DianaGray10
 

Recently uploaded (20)

March Patch Tuesday
March Patch TuesdayMarch Patch Tuesday
March Patch Tuesday
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
 
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdfQ4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
 
Where developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingWhere developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is going
 
LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0
 
Planetek Italia Srl - Corporate Profile Brochure
Planetek Italia Srl - Corporate Profile BrochurePlanetek Italia Srl - Corporate Profile Brochure
Planetek Italia Srl - Corporate Profile Brochure
 
How to release an Open Source Dataweave Library
How to release an Open Source Dataweave LibraryHow to release an Open Source Dataweave Library
How to release an Open Source Dataweave Library
 
My key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIMy key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAI
 
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechWebinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
 
20140402 - Smart house demo kit
20140402 - Smart house demo kit20140402 - Smart house demo kit
20140402 - Smart house demo kit
 
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series  - Day 1UiPath Studio Web workshop series  - Day 1
UiPath Studio Web workshop series - Day 1
 
EMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarEMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? Webinar
 
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
 
AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024
 
Oracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxOracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptx
 
Introduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationIntroduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its application
 
IT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingIT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced Computing
 
How to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxHow to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptx
 
Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.
 
UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2
 

Mini-Training: To cache or not to cache

  • 1. TO CACHE OR NOT TO CACHE MAXIME LEMAITRE - 06/02/2013 Private and Confidential
  • 2. Agenda • • • • • Introduction Definition, Concepts, … and a quiz Caching in Web Applications Caching for WebFarms and IS Conclusion
  • 3. ??? Database Cache CPU Cache Browser Cache Can you give me an example of cache system? Web Cache ??? Disk Cache Memory Cache Output Cache DNS Cache ??? Memoization Distributed Cache ??? 3
  • 4. Caching introduction A cache transparently stores data “somewhere” so that future requests can be served faster If requested data is in the cache (cache hit), this request can be served by simply reading the cache, which is comparatively faster. Otherwise (cache miss), the data has to be recomputed or fetched from its original storage location, which is comparatively slower 4
  • 5. Caching why ? how ? when ? who ? • Why ? – – – – Better performance Better scalability Better robustness Reduce costs system loads faster/better responsiveness limit bottlenecks in a system can support more load reduce round trips, servers and hardware • How ? – Many APIs/Frameworks available to meet all of your needs – Many types of caching always easy to use “think cache” ! • When ? – Not too late “Designed with cache” is better than “Designed for being cached” • Who ? – You (The Devs) you will never have a business request to use caching Do you remember « Quality of Service » ? Caching is typically implicit and you will have to put it in your projects by yourself 5
  • 6. Caching short quiz • Does Caching always improve performance ? No : even if performance is the target, an unhealthy cache will hurt performance. « Cache everything » is also counterproductive. • Is Caching small data items useless ? No : ideal caching candidates are items frequently accessed, long to compute or that change infrequently. • Is Caching mandatory for web apps ? Yes : because of high traffic, dynamic page content, … and because our business involves frequently updates (lives, odds, …) • Is Caching only for data access layer ? No : you can cache everything. Caching raw data is only a small part of the job. • Caching == indexes ? No : Indexes are a way to get data faster ; cache is a copy of data (stale) 6
  • 7. Caching terminology • • • • • • • • • • • • Cache Hit : when requested data is contained in the cache Cache Miss : when requested data in not in the cache.Has to be recomputed or fetched from its original storage Cache Key : unique identifier for a data item in the cache Expiration – Absolute : item expires at a specific date, regardless of how often it is accessed – Sliding : specifies how long after an item was last accessed that it expires Backing store : persist cached data on disk Cache Scavenging : deleting items from the cache when memory is scarce MRU/LRU/LFU : algorithms for removing objects from the cache when scavenging Memoization : avoid repeating the calculation of results for previously processed inputs Cache Dependancy : item’s lifetime in the cache to be dependent on other application elements such as files or databases Local Cache : caching data on clients rather than on servers Distributed Cache : extension of the traditional concept of cache that may span multiple servers … 7
  • 8. Caching in Web Applications in the next slides • CDN (forward proxy) – Serve content to end-users with high availability and high performance (Images, Scripts, Medias, …) • User-scoped caching (Session) – allows us to store data that persists between multiple requests on a per-user basis • • • • Memory cache Browser cache HTML5 cache features ASP.NET caching techniques 8
  • 9. System.Runtime.Caching.MemoryCache who does not know this cache, does not know very well BetClic • • • • Previously called asp.net cache or Web Cache Built-in, supported and recommended by Microsoft Basically a key-value store (dictionary) that lives throughout the lifetime of the application but ASP.NET automatically manages removal of cached items (Expiration, Eviction, Dependancy, …) Be aware of double-checked locking (see below) 9
  • 10. Browser Cache part of HTTP protocol Resources that are cached locally by the browser are controlled by three basic mechanisms, defined by HTTP Headers. • Freshness allows a response to be used without re-checking it on the origin server, and can be controlled by both the server and the client. – – • Validation is used to check whether a cached response is still good after it becomes stale. – – • “Expires” response header gives a date when the document becomes stale “Cache-Control: max-age” tells the cache how many seconds the response is fresh for. For example, if the response has a ”Last-Modified” header, a cache can make a conditional request using the ”If-Modified-Since” header to see if it has changed. The ”Etag” mechanism allows for both strong validation rather than the IfModified-Since header which is often referred as weak validation. Invalidation is usually a side effect of another request that passes through the cache. For example, if URL associated with a cached response subsequently gets a POST, PUT or DELETE request, the cached response will be invalidated. What’s the difference between a response 200 (from cache) and 304 (Not Modified) ? 10
  • 11. Output Cache an efficient cache for dynamic content • Caching the HTML that is generated as a result of a request • Simple add an attribute to a Controller or an action • Able to cache multiple versions of the same controller action based on the request parameters used to call the action • By default content is cached in three locations: the web server, any proxy servers, and the user’s web browser, but you can have fine-grained control Warning : do not add output cache to something which is bound to user like session 11
  • 12. Output Cache Donut Caching Vs Donut Hole Caching • Donut caching Server-side caching technique in which the entire page gets cached except for a small portions which remain dynamic. It’s not a native feature of MVC (why ?) but there are Nuget Packages. • Donut Hole Caching Donut hole caching is where you cache one or more parts of a page, but not the entire page. It is handled by using the built-in OutputCache attribute on one or more child actions (called using Html.Action or Html.RenderAction from the parent view) 12
  • 13. HTML 5 Web Storage Local Storage /Session Storage • A "super cookie“ that allow us to persist data (up to 5 MB) in the browser (shared between tabs and keep after restart) • Only strings (thanks to JSON) • JS API to store, get, remove, clear, get remaining space, … • Many frameworks available such as http://www.jstorage.info/ ( Caching & TTL Support for all browsers) • Used at betClic for Live/Multiplex (Keep static translations) 13
  • 14. HTML Cache Manifest for offline web applications • HTML5 feature to access a web application even without a network connection – – • Cache Manifiest File : Allows a dev to specify which files the browser should cache and make available to offline users. – – – • Avoid the classic « 404 Not Found » Page, but not only … In the Top 5 supported HTML5 features in mobiles devices CACHE files will be explicitly cached after they're downloaded NETWORK files are white-listed resources that require a connection ; resources bypass the cache, FALLBACK fallback file if a resource is inaccessible Also some APIs to check the cache state and switch to online too (work offline) 14
  • 15. Caching for Information Systems and Web Farms On the Road to another type of cache All previous techniques are good but suffer from majors inconvenients : – Cached Data reside in the same process/server If we have 30 front end servers, they will have to set up their own cache. – No High availability If the web site is restarted/the pool recycled, all cached items are lost. – Cached Data is limited to the server capacity If all cached data become important, we need additional memory on the server – Does not fit very well in WebFarms/Information Systems imagine a company with databases in Gib and frontend server in Paris …  Couldn't we find a way to solve all these problems? Distributed Cache 15
  • 16. Distributed Cache introduction Quite recent in web applications because • • Memory is now very cheap and network cards are very fast Works well on lower cost machines usually employed for web servers as opposed to database servers which require expensive hardware Currently two main approaches • • Use a real distributed cache Use a NoSql Database Memcached 16
  • 17. Typical Web Architecture without distributed cache Users • Need to route users to same machine (i.e. sticky sessions) Web Tier • Each machine round trips for data • Some data might be expensive to retrieve • Cached data is typically stored in the memory of one (each) server Database • CPU and disk can get saturated due with an increase traffic 17
  • 18. Scalable Web Architecture with distributed cache Users Web Tier Caching Tier Database • No stick load balancing needed – all servers have copy of cached data • Easy access to cache cluster • Multiple machines means scale and potential for high-availability • More machines == more memory for cache objects • Reduces load on database 18
  • 19. Windows Server AppFabric an example of Distributed Caching • • • A Distributed in-memory cache for “data” .Net Client Api (Nuget Package) Two Patterns : – Cache Aside – Read-Through and Write-Behind • Main Features – – – – – – – – – – Logical Containers (Cache/Region) Local Cache Expiration/Eviction Notifications Secure API High Availability Concurrency Model Tags Monitoring API … //create DataCacheFactory based on config file var dcf = new DataCacheFactory(); //get the cache named "TestCache" var cache = dcf.GetCache(“MyCache"); //Add an item called "Test" - throws if exists cache.Add("Test", new Data { TheData = "Test" }); //Get "Test" - add if not in cache (cache-aside) var test = cache.Get("Test") as Data; if (test == null) { test = new Data {TheData = "Test" }; cache.Add("Test", test); } 19
  • 20. Windows Service AppFabric Caching Bonuses • Additional ASPNET Providers: – Session State Provider – Output Cache Provider 20
  • 21. Caching challenges remember this ! • Adding caching to a system is always easy • Items in the cache may can become out-of-date or stale – Find to best expiration duration • Be careful of multiple cache level – Db Cache + Service Cache + API Cache + Browser Cache = ??? • Be careful of Cache Health – Always monitor your cache • Be careful of Cache Context – Do not include User-Specific data • Find the best granularity for your usage – Large list Vs many small items ? • … 21
  • 23. Appendices • • • • • • • • http://www.mnot.net/cache_docs/ MvcDonutCaching Package : http://nuget.org/packages/MvcDonutCaching AppFabric.Client Package : http://nuget.org/packages/ServerAppFabric.Client/ http://html5demos.com/ http://www.html5rocks.com Caching Architecture Guide for .NET Framework Applications : http://msdn.microsoft.com/en-us/library/ee817646.aspx http://redis.io/ http://www.infoq.com/news/2011/11/distributed-cache-nosql-data-sto 23
  • 24. Find out more • On https://techblog.betclicgroup.com/
  • 25. About Betclic • • • Betclic Everest Group, one of the world leaders in online gaming, has a unique portfolio comprising various complementary international brands: Betclic, Everest Gaming, bet-athome.com, Expekt… Active in 100 countries with more than 12 million customers worldwide, the Group is committed to promoting secure and responsible gaming and is a member of several international professional associations including the EGBA (European Gaming and Betting Association) and the ESSA (European Sports Security Association). Through our brands, Betclic Everest Group places expertise, technological know-how and security at the heart of our strategy to deliver an on-line gaming offer attuned to the passion of our players.