1. Introduction to OpenResty
OpenResty
It 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 the dependencies. It is used to conveniently build dynamic Web applications, Web services, and dynamic gateways that can handle ultra-high concurrency and high scalability. You can use the Lua script to call the C and Lua modules supported by Ngnix to quickly build a high-performance web application system with 10K to 1000K single-machine concurrent connections. OpenResty's goal is to allow web services to run directly inside Nginx services, using Ngnix's non-blocking IO model to provide consistent high-performance responses to HTTP client requests and back-end DBs.
OpenResty is based on Nginx
development and can be simply considered as a combined version of Nginx
+ lua-nginx-module
.
Official website: https://openresty.org/cn/Official
document: https://github.com/openresty/lua-nginx-module#version
Openresty best practice: https://github.com/moonbingbing/openresty-best-practices , https://moonbingbing.gitbooks.io/openresty-best-practices/content/
wiki used by nginx: https://www.nginx.com/resources/wiki/
Lua module wiki in nginx3rd: https://www.nginx.com/resources/wiki/modules/lua/
2. Two important elements of high-performance server
For the "high-performance" server, our concern is not the performance of the language, but the cache, and the language level should support asynchronous non-blocking.
1. Cache speed
Memory> SSD> mechanical disk; local machine> network; in-process> inter-process. The goal of the cache system is to hope that the hit rate in the process is the highest, then the overall efficiency of the cache system is also the highest.
2. Asynchronous non-blocking refers to the event-driven approach (notify after the event is completed)
Hope to access the database, access the network, and access some slow IO devices, do not spend a lot of time on waiting. Instead, we use an event-driven approach to notify us when the system completes a task. In this way, the idle resources of the server CPU can be used to serve client connections.
3. The technology included in OpenResty
- Nginx: Not only load balancing + reverse proxy and other functions, Nginx c module development cost is high.
- LuaJIT: OpenResty uses LuaJIT, LuaJIT is the main performance Lua.
OpenResty has subverted the development model of high-performance servers. OpenResty
In essence, LuaJIT
the virtual machine is embedded in the Nginx worker, so the efficiency is particularly high, and in performance, it is OpenResty
close to or exceeds the Nginx c module:
Because Nginx uses a master-worker
model, that is, a master
main process manages multiple worker
processes, the basic event processing is placed worker
in the middle, master
only responsible for the initialization of the whole drama and worker
the management of the right. In OpenResty, each worker
uses a LuaVM, and when each request is assigned worker
, LuaVM
a coroutine
coroutine will be created in this . Data isolation between coroutines, each coroutine has independent global variables _G
.
Coroutines in Lua are similar to threads under multithreading. They have their own stacks, local variables, instruction pointers ..., but share information such as global variables with other coroutines. The main difference between threads and coroutines is that in the case of multiprocessors, conceptually, multithreading is to run multiple threads at the same time, while coroutines use code to complete the switching of coroutines. Only one coroutine program is running at any time. And this running coroutine will be suspended only if it is explicitly required to be suspended.
四、OpenResty
Comparison with other languages on the market
- node.js: The first one to put asynchronous non-blocking features into its own language, front-end students can quickly cut in. However, node.js uses callbacks to implement asynchronous non-blocking, and the code is cumbersome to write.
- Python: Asynchronous support was added after 3.4, such as asynchronous io and aiohttp; 3.5 introduced coroutines. The disadvantage is that the version span is large, because many people still use 2.7.
- Golang: It has been very hot in recent years. Disadvantages: Go keyword is required for code writing; online hot debugging is inconvenient (
SystemTap
limited support provided). - Baas: Backend as a Service, the company provides mobile application developers with integrated cloud backend boundary services.
reference:
https://www.zhihu.com/question/23048744