Parallel asynchronous operation patterns
A common source of bad performance is running operations sequentially that could be completed in parallel.
For example, a naive implementation of loading a cart and then the contained products would be as follows:
Figure 7.2: Load cart then each of the three products contained from fakestoreapi
In this case, the operation completion time is composed of the sum of the following:
- Request-response time for
GET /carts/{cartId}
- Request-response time for
GET /products/1
- Request-response time for
GET /products/2
- Request-response time for
GET /products/3
There is a requirement for the /products/{productId}
calls to be done after the GET /carts/{cartId}
call completes since that’s where the product IDs are coming from. What isn’t required is for each product call to wait for the previous one to complete; the calls only depend on data from the GET /carts/{cartId}
call. This is an...