缓存:全链路缓存

为什么需要缓存?何时应该使用缓存?在一个完整的服务中哪些地方可以用到缓存?

Why Cache

A cache is a high-speed data storage layer that storage a subset of data. Caching allows you to efficently reuse previously retrieved or computed data.

缓存是一种高速数据存储层,用于存储数据子集。缓存可让您高效地重复使用之前检索或计算的数据。

目的:尽可能缩短网络请求的链路;降低响应时延;减轻服务压力;

副作用:需要额外处理缓存一致性问题;

使用原则:多读少写场景;多写场景不适合缓存;

Benefit:

  • Reduced Latency
  • Lowered Server Load
  • Improved UX (User Experience)

Disadvantage:

  • Data Inconsistent.
  • High server and storage costs. The cache components require more memory, disk, compute resources.

When to use Cache

  1. An application or a functionality needs to frequently read the same data, besides the data is rarely updated.
  2. Application scenarios that require low latency and fast response.

Where Can Caching Be Used

  1. App/Browser: HTTP response can be cached by the browser. App can cache some static resources
  2. CDN: caches static web resources. The clients can retrieve data from a CDN node nearby.
  3. Load Balancer: can cache some static resources.
  4. Services:
  5. Distributed Cache
  6. Full-Text Search:
  7. Database
  8. Message Queue:

How to use Cache

Browser Cache

浏览器的缓存可分为:

  • 强制缓存
    • 在浏览器发送请求之前,会先查询缓存;
    • 如果缓存标识有效,则不发送请求,直接使用缓存;
    • 如果缓存标识有效,缓存结果不存在,发送请求;
    • 如果缓存标识无效,发送请求;
    • 缓存标识使用:
      Cache-control
      • Cache-control: no-store
        :禁用缓存;
      • Cache-Control: no-cache
        :表示缓存需要重新验证
      • Cache-Control: max-age=31536000
        :缓存在指定时间内有效(相对时间)
      • Cache-Control: public
        :标识缓存为共享缓存;
      • Cache-Control: private
        :标识缓存为私有缓存;如果缓存用户级别的信息,需要使用此标识,否则可能信息泄露;
  • 协商缓存
    • 当强制缓存失效,或缓存需要重新验证,则执行协商缓存流程,来验证缓存是否可用; 协商缓存也有HTTP/1.0和1.1两个版本:
    • 浏览器发送请求,携带需要校验的缓存资源Id(Etag)
    • 服务端进行验证,是否有效;  
      • 有效则返回304,不携带资源,表示缓存可用
      • 无效则返回200,携带新的Etag和响应;
    • 不依赖时间,更灵活可靠;如果资源未修改,但是超时了,只需要修改Etag即可,仍然可以使用缓存,因为它不是基于时间做校验的;

CDN

Benefits:

  • Reduced Latency
  • High Availability
  • Improved Security
  • DDoS Protection

Use CDN when:

  • Delivering static assets like images, CSS files, JavaScript files, video content.
  • Need high availability and performance for user across different geographical locations.
  • Reducing the load on the original server is a priority. Use Original Server when:
  • Serving dynamic content that changes frequently.
  • Personalized for individual users.
  • Handling tasks that require real-time processing or access to up-to-date data.
  • Requires complex server-side logic that cannot be replicated or cached by a CDN.

何时使用CND服务:

  • 传输图片、CSS、JavaScript文件、视频内容等静态文件
  • 需要为不同地理位置的用户提供高可用高性能服务
  • 优先考虑减轻原始服务器的负载

使用原始服务器:

  • 提供动态内容
  • 针对不同用户进行个性化设置
  • 处理实时任务或获取最新数据
  • 需要复杂的服务端计算逻辑

Server Cache

#TODO