1. OpenResty

OpenResty is a high-performance web platform based on Nginx and Lua, which integrates a large number of sophisticated Lua libraries, third-party modules and most of its dependencies. It is used to easily build dynamic web applications, web services and dynamic gateways that can handle ultra-high concurrency and scalability.

The access layer caching technology is to use OpenResty technology for secondary development in Lua language

image.png

2. Nginx + Redis

The left side of the figure below is a commonly used architecture. HTTP requests are forwarded to tomcat through nginx load balancing, and tomcat reads data from redis. The entire link process is serial. When tomcat hangs or the number of tomcat threads is exhausted, it cannot be normal. Return data. `lua-resty-rLL1Kf2Nr3DBLMpwsJ7BcqPorxtAvqgSG using OpenResty

image.png

3. Compression reduces bandwidth

If the data is larger than 1K, nginx compresses it and saves it to redis:

  • Improve the read speed of redis
  • Reduce bandwidth usage

Compression will consume CPU time, and the tps will be higher if data less than 1K is not compressed.

OpenResty does not provide the implementation of the redis connection pool. You need to use lua to implement the redis connection pool yourself. There are [examples] implemented on the Internet (http://wiki.jikexueyuan.com/project/openresty/redis/out_package.html)

The value of Redis is saved in json format {length:xxx,content:yyy}, content is the compressed page content, length is the size of content before compression, and the length field is to judge according to the size of length when reading redis Whether to decompress content data.

Use the lua-zlib library for compression.

image.png

4. Regular update

Execute regularly according to steps 1 and 2 in the figure below. The nginx lua timer periodically requests the url of the tomcat page, and the returned page html is stored in redis.

The validity period of the cache can be set to be longer, such as 1 hour, to ensure that the tomcat hangs within 1 hour, and the cached data can still be used to return. The regular update time of the cache can be set to a shorter time, such as 1 minute, to ensure that the cache is updated quickly.

image.png

5. Request forwarding

The browser opens the page:

  • nginx first gets the page html from redis
  • When there is no data in redis, get the page from tomcat and update redis at the same time
  • Return the page HTML to the browser

image.png

6. Single process regular update

All worker processes of Nginx can process front-end requests and forward them to redis. Only nginx worker 0 runs scheduled tasks to update redis regularly. In the lua script, the worker process ID is obtained through ngx.worker.id().

image.png

7. Configurable

By configuring the URL that needs to be cached in the management background, you can configure the cache URL, cache validity period, and regular update time, such as modify?url=index&&expire=3600000&&intervaltime=300000&sign=xxxx, the value of sign is the management background secretkey pair modify?url=index&&expire =3600000&&intervaltime=300000 is obtained from the signature operation. The nginx side uses the same secretkey to perform the signature operation on modify?url=index&&expire=3600000&&intervaltime=300000.

image.png

Likes(0)

Comment list count 0 Comments

No Comments

WeChat Self-Service

WeChat Consult

TaoBao

support@elephdev.com

发表
评论
Go
Top