Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

With popular software out today like Node.js, Celery, Twisted, and others boasting about being asynchronous, what does it mean?

I've gone through the basic Node.js tutorials and written a few Node.js scripts, but still don't feel comfortable with the topic. Please note that I haven't had I had any formal introduction to the topic.

  1. How exactly does asynchronous software work?
  2. What are the pros and cons?
  3. What are callbacks?
  4. Lastly, how does a synchronous web server perform vs. an asynchronous web server?
share|improve this question

5 Answers 5

up vote 8 down vote accepted

Wikipedia might be a good place to start with some general concepts and basic information.

This quote from that page sums it up but probably needs a bit of knowledge to fully understand it.

In programming, asynchronous events are those occurring independently of the main program flow. Asynchronous actions are actions executed in a non-blocking scheme, allowing the main program flow to continue processing.

So with synchronous programming the user initiates an action on the program but then has to wait for the operation to complete before being able to do anything else. An example might be saving a file to disk, you can't do anything else until the file is saved.

With asynchronous programming the user initiates an action, but then can carry on doing other work while the operation completes. The program then notifies the user in some way that its finished. An example here might be printing a document. Here you initiate the print then (after some set up) can carry on while the document is sent to the printer. You get some sort of notification that the printing is complete.

Asynchronous applications rely on multithreading or spawning child processes that do the work.

Callbacks are one mechanism where the calling code can do something when the asynchronous operation is complete. You'll register the callback with the long running process in some way then when it completes the code defined by the callback will be executed.

share|improve this answer
  • Synchronous access is when a call fetches * some * data, retrieves it, and directly returns it
  • Asynchronous access is when a querry to fetch the data is submitted, and the data is retrieved in a callback function passed in the initial call

The basic difference is that in the case of asychronous access you don't know for sure when the data will arrive and if it will even arrive.

The main advantage of asynchronous access is that you avoid lags when waiting for the network connection or the database to work or anything else that could slow down or even freeze your client code. The main disadvantage is added complexity and the fact that you can't rely on the data arriving and this has to be reflected in your code. Using asynchronous access however, you can define a timeout period on the client side and issue an error after the timeout occured and still do something while waiting for the data (like for instance showing a hourglass).

If it's still a question to you, all direct function calls are synchronous. I'm sure you know the advantages and disadvantages to sychronous calls.

share|improve this answer

I just spent the last few weeks implementing a handful of asynchronous web services. Here's the general idea:

Synchronous messaging is the "standard" pattern. Client sends a request to Server, then waits for Server to send a response. It's a lot simpler to program, on both ends, but it has the disadvantage that the Client will be blocked until it gets a response back.

Asynchronous messaging is a bit more involved. Client sends a request to Server, and Server replies with an acknowledgement. (Not a response, but a "Yes, I got your request and I'll deal with it when I can.") Server stores the request somewhere, such as in a queue. When the Server is ready, it processes the request, and sends the response to the Client, which has to be able to receive incoming messages, and there has to be some way to associate this response message with the request it sent earlier, in case it's sent more than one request in the meantime.

It's a lot more involved, but it keeps the client from blocking and having to worry about timeouts, etc.

share|improve this answer

SearchNetworking.com has this explanation:

In computer programs, asynchronous operation means that a process operates independently of other processes, whereas synchronous operation means that the process runs only as a result of some other process being completed or handing off operation. A typical activity that might use a synchronous protocol would be a transmission of files from one point to another. As each transmission is received, a response is returned indicating success or the need to resend. Each successive transmission of data requires a response to the previous transmission before a new one can be initiated.

Wikipedia has a summary of callback though don't forget the context can matter here as in some cases a callback is viewed differently than a postback.

Imagine a program that takes in 3 numbers and outputs the median of those numbers. There are a few operations to such a program: Get the 3 numbers from the user, sort the numbers to find the middle value, and output the result. This has to be done in that order as you couldn't sort before you have the values and can't output the result until it is computed.

share|improve this answer

So, how exactly does asynchronous software work?

By having a call or request return more or less immediately, but without a return value or response, which is instead sent at some later time.

What are the pros and cons?

Pro: the call does not block. If the caller is single-threaded, this enables it to do something else meanwhile. If it is multithreaded but with significant overhead per thread, it may require fewer resources.

Con: it's a more complex programming model. Harder to learn, easier to make mistakes.

What are callbacks?

The pieces of code that deal with the response.

Lastly, how does a synchronous web server perform vs. an asynchronous web server?

That depends on the underlying platform and the demands of the application being run. The general expectation is that an asynchronous server is better at handling large numbers of requests, especially when they require little computation but may have to wait for something else (such as a DB request) before they can complete.

As a warning against blanket statements: Java nio is Java's way of doing asynchronous networking. But there is some good evidence that it may actually perform worse than plain old simple blocking IO for most apps on modern JVMs (in part because those have gotten a lot better at dealing with lots of threads).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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