6. 6
Why do we need Caching?
1. To reduce the number or retrieval queries
made to a database
2. To reduce the number of requests made to
external services
3. To reduce the time spent computing data
4. To reduce filesystem access
5. Reduce web server load
6. Send less data to visitor
... and many more...
10. 10
Caching Techniques – Full Page Caching
Full pages that don't change
- blogs
- about us
- contact us etc...
11. 11
Caching Techniques – Granular page caching
Most common technique
Usually small blocks of page are cahced
- most read articles block
- footer block
- header block
12. 12
Caching Techniques – SQL query caching
1. Database Level (MySQL)
- Limited in size
- Reset on every insert / update / delete
- Server and connection overhead
- Good for tables where insert / update / deletes are very less
- Good for getting accurate results with some speed improvement.
2. Application Level
- Store SQL results in local storage (ram / memcache ..)
- Reduce database connections
- Problems with updated data
13. 13
Caching Techniques – complex processing
Caching results after complex calculations, cpu intesive tasks
- config file parsing
- xml parsing
- algorithm outputs
- objects / arrays
14. 14
Caching Techniques – Content pre-generation
Content pre-generation is cache the ouput / data for users in advance
before user perform any action on the website.
- create a blog post cache as soon as post is creating in the admin without
waiting for users to access it from frontend.
- caching complex calculation results using some cron scripts from
background.
15. 15
Caching Techniques – Webservice Response
Caching
Caching the response of webservices into local data store to avoide
delays.
16. 16
Caching Techniques – Browser Caching
Response headers:
- Expires (Gives the date/time after which the response is considered stale)
Expires: Thu, 01 Dec 2013 16:00:00 GMT
- Last-Modified (The last modified date for the requested object)
Last-Modified: Tue, 15 Jan 2013 12:45:26 GMT
- ETag (An identifier for a specific version of a resource)
ETag: "737060cd8c284d8af7ad3082f209582d"
- Cache-Control (Tells all caching mechanisms from server to client
whether they may cache this object. It is measured in seconds)
Cache-Control: max-age=3600
18. 18
Caching Techniques – #xx Your Call
Only limited by your imagination !
When you have data, think :
Creating Time :
Modification Frequency :
Retrival Frequency :
Based on above factors decide on caching technique
19. 19
How to find cacheable data
- Look for MySQL slow query log
- Page loading time
- Frequency of data change
- Frequency of data use
21. 21
Caching Storage – MySQL query cache
Use it :
Don't rely on it.
Good if you have:
Lots of reads.
Few different queries.
Bad if you have:
Lots of inserts / updates / deletes.
Lots of different queries
22. 22
Caching Storage – Disk
Good for :
- Data with few updates
- Computed Results
Bad for :
- High reads may cause locking issues.