What the web should really look like, or: what we might learn from cryptocoins and accountants.
Given any system, anytime, anywhere, state is a derivation of some sequence of events. Always. It is like that in the real world, as we already know that events can never be exactly reproduced without replaying them in pure isolation from whatever else that might influence it. In practice, we just try to marginalize the outer effects when trying to reproduce something. This is true for all social science as well as quantum physics and everything in between. In other words: what we see, what we observe is a series of facts, strung together by our minds as a series of events. Many philosophers have argued that causation in itself is a product of our mind. Especially Leibniz has some interesting thoughts about this. Suffice to say that a common view on causation is that the series of facts are empirically witnessed, but that any connection between these facts is a mere product of our mind: we construe causality in our minds1.
State, on the other hand, is whatever "is" at a given moment in time. A photograph, for example, is more or less a record of the state all objects were in at that point in time. Or, you could pause a video and observe this state of whatever was filmed. But state is not what's important. Whatever is happening is important. Or, even better: whatever caused things to happen, and what actions people take might cause other things to happen. John picks up an axe. Will he murder again? Rita looks away. Will she love him ever again? Stuff like that.
Back to the digital world. We have known for a long time that state is complicated. Because if some object holds state, it means that that object can change and we must be very keen on trusting the object to either be responsible for it's own state by receiving messages or events or being told by other objects that it's state should change. This means that there is always a question of "responsibility". This effectuated principles such as Law of Demeter / Principle of Least Knowledge, Separation of Concerns and the Single Responsibility Principle. In object oriented design, we tried to emphasize this aspect with stuff like Design by Contract and Encapsulation.
Lately we found out that it's good to do Composition over inheritance, moving from complex software architecture to be more "Pure", which means: less "Stateful". In other words: we decided that Declarative is the new black. Yeah. That last one is mine. From a few years back. Ever since that post, people start picking up on it everywhere. Well, no... not really. Well, they do, but it had nothing to do with my influence on the world's events. Or .... did it? Let's be reasonable. It probably didn't ;) But then again: causality is not something we can be certain about; we can only guess. So my truth is that everyone is starting to pick up on Scala and Haskell. Because I mentioned it. Prove me wrong. ;)
Fact of the matter is: we turned the world inside out. We thought that state was the thing that was real and should be captured and displayed. We wanted to show € 40,- as our account balance. We didn't bother with thinking about how it got to that amount. Whenever we heard "subtract 20,-", we just did. That is what we did while managing state. If you're lucky and there was time, we kept a log. The log hopefully did it's job, when things go wrong. If we're lucky, we can reproduce what happened.
When I talk about this in terms of account balance, you're surely thinking: that's ridiculous. I would never build it that way. No bank would work that way. Finance doesn't work that way.
True. Why not? Because it needs to be correct. Any time, all the time. Correct and validated. There you go. Whenever a change is initiated for an account balance, the change is recorded and validated. Same for cryptocoins. All cryptocurrencies work that way. It is a big long event log of things that changed. And everyone is able to validate the history of events. This is not a log you keep when you're travelling, forgetting to write down most things of what you have seen. It is a truthful representation of things that happened, that we can play back. We can go back to square one, repeat all events, and the end result will be the same.
This is what event sourcing is. And this is why declarative is the new black. And why state is evil. And why caching is ugly and unreliable. State is the result of whatever happened. A mere snapshot. A photograph. Not reality. Reality is whatever happens and has led to a certain state. State is whatever is observed at a point in time. That's why state is true only for whoever watches.
It can not be a coincidence that ephemeral is a word that pops up more than often lately. We want to be able to replay things. We want Event Sourcing, Immutable acyclic directed graphs as the backbone of our vcs (ahem, git
, ahem), binlogs, Dockerfiles, Makefiles, declarative builds. State is not interesting. How we ended up at a state is interesting. And, it only comes to mean something whenever someone is watching, or, when it starts to have an effect on anything outside it's being. And that's why the observer effect is probably one of the most complex things to study right now. As long as no-one watches, things are simple. They, as you probably know, can be2 in all thinkable states anyway, right? ;)
So, without observing state, or even better: construing state for the purpose of observing facts, the facts don't mean anything. At least, not to the observer.
There are two lessons here:
- If you say that state is a single source of truth 3, think again. State is a representation of truth. The source of truth is events following a trusted starting state.
- If you keep state somewhere, can you be sure that is a result of an event? If not, either you should rely on a starting point (trusted state) or disregard it.
In other words: avoid state wherever you can. Be declarative. Go functional. It is the path to truth. No matter which direction in time you replay it. There is no fake news if you have the facts that led to a certain state. There is only history. Recorded and validated. And acted upon.
Unfortunately, we cannot reconstruct history this way. Ultimately we would end up at some point that we have to say: this is how things were at a certain point in time and trust that. We've already learned this from Gödel.
But let's at least try to not screw things up while we're watching ;)