# Aurora DSQL, and the clock doing the hard part

> AWS previewed a distributed SQL database at re:Invent with active-active multi-region writes and strong consistency. The mechanism underneath is a hardware clock in every instance.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2024-12-14
- Tags: AWS, Aurora DSQL, PostgreSQL, Distributed Systems, Databases
- URL: https://deyanpeev.com/posts/aurora-dsql-and-the-clock

Amazon Aurora DSQL was announced in preview at re:Invent on the third of December.
It's a serverless distributed SQL database, PostgreSQL-compatible, with active-active
availability across regions and strong consistency.

Pretty much every word in that description is a claim that has historically come
with a catch. Two regions both accepting writes usually means eventual consistency
and a conflict resolution strategy you have to reason about. So the question is how
DSQL does it, and the answer has to do with time.

# Ordering events across regions

Two regions, both taking writes, and every reader seeing a consistent story. The
obstacle is agreeing on the order of events that happened in different places.

If Frankfurt and Virginia both commit a transaction at what each believes is the
same moment, something has to decide which came first. The usual answers are all
unpleasant. Send every write through one region, and you've given up on
active-active and added a round trip. Let both proceed and reconcile afterwards and
strong consistency is gone, plus application developers now have a conflict problem
to deal with. Or you run a consensus protocol across regions for every transaction,
and pay for the correctness with latency.

It's hard because computers don't agree on the time. Clock drift means two
timestamps from two machines cannot be compared, so ordering has to be established
by communication instead, and communication between regions is expensive.

# What DSQL does instead

It makes the clocks trustworthy. The Amazon Time Sync Service provides a hardware
reference clock to EC2 instances, accurate enough that the uncertainty between two
machines is bounded and small. Once you can put a number on how wrong a timestamp
might be, you can use timestamps for ordering. If two events are further apart than
the uncertainty, their order is known without anybody having to ask.

A transaction looks different after that. Reads and writes proceed locally against a
snapshot, and the cross-region coordination happens only at commit, as a check for
conflicts against concurrent transactions. There's no negotiation for every
statement. The expensive part is paid once, at the end, and only by transactions
that need it.

This is the same family of idea as Spanner's TrueTime. Bounded clock uncertainty as a
primitive is a decade-old idea in the literature. The new bit is that it arrives as
a service you can call from a Lambda.

# Retries and long transactions

With any distributed database I want to know what it asks me to give up. Here its
mostly the transaction model and not the data model.

Optimistic concurrency at commit means a transaction can fail at the end because
something else touched the same rows. Application code has to be prepared to retry.
That's ordinary discipline if you've used serialisable isolation. It will be a
surprise if you have only ever used a single Postgres instance, where the lock waits
and the transaction simply takes longer.

Long transactions also get expensive in a new way. A transaction holding a snapshot
for a long time has a larger window in wich to conflict, so the "read-modify-write
across ten seconds of application logic" pattern that a single instance tolerates
becomes a retry generator.

And "PostgreSQL-compatible" has to be read carefully. It means the wire protocol and
a large amount of SQL. It does not mean that arbitrary Postgres features and
extensions are present. For a preview, the gap between "my client connects" and "my
schema runs unchanged" is what an evaluation should be measuring.

# Where I would and would not put it

The case it's built for is narrow. It's an application that needs to serve writes
from more than one region with real consistency, and that has so far solved it by
pinning writes to a primary region and accepting the latency, or by sharding by
geography and accepting the complexity.

For everything else, a single Aurora or RDS instance with a read replica is simpler
and cheaper and better understood, and the operational knowledge for it already
exists in every team. Distributed SQL answers a specific constraint, and most
applications don't have it (their databases fit on one machine).

I'm writing about it anyway because of the mechanism. Precise time as infrastructure
is easy to overlook until something is built on it. It has now made the same design
possible at two different companies, and both times a better measurement replaced a
cleverer algorithm.

This is a preview, so all of the above is me reading the design and I'm not
recommending anything yet. What I would test first is the retry behaviour under
contention, and how far "PostgreSQL-compatible" goes.
