A global product stands or falls on its user experience, and four properties of an application shape that experience: performance and low latency, whether the app does what the user expects, security, and features and UI. The first two depend heavily on the database behind them, and once low latency becomes a hard requirement, that database is usually distributed.

Four pillars of user experience

Performance is user experience. A small increase in latency can lose the attention a visitor has just granted you.

Behaving as expected is equally basic: if I rename myself to ‘Robert’ and reload the application, my name should still be Robert, not Brecht. Whether an application can keep that promise is a property of its database. In the distributed space, only a subset of recent databases deliver such guarantees — there be dragons, unless the choice falls on a strongly consistent rather than an eventually consistent database.

Security rarely looks like a UX concern at first, but once users notice flaws, the damaged relationship can be beyond repair. Features and UI act on the conscious and unconscious mind, and people often want a product only after experiencing how it looks and feels. A database that saves time on setup and configuration leaves more effort for those features; some current databases need no configuration or server provisioning and ship with APIs such as GraphQL out of the box.

The quest for distribution

Content delivery networks

Once application code is close to optimal, network latency and the read/write performance of the database remain the bottlenecks. Lowering them means keeping data physically near the client — distributing it globally — and making multiple machines work together by replicating it.

One distribution model is already mainstream: the Content Delivery Network, which speeds up delivery of static data. The Jamstack community leans on CDNs and preassembles React/Angular/Vue front ends into static sites with tools such as Next.js/Now, Gatsby and Netlify.

CDNs fall short wherever static generation is not an option, notably for:

  • applications needing real-time updates for instantaneous user-to-user communication, such as chat, collaborative drawing or writing, and games;
  • applications presenting data in so many forms — filtered, aggregated, sorted, manipulated — that not everything can be generated in advance.

Distributed databases

Highly dynamic applications generally need a distributed database, which turns the single node into a global network: connect from a continent and you are routed to the closest node. Think of database employees waiting by a phone — the one you reach tells you a colleague is nearer and forwards the call, except a distributed database does this routing automatically, so you never bother the employee on the other continent.

Beyond latency, a multi-region database buys two more things. The first is redundancy: if one location were obliterated by a Godzilla attack, duplicates of your data survive elsewhere.

The second is scaling. A single server quickly becomes the bottleneck, whereas a distributed database replicates over multiple servers and scales up and down with demand; the most advanced offerings are “serverless”, handling scaling entirely for you, with billing based only on your application's usage.

Distributing dynamic data is harder than distributing static files. Data changes rapidly and is filtered and sorted, and early approaches to that complexity had to sacrifice performance or scalability. How the quest evolved is worth tracing.

How traditional databases distribute

Building on traditional databases (MySQL, PostgreSQL, SQL Server) was the logical move, given the effort invested in them — but they were never designed to be distributed and take a simple approach. Reads are scaled with read replicas: copies of the data that accept reads but not writes and offload queries from the node holding the original. Data is incrementally copied to replicas as it arrives.

The simplicity has a cost: replica data is always older than the original, so reading from a replica may return an older value than the primary would — a “stale read”. Programmers must know this and program around it. Read-what-you-write does not hold against replicas.

Optimistically applying write results on the front end before all replicas see them buys a slightly better experience, but a page reload may return the UI to an earlier state if the update has not reached the replica; the user then believes the change was never saved.

The first generation: horizontal writes and the NoSQL label

The obvious bottleneck of replication is that every write goes to the same node. That machine can be scaled up, but it hits a ceiling; as writes grow, the database eventually stops accepting new data fast enough. Distributing writes as well as reads requires a database that holds multiple copies and lets you write to each of them, which forces the nodes to communicate updates to one another — traffic that is no longer one-way as in the traditional system.

Such databases can still suffer stale reads and introduce further issues around writes; which of these bite depends on the availability-versus-consistency trade-off each database makes.

This first generation is commonly filed under the “NoSQL movement”, a name shaped by databases such as MongoDB and Neo4j, which also offered alternative query languages and different modeling strategies — documents or graphs instead of tables — often without traditional features such as constraints and joins. Over time it proved a terrible name, since many “NoSQL” databases did provide some form of SQL, and interpretations multiplied. NoSQL was read to mean databases that:

  • do not provide SQL as a query language;
  • do not provide only SQL (Not Only SQL);
  • do not provide typical traditional features such as joins, constraints and ACID guarantees;
  • model data differently (graph, document or temporal model).

Newer databases that are non-relational yet offer SQL were dubbed “NewSQL” to avoid the confusion.

CAP, and where vendors over-applied it

The first wave of distributed databases took its design cues directly from the CAP theorem: during a network partition — when two nodes can no longer exchange new data, for reasons ranging from cable faults to the well-documented case of sharks biting Google's submarine cables — you cannot have both Consistency and Availability. In CAP terms, Consistency means the data is always correct, though not necessarily reachable by your application. Availability means the database stays online and your application can always reach the data, but correctness is not guaranteed and nodes may not agree with one another. Because true 100% availability does not exist, the industry measures availability in nines (99.9999%, for instance), since some sequence of events will eventually cause downtime.

Visualization of the CAP theorem, a balance between consistency and availability in the event of a network partition. We generally speak of high availability since there is no such thing as 100% availability. 

The trouble came when vendors interpreted the theorem too broadly. They picked either potential data loss or availability as a permanent posture, regardless of whether a partition was actually happening. The theorem's framing also underemphasized a key point: high availability and consistency are both achievable when no partition exists — which is the normal state of affairs. The PACELC theorem fills that gap. Its final three letters stand for Else Latency Consistency, describing the trade-off during normal operation: in the absence of a partition, the database must balance latency against consistency.

According to the PACELC theorem, increased consistency results in higher latencies (during normal operation).

Put simply, stronger consistency guarantees raise latency when the network is healthy. Reality is subtler still, as the following sections show.

What weak consistency does to the interface

Consider an application that lets users assemble teams by dragging and dropping people between them.

Dropping a person into a team triggers an update. If the database cannot guarantee that the application will read the result of that update immediately, the UI must apply the change optimistically — and then things go wrong in predictable ways:

  • A page refresh makes the update vanish; another refresh brings it back.
  • The update conflicted with another write and was never stored. It may be silently dropped, and the user only discovers the loss on a later reload.

This tension has fuelled many arguments between front-end and back-end teams. Front-end developers want real feedback: once the user sees confirmation and acts on it, the change must be durably saved. Back-end developers want a scalable, performant system and saw no route to it other than sacrificing exactly those guarantees. Both sides were right, and there was no golden bullet. As transaction volume grew and the database became the bottleneck, the only options were traditional replication or a distributed database that traded strong consistency for eventual consistency. Under eventual consistency an update is applied on all machines eventually, but the next transaction is not guaranteed to see it: rename yourself to "Robert" and query immediately, and you may not get "Robert" back.

The Consistency Tax

Working around eventual consistency costs developers real effort. They must internalize the database's limitations, invent UX hacks to conceal them, and write extra backend layers to cover the various failure scenarios. Those workarounds have reshaped how both front- and back-end developers do their jobs, adding significant technical complexity without producing an ideal experience. We call the effort required to guarantee correctness on top of such a system the Consistency Tax: what an application pays for using software whose consistency guarantees do not hold up under today's webscale concurrency.

A second generation that skips the tax

A newer class of distributed databases offers strong consistency rather than eventual consistency. These systems scale, do not lose data, and do not return stale data — they behave as you would expect, so there is nothing to learn about their limits and no tax to pay. Write a value, read it back, and you always see the update; concurrent updates are applied in the same temporal order in which they were written. At the time of writing, FaunaDB, Spanner, and FoundationDB are the only databases offering strong consistency without limitations — a property also known as Strict serializability.

Revisiting PACELC

The second generation achieves what was long thought impossible: a preference for consistency alongside low latency. That is possible thanks to intelligent synchronization mechanisms such as Calvin, Spanner, and Percolator, covered in detail in article 4 of this series. Older databases still struggle to deliver high consistency at low latency; systems built on these algorithms do not share that limitation. Database design has a large influence on the latency attainable at high consistency. Since these algorithms make strong consistency and low latency compatible, there is usually no good reason to give up consistency — at least while no partition occurs. The one case for doing so: extremely low write latency is the only thing that matters and you accept losing data to get it.

intelligent algorithms result in strong consistency and relatively low latencies

Does "NoSQL" still fit?

Categorizing this new generation is no longer trivial. Plenty of effort still goes into explaining what NoSQL means, and none of the explanations hold up completely now that NoSQL and SQL systems are converging. New distributed databases borrow from multiple data models — Document, Graph, Relational, Temporal — and some offer ACID guarantees or even SQL support. What they retain from NoSQL is the motivation: solving the limits of traditional databases. No single label can capture a database's behavior. A more useful future description answers these questions:

  • Is it strongly consistent?
  • Does distribution rely on read-replicas, or is it truly distributed?
  • Which data models does it borrow from?
  • How expressive is the query language, and where are its limits?

Where this leaves us

Applications can now draw on a new generation of globally distributed databases that serve dynamic data from the nearest location, CDN-style. The road there was not smooth. First-generation databases made consistency choices driven largely by CAP, and those choices forced more code on developers while degrading the user experience. Only recently has the database community produced algorithms that let distribution combine low latency with strong consistency — an era in which data access and consistency no longer have to be traded against each other.

Concrete examples of what eventually consistent databases get wrong are the subject of the next article in this series:

  1. Why should you care?
  2. What can go wrong?
  3. What are the barriers to adoption?
  4. How do new algorithms help?
CSS-Tricks star logo

Sponsor CSS-Tricks!

We've got lots of ways to help you connect with the greatest front-end community in all the land.

CSS-Tricks Newsletter

Sign up to get front-end roundups, browser updates, and exclusive content sent to your inbox bi-weekly.

Ads are self-hosted