The scenario is:
- client makes request to server A
- Server A makes potentially multiple requests to server B. Edit to clarify, server A makes the requests concurrently using Futures.
- Server A blocks until all results return
- Server A collates responses into single response and returns to client
So currently the approach is the block using java Futures and return the results when they come in. My concern is this is quite thread intensive and I'm worried we will run out of resources on the server. I was thinking of going async in server A by using
DeferredResult but since there is a thread for each request to server B, that doesn't seem to carry much benefit.
Alternatively, we could do
- Server A makes multiple requests to Server B
- Server B immediate returns a token for each request
- Server A tracks the tokens and returns
- Server B gets each result, puts it in memcache, sends a JMS message to server A. Edit: I just realized I could send the result in the JMS message, so no need for memcache at all.
- Server A can complete the DeferredResult after it collects all the responses from JMS.
This has the advantage of being much much simpler in code (no complicated futures work) at the cost of being more complicated architecturally. It also introduces more layers (JMS) for failure. It also has the advantage of being much less resource intensive.
This is a high use system. Calls to system B can take anywhere from 1 to several seconds.