SEDA (staged Event-driven architecture) is not a very new technique, but it always shows some shadow in the data I read, so I took some time to look at the papers related to it. The goal of Seda is to build an Internet system that supports large concurrency and to overcome the drawbacks of multi-threaded and event-driven server-side models. But Seda from academia, and this thing is still somewhat complicated, so so far the application of industrial grade is not a lot. Its official website is http://www.eecs.harvard.edu/~mdw/proj/seda/, some of the above papers are very good, it is worth reading.
For service-end processing models, there are currently two widely used:
1, multithreading processing model. This model consists of a main thread and multiple work threads, which are responsible for receiving requests and distributing the received requests to the appropriate work threading process, and then returning the response to the client directly after the work thread finishes processing the request. This model is very simple, also has a wide range of applications, especially the usual back-end modules, processing logic in the work line Chengri is blocked. The disadvantage of this model is also obvious, if a large number of work threads are blocked for some reason (such as the connection of the DB response is too slow, from the disk to obtain file data, etc.), will make subsequent requests are not processed, and the most classic example of Apache.
2, event-driven (event-driven) processing model. This model overcomes the concurrent volume disadvantage of the multithreaded model and uses event-driven processing for the request, where events are usually IO events, and the underlying implementations depend on non-blocking io and IO multiplexing. The event-driven model is ideal for IO-intensive applications, which are even less costly to CPU than switching between threads, such as various proxy. Now the proxy, basically standard epoll, so that can support the number of concurrent requests in the million level. Like Lightty, Nginx, the classic application of the event-driven model, the request processing process is driven by a state machine, and each state switch is triggered by an event, which is sufficient for a single thread (and, of course, a multiple-process event-driven model if you need to support higher processing power). The disadvantage of event-driven is: 1 A single processing stage can not block too long, if there is such a problem, the general can use specialized threading, processing and then triggering the event to let the main thread to deal with. 2 The entire event-driven process is usually fixed and completed within a thread by the scheduler, making it difficult to do the generic so that applications can customize the process.
Based on the advantages and disadvantages of the two models mentioned above, Seda is proposed, and its core idea is that a request is divided into multiple stage for processing, each stage separate from each other, and multiple stage of a request can be serialized or paralleled. Stage internal use of Event-driven, the new request is placed in the event queue, the system from the thread pool Select one of the threads through the event handler processing, event handler after processing the request to the next stage. The following is the SEDA process for a hypothetical Web server:
For each stage in the Seda, it consists of the following three parts:
1 Enter the event queue. There is a limit to the size of the event queue in Seda. Therefore, if the event queue reaches the threshold, the new event may be rejected or forwarded to a specific stage (such as error handling stage).
2 thread pool: This thread pool is transparent to the application, and each stage thread pool is independent of each other. For requests and features, the thread pool can be dynamically resized so that not one stage thread pool runs out of resources.
3 Event Handler,event Handler receive the event given by the system, do a specific logical processing after the event distribution to Other stage. Event handler often needs to be written by application developers.
SEDA Stage Internal structure diagram
In view of the state of each stage runtime, Seda introduced resource controller to adjust stage resource allocation and scheduling parameters. The core of the two controller is the thread pool controller and batching controller. The thread pool controller is used to control the run-time size of the thread pool, such as allocating more threads when the event queue is large, or reducing the number of threads. The batching controller is used to control the concurrency (batching factor) of the event that the event handler handles concurrently, and when batching increases, the throughput is increased but the event average response time becomes longer. When the batching factor to an hour, the opposite is the case. The control effect of batching controller makes batching factor fluctuation to control throughput and response time.
SEDA Controller structure diagram
An implementation framework for Seda is sandstorm, and developers can develop applications based on it. Limited to energy, did not do it in combat, but in terms of use, it is very simple. As you can imagine, Sandstorm provides some programming interfaces for event handling and configures stage policies through XML files (the intrinsic style of Java). For the use of such a phased event-driven framework, it is important to note that there is no sharing of data between stage and a reasonable division of stage strategies.
In the application, the Seda stage processing model can be used to component, modular, decoupling the different stages of processing, which may be suitable for such as workflow and other enterprise business process scenarios. But like a simple back-end module, it may be difficult for a request to classify multiple stage, and this stage strategy also increases the complexity of the processing process and the performance loss. Therefore, according to the different application characteristics, the practicability of Seda is still questionable.