Concurrent network service programs-Architecture Design Considerations

Source: Internet
Author: User
Switch from: Provide (24x7 uninterrupted service)

Starting point of design:

First, the network model, epoll event trigger mechanism, reactor, and proactor are similar.

Second, single thread or multi-thread

In a word, based on business needs (Io or CPU computing), multithreading can make full use of the advantages of multi-core processing, but may occupy a large amount of CPU resources.

In addition, multithreading does not increase the number of concurrent threads (restricted by the process's fd and epoll restrictions). However, smooth service can reduce the request latency.

If it is a single thread, the epoll timeout model can be fully utilized. However, a long task occupies CPU resources.

For multithreading, you can use the consumer-producer model, task queue, and thread pool.

It should be noted that the switching overhead and lock brought about by multithreading cannot be ignored. You do not need to think that you are pursuing multithreading and should design it based on the business scenario.

Third, the thread is still a process (depending on whether or not fork is used. In principle, the thread should be used whenever possible, except for fork, and the thread switching overhead is smaller than the process. In fact, the thread is generated by calling the clone system in Linux and has its own task_struct structure ).

Fourth, inter-process communication

There are shared memory, FIFO, message queue, pipe, etc. Pay attention to the shared memory, and you need to combine the lock.

We recommend that you use the TCP method. This method has many advantages, which makes it easy to deploy multiple processes on multiple servers.

Fifth, heartbeat Packet

One server needs to know the service status of the other server, so it needs to send heartbeat packets continuously.

Sixth, locks and condition Variables

Recursive lock or non-recursive lock?

The default locks are non-recursive locks. However, we recommend that you specify attributes when creating mutex to ensure code portability. We recommend that you use non-recursive locks. Recursive locks can cause unnecessary troubles and hide program bugs.

Lock encapsulation. The principle is resource acquisition is initialization.

Conditional variables must be combined with locks.

To release resources (thread cancellation point), you need to install the callback function for thread exit and protect possible code (pthread_testcancle surrounded by code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.