Sunday, June 8, 2014

Paper notes: A Study and Toolkit for Asynchronous Programming in C#

The .NET framework mainly provides two models for asynchronous programming: (1) the Asynchronous Programming Model (APM), that uses callbacks, and (2) the Task Asynchronous Pattern (TAP), that uses Tasks, which are similar to the concept of futures.

The Task represents the operation in progress, and its future result. The Task can be (1) queried for the status of the operation, (2) synchronized upon to wait for the result of the operation, or (3) set up with a continuation that resumes in the background when the task completes.

When a method has the async keyword modifier in its signature, the await keyword can be used to define pausing points. The code following the await expression can be considered a continuation of the method, exactly like the callback that needs to be supplied explicitly when using APM or plain TAP.

Do Developers Misuse async/await?
  1. One in five async methods violate the principle that an async method should be awaitable unless it is the top level event handler.
  2. Adding the async modifier comes at a price: the compiler generates some code in every async method and generated code complicates the control flow which results in decreased performance. There is no need to use async/await in 14% of async methods.
  3. 1 out of 5 apps miss opportunities in at least one async method to increase asynchronicity.
  4. 99% of the time, developers did not use ConfigureAwait(false) where this was needed.
The async/await feature is a powerful abstraction. asynchronous methods are more complicated than regular methods in three ways. (1) Control flow of asynchronous methods. Control is returned to the caller when awaiting, and the continuation is resumed later on. (2) Exception handling. Exceptions thrown in asynchronous methods are automatically captured and returned through the Task. The exception is then re-thrown when the Task is awaited. (3) Non-trivial concurrent behavior.

Each of these is a leak in the abstraction, which requires an understanding of the underlying technology - which developers do not yet seem to grasp.

Another problem might simply be the naming of the feature: asynchronous methods. However, the first part of the method executes synchronously, and possible the continuations do as well. Therefore, the name asynchronous method might be misleading: the term pauseable could be more appropriate.


Sunday, June 1, 2014

Not about the UI and the database

When you ask an outsider which components an average application consists of, he will most likely be able to identify the user interface and the database. He will also recognize that there is something in between that takes the input from the user interface, applies some logic and persists the result in the database.

In the past, trying to make sense of what goes on the middle, we started - with the best intentions - layering things. Each layer had its own responsibility and would build upon previous layers. Although there was a layer for business logic, we never really succeeded in capturing the essence. In the end we would still be orchestrating database calls, but now we would be forced to go through a bunch of indirections in the form of anemic layers and objects.

Some people saw these designs for what they were, broke free and started optimizing for the shortest path - from user interface to database with the least amount of effort. By aiming to serve the common denominator and by putting their trust in dark magic, frameworks popped up that would allow you to slap together an application in a matter of hours.

The problem with these frameworks is that they leave you with very little room for your own, and you often end up jumping through hoops when you need to deviate from the path carved out for you.

That's not the only problem though - applications are a lot more than a user interface and a database. What lives between those two is more than a technical necessity - it's a place where you get to build a model of the problem you are solving. The model gives you an opportunity to learn from and to communicate with domain experts, peers and users. And that's exactly where most businesses make the difference, not by having a fancy user interface or a carefully designed database schema, but by really understanding and by being absorbed by the problem they are solving. It's the user interface and the database that are the necessary evil we bring upon ourselves by solving problems using computers.

The state that lives in your database is a side effect - the result of the model's behavior. The user interface tries to make it as easy as possible for users to drive and use the model.

Although the user interface and the database are important, it's the model that is the heart and soul of your application.

(*) Disclaimer: all these drawings are simplistic by design.

Sunday, May 25, 2014

Eventual consistency in the Wild West

San Francisco, 1852. With the California Gold Rush at its peak, successful gold-seekers wanted to protect all their precious gold nuggets by storing them in a strong safe. At the time, it wasn't that easy to have access to a safe though. At the very beginning, it were just a few local merchants that owned one. Not much later, bankers swamped the area hoping to get their piece of the pie - bringing the strongest safes money can buy.

James King of William - who had made a fortune himself mining gold - was one of the first to found a trustworthy bank in San Francisco.
With the city growing from a small 200 residents in 1846 to about 36.000 in 1852, it became harder and harder for the bank to accommodate all their customers in that one office. It needed to expand its operations.
Three months later a new branch opened up on the other side of town. James determined to build a strong brand, wanted to allow customers to go to any of the two branches to deposit and withdraw their money. This meant that the books of the two branches had to be kept consistent. To maintain the books, James commanded all the clerks to duplicate all new records. Two hired horsemen would then come in every few hours and bring those records to the other branch. Since the books were now the same in both branches, customers could deposit and withdraw money in both sides of town. For James, life was good, he was now raking in twice as much.

Pancho and Lefty, two bandits fled from Mexico, trying their luck in California, were instead of spending their Friday afternoon in search of gold, trying to forget their gold drought by playing cards and drinking cheap Whiskey in the local saloon. While Lefty kept rambling on about how unlucky they had been these last few weeks, Pancho paid closer attention to the conversation going on between the saloon keeper and this well-dressed rider that had just entered. The leather bound notebook the rider was carrying stood out immediately - it carried this familiar looking mark and looked expensive. Even though Lefty kept on rambling, Pancho could make up quite a bit eavesdropping on the saloon keeper and the rider's conversation. That suddenly came to an end though, when the rider knocked back his drink and got ready to leave - "Well, I better get going before the boss man notices that his books are no longer up to date."
Pancho watched the rider step outside and get back on his horse. Once the rider was out of sight, Pancho with an intense gleam in his eyes, cut off Lefty and ordered him to shut his mouth - "Shut it fool, I have a plan that's going to make us some easy money."

That same day, Pancho and Lefty gathered all their coins, opened an account for Pancho at James King's bank and made their first deposit. Afterwards, they stuck around the bank's side entrance just to make sure one of the horsemen came by to pick up the books. Twenty minutes later, the same rider they saw at the saloon showed up - Pancho and Lefty spontaneously looked the other way, avoiding to be seen by the rider. They hung around a little longer, to verify that when the driver exited the bank, he was carrying a notebook - a notebook containing the records of the account they just opened and their first deposit.

The next day, Pancho and Lefty got up early. They only got a few hours of shut-eye in, they had been up all night sneaking around, scouting the biggest ranch in town. To make their plan work, they needed to borrow a fast horse.
Impatiently waiting in front of the bank, Lefty breathed a sigh of relief when the clerk opened the bank at nine AM sharp. Before he entered, he looked one more time to the left, where Lefty was standing in the shadows holding the horse - ready to go. Being the first customer to enter the bank, he walked straight up to the counter, pulled out his token of authentication and told the clerk he wanted to make a withdrawal for all that was on his balance. The clerk looked at the token carefully, but didn't ask any further questions - he still remembered seeing Pancho yesterday, it wasn't like he owned a fortune.

Money in hand, Pancho firmly walked out of the bank. To make the plan work, he now needed to jump on that horse as quickly as possible and ride off to the branch on the other side of town - if he made it there before the hired horsemen had the chance to transfer the latest records, he might be able to double his assets in a single morning.

Pancho didn't spare the horse at all, frantically digging his spurs into its sides. He made sure to take the dirt road through town, avoiding obstacles and people where he could.
Tying the horse down next to the bank, he watched one of the horsemen walk out of the bank. Damn. Had he been too slow, were the books already consistent again?
Pancho broke out in a cold sweat, but he had to try. He shuffled into the bank and got in line - there were three customers in front of him. Every time he looked up at the clock, he got more anxious. By the time his turn came, he was terrified. Without looking up, he pulled out his token of authentication once more, and mumbled to the clerk that he wanted to withdraw all of his money. The clerk noticed the drops of sweat on Pancho's forehead and how Pancho's hands were shaking. "Poor soul," the clerk thought, "he must have caught cholera like many others, they're falling like flies." The clerk went into the back, looked into the books, opened the safe and handed Pancho everything he was worth.

James King spent very little of his time at one of his banks - he was too busy looking for new ventures to fund. When he was in town and had the time, he did make a habit of jumping by to look in the books right before closing time. Today, one of the clerks walked up to James King as soon as he arrived; "I'm afraid there has been made a mistake in the books, sir. The books show that one customer withdrew all of his money twice today. First here, and then in the other branch. Something must have gone wrong copying the records." Going through the records, it was obvious to James King what had happened; they had been robbed.

"Thank the lord that it's only for a small amount. Let's make sure this doesn't happen again." James started by hiring two extra horsemen, but to be even more sure he also had to introduce some new rules. A customer could now only withdraw $5 a day, unless he had proven to be a good and reliable customer. If a customer needed to withdraw extraordinary large amounts, he needed to inform a specific branch one day in advance. This allowed both branches to keep serving all customers smoothly while not giving up on operating in a semi-autonomous fashion. James was even considering opening a new branch in the famous city of angels, Los Angeles.

Next time; isolation levels in the Wild West.