The pursuit of speed for large Web applications does not stop at using only the browser cache, because the browser cache is always to improve the speed of second access, for first access acceleration, we need to optimize from the network level, the most common means is CDN acceleration. By caching static resources (such as javascript, CSS, images, etc.) to the same network operator’s CDN nodes close to the user, that is, CDN caching

Unlike browser caches, CDN caches are server-side caches

CDN acceleration principle

  1. The content source (which can be understood as the server location) is in the countryA provinceInside, but users fromAll over the country
  2. The CDN service provider is inEvery province in ChinaThe CDN is deployed
  3. Only one user in a region needs to load the resource first
  4. CDN can be used by placing static resources (such as javascript, CSS, images, etc.)Cached close to the userOn the CDN node of the same network carrier, that isCDN cache
  5. When all other users in the area access the resource, theVisit the nearest to yourselfCDN nodes on the same network line
  6. When the request reaches the CDN node, the node determines whether its content cache is valid
  7. If it works, it responds to the cached content to the user immediately, thus speeding up the response
  8. If the CDN node’s cache is invalid, it will go to us according to the service configurationContent source serverGet the latest resource response to the userAnd will the contentCache it againIn order to respond to subsequent users

Flow chart of CDN access resources

How to configure

On the whole, phi and phiBrowser cacheconfigurationsynchronizedCan beIt is worth noting that:

  • Browser cacheIs in accordance with theTCP header RulesDo your own cache processing
  • CDN cacheIs made up ofThe CDN service providerTo control the cache,They don’t conflict with each other

Problems arise

Problem scenario: If our browser cache is set to cache-control: Max-age =600, that is, the cache lasts 10 minutes. However, the CDN cache configuration sets the file cache time to 1 hour. At this time, if the server resource is updated, then if the resource is modified and uploaded to the server 12 minutes after being accessed, the user reaccesses the resource, the response code will be 304, and the latest resource cannot be obtained. Access again after an hour to update the latest resources

The whole process of a user accessing resources

  1. When a user accesses our business server, the first thing that happens is HTTP caching
  2. If the HTTP cache passes the verification, the response is sent directly to the user
  3. If the CDN does not pass the verification, the CDN cache processing continues. After the CDN cache processing is complete, the CDN cache is returned to the client, which stores the HTTP cache rules and responds to the user.
  4. The CDN node will request the latest resources from the server every once in a while, which is determined by the CDN service provider

To solve the problem

  1. We can discuss CDN caching with the CDN service providerShortening of effective timeTo match the browser cache
  2. Or use the CDN service provider when the browser cache is invalidForced update, the CDN cache is updated