High-performance communication library-zeroMQ, high-performance communication-zeromq
First, let me introduce ZMQ (full name: ZeroMQ ):
Official: "ZMQ (ZMQ for short) is a simple and easy-to-use transmission layer, a socket library like a framework. It makes Socket programming simpler, more concise, and more efficient. Is a message processing queue library that can be elastically scaled between multiple threads, kernels, and host boxes. ZMQ's clear goal is to "become part of the standard network protocol stack and then enter the Linux kernel ".
Compared with other message-oriented middleware, ZMQ Is Not A Message Queue Server in the traditional sense. In fact, ZMQ is not a server at all. It is more like a underlying network communication library, the Socket API is encapsulated to abstract network communication, Process Communication, and thread communication into a unified API interface.
Is the architecture of zeroMQ
Briefly,
From top to bottom are network, zeroMQ, and user applications, which is equivalent to adding a level of message and communication library at the application layer and network layer.
The application layer uses the socket through the API (this socket is different from the traditional socket) to interact with zeroMQ.
ZeroMQ has multiple worker threads. Each worker thread can create multiple socket connection objects (monitored by listener), and can also be used as a remote connection object of the socket Client.
Socket objects include engine and session objects. engine is responsible for connection management (interacting with the network, such as reading data from the network, queuing of messages, and accepting new connections ); session objects are mainly responsible for interacting with the socket of zeroMQ. They use an asynchronous event-driven lockless queue pipe.
ZeroMQ uses the following high-performance features:
1. Lock-free queue model
For the data exchange channel pipe between cross-thread interaction (client and session), The lockless queue algorithm CAS is used. asynchronous events are registered at both ends of pipe, when a message is read or written to pipe, the read/write events are automatically triggered.
2. Batch Processing Algorithms
For traditional message processing, each message needs to be called by the system when it is sent and received. In this way, the system overhead of a large number of messages is large, and zeroMQ is used for batch messages, adaptive Optimization is performed to receive and send messages in batches.
3. Bind threads in multiple cores without CPU Switching
Different from the traditional multi-thread concurrency mode, semaphore or critical section, zeroMQ makes full use of the advantages of multiple cores. Each core is bound to run a worker thread to avoid CPU switching overhead between multiple threads.