Empiricism

Empiricism , in the philosophy of science, is defined as “emphasizing evidence, especially as discovered in experiments. It is a fundamental part of the scientific method that all hypotheses and theories must be tested against observations of the natural world rather than resting solely on a priori reasoning, intuition, or revelation.” 1

By this definition, empiricism is closely related to experiment. However, I keep both concepts in my list of five because experiments can be carried out in such controlled circumstances that we could easily be experimenting with ideas that don’t translate into meaningful reality, in an engineering sense.

Even in modern physical engineering, with all of our computer models and simulations, we still see engineers test the things that they create, often to destruction, to learn how accurate or not their simulations are. Empiricism is a vital aspect of engineering .

For readers who are not so interested in counting angels on the head of some semantic pin, why does this matter?

Unlike pure science, engineering is firmly rooted in the application of ideas to solving real-world problems. I could easily decide that I needed to achieve some goal of architectural purity or some performance target that required me to invent and explore new techniques in software, but unless these ideas are realized in some tangible value and unless my software can do more things that matter or deliver new value, they are irrelevant, however much I experimented with them.

  1. Source: Wikipedia, https://en.wikipedia.org/wiki/Empiricism

Grounded in Reality

The other dimension to this is that our production systems will always surprise us, and they should! Ideally they will not surprise us too often in very bad ways, but any software system is really only the best guess, so far, of its developers. When we publish our software into production, this is, or should be, an opportunity to learn.

This is an important lesson that we can learn from science and engineering in other disciplines. One of the deeply important aspects of a scientific, rational approach to solving problems is the idea of skepticism. It doesn’t matter who has an idea, how much we would like the idea to be true, or how much work we have put into an idea; if the idea is bad, it is bad.

Evidence from looking at the impact of choices in software products suggests that, for the best companies, only a fraction of their ideas produce the effects that they predicted.

Features are built because teams believe they are useful, yet in many domains most ideas fail to improve key metrics. Only one third of the ideas tested at Microsoft improved the metric(s) they were designed to improve. 2

Empiricism, making decisions based on evidence and observations of reality, is vital to making sensible progress. Without that analysis and reflection, organizations will continue to proceed on the basis of only guesswork and will continue to invest in ideas that lose them money or reputation.

Separating Empirical from Experimental

We can be empirical by using the information that we gather as part of our experiments to make decisions. We explore that aspect in the next chapter. We can also be empirical by observing the outcome of our ideas less formally. This is not a replacement for being experimental, but rather a way in which we can improve the quality of our characterization of the current situation at the point when we are thinking of our next experiments.

I am aware that in exploring the ideas of empiricism and experimentation separately, I am in danger of descending into the arcana of philosophy and etymology. This is not my intent, so let me illustrate why it is worth considering these two closely related ideas independently with practical examples.

“I Know That Bug!”

A few years ago, I had the fantastic experience of building one of the world’s highest-performance financial exchanges from scratch. It was during this period of my career that I started to take engineering thinking and discipline seriously in my approach to software development.

  1. In a paper titled “Online Experiments at Large Scale” (https://stanford.io/2LdjvmC), the authors describe how two- to three-thirds of ideas for changes to software produced zero or negative value for the organization that implemented them.

We were about to make a release into production when we found a serious bug. This was a relatively unusual occurrence for us. This team employed the disciplines described in this book, including continuous delivery, so we had constant feedback on the continual flow of small changes. We rarely found big problems this late in the day.

Our release candidate was undergoing final checks before release. Earlier in the day, one of our colleagues, Darren, told us at stand-up that he had seen a weird messaging failure on his development workstation when running our suite of API acceptance tests. He had apparently seen a thread that was blocked in our underlying third-party pub-sub messaging code. He tried to reproduce it, and could, but he could do so on only one particular pairing station. That was weird, because the configuration of our environments was wholly automated and version controlled using a fairly sophisticated infrastructure-as-code approach.

Later that afternoon, we had started work on the next set of changes. Almost immediately, our build grid showed a dramatic change with lots of acceptance tests failing. We started exploring what was happening and noticed that one of our services was showing a very high CPU load. This was unusual because our software was generally extremely efficient. On further investigation, we noticed that our new messaging code was apparently stuck. This must be what Darren had seen. Clearly, we had a problem with our new messaging code!

We reacted immediately. We told everyone that the release candidate may not be ready for release. We started thinking that we may have to take a branch, something that we generally tried to avoid, and back out our messaging changes.

We did all this before we stopped and thought about it. “Hang on, this doesn’t make any sense; we have been running this code for more than a week, and we have now seen this failure three times in a couple of hours.”

We stopped and talked through what we knew; we collected our facts. We had upgraded the messaging at the start of the iteration, and we had a thread dump that showed the messaging stalled; so had Darren, but his dump looked stalled in a different place. We had been running all of these tests in our deployment pipeline repeatedly and successfully for more than a week, with the messaging changes.

At this point we were stuck. Our hypothesis, failing messaging, didn’t fit the facts. We needed more facts so that we could build a new hypothesis. We started again, where we would usually start solving a problem but had omitted to on this occasion because the conclusion had looked so obvious. We characterized our problem, so we started gathering data to tell the story. We looked at the log files and found, as you may have guessed, an exception that clearly pointed the finger at some brand new code.

Long story short: the messaging was fine. The apparent “messaging problem” was a symptom, not a cause. We were actually looking at a thread dump that was in a normal waiting state and working as it should. What had happened was that we had been hit by a threading bug in some new code, unrelated to messaging. It was an obvious, simple fix, and we would have found it in five minutes with no fuss if we hadn’t jumped to the conclusion that it was a messaging problem; in fact, we did fix it in five minutes once we stopped to think and built our hypothesis based on the facts that we had, rather than jumping to some wrong but apparently “obvious” conclusion.

It was only when we stopped and listed the facts of what we were seeing that we realized the conclusions we had jumped to really didn’t fit those facts. It was this and this alone that prompted us to go and gather more facts—enough to solve the problem we had, rather than the problem we imagined we had.

We had a sophisticated automated test system, and yet we ignored the obvious. It was obvious we must have committed something that broke the build. Instead, we joined together various facts and jumped to the wrong conclusion because there was a sequence of events that led us down the wrong path. We built a theory on sand, not validating as we went, but building new guesses on top of old. It created an initially plausible, seemingly “obvious” cause, except that it was completely wrong.

Science works! Make a hypothesis. Figure out how to prove or disprove it. Carry out the experiment. Observe the results and see they match your hypothesis. Repeat!

The lesson here is that being empirical is more complex than it looks and takes more discipline to achieve. You could imagine that when we correlated the problem Darren had seen with the failing tests we were being empirical and reacting to the messages reality was sending us. However, we weren’t. We were jumping to conclusions and skewing the facts to fit our preferred guess at what was going wrong. If at that point we had simply walked through “what we knew” in a more organized way, it would have been completely obvious that this wasn’t a “messaging problem” because our messaging changes had been working all week and hadn’t changed since they had been working.

Avoiding Self-Deception

Being empirical requires us to be more organized in how we assemble the signals we gather from reality and assemble them into theories that we can test through experimentation.

Human beings are remarkable, but being as smart as we are takes an enormous amount of processing. Our perception of reality is not “reality,” and we have a series of biological tricks to make our perception of reality appear to be seamless. For example, our visual sampling rate is surprisingly slow. The smoothness of your perception of reality, as gathered through your eyes, is an illusion created by your brain. In reality, your eyes sample a small patch of your visual field, scanning at a rate of roughly once every couple of seconds, and your brain creates a “virtual reality” impression of what is really going on.

Most of what you see is a guess made up by your brain. This matters because we have evolved to fool ourselves. We jump to conclusions, now because if we had taken the time to do a detailed accurate analysis of our visual field back in the days when we were fighting for survival, we would have been eaten by a predator before we had finished.

We have all sorts of cognitive shortcuts and biases that we have evolved over millions of years to allow us to survive in the real world. However, in the world that we have created, our modern high-tech civilization has taken the place of the dangerous savanna populated by predators, and we have developed a more effective way to solve problems. It is slower than jumping to often wrong conclusions, but it is dramatically more effective at solving problems—sometimes even staggeringly hard problems. Richard Feynman famously characterized science as follows:

The first principle is that you must not fool yourself – and you are the easiest person to fool. 3

Science is not what most people think it is. It is not about large hadron colliders or modern medicine or even physics. Science is a problem-solving technique. We create a model of the problem before us, and we check to see if everything that we currently know fits the model. We then try to think of ways in which we can prove the model is wrong. David Deutsch says that the model is composed of “good explanations.” 4

Inventing a Reality to Suit Our Argument

Let’s look at another example of how easily we can fool ourselves.

While we were building our super-fast exchange, 5 we did a lot of experimentation with the creation of very fast software. We discovered, through experiment, lots of interesting things. Most notable was an approach to software design that we dubbed mechanical sympathy .

In this approach, we designed our code based on a fairly deep understanding of how the underlying hardware worked so that we could take advantage of it. One of several important lessons we learned, through experimentation, was that once you had eliminated dumb mistakes, 6 the most significant impact on the raw performance of a piece of code in a modern computer was a cache-miss.

Avoiding cache-misses came to dominate our approach to design for the seriously high-performance parts of our code.

One of the most common causes of a cache-miss for most systems that we found by measurement was concurrency.

When we were building our exchange, a common idea in the software industry, the received wisdom at the time ran something like this: “Hardware is approaching physical limits that mean that CPU speed is no longer increasing. So our designs will have to ‘go parallel’ to keep them performing well.”

There were academic papers on this topic and languages specifically designed to make parallel programing easier and more pervasive in solving everyday programming problems. In reality, there is a lot wrong with this model, as we demonstrated, but for the purpose of this story I will look at only one aspect. There was an academic language being talked about at the time that aimed to automatically parallelize solutions. 7

  1. Nobel prize–winning physicist, Richard Feynman (1918–1988), https://bit.ly/2PLfEU3

  2. The Beginning of Infinity” by David Deutsch, https://amzn.to/2IyY553

  3. Read more about the innovative architecture of our exchange here: https://bit.ly/3a48mS3 .

  4. The most common performance mistake is to use the wrong kind of data structure to store something. Many developers do not consider the time of retrieval of different kinds of collections.

For small collection sizes, a simple array (O(n) on retrieval) may be faster than something like a hash table (with O(1) semantics). For larger collections, the O(1) solution will be best for random access. After that, the implementation of the collections can start to have a cost.

A demonstration of the power of this language was shown by processing the text of a book to parse out words from the stream of characters. Given our experience and our belief in the large costs of concurrency, at least when the problem demands that we combine the results from the different concurrent threads of execution, we were skeptical.

We didn’t have access to the academic language, but one of my colleagues, Mike Barker, did a simple experiment. He implemented the same algorithm that the language academics were describing in Scala and a simple, brute-force approach in Java and then measured the results by processing the text of Lewis Carol’s Alice in Wonderland over a series of runs.

The concurrent Scala algorithm was implemented in 61 lines of code; the Java version took 33. The Scala version could process an impressive-sounding 400 copies of the book per second. That’s impressive until you compare it to the simpler, easier to read, single-threaded code in Java that could process 1,600 copies per second.

The language researchers had started with a theory—that parallelism was the answer—but they had gotten so caught up in an implementation that they never thought to test their starting premise, which was that this would result in a faster outcome. It resulted in a slower outcome and more complex code.

Separating Myth from Reality: Example

It is understood that CPU development has reached a limit and that ever-increasing clock-cycle speedups have paused. Clock cycles have not increased since around 2005! There are good reasons for this, based on the physics of making transistors out of silicon. There is a relationship between the density of the transistors and the heat that they generate in operation. Building a chip that goes much faster than 3GHz means that overheating becomes a serious problem.

So if we can’t get speed gains by increasing the rate at which we process instructions linearly in our CPUs, we can parallelize, and the processor manufacturers have. This is good: modern processors are marvelous devices, but how do we use all that power? We can do work in parallel!

This is fine for running unconnected, independent processes, but what if you want to build a fast algorithm? The obvious conclusion (guess) is that it is inevitable that the solution to this problem is to parallelize our algorithms. In essence, the idea here is that we can speed things up by throwing more threads of execution at the problems that we tackle.

There have been several general-purpose programming languages built upon this assumption to help us to more effectively write parallel solutions to problems.

  1. Presentation outlining automatic parallelization: https://bit.ly/35JPqVs

Unfortunately, this is a much more complex problem than it looks. For some unusual tasks, parallel execution is the answer. However, as soon as there is any need to bring the information from those different threads of execution back together again, the picture changes.

Let us gather some feedback. Instead of jumping to the conclusion that parallelizing things is the answer, let us gather some data.

We could try something simple. For example, let’s write a trivially simple algorithm to increment a simple integer 500 million times.

Without any feedback, it seems obvious that we could throw lots of threads at this problem. However, when you carry out this experiment and gather the data (feedback), the results may surprise you:

Method

Time (ms)

Single thread

300

Single thread with lock

10,000

Two threads with lock

224,000

Single thread with CAS

5,700

Two threads with CAS

30,000

The table shows the result of this experiment carried out using different approaches. First, the baseline test. Write the code on a single thread and increment a long value. It takes 300 ms to get to 500 million.

As soon as we introduce the code to synchronize things, we start to see some costs that we hadn’t anticipated (unless we are low-level concurrency experts). If we still do all the work on a single thread but add a lock to allow the results to be used from a different thread, it adds 9,700 ms to the cost. Locks are extremely expensive!

If we decide to divide up the work between only two threads and synchronize their results, it is 746 times slower than doing the work on a single thread!

So locks are extremely expensive. There are more difficult to use, but more efficient, ways to coordinate the work between threads. The most efficient way to do this is a low-level concurrency approach called compare-and-swap (CAS). Sadly, even this approach is 100 times slower than work on a single thread.

Based on this feedback, we can make more informed, evidence-based decisions. If we want to maximize the rate at which an algorithm makes progress, we should try to keep as much work as possible on a single thread, unless we can make progress and never join the results back together again.

(This experiment was first carried out by Mike Barker when we worked together a few years ago.)

The example in the preceding sidebar is a demonstration of several of the concepts at the heart of this book. It demonstrates the importance of feedback, experimentation, and empiricism.

Guided by Reality

The researchers in this scenario were acting with good intent, but they had fallen into the trap that is pervasive, outside of the realms of science and engineering: they had come up with a guess to solve the problem and then rushed ahead to implement their guess without checking first to see if their guess was right or wrong.

It took Mike a few hours of coding, using the researcher’s own sample problem, to show that their assumed solution didn’t make sense. Being skeptical and checking our ideas is work, but it is the only way to make real progress rather than proceeding on guesswork, supposition, and hubris.

The best way to start is to assume that what you know, and what you think, is probably wrong and then figure out how you could find out how it is wrong .

The programming-language academics in this story had bought into a myth that was not grounded in reality. They had built their model for parallelizing programming languages, because it was a cool problem to solve if you were a language academic.

Unfortunately, this did not take into account the costs of parallelism; they had ignored the reality of modern computer hardware and computer science. It has long been understood that parallelism costs when you need to “join results back together.” Amdahl’s law shows that there is a harsh limit to the number of concurrent operations that make sense, unless they are wholly independent of one another.

The academics assumed that “more parallelism is good,” but that is an idea that is based on some kind of imaginary, theoretical machine, where the costs of concurrency were low; such machines don’t exist.

These academics were not being empirical, though they were being experimental. This lack of empiricism meant that their experiments were the wrong experiments, so the model that they built did not match real-world experience.

Empiricism is the mechanism through which we can sense-check the validity of our experiments. It helps us to place them into context and, in effect, test the validity of the simulations of reality at the heart of our experiments.

Summary

Engineering rather than pure science demands of us some consideration of the practicality of our solutions. This is really where empiricism comes into play. It is not enough to look only at the world, make guesses based on what we see, and then assume that our guesses must be correct because we got the information that informed them from the real world. That is poor science and poor engineering. However, engineering is a practical discipline. So we must, continually, be skeptical about our guesses, and the experiments that we create to test them, and check them against our experience of reality.