Study Notes on Event Driven Model

Event Driven Model and Concurrency

Key Concept: Single Thread and Concurrency

  • In the event driven model, despite operating on a single thread, concurrency is achieved through interleaving the processing of multiple requests.

    • Interleaving: This technique allows the single execution context to handle different tasks within the same thread by switching between them as needed.

  • Comparison to Multi-Process and Multi-Thread Models: In contrast to the multi-process and multi-thread models, where each context handles only one request at a time, the event driven model can manage multiple requests simultaneously through context switching.

Execution Contexts

  • Execution Contexts in Multi-Process and Multi-Thread:

    • Each process or thread represents a distinct execution context involving resources allocated to handle specific tasks.

    • To achieve increased concurrency, multiple processes or threads can be added.

    • If the number of execution contexts exceeds the available CPU cores, context switching is employed to optimize resource usage.

Event Handling Process

  • Request Handling Example: A detailed breakdown of how requests are processed:

    1. Client Requests

    • For instance, let's consider requests from three clients (C1, C2, C3).

    1. Receiving Requests

    • Client C1's connection request is received and dispatches an accept operation.

    • The actual HTTP request message from C1 is then processed.

    1. Parsing HTTP Message

    • The message is parsed to extract necessary files.

    1. Initiating I/O Operations

    • The system initiates I/O to read the required file from the disk.

    • At this point, C1’s request is in a waiting state, pending the completion of the disk I/O operation.

Simultaneous Client Processing

  • Handling Additional Client Requests

    • While C1’s request is waiting for disk I/O, suppose requests from C2 and C3 come in:

    • C2's Request

      • The connection request from C2 is accepted, but the process must wait for the HTTP message to be received over the network.

    • C3's Request

      • C3's connection request is also accepted and is in the handling phase (accept connection handler).

Progressing Requests

  • Time Progression: As time progresses, the following occurs:

    1. C3's Request

    • Movement progresses, resulting in C3 waiting on an event (the reception of the HTTP message).

    1. C2's Request

    • This request might still be waiting for disk I/O to read the necessary file.

    1. C1's Request

    • The file for C1 might already be in the process of being sent to the user in chunks of a specific byte size.

Conclusion: Managing Multiple Requests

  • The key takeaway from the event driven model is that, although it uses a single thread as its execution context, it is capable of concurrently handling multiple client requests through interleaved processing. This mechanism effectively allows for responsiveness and efficiency within a single-threaded environment, demonstrating the power of non-blocking I/O operations and event handling.

  • Overall, the event driven architecture provides benefits in environments where high concurrency is needed without the overhead associated with multi-threading or multi-processing.