Backend engineers can’t write elegant and performant application-layer code for many types of everyday business logic. That’s because no one has yet invented a “denormalization engine”, a database with a more general kind of indexer.
To see what I mean, let’s dive into an example application and talk about its data architecture: how it works today vs. how we’d ideally want it to work.
Example: A messaging application
Say we want to build a messaging application with various conversation “rooms”. Basically a clone of Facebook Messenger, which has one room per person you’re chatting with:
Facebook Messenger clone showing 7 rooms (2 unread, 5 read) Notice that in this screenshot, there are two unread rooms. We’ll define an unread room (with respect to the current logged-in user) as “a room whose last message’s timestamp is greater than the timestamp when the current user last viewed it”.
Naturally, we want to be able to render a UI like this:
Hm, where did this “2” come from? How do we architect the calculation of the number of unread rooms? It seems like a straightforward problem, an example of what I mean by “everyday business logic”.
We’ll consider two architectural approaches for counting unread rooms: the normalized approach and the denormalized approach.
The normalized approach
When building an application, there’s always a core schema of data types and fields that we absolutely need in our database. That’s the normalized schema for our application.
Designing a normalized schema is largely product-definition work, not engineering implementation work. That’s because the set of possible values for the data in a normalized schema corresponds to the set of possible states that your application can meaningfully be in.
Roughly, the space of possible data values that constitute valid entries into your normalized schema, is supposed to represent the space of meaningful states that your application can be in. Good programmers put a lot of thought into designing their application’s normalized schema.
In our messaging app’s normalized schema, there has to be a User type (a.k.a. User table, User collection) and a Room type. There also has to be a Message type, which looks like what you’d expect:
And there has to be a RoomUser type, which stores facts about pairs, such as the last time user “ liron-shapira” (yours truly) saw the inside of room “r31” (conversation with Sasha Rosse):
Notice that the aforementioned timestamp, 1310 (don’t try to make sense of these fake timestamp numbers), is before Sasha Rosse sent me a message containing the content “Perfect!” This is how the application knows that room r31 is an unread room, and how the UI decides to bold its fonts here: