A couple of days ago I was asked to explain the difference between synchronous and asynchronous I/O. This was something that I covered last year at SQLBits II in my presentation "I/O! I/O! It's off to work we go...". However, when I was asked again the other day I thought of a rather good analogy.
Synchronous Shopping
It's Saturday and I need to go to Waitrose. I get in the car and head off to my nearest store in sunny Dartford. I pick up my trolley and look at the shopping list. Pasta, Sauce, and mince for the bolognese. No prizes for guessing what we are having for dinner (unlike the SQLBits logo competition). First of all I make my way to the pasta aisle. I look at my options and select the spaghetti. Secondly, I glide over to the sauces and select something wholesome and organic that still comes out of a jar (I am no Gordon Ramsay). Finally, over to the meat counter to pick up some nice mince. With all my products in the basket I proceed to the checkout and make my way back home. This is "Synchronous Shopping": simple, structured, sequential & calm.
Asynchronous Shopping
It's Saturday again and Spag bol is back on the menu and I need to get to Waitrose - with the kids. We have an argument who is going to sit in the front and who is in the back, what music will be played from which ipod. With the "Robots in Disguise" blaring out of my largely defunct stereo we head off to Waitrose. We get the trolley and armed with my little helpers we enter the store. "Lucy go and get the Pasta" and off she goes as I make my way over to the sweets aisle (asynchronous help isn't free). "Kate can you get the sauce?". Finally, "Oliver can you please pick me some mince". Oliver is six years old so this is more of a hope based request. As I make my way round the store my helpers start to return. Kate has picked something tomato-ey but with mushrooms in that is sure to delight her siblings. Lucy returns next with some tagliatelle. Interesting choice. Finally, Oliver returns and as if by magic this coincides with my arrival in the sweetie aisle. However, he has mince so all is good with the world. Welcome to "Asynchronous Shopping": complex, unstructured, non-sequential & hectic. It can however be a lot quicker than when I do it by myself.
I/O
So how does this relate back to I/O? Well your asynchronous reads and writes are to your data files whilst your synchronous activity is to the transaction log. Your synchronous I/O is structured and sequential. It uses the standard readfile and writefile api's. This is why we put this file onto mirrored drives where the disk head can simply move in one direction. Asynchronous I/O is different. It uses different API's (ReadfileEx and WritefileEx). It fires off requests but doesn't wait for a response before moving on. When an asynchronous I/O is required the task that requested it is put into a pending state and taken off the scheduler whilst it waits for the I/O to complete. Something else is moved onto the scheduler in the meantime. Once the task is complete SQL Server is notified the operation is now runnable and can get put back onto the cpu by the scheduler when the yield for the task that has been running occurs.
Service Broker
Service broker is an asynchronous shopper. However, I won't go into that now. I hope that this will be one of the sessions selected for SQLBits III and I'll get to talk about it then. The guys are getting ready to open the voting for sessions and abstracts as well as the voting for the world famous SQLBits Logo competition. I will update you all when the voting goes live but expect an announcement soon.
Cheers, James