Numbers
Numbers have no physical existence. They are virtual. Humanity created numbers to manage the present and predict the future. We needed to count animals in the herd, days, harvest, and people in the tribe. Later we realized that with numbers we can predict the future.
By observing the motion of the planets, astronomers have calculated their orbits and can accurately predict eclipses centuries in advance. Meteorologists analyze temperature, pressure, humidity and predict the weather. Insurance companies calculate accident risks, doctors evaluate the effectiveness of treatments, and economists forecast market growth. The more data, the more accurate the forecast. But everything starts from measuring and counting.
Counting in everyday life
You wake up in the morning an hour before work starts. One hour is enough to have a calm morning routine, fry eggs and have breakfast. You go to the bathroom, squeeze toothpaste onto your brush. While you are cleaning your teeth roughly estimate how much toothpaste is left - not much. After the bathroom you go to the kitchen. You open notes on your smartphone on the way and add the twentieth item to the shopping list. The list is getting full. More importantly, there are only six eggs left in the fridge - enough for three breakfasts. You plan to go online shopping this evening. Today is Thursday and goods will be delivered on Saturday, in two days. Perfect timing.
I've just described five minutes of human life. It required measuring and counting six different things. And there were no issues. We are very good at counting. Numbers help us to manage everyday life, make it more predictable and comfortable. The same as it was thousands of years ago.
Problems begin in the evening when we go to shop online.
Counting online
The computer era and then the global network created a need to operate with very big numbers. Numbers still serve us and still make our life more predictable and comfortable, but we don't know them anymore. Computers do calculations for us. We are not able to count at the speed of billions of operations per second. It is beyond our natural abilities. We can't even imagine that process. It is too fast.
We've moved many everyday activities to online web services, messengers, online shops, and video calls. We get payments in pure numbers that arrive as another record in some database and are displayed in a mobile banking app. We pay numbers online to buy things. In other words, we partly live in virtuality.
The impression that the global network is huge is not wrong. It is huge. However, one online shop may be served by one real server in a data center. It is a relatively small box, smaller than a single shelf in an offline supermarket. Virtual parts of us, our motives and activities, and data from thousands of customers are placed into a small box full of microchips and advanced electrical components.
In the physical world, there is no way thousands of customers could go near a single shelf. In virtual reality, customers are put inside the sophisticated machine. It is a highly concurrent space. Activities of thousands of customers are handled inside. And this is a problem because at the end of the day we eat not numbers but eggs. Every number should represent a real pack of eggs waiting for a customer somewhere in a warehouse. A number can be copied in the computer memory, but the last pack of eggs can't be copied in a warehouse. Only one customer must be able to get the last pack.
When we go to an offline shop to buy eggs the process is straightforward. There are other customers in the supermarket. They also buy eggs. If you take the last pack of eggs from the shelf and put it in your basket, nobody else will be able to take it from the shelf anymore. The laws of reality naturally handle it.
Things change online. When thousands of customers shop online there is a big chance that two or three customers could take the same last pack of eggs. Poorly designed virtual shelves allow that mistake. Obviously, in the end only one customer would get the pack of eggs. Others would be frustrated by not receiving eggs during the next delivery.
This is an example of when counting must be implemented correctly to avoid race conditions. We need a bridge between virtuality and reality.
Adapter for virtuality
Virtuality is a model of reality. Virtuality consists of a huge amount of numbers. We change numbers, and laws of the model change. In virtuality, we can create as many eggs as we want and store them as a very-very big number that could be larger than the amount of atoms in the observable universe.
Our virtual reality is very convenient because it is fast, flexible and accessible just with a mobile app or browser. The problem begins when virtuality should be connected back to reality. We can't eat numbers, can we? We eat eggs that are limited in the real world.
This is where we should create a way to connect two worlds. Connect the concurrent and incredibly fast digital realm to limited reality. The solution lies in making our virtual counters work like reality does—one item, one customer, one transaction. We need a proper adapter. Let's start from a broken one.
Non-atomic counters
The number of egg packs in a warehouse is stored in computer memory. When two customers want to put the last pack of eggs in their basket, the computer should verify if the number is not zero, if there is at least one pack of eggs in a warehouse. Online shops can't sell eggs that don't physically exist.
How to verify eggs exist? Retrieve a number of packs from computer memory, subtract number one that represents a pack of eggs, and set the result back to computer memory. Easy, right? Not quite. Note that there is a time gap between getting a number and setting the changed number back. This is a potential issue.
There is a chance that another customer will get the same number from computer memory before the first customer's updated number is saved in computer memory. This causes race conditions. Both customers get a pack of virtual eggs represented as numbers, but in the real warehouse there is just one last pack. Unfortunately, one customer will not get eggs. Not a happy end.
Atomic counters
There is a simple fix that keeps all customers happy. While the first customer buys eggs, no other customers should be able to change the number in computer memory. The process should be non-divisible - atomic. In reality, one pack of eggs can't be placed in two different baskets at the same time. Virtuality should mirror this law.
When three customers simultaneously click the "Add to cart" button, the first customer should obtain an exclusive lock on the computer memory that stores the number of eggs, subtract one, set the changed value back to memory, and only after that release the lock. The other two customers must wait.
In essence, it creates a queue of customers waiting their turn to get exclusive access to the virtual shelf with eggs. This is how atomic counters properly bridge the gap between the digital and physical realms.
Top comments (0)