For the choice of high-performance web servers, this is a headache for many people. In fact, Apache, lighttpd, and Nginx all use their advantages. Under what circumstances, how do we choose a high-performance Web server that suits us, and how to build a framework environment that suits us? This is a very troublesome thing. Next, in the ADC
2012 (Alibaba Developer Conference
2012) conference, 51CTO reporter had the honor to interview Yitao Data Platform and Product Department technical expert – Qingwu (nickname), to explain some advantages and disadvantages of Nginx_lua for us, as well as the choice of high-performance servers.
First, let us understand the design guiding ideology of Nginx_lua:
1. Rapidly develop high-performance, high-concurrency network services based on Nginx.
2. Provide a “synchronous non-blocking” I/O access interface to simplify business logic development in the I/O multiplexing system:
■ The subject of “synchronization” is the timing relationship between the user code and the I/O request processing flow initiated by it, which means that the user code will be suspended until the I/O request processing is completed.
■ The main body of “non-blocking” is the service process, which means that the processing of I/O requests will not cause the service process to block and wait, but can continue to process other user logic requests.
The characteristic of Nginx is that it occupies less memory and has strong concurrency capability. In fact, the concurrency capability of nginx is indeed better than other web servers of the same type. At present, users of nginx websites in mainland China include: Sina, Netease,
Tencent, and another well-known micro-blog Plurk also use Nginx.
Selection of Nginx server and Lua version
1) The choice of Nginx high-performance open source WEB server
Qingwu started to contact the Nginx server in 2008. At that time, the high-performance open source WEB server also had lighttpd, so why did Qingwu, a technical expert of Yitao.com, choose Nginx? Which aspect of Nginx has its advantages? Qingwu said that in the comparison between lighttpd and Nginx, there is an obvious shortcoming that the module mechanism of lighttpd is not well designed, and the module mechanism of lighttpd is too much to deal with the request processing logic of the module itself and the processing of the underlying network events. Combined together, it is not as clear as Nginx’s module structure. Of course, Nginx’s module design also borrows from Apache’s module design to a large extent, so this block has an inherent advantage. At that time, he actually came into contact with lighttpd for the first time, and after Nginx came out, after comparing the differences in their module structures, he felt that Nginx seemed to have more advantages. According to actual measurements, for network I/O-intensive applications like ours, as long as the logic you implemented is not too flawed, the difference between using lighttpd or Nginx is not particularly large.
In the process of comparison and selection, first start from the architecture. If there is a problem, no matter how you implement it, it will be a problem. Therefore, my comparison is first on the architecture construction. The service model of single thread and single process per connection or per request , It will be brushed off directly, and it is certainly impossible to achieve a high service capability. The rest are all based on the RO multi-channel structural system, so we only test this system. In fact, we use an IPP request to stress test to see how the quality of its implementation is. Generally speaking, once this part is structured After the system is decided, the measured difference in performance is not particularly large, unless it is said that one feature is implemented and the other is not. The difference we measured usually fluctuates around 10%-20%.
2) Selection of Lua version
In the communication between Xiaobian and Qingwu, I learned that the current Lua version used by Yitao.com is 5.1.2. When Xiaobian asked whether the higher the version, the better the performance, Qingwu thought it was not right. For lua, every version change means that it will add new syntax elements or change some internal implementation methods. Strictly speaking, it does not mean that its performance is good. For example, for 5.2 and 5.1, regardless of the modification of the environment table or other mechanisms, strictly speaking, it is a new language. So at present, the biggest obstacle to migrating to 5.2 is actually the change in the concept of the underlying interface in 5.2. Because a lot of work has been done on some aspects in 5.1, and then its mechanism of global table plus environment table is used. However, in 5.2, the concept of the global table is completely canceled, and a series of interfaces for operating the environment table at the CU level are also cancelled. For us, it is definitely not possible to smoothly migrate to 5.2. If there is such a demand, we can do it, but There is no need for this yet. Another problem that prevents us from upgrading the version number is Lua
JIT, lua
The performance of JIT is much higher than that of standard lua, so we usually use JIT in the deep layer, but luaJIT currently does not support lua5.2 so tightly, it is still mainly 5.1, so I can’t make a longer one time follows lua
JIT footsteps to come.
In the application of Yitao.com, Qing Wu introduced that Nginx_lua is mainly used in two places, one is the traditional Yitao database quantum statistics shop, and the data interface part is completely done with Nginx_lua. The other is that part of the data interface of Yitao’s advertising department also uses Nginx_lua.
Nginx_lua performance test comparison
In fact, many people have been using the combination of Nginx_php. What are the advantages of the combination of Nginx_lua? Qingwu said that there must be inter-process communication between Nginx+php, so the basic performance overhead will be very large. Lua is embedded in the Nginx process, it does not need two sets of processes to work there independently. So this block has a decisive advantage in terms of structure. In addition, the communication between threads requires a lot of deserialization and serialization work, and the additional situation brought by the two sets of processes is more processes and more switching overhead, so Nginx_php on a single machine is much lower than Nginx_lua . But relatively speaking, we still have to go back to what we do, because the biggest disadvantage of Nginx_lua at present is that the surrounding modules are quite unsound, and we need a lot of time to accumulate these modules. PHP has been accumulated for more than ten years. If your performance requirements are not so high, and my concurrent number is dozens, then you are most suitable to use PHP. But if it is like the data interface of Yitao Data, the number of machines is just a little, because a lot of my costs are on the MySQL cluster, which is the main force of this area, so I hope to reduce the cost of the external data interface as much as possible, and the number of concurrency is very large. , php is definitely not working, then we have to choose Nginx_lua. However, the disadvantage of this module to the module does not seem to be that great, because its logic is relatively fixed, and we can bear the cost, so we can customize some modules for this logic.
From the above two performance test charts, we summarize the applicable scenarios of Nginx_lua:
Network applications whose blocking time of network I/O is much higher than CPU computing time and whose upstream resources are non-bottleneck (scalable), such as high-performance network middle layer, HTTP REST
Interface services, etc.;
It is expected to simplify the system architecture and make the service homogeneous to the Nginx Web site;
Advantages and disadvantages of Nginx_lua
Regarding the disadvantages of Nginx_lua, Qingwu also introduced a problem that the peripheral modules are not perfect and not sound when comparing it with Nginx_php just now. If the thing you use is more complicated, the productivity may not increase. At present, the most suitable personnel for Nginx_lua are the data interface layer and all the network middle layers. You need the most concurrency and high-performance network middle layer. Because its own logic is relatively simple, or it can be realized completely by using lua itself, this is the most effective ratio of use. So if you are going to make a complicated WEB access site, there are a lot of templates to be set, a lot of complex logic embedded in it, and then you want to access mail and other services, I think it is still php or other relatively mature ones. language. As far as our current application is concerned, the middle layer will use a lot of lua, but the front-end presentation layer will either be moved to the browser and implemented in the form of JS+templates, or it will be implemented in PHP.
Another disadvantage is that there are not many auxiliary tools for debugging, because advanced PHP programmers often use XDebug or other debugging tools, which can be debugged in a single step and online. Compared with php, there is still a lack of such a mechanism. At that time we will follow the example of XDebug
To implement the DPT V2 protocol, we implement compatibility with DPT
A mechanism such as V2 is internally connected to Nginx_lua, so that Nginx_lua can also be debugged in a single step. We will also share it with you at that time.
Finally, let’s summarize some advantages and disadvantages introduced by Qing Wu:
Advantages:
The form of synchronous non-blocking I/O is intuitive and easy to understand, and the concurrent service capability is strong
Low CPU and memory overhead
It has a high degree of integration with Nginx and can easily bond existing Nginx module functions
Disadvantages:
It is a new technology solution. Compared with widely used development methods such as PHP and Ruby, Lua
Language, surrounding ancillary facilities are not perfect enough, it takes time to accumulate
Nginx_lua needs and performance pursuit
In terms of requirements, Qingwu believes that this part of the data interface of Yitao.com can be fully satisfied. As for other requirements, we need to find out in detail and find the best solution. Because in the computer industry there is no such thing as a one-trick trick.
So as a technician, should the pursuit of performance be suitable or an endless pursuit? Qingwu said that it depends on whether we are doing business or personal affairs. If it is in a company, such as specific matters, and then a team collaboration, then it is inappropriate to blindly pursue the limit of performance. Behavior, because your pursuit requires paying corresponding costs and expenses, which are often intolerable in an enterprise environment. The most suitable architecture is often the architecture that is aimed at you to solve the problem, rather than pursuing the most efficient architecture. So when we do projects in the enterprise, it is obviously best to stop in moderation. You don’t need to put in more effort if you overwhelm the greatest needs of the user, because there are many other problems, and you don’t need to stay on the issue of performance. Performance is only one of the problems. On one problem There is no need to put too much energy into it. However, from a developer’s personal point of view, it is a good idea and behavior to pursue the performance limit, because the developer’s own pursuit of the performance limit reflects the pursuit of perfection. To the bottom of the special research, and special research is the most effective motivation to improve personal quality. So let’s look at this issue separately.
Pay more energy to do it, because there are many other issues, you don’t need to stay on the issue of performance, performance is only one of the issues, and there is no need to invest too much energy on one issue. However, from a developer’s personal point of view, it is a good idea and behavior to pursue the performance limit, because the developer’s own pursuit of the performance limit reflects the pursuit of perfection. To the bottom of the special research, and special research is the most effective motivation to improve personal quality. So let’s look at this issue separately.