I know it is a common practice to send cpu-bounded heavy tasks to background processes, in order to free a web server to serve other requests.
But – what if the request must know the result of that computation? Is there a known pattern, where the web server waits for a background process to finish its job, before it responds? I couldn’t find any example of this use case.
In my case, for instance, I want to send an HTTP post request’s body to Spark Streaming process that runs separately from the server, wait for the result and then respond to the request. (Server and Spark Streaming communicate through Kafka)
Many thanks
The web server is expected to give HTTP replies (to the web clients, i.e. browser) fairly quickly. A user would be unhappy if a web page loads in more than a second or two.
However, you could use AJAX and websocket technologies (with HTML5 and Javascript) in your web page. Then your web page could query asynchronously or periodically the web server (which would query your other process)
2
What you are describing is asynchronous programming, and more specifically the non-blocking IO model. It has concrete implementations in many languages, including Go, JavaScript( NodeJS ), Clojure, Erlang, C#, P, F#, Python, Perl, Ruby, C, etc.
The Go language, for example, was designed with exactly this sort of problem in mind. It uses goroutines to hand off execution to a non-blocking process. There is an example app floating around somewhere that is a url shortener app that fronts requests with a simple routine and passes off the shortener computation to a goroutine that can then be passed on to other logical or physical nodes, allowing the program to scale as much as needed.
If you know JavaScript, consider looking at NodeJS examples using async and Promises.
Your specific implementation will depend on your technology stack. I don’t know the details of the implementation of Spark Streaming and Kafka, but the basic idea is that your program creates an asynchronous request to Spark Streaming, which it tracks using a Promise or equivalent. It continues to process new requests while waiting for the existing request result to be returned. Once the result is returned, it sends its response.