Where cache can located?
Trade-off of having a Cache
Advantages:
Disadvantage(s):
Implementation:
Redis
memcached
What we usually cache?
Cache update strategy - Write around
Write fast / Read slow
Process: -The application is responsible for reading and writing from storage. The cache does not interact with storage directly.
Write - Data is written only to the backing store without writing to the cache. So, I/O completion is confirmed as soon as the data is written to the backing store.
Read, check cache first, then load from DB store in the cache is there is a cache miss
Adv:
Advantage: Good for not flooding the cache with data that may not subsequently be re-read.
Disadvantage(s):
What is it good for?
Good for applications that don’t frequently re-read recently written data.
This will result in lower write latency VS higher read latency which is a acceptable trade-off for these scenarios.
Cache update strategy
Cache-aside Write-through Write-behind (write-back) Refresh-ahead
Cache update strategy - Write-through
Write slow/ read fast
Process: The application uses the cache as the main data store, reading and writing data to it, while the cache is responsible for reading and writing to the database.
Using the write-through policy, data is written to the cache and the backing store location at the same time. The significance here is not the order in which it happens or whether it happens in parallel. The significance is that I/O completion is only confirmed once the data has been written to both places.
Ads:
Disadvantage(s):
What is it good for?
Cache update strategy - Write-behind (write-back)
Write/read all fast, data loss risk
Process:
Data is written to the cache and Then I/O completion is confirmed. The data is then typically also written to the backing store in the background but the completion confirmation is not blocked on that.
Advantage: Low latency and high throughput for write-intensive applications.
Disadvantage: Potencial data loss when cache is down before write to DB completed.
What is it good for?
Cache update strategy - Refresh-ahead
You can configure the cache to automatically refresh any recently accessed cache entry prior to its expiration. Refresh-ahead can result in reduced latency vs read-through if the cache can accurately predict which items are likely to be needed in the future. Disadvantage(s): refresh-ahead Not accurately predicting which items are likely to be needed in the future can result in reduced performance than without refresh-ahead.
What is CDN
A content delivery network (CDN) is a globally distributed network of proxy servers, serving content from locations closer to the user. Generally, static files such as HTML/CSS/JS, photos, and videos are served from CDN, although some CDNs such as Amazon’s CloudFront support dynamic content. The site’s DNS resolution will tell clients which server to contact.
Push CDNs
Push CDNs receive new content whenever changes occur on your server. You take full responsibility for providing content, uploading directly to the CDN and rewriting URLs to point to the CDN. You can configure when content expires and when it is updated. Content is uploaded only when it is new or changed, minimizing traffic, but maximizing storage. Sites with a small amount of traffic or sites with content that isn’t often updated work well with push CDNs. Content is placed on the CDNs once, instead of being re-pulled at regular intervals.
Pull CDNs
Pull CDNs grab new content from your server when the first user requests the content. You leave the content on your server and rewrite URLs to point to the CDN. This results in a slower request until the content is cached on the CDN. A time-to-live (TTL) determines how long content is cached. Pull CDNs minimize storage space on the CDN, but can create redundant traffic if files expire and are pulled before they have actually changed. Sites with heavy traffic work well with pull CDNs, as traffic is spread out more evenly with only recently-requested content remaining on the CDN.
CDN trade-offs
adv: improve performance in two ways:
Disadvantage(s):
1.CDN costs could be significant depending on traffic, although this should be weighed with additional costs you would incur not using a CDN.
2/Content might be stale if it is updated before the TTL expires it.
3.CDNs require changing URLs for static content to point to the CDN.