Traditional cache Algorithms

Source: Internet
Author: User
Document directory
  • Least frequentlyused (LFU)
  • Least recentlyuser (LRU)
  • Least recentlyused 2 (lru2)
  • Two queues (2q)
  • Adaptivereplacement cache (ARC)
  • Most recently used (MRU)
  • First in first out (FIFO)
  • Clock
  • Simple time-based
  • Extendedtime-based expiration
  • Sliding time-basedexpiration
  • HTTP freshness cache Algorithm
Cache Purpose

A. Ease Network bottleneck

B. Reduce the number of original service (such as database) Requests

C. Reduce request latency (accelerate response time)

Basic cache processing steps

1. Accept network requests

2 Resolution request

3. check whether a local copy is available,

If no, obtain a copy from the database and save it to the local device.

4 check the copy effectiveness

If it fails, obtain a copy from the database and save it to the local device.

5. Send a Request Response

Traditional cache algorithm least frequentlyused (LFU)

The LFU algorithm calculates the frequency of use of each cached object and deletes the least frequently used cached object.

Least recentlyuser (LRU)

The LRU algorithm finds the least recently used cache object and then deletes it. new objects will be placed on the top of the cache. When the cache reaches the capacity limit, the objects at the bottom will be kicked out and the recently accessed cache objects will be placed on the top of the cache pool.

Least recentlyused 2 (lru2)

The lru2 algorithm puts the objects that have been accessed twice into the cache pool. When the cache pool is full, it kicks out the cache objects that have been used for at least two times. Because the object needs to be tracked twice, the access load will increase with the increase of the cache pool. Therefore, this algorithm is not suitable for large-capacity cache pools.

Two queues (2q)

The 2q algorithm puts the accessed data in the LRU cache. if the object is accessed again, it is transferred to the second and larger LRU cache. The transfer cache object is to keep the first cache pool as 1/3 of the second cache pool. When the cache access load is fixed, replacing LRU with lru2 is better than increasing the cache capacity. This mechanism is better than lru2.

Adaptivereplacement cache (ARC)

The ARC algorithm is composed of two LRU types. The first is L1, the entry contained is recently used only once, and the second LRU is L2, contains the entries that have been used twice recently. L1 places new objects, while L2 places common objects. It is one of the best performance cache algorithms that can be self-tuned and have low load. In addition, historical objects are stored. You can remember the objects to be removed and check whether the deleted objects can be left.

Most recently used (MRU)

The MRU algorithm is used to remove the most recently used objects. When a visit is made, some tasks cannot be predicted, finding the least recently used objects in the cache system is a very time-complex operation. This algorithm is the most common algorithm in the database memory cache. whenever a cache record is used, it is placed at the top of the stack. When the stack is full, replace the objects at the top of the stack with new objects.

First in first out (FIFO)

The FIFO algorithm is a low-load algorithm that does not require high management of cached objects. A queue is used to track all cache objects. The most common recently cache objects are placed behind them, and the cache objects earlier are placed before them. When the cache capacity is full, the cache objects listed above will be kicked out, and new cache objects will be added.

Second Chance

The Second Chance algorithm is modified through FIFO, which improves the cost of FIFO. the system checks whether the object to be kicked out has a previously used flag (1 in BIT). If not, the system clears the flag, then, the cache object is added to the queue as a new cache object. as you can imagine, this is like a ring queue. when we hit this object again in the head of the team, we immediately kicked the object out because we didn't have this flag. faster than FIFO.

Clock

The clock algorithm holds a circular list containing cached objects. The header Pointer Points to the oldest cached object in the list. When cache miss occurs and there is no new cache space, you can find the flag of the cache object pointed to by the pointer to determine what to do. If the flag is 0, replace the cached object with a new cache object. If the flag is 1, increment the head pointer and repeat the process until the new cache object can be placed.

Simple time-based

The simple time-based algorithm invalidates cache objects through absolute time periods.

Extendedtime-based expiration

The extendedtime-based expiration algorithm uses relative time to release cache objects. For new cache objects, a specific time is saved, for example, every 5 minutes, at 12 o'clock every day.

Sliding time-basedexpiration

Slidingtime-based expiration is the starting point of the managed cache object, which is counted from the last access time of the cache. The cached object is released more than the specified time.

HTTP freshness cache Algorithm

The HTTP protocol has a cache-control field to specify the cache mechanism that requests and responses follow. if you need to determine whether the response is fresh or stale, you only need to compare its life cycle with age.

Expressed by formula:

Response_is_fresh = (server_freshness_limit ()> current_age)

Preservation Life Calculation Method

Note:

Max-ageIndicates that the client can receive responses with a lifetime not greater than the specified time (in seconds.

Instance:

Max-age = 8 indicates the maximum lifetime is 8 seconds. After 8 seconds, the browser must go to the server to re-read the data. This time is based on the time when the user reads the page,

ExpiresIt is the absolute time when the cache expires. If the cache expires at the specified time point, the client will not obtain the cache and directly go to the server to request the latest one.

Last-modifiedThe last modification time of the document. if it is a static file, the client sends the time in its cache, and the server compares it. If no modification is found, a header is directly returned. The status code is 304, the number of bytes is very small. (The advanced version also adds the etag to determine whether the file is changed ).

Actual algorithm pseudocode:

Freshness_lifetime freshness if (set max_age) {// Priority 1: max_age freshness_lifetime = max_age} else if (set expires) {// Priority 2: expires freshness_lifetime = expires-original data time} else if (set last_modified) {// Priority 3: last_modified freshness_lifetime = (INT) (0.1 * max (0, last_modified-original data time) set the inspiration mode} else {freshness_lifetime = default minimum life cycle setting the inspiration mode} If (set the inspiration Mode) {check the two boundary values freshness_lifetime = freshness_lifetime> default maximum lifecycle? Default minimum life cycle: freshness_lifetime; freshness_lifetime = freshness_lifetime <default minimum life cycle? Default minimum life cycle: freshness_lifetime ;}

Age Calculation Method

Time when the cached data was last updated to the present

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.