Cache
👝

Cache

Look-Aside Cache vs. Inline Cache

Look-Aside Cache

notion image
How it reads
  • Application requests data from cache
  • Cache delivers data, if available
  • If data not available, application gets data from backing store and writes it to the cache for future requests (read aside)
How it writes
• Application writes new data or updates to existing data in both the cache and the backing store -or- all writes are done to the backing store and the cache copy is invalidated
Examples Amazon ElastiCache (Memcached and Redis) or libraries like Ehcache and Google Guava for in-memory caches
 

Inline Cache(Cache through)

notion image
How it reads
  • Application requests data from cache
  • Cache delivers data if available
  • Key Difference: If data not available, cache retrieves data from backing store (read thru), caches it, then returns the value to the requesting application
How it writes
  • Application writes new data or updates existing data in cache
  • Cache will synchronously (write through) or asynchronously (write behind) write data to the backing store
Examples
Application-specific implementations like Amazon DynamoDB Accelerator (DAX) and standards-based implementations like HTTP caching (either with a local caching client or an external cache server like Nginx or Varnish).

levels of cache

if we implemented a service API operation that retrieved product information by product ID, the code might look like the following example. This code looks up product info in a local cache, followed by a remote cache, followed by a database:
Java
public GetProductInfoResponse getProductInfo(GetProductInfoRequest request) {

  // Which product are we looking up?
  // Who called the API? What product category is this in?

  // Did we find the item in the local cache?
  ProductInfo info = localCache.get(request.getProductId());
  
  if (info == null) {
    // Was the item in the remote cache?
    // How long did it take to read from the remote cache?
    // How long did it take to deserialize the object from the cache?
    info = remoteCache.get(request.getProductId());
	
    // How full is the local cache?
    localCache.put(info);
  }
  
  // finally check the database if we didn't have it in either cache
  if (info == null) {
    // How long did the database query take?
    // Did the query succeed? 
    // If it failed, is it because it timed out? Or was it an invalid query? Did we lose our database connection?
    // If it timed out, was our connection pool full? Did we fail to connect to the database? Or was it just slow to respond?
    info = db.query(request.getProductId());
	
    // How long did populating the caches take? 
    // Were they full and did they evict other items? 
    localCache.put(info);
    remoteCache.put(info);
  }
  
  // How big was this product info object? 
  return info;
}

Reference