Network Basics: 3. Application Scenarios of Reactor

Reactor

1. Why does reactor work with non-blocking IO?

  1. In a multi-threaded environment, put a listenfd into multiple epolls for processing
  2. In the case of edge triggering, when the read event is triggered, read reads out the read buffer in an event loop
  3. Select has a bug. When a piece of data arrives, select will report a read event; however, the data may not pass the checksum detection so it is discarded, and select has already reported the read event. If you use blocked io read to read at this time, it will will block the thread

2. Does IO multiplexing have to be combined with non-blocking IO?

no

  1. Take MySQL as an example, select receives connections, and each connection is a thread
  2. libevent is to add a system call to check how much data is in the buffer, but the efficiency is relatively low

Redis

1. What is Redis?

Redis is an in-memory key-value store database that can be used as a cache, message queue, and data store. It supports a variety of data structures (such as strings, hash tables, lists, sets, and ordered sets), provides a wealth of command operations, and has efficient read and write performance and reliability. Redis also supports features such as master-slave replication, persistence, and clustering, making it a popular open source database solution.

2. Why does Redis use a single Reactor?

  1. single-threaded business logic
  2. When operating specific commands, the time complexity is relatively low

3. What optimizations has Redis made for Reactor?

  1. Redis adopts a multi-threading model, which separates network I/O and command execution into different threads, so that Redis can process multiple client requests at the same time.
  2. Redis uses an efficient event-driven mechanism such as epoll in network I/O, which can quickly respond to a large number of concurrent requests.
  3. Redis uses some technologies based on asynchronous I/O, such as AOF rewriting and RDB persistence operations, which can complete disk operations without affecting the performance of the main thread.
  4. Redis implements its own message communication mechanism, and improves the efficiency of message transmission by optimizing the structure of network data packets and compressing data.
  5. Redis adopts zero-copy technology, which reduces the overhead of data replication and improves network I/O performance.

Memcached

1. What is Memcached?

Memcached is a high-performance, distributed memory object caching system that is often used to accelerate dynamic web applications and reduce database load. Memcached can store data in memory, thus avoiding the overhead of accessing the database every time. It supports multiple programming languages ​​and operating systems, and is scalable and highly available.

2. Why does Memcached use multiple Reactors?

  1. Easy to operate kv data
  2. Can handle business with high concurrency

Memcached uses multiple Reactors mainly to improve the concurrent processing capability and throughput of the system. Memcached is a high-performance distributed memory object caching system that needs to handle a large number of client requests and respond to these requests quickly. The use of multiple Reactors can distribute requests to different threads for processing, increasing the throughput of the server; at the same time, multiple Reactors can make full use of the advantages of multi-core CPUs to improve the concurrent processing capabilities of the system. In addition, multiple Reactors can also ensure that Memcached handles requests from different clients equally, avoiding the situation that other client requests are blocked due to too many requests from one client.

Nginx

1. What are the characteristics of Nginx?

  1. reverse proxy
  2. multi-processing business

Nginx is a high performance, open source web server and reverse proxy server. It can be used as an HTTP server, SMTP server and TCP/UDP proxy server, supports many different application platforms and languages, and has the advantages of high concurrency and low memory consumption, and is widely used in Web servers.

2. Why does Nginx use multiple processes?

  1. Complex types of business
  2. Locking can be avoided by running in process isolation
  3. Relatively independent between requests

The main reason why Nginx uses multi-process is to improve the performance and concurrency of the server. Nginx adopts the Master/Worker model, the Master process is responsible for managing the Worker process, and the Worker process handles client requests.

When a client connects to Nginx, the Master process assigns the connection to a Worker process for processing. If the Worker process is processing other requests, it can process multiple requests at the same time, thereby improving the concurrency capability. At the same time, each worker process is independent and will not affect each other. Therefore, when a problem occurs in the worker process, the master process can quickly restart a new worker process, ensuring the stability and reliability of the server.

In addition, Nginx also uses an event-driven model, which is based on event callbacks to process requests, avoiding performance loss caused by thread context switching, which is also one of the important reasons for Nginx's high performance.

3. How does Nginx handle Reactor?

  1. Solve the shocking group by locking in the user state, the purpose is to handle the load balancing of the connection at the user layer
  2. When the process reaches 7/8*connections, no longer process the connection, let other processes process the connection
  3. When all processes reach 7/8*connections, connection processing will become slow

Nginx uses Reactor mode to handle concurrent connections. In this mode, Nginx hands over all IO operations to a separate thread (or process), called the "main process". When new connection requests arrive, they are placed in a queue, and the main process periodically checks this queue for processing.

When the main process detects a new connection request, it creates a child process called a "worker", which handles the actual request. Since Reactor mode is event-driven, worker processes wait for events to occur instead of polling file descriptors to check if data is available. When an event occurs, the worker process will trigger the corresponding callback function to handle the event.

By using the Reactor mode, Nginx can efficiently manage a large number of concurrent connections without resource competition or blocking. Also, since Nginx's architecture is very modular, new features and extensions can be easily added.

Recommend a free open course of Zero Sound Academy. I personally think the teacher taught it well, so I would like to share it with you:

Linux, Nginx, ZeroMQ, MySQL, Redis, fastdfs, MongoDB, ZK, streaming media, CDN, P2P, K8S, Docker, TCP/IP, coroutines, DPDK and other technical content, learn now

Guess you like

Origin blog.csdn.net/weixin_44839362/article/details/130333173