Common caching techniques

Introduction

​ In projects, everyone often encounters situations where high concurrency is handled, and caching is one of the effective means to deal with high concurrency. This article briefly introduces commonly used caching methods. Of course, there is a prerequisite for using caching: the data is not changed in real time.

method

1. CDN

CDN is generally used to cache static resources, but with a slight modification, it can be used to cache the return of dynamic interfaces. When the requested resource does not exist in the CDN, the request will go to the back-to-origin machine. On the Nginx of the back-to-origin machine, the request will be forwarded to different services according to routing rules, and the returned result will be stored in the CDN. As shown below:

Insert picture description here

Nginx implementation

upstream fs.com_backend {
    server   127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=10s;
    check interval=3000 rise=3 fall=3 timeout=1000 type=tcp default_down=false;
}

upstream hd.com_upstream {
    server   127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=10s;
    check interval=3000 rise=3 fall=3 timeout=1000 type=tcp default_down=false;
}

server {
        listen       8080;
        server_name  aaa.d2s.com;
        charset utf-8;

        access_log /home/work/logs/nginx/d2s.com.log web;
        error_log /home/work/logs/nginx/d2s.com.log.err;

        error_page   403 404 500 502 503 504  http://hd.com/webfile/zt/hd/2014042802/tw.html;

        expires -1;

        location ~ /.git {
            deny all;
            access_log off;
            log_not_found off;
        }

        location ~ /([^/]*)/openbuy/info {
            expires 300s;
            proxy_set_header Host "****";
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://fs.com_backend;
        }

        location ~ /([^/]*)/eventapi/ {
            rewrite /(.*)/eventapi/(.*) /$1/$2 break;
            expires 300s;
            proxy_set_header Host "****";
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://hd.com_upstream;
        }
}

Precautions

  1. Pay attention to flow

    Under normal circumstances, there will be fewer back-to-origin requests, but in some cases, it will cause a surge in requests. For example, when the market pushes ads on Google, a lot of data will be added after the request. If the CDN does not handle it, these requests will penetrate The CDN hit the return-to-origin machine, causing a surge in requests. If the service performance is limited, not only the service will be affected, but other functions on the Nginx may also be affected.

  2. Pay attention to stability

    Because the request will be forwarded to the service, if the service is unstable, the returned data is wrong or the service is 500, Nginx returns without making any judgment, which will cause all the data requested by the user to be wrong for a period of time. Once this happens, when it is reported to the development, because the CDN cache is invalid and the correct data is re-acquired, the symptoms have disappeared and it is difficult for development to locate the problem. It is recommended to add related correctness checks on Nginx.

Two, Nginx cache

​ Nginx can also cache the returned results, and because Nginx itself can support high concurrency and is easy to configure, using Nginx caching is an extremely convenient and effective method.

Nginx partial implementation

server {
  listen 80;
  #listen 445;
  server_name  abc.com;

  access_log /home/work/logs/nginx/abc.com.log albproxy;
  error_log /home/work/logs/nginx/abc.com.err;

  #include vhosts/black.ini;

  error_page   403 404 500 502 503 504  http://promo.com/webfile/globalweb/$local/404.html;


  if ($app_uri ~* "^(hello/getstock.*)"){
    set $cache_rewrite "nginxcache3s";
  }

  location ~* ^/[^/]*/nginxcache3s/.* {
    proxy_pass_header X-Accel-Expires;
    rewrite ^/([^/]*)/nginxcache3s/(.*)$ /$1/$2 break;

    proxy_cache cache_one;
    proxy_cache_valid 200 3s;
    proxy_cache_key $host$uri;
    proxy_ignore_headers Expires Cache-Control;

    proxy_set_header Host  $host;
    set_real_ip_from   10.0.0.0/8;
    real_ip_header     X-Forwarded-For;
    proxy_set_header   X-Real-IP        $remote_addr;
    real_ip_recursive on;
    proxy_set_header X-Forwarded-For  $http_x_forwarded_for;
    #后端标识ssl请求
    proxy_set_header Missl $sslparam;
    proxy_set_header LOGID $logid;
    proxy_pass http://web_backend;
  }
}

Precautions

  1. Use with caution

    It is really convenient to use Nginx caching, and it can also quickly improve interface performance. It can be said to be an essential medicine for home travel. However, these operations generally require the help of operation and maintenance students. If you use this method frequently, one is that it takes up a lot of time for operation and maintenance to do this kind of repetitive work, the other is that various problems may occur during the operation, and the third is that it will cause Nginx files. Very complicated, and later migration and maintenance costs will be higher.

Three, Redis

​ Redis is one of the necessary solutions to deal with high concurrency situations. The general process is as follows:

Insert picture description here

Precautions

  1. Cache penetration

    If Redis has no data, there is no data when the data is obtained. At this time, if the result is not written to Redis, every request will penetrate Redis. Suggest

    • If the acquired data is empty, save the data
    • Increase basic verification
  2. Cache breakdown

    If there is a hot key with a hot key, the request qps is extremely high. When the key fails, a large number of requests will go to the logic layer, resulting in unstable service. It is recommended

    • Hotspot data is set to never expire. The data can be updated asynchronously using schemes such as scripts.
    • Add mutex lock. If the key does not exist, only one request is allowed to enter the logic layer.
  3. Cache avalanche

    If a large number of keys expire at the same time, all requests will go to the logic layer, resulting in unstable services. It is recommended

    • The expiration time of cached data is set randomly to prevent a large amount of data from expiring at the same time
    • If the cache database is deployed in a distributed manner, evenly distribute the hot data in different cache databases
    • Hotspot data is set to never expire

Four, MemoryCache

​ MemoryCache is a memory cache. Generally, services and MemoryCache are placed on the same machine, and MemoryCache and Redis are used together. Although Redis has extremely high performance, it still needs to obtain data through requests. MemoryCache obtains data directly from local memory, which is faster. As shown below:

Insert picture description here
​ When there is no data in Redis, whether you need to get data and write to Redis again, you need to design according to specific business conditions. For example, if Redis is written asynchronously, there is no need to execute the logic of calculating data, and there is no need to write to Redis again.

Precautions

  1. Data loss risk

    Memeory Cache is a memory cache, there is no persistence mechanism, the data will be gone after restart.

  2. UNCLE

    The use of Memeory Cache is not standardized, which can easily cause a large amount of memory consumption and cause OOM problems

  3. Storage data change risk

    In Golang, if the stored data is a pointer structure, after the storage operation is completed, the data pointed to by the pointer changes, the data in the Memeory Cache will also be changed

to sum up

​ This article introduces four commonly used programs, CDN, Nginx cache, Redis, MemoryCache, flexible combination of these four levels of programs, can effectively deal with high concurrency problems. If you have other plans, please discuss them together.

​ The pictures in this chapter can be viewed at https://www.processon.com/view/link/5ec7e780f346fb69070fe20a

At last

If you like my article, you can follow my public account (Programmer Mala Tang)


Detailed explanation of CDN request process

Thoughts on the career development of programmers

The history of blog service being crushed

Common caching techniques

How to efficiently connect with third-party payment

Gin framework concise version

Thinking about code review

Guess you like

Origin blog.csdn.net/shida219/article/details/106761714