Web-development – Best practices when managing long running asynchronous jobs

asyncweb-development

I am in the design phase of a project where the end user will submit a request from a web page that will spawn a long running asynchronous processed job. Is there a "best practice" for this problem? Are web services and service brokers a good way to go? Is Microsoft messaging queue applicable here?

Best Answer

I don't know about "best practice". I do know the most common mistakes.

First Mistake: DOS Yourself

You use the webhandler to process the long running job. This can be bad or extremely bad depending on your percentage of hits that become long running jobs, how long they run and how much sustained traffic you get.

You want to make sure that you aren't getting more than 1 long running job within the period of time it takes for that long running job to complete. If you do you DOS yourself. It will also get worse the more traffic you get assuming the percentage and time stays consistent. It's one of those problems which self-imposes a limit on traffic growth.

Second Mistake: Spawning from the webhandler

Spawning a process from the web handler to handle a long running process can be tricky, and as a result also error prone.

  • You have to dissociate from the parent correctly otherwise the webhandler waits for the child to complete.
  • When you fork a child in unix it inherits open handles from the parent. These will automatically be closed unless they are overwritten. This includes things like database connections, filehandles, other open network connections. All of which get closed when the child process completes.

Options

I usually use at(1) to cleanly dissociate from the webhandler without forking.

You can also use a polling implementation with cron.

You can communicate to another server process that handles the processing. That communication can be done with sockets, pipes, or higher level abstractions like a REST http call or routing a queue message.

Related Topic