Get the App
SLTechnology News&Howtos  ›  Servers  › 

OpenResty- Enterprise Theory and practice

Shulou Source: shulou.com Published: 2022-06-02 03:28:49 10月04日 Update

OpenResty background

Nginx has many features and benefits, but it has always been a difficult problem to develop on Nginx. Nginx modules need to be developed in C, and must comply with a series of complex rules. The most important module developed in C must be familiar with the source code of Nginx, which makes developers afraid of it.

OpenResty (also known as ngx_openresty, hereinafter referred to as OR) is a core based on Nginx. By extending third-party functional modules (such as MySQL, PostgreSQL,Memcached,Redis, etc.), we can use lua (scripting language) to develop the business of Nginx core and three-party extension modules.

OpenResty advantage

Core

Based on the Nginx core, it runs completely inside the Nginx service.

Stable operation, lightweight, low memory consumption.

Event-driven model and non-blocking Iamp O

Using the asynchronous non-blocking event driver of Nginx itself to carry out the communication of iUniverse O

Using the efficient multiplexing model of Nginx itself (epoll/linux, kqueue/freebsd, eventport/solaris)

Note: not only the network communication with HTTP clients is non-blocking, but the network communication with many remote backends such as MySQL, PostgreSQL, Memcached and Redis is also non-blocking.

Lua

Compared with nginx module (C) development, lua itself has the characteristics of simple syntax, weak type, and its own GC (no manual memory management), which greatly reduces the difficulty of development.

Performance

Quickly construct a high-performance Web application system capable of concurrent connection of a single machine above 10K or even 1000K.

OpenResty principle

Nginx uses the master-worker model, one master process manages multiple worker processes, the basic event handling is placed in woker, and master is responsible for some global initialization and worker management.

In OpenResty, each woker uses a LuaVM, and when a request is assigned to a woker, a coroutine (co-program) is created in this LuaVM, and data is isolated between the co-programs.

Tags: Development core module blocking event model management communication between memory foundation right and wrong characteristics network network communication process run drive complex daunting Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info Shulou Technology Xiaomi Docker