Cache policy Optimization

Source: Internet
Author: User

zhanhailiang 日期:2014-12-12
Requirements Background

Typically, the cache logic is to set an expiration time, and if the cache fails, the request backend reads the data and updates the cache. However, this scenario can be problematic in high-QPS scenarios where all requests are fully propagated to the backend during the time period when the cache fails, the request backend reads, and the cache is updated, and the scenario has a large number of requests to the backend. So our goal is to reduce this part of the request.

Implementation Scenarios
  • Probability-based implementation mechanism

    • Cache storage twice times the Expiration Time 2 * expire,
    • If the request is in [Start,start + expire], the read-only cache is available;
    • If the request is in (start + Expire,start + 2 * expire), then only a certain proportion (factor=10, representing a scale of 10%) of the request back-end read the data and update the cache, other requests continue to use the current cache;
    • If the cache is invalidated, the backend is directly requested to read the data;
  • Lock-based implementation mechanism

    • Cache storage twice times the Expiration Time 2 * expire,
    • If the request is in [Start,start + expire], the read-only cache is available;
    • If the request is in (start + Expire,start + 2 * expire), then the current request lock is determined;
      • If there is a request to the backend to read the data, the request for the temporary use of the cache;
      • If none, then set the request lock, and request the backend to read the data and update the cache, after successful removal of the request lock;
    • If the cache is invalidated, the backend is directly requested to read the data;

The code is as follows: https://github.com/billfeller/blog.csdn.net-billfeller/blob/master/cache.php

Programme evaluation
    • From the end result, the lock-based implementation mechanism minimizes back-end requests. Taking factor=10 as an example, the probability-based implementation mechanism can reduce the number of requests by 90%, but the lock-based implementation mechanism guarantees that only one back-end request can be made in any scenario.
    • From the implementation perspective, the lock-based implementation mechanism requires additional memcached operations, with the worst case requiring 3 additional memcached operations;

Cache policy Optimization

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.