@backstreetbrogrammer
--------------------------------------------------------------------------------
Chapter 02 - Virtual Threads - Thread per client OMS
--------------------------------------------------------------------------------
Suppose, we have an Order Management System (OMS) which is working as a TCP server and receiving stock trading orders from various clients.
Once the order is received, following processing is done on the Order object before it is sent down to algorithmic trading engine or directly to exchange (DMA):
- Validate the order client's wallet if enough funds
- Enrich the order with latest market data (best bid / best ask)
- Update the latest order state to persistence (log or database)
Each of the following takes around 1 second:
- parse the request
- create an Order from the request
- validate the order client's wallet if enough funds
- enrich the order with latest market data
- update the latest order state to persistence
- send the order to downstream
The connection listening thread that calls accept() creates and starts a new Thread to handle the connection and quickly goes back to accepting more connections.
The handle() method is left unchanged, but it's now executed in parallel by multiple threads, each handling their connection.
The computation time of the previous example goes from 6 + 6 + 6 = 18 seconds to max(6, 6, 6) = 6 seconds.
Now all requests from separate users are completely independent.
However, there are few caveats:
- Within a single request in the handle() method =: parsing, validating, persisting and sending the order to downstream is all done in the same thread.
- Threads are scarce, and there is a limitation imposed on the maximum number of threads which can be created in an OS. Thus, the design doesn't scale well when the number of client connections will increase.
Github: github.com/backstreetbrogramm...
- Upgrade to Java 21 Playlist: • Upgrade to Java 21
- Apache Spark for Java Developers Playlist: • Apache Spark for Java ...
- Top Java Coding Interview Problems Playlist: • Top Java Coding Interv...
- Java Serialization Playlist: • Java Serialization
- Dynamic Programming Playlist: • Dynamic Programming
#java #javadevelopers #javaprogramming
Негізгі бет Ғылым және технология 09 - Thread per client OMS
Пікірлер