Concurrency Strategies in FASTapi

Deepanshu Mehta
4 min readNov 11, 2023

--

Concurrency strategies in software development refer to the approaches and techniques used to manage and handle concurrent operations and requests within an application or system. These strategies are essential for ensuring that the application can effectively handle multiple tasks and requests simultaneously without conflicts or performance degradation.

Some common concurrency strategies include:

  1. Threading: Using multiple threads to execute tasks concurrently. Each thread represents an independent flow of control within the application.
  2. Multiprocessing: Running multiple processes that can execute tasks independently. This approach can take advantage of multi-core processors and distributed systems.
  3. Event-Driven Programming: Handling requests and operations based on events, such as user interactions or data availability, without blocking the main thread. This approach is commonly used in asynchronous programming.
  4. Locking and Synchronization: Using locks, semaphores, and other synchronization mechanisms to control access to shared resources and prevent conflicts in concurrent operations.
  5. Parallelism: Leveraging parallel processing to execute tasks simultaneously, often used in scientific computing and data-intensive applications.
  6. Isolation Levels and Transactions: Managing concurrent access to data by defining isolation levels and implementing transactional mechanisms to ensure data consistency and integrity.

Also the web servers like ASGI, can use different concurrency strategies such as thread-per-connection, process-per-connection, or event-driven programming to handle concurrent requests.

ASGI servers can use different concurrency strategies to handle concurrent requests, such as:

  1. Thread-Per-Connection: In this strategy, each incoming connection is assigned to a separate thread, allowing multiple requests to be processed concurrently. This approach can be effective for handling I/O-bound tasks and can simplify the management of concurrent connections. Also, this gives feature of Resource Isolation By assigning a dedicated thread to each connection, the “Thread-Per-Connection” strategy provides a level of isolation, ensuring that the processing of one connection does not impact the processing of other connections.
  2. Process-Per-Connection: With this strategy, each incoming connection is assigned to a separate process. This approach can take advantage of multi-core processors and distributed systems, allowing for parallel processing of requests. It provides a high level of isolation between requests but may have higher memory overhead compared to thread-per-connection.
  3. Event-Driven Programming: Event-driven programming allows the server to react to events, such as incoming requests or data availability, without blocking the main thread. This approach is commonly used in async programming and can efficiently handle a large number of concurrent connections without the need for a separate thread or process per connection.

> In an event-driven application, there is generally a main loop that listens for events and triggers a callback function when one of those events is detected In ASGI, the server listens for incoming requests and events, and when an event is detected, it triggers a coroutine function to handle the event. The coroutine function can then perform any necessary processing, such as retrieving data from a database or sending a response to the client, without blocking the main thread.

In the context of ASGI, events refer to various types of messages or signals that are generated by the server or the client and are used to trigger specific actions or behaviors in the application. Here are some examples of events in event-driven programming with ASGI:

  1. HTTP Requests: When a client sends an HTTP request to the server, it generates an event that triggers the server to process the request and send a response back to the client.
  2. WebSocket Messages: WebSocket is a protocol that enables real-time communication between the client and the server. When a client sends a WebSocket message to the server, it generates an event that triggers the server to process the message and send a response back to the client.
  3. Server-Sent Events (SSE): is a technology that enables the server to send real-time updates to the client. When the server sends an SSE message to the client, it generates an event that triggers the client to process the message and update the UI accordingly.
  4. Database Events: When a database operation is performed, such as an insert, update, or delete, it generates an event that triggers the server to process the operation and send a response back to the client.
  5. Timer Events: Timer events are used to trigger specific actions or behaviors at a specific time or interval. For example, a timer event can be used to trigger a periodic update of a dashboard or a notification to the user.

--

--

Deepanshu Mehta
Deepanshu Mehta

Written by Deepanshu Mehta

I am Full Stack Developer Python/Django + Go/Gin + Javascript/ReactJS

No responses yet