You are writing a multi-threaded software piece for NSE for maintaining the volume of Trades made by its individual brokers (icici direct, reliance ). It’s a highly concurrent scenario and we cannot use lock-based thread safety due to the high demand of throughput. How would handle such a scenario?

By | March 5, 2023

In a highly concurrent scenario where lock-based thread safety may not be feasible due to high demand for throughput, there are several alternative strategies that can be used to ensure correct and efficient management of trade volumes for individual brokers. Here are a few possible approaches:

  1. Atomic variables: Using atomic variables, such as AtomicLong, can provide thread-safe access and updates to a shared counter for each broker’s trade volume. Atomic variables use low-level hardware primitives to ensure thread-safe access without the need for locks, making them a good choice for high-throughput scenarios.
  2. Read-write locks: Read-write locks can allow multiple threads to read the trade volume for a given broker simultaneously while ensuring that only one thread can update the volume at a time. This can be useful if there are many read operations and relatively few write operations.
  3. Thread-local storage: If each thread is responsible for updating the trade volume for a specific broker, then using thread-local storage can eliminate the need for synchronization altogether. Each thread can maintain its own copy of the trade volume counter for its assigned broker, and updates can be made without contention with other threads.
  4. Partitioning: If there are many brokers and trade volumes, it may be possible to partition the trade volumes across multiple data structures or nodes. This can reduce contention and improve throughput by limiting the scope of shared state that needs to be synchronized.
  5. Non-blocking data structures: There are various non-blocking data structures, such as queues and maps, that can provide thread-safe access and updates without the use of locks. These data structures use techniques such as compare-and-swap (CAS) operations to ensure consistency without blocking threads.

The optimal solution will depend on the specific requirements and constraints of the system, as well as the characteristics of the workload and the hardware. It is important to carefully measure and benchmark different approaches to determine the best strategy for a given scenario.

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *