Share via


How to be a good web service? Syncronous or asyncronous, let's get straight!

Web service method can be either syncronous or asyncronous. A high quality web service should be very clear about whether each end point is syncronous or asyncronous.

What are syncrhonous operations?

Syncronous operations completes the operation at the time of completion of sending response. It is suitable for operations that can be completed at milliseconds level. Operations only involve database reading operation typically can be completed at this time frame. Syncronous operation returns result in the response to the client.

What is asyncronous operation?

Asyncronous operations completes the operation after the completion of sending response. It is suitable for operations that may last longer than a typical http request timeout. Examples include async Blob copy method of Azure Storage service, create job method of Azure media services. Both method operate on Gigabyte level of data, it may take  hours to complete those operations depending on the job size and work load on server side. The response of these operations typically contain a unique id to identify the submitted request, and client can check the status of the operation using the ID given.

What are the common pitfalls?

There are two possible types of error. Among them, type I error is much more common than type II error.

I. Asyncronous operation was implemented as syncronous.

Type I error occurs when the response is returned prior to the completion of the operation. Upon receiving the response, the client may take action based on the assumption that the prior operation had already been completed. This type of error basically breaks the atomicity of the web service operation.

The fix of the operation can be either delay the response until the operation is completed or convert this "syncronous" operation into an asyncronous one.

II. Syncronous operation was implemented as asyncronous.

This type of the error is very rare simply because implementation of asyncronous operation typically takes a significant more resources, it is unlikely that developer will take the pain to write a complicated async web service which can be completed instantaneously. :)

Comments

  • Anonymous
    September 29, 2013
    Most enterprise services (especially ones updating data, or posting transactions) are best implemented with asynchronous services (regardless of response time) as it fits better with Enterprise Service Bus or Messaging architectures. It is of course (relatively) easy to map synchronous to asynchronous (though the reverse is not often true), but in general asynchronous services gives you more flexibility.  E.g. message queues can automatically recover from temporary network failures (good if you operate over the internet).

  • Anonymous
    September 30, 2013
    interesting, can u give a few examples?