Blocking vs Non-Blocking Processes

Blocking Processes

Imagine a teacher wants to test his students’ math ability. He gets them to stand in a line and he starts at the top, asking the first student: “What is 8 * 9?” The student replies with the correct answer almost immediately and can sit down. Things are going well. Except the teacher now asks a really difficult question to the next student and she has think for a whole minute. Meanwhile, all remaining students are idly waiting in line. Eventually the teacher gets to the end of the line, but the math test took up the whole class.

This is how blocking processes work.

Non-Blocking Processes

A different teacher conducts her math tests like this: She lines up the students, quickly moves down the line giving each a card with the problem to be solved, and she then stands by the whiteboard ready to write down their answers. The students are now working on their problems simultaneously and nobody is idle.

This seems like a very fast and effective way of conducting a math test and things are going well. Except these students, though brilliant at math, have zero common sense (much like computer programs). They solve their problems but then don’t know to report their answers to the teacher. Fortunately, the teacher foresaw this and wrote what to do on the back of the card. After completing their problems, the students turn over their cards and follow the instructions to move to the board and tell the teacher the answer. In programming this secondary instruction is called a “callback”.

Though this “non-blocking” style is a little more complicated than the previous “blocking” style, the math test has been completed in a fraction of the time and the class can get on with doing something else. Great, right? No wonder Twitter switched over from Ruby, a blocking language, to Node, non-blocking.

Okay this time, imagine a slightly different scenario. The class is studying Japanese and wants to learn the language for days of the week. The teacher, however, doesn’t speak Japanese and so instructs the students to look up the words in their dictionaries. To speed up the process, she delegates each day, Monday through Sunday, to a different student. Their secondary, “callback”, instruction is to move to the board and report the answer to the teacher.

Here we hit a snag: some students are faster with the dictionary than others. In fact, the student looking up “Friday” returns to the board first and his answers gets written down first. In the end the class has a list of seven Japanese words but no idea which of the days of the week each corresponds to. Clearly the “callback” instruction to the students was inadequate: they should, perhaps, have been told to report the English word given to them along with the Japanese translation.

I hope this elaborate metaphor has given you some insight into the relative merits of blocking and non-blocking processes. =)

Blocking vs Non-Blocking Processes

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s