0

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

1

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)

  • Sorry, I didn't mention - I have a requirement that the client sends only one request (It is not a person/browser but a machine). The question is whether to do this computation in the web server or in another process. The web server also has to handle thousands of requests / second. – AlonL Feb 5 '15 at 8:32
  • 1
    Please improve & edit your question to tell all this. Show the actual request URLs & HTTP messages... – Basile Starynkevitch Feb 5 '15 at 8:37
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.