Chapter 1: What is cache



Chapter 1: What is cache

What is cache



Its a well known fact that reading from RAM is much more efficient than reading from disk.
Reading from several other sources like database, file-system or from a networking system is also much slower than RAM.
In a distributed computing environment, the central point of contact for all the servers is usually a database.
To handle the load on a website, it is required to add more servers so that more requests per minute can be handled by the system.

It is easy to keep on adding the number of servers as the traffic on a site increases but the increasing number of servers puts a heavy load on the database. The load can cause the site to perform slowly even if static resources like images, video etc are read from the disk each time a request comes. To remedy this, the server can maintain a hash-map like structure in the RAM which will put the request as a key and the requested object as a value in the hash-map when the object is first requested. After that, all requests for the same object can just refer this hash-map and get served from the RAM instead of making a call to the file-system or the database. This strategy thus speeds up the time a server takes to respond to requests.

Maintaining such a hash-map in the RAM is called caching.

Designing a cache



Caching objects helps speed up the servers but the following factors need to be kept in mind when designing a cache:
  1. Freshness of cache: Cached objects represent some underlying resource which may be vulnerable to changes from other clients.
    For example, a user's image on a networking site may be cached by the servers such that visitors of that profile need not fetch that image from the file-system again and again. However, if the user changes his profile picture, than the cached entry becomes stale and needs to be refreshed by reloading from the file-system.
    While designing a cache, its thus important to consider if the cached objects could become stale and a way to refresh such objects when new data arrives.


  2. Size of cache: Cache size is typically much smaller than the underlying storage. Thus, its important to discard cached objects which are not expected to be used frequently. The cache size can be kept small by various strategies:
    a) Discarding objects which have not been accessed in a long time. This kind of cache is called Least Recently Used (LRU) cache.
    b) Having dynamic metrics of an object's access like how frequently an object is accessed. This is similar to first option with the difference that LRU approach only considers the time of last access while dynamic approach takes into usage of an object over time. So, a very frequently accessed object may fall out of cache in the LRU approach if its not requested for some time but in the dynamic approach, that resource will remain in cache until its frequency of access drops below a threshold.


  3. Distributed cache: A single computer's cache may prove to be insufficient for a heavy-traffic website. In such a case, a distributed cache can be designed which will combine the cache of several machines to provide a larger cache. This is explained in more detail in Chapter 2 - Caching software


  4. What objects to cache: Since the size of cache is limited, it makes sense to not keep extremely heavy objects in cache. For example, a 1 GB cache is meant to hold images, then it does not make sense to hold a 100 MB video in the same cache. Similarly, if an object's underlying resource is liable to change very frequently, then also it should not be cached.


  5. Latency: Although cache is designed to reduce latency, it makes sense to grade objects based on their latency and include normal-latency-of-access as one of the factors in deciding what objects to cache. Thus, if an object is to be read from a local disk while other one is to be read from a secondary storage like DVD drive or obtained through a network, then the higher latency objects among these should be given some preference in the cache.

Read full article from Chapter 1: What is cache


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts