Systemsfor Aravinda
0 / 78 core
Week 12 · Production stories

Uber Schemaless and Ringpop

1 hours · core path

Read first: Partitioning and sharding, Replication

In one sitting

Schemaless and Ringpop solve different problems and should not be drawn as one system. Schemaless is an append-only map of immutable cells on MySQL, 4096 shards in a typical configuration, a master and two minions, hinted handoff when the master is down, and idempotent writes. Ringpop is a Node.js library that uses SWIM-style gossip and a consistent-hash ring so application workers agree, approximately, on who owns a key.

Uber published two systems that interviews tend to glue together. Schemaless is the trip store. Ringpop is a library the application tier uses to agree on who owns a key. One of them stores trips in MySQL. The other one does not store trips at all.

The problem

The trips table in Postgres had become so painful to migrate that adding a column was an outage. A trip accumulates fields as the product changes. A schema change that rewrites that table took the product down with it. They wanted a store that would accept a new field without an alter, keep the old value, and still sit on a database they already knew how to operate.

That problem is about data. A second, separate problem is about the application tier. Many worker processes need a fast answer to “which of us owns this key,” and they did not want a coordination service in the path of that answer. Membership changes. Nodes die. The answer has to update without a round trip to a consensus cluster.

Those are different problems. The first one produced Schemaless. The second one produced Ringpop. A diagram that uses Ringpop to shard MySQL, or Schemaless to gossip membership, has merged them.

The design

Schemaless

Schemaless is an append-only map of immutable cells. A cell is identified by a row key, a column name, and a ref key. The payload is JSON. A new version of a value is a new cell with a higher ref key. The old cell stays. The store does not enforce a schema. A new column name is just a new cell. Nothing in MySQL has to be altered for the field to exist, which is the property they built the system to get.

The column name in a walkthrough has to be concrete, so call one of them status and treat that name as an example, not as Uber’s schema. A trip’s row key might be the trip id. The first status cell is (trip id, status, ref 1) and its JSON is the first document. An update writes (trip id, status, ref 2) with the new JSON. Reading the latest status is reading the cell with the highest ref key for that row and that column. Ref 1 is still there. Append-only means you do not overwrite it.

Because the store does not check the schema, it also will not save you from a misspelled field. The JSON is whatever the writer sent. That is the cost of making “add a column” stop being an outage.

Storage is MySQL. A typical configuration is 4096 shards. The writeup calls 4096 typical, not a law. It is also 2^12, which makes the integer easy to split in half repeatedly. That is arithmetic, not a claim about why they picked it. Each shard has a master and two minions, and the minions are spread across data centers. Count the roles before you count machines. These notes do not say one role per machine.

typical configuration, database roles, not machines
4096 shards
each shard has 1 master + 2 minions = 3 roles
4096 * 3 = 12288 roles

Replication to the minions is asynchronous. The master can acknowledge a write before either minion has it. Reads default to the master, so a client sees its write. That is read-your-writes on the happy path: the read goes to the place that took the write. A read of a minion can miss that write for as long as replication is behind. The default exists so the common read does not do that.

Asynchronous replication can lose the tail if the master dies after acknowledging a write and before a minion has stored it. That is the general rule for asynchronous replicas, not a published Uber incident. These notes do not describe a specific tail-loss outage. Say the consequence of the mechanism. Do not narrate an outage you do not have.

If the master is down, writes can be accepted onto another node and handed off later. That is the same idea as hinted handoff: a fallback node takes the write so the write still succeeds, and the write is sent home when the preferred node is back. Until the handoff lands, the write is on the fallback, not on the master. Availability of the write goes up. The guarantee that “the master has it” waits. Schemaless is still a master and two minions on MySQL shards. It is a different system from the leaderless quorum store in the Dynamo paper, which uses hinted handoff as one technique among several. Same idea. Different architecture. Do not draw preference lists on the trip store.

Worker nodes speak HTTP. They fan out and they aggregate. A request whose row keys land on several shards is split by a worker, sent to those shards, and combined on the way back. The client talks to the worker. The worker talks to MySQL.

Writes are idempotent, so the client library can retry. A cell’s identity is the row key, the column name, and the ref key. Resending that same identity is the same cell. A higher ref key is a new version on purpose. A retry that minted a fresh ref would be a new version, not a retry. The idempotent retry resends the cell the client already chose.

Workers use a circuit breaker against a sick storage node. Closed, the calls go through. Open, the calls fail immediately for a cool-down, so a shard that is already stuck does not hold the worker’s fan-out. Half-open, a few trial calls check whether it has recovered. These notes do not publish the cool-down or the failure threshold. Name the three states. Do not invent the numbers.

Secondary indexes are eventually consistent. The cell write is on the master, and a read of that cell from the master sees it. A query that finds the trip through a secondary index can still miss it. The index catches up later. If the next screen must show the write, it reads the cell from the master, not the index.

flowchart LR
  client[Trip client]
  worker[Schemaless worker]
  master[MySQL shard master]
  minionA[Minion]
  minionB[Second minion]
  client --> worker
  worker --> master
  master -->|async replication| minionA
  master -->|async replication| minionB

Ringpop

Ringpop is a Node.js library for the application tier. It is not a database. It does not store the trip cell, and it does not replace the 4096 MySQL shards.

It uses a SWIM-style gossip so members notice joins and deaths. A member pings a peer. If the ping fails, it sends a ping-req: it asks some other member to ping the suspect. A failed direct ping can be a broken link on the asker. A failed ping from someone else is evidence the suspect is actually unreachable. Joins spread by the same gossip. Membership is something the processes tell each other, not a row in a coordination service.

On top of that membership it keeps a consistent-hash ring with virtual nodes. FarmHash is the hash. A red-black tree holds the ring, so the owner of a key is the successor of the key’s hash in an ordered tree, rather than a scan of every member. Virtual nodes are there so the ring stays balanced. The property you can quote, because it is the property of consistent hashing, is that adding a node moves about 1/N of the keys, where N is the number of nodes. These notes do not publish Ringpop’s N or its virtual-node count. Do not borrow the 4096 from Schemaless and stick it on the ring. That number is a shard count for MySQL.

The point of the library is that the workers agree, approximately and quickly, on who owns a key, without a separate coordination service for that decision. Approximately, because gossip takes time to spread. Two workers can disagree on the owner until the membership change reaches both of them. These notes do not publish that window. Say it exists. Quickly, relative to calling out to a consensus service on every lookup. A consensus service would give you one owner and a round trip. Ringpop gives you a local computation on a membership set that is allowed to be briefly stale. That is the trade, and it is an application-tier trade.

flowchart LR
  nodeA[App node]
  nodeB[Peer app node]
  ring[Hash ring in the process]
  nodeA -->|ping| nodeB
  nodeA -->|ping-req| nodeB
  nodeA --> ring

Keep the boxes apart

Schemaless Ringpop
Job Trip store Who owns a key in the app tier
What is stored Immutable JSON cells in MySQL Membership and a hash ring
Write path Append a cell on the shard master There is no trip write
Copies Master and two minions, minions across data centers, async The ring is computed in each process
When the preferred node is down Accept the write elsewhere, hand it off later Gossip notices the death, the ring moves the key
What the client can rely on Read the master to see the write; secondary indexes lag Agreement is approximate until gossip arrives
Coordination The shard master No separate coordination service

A sentence that fails a design review sounds like this: “Ringpop shards the trips across 4096 MySQL nodes with SWIM.” Schemaless has the shards, the MySQL, the master, and the minions. Ringpop has the SWIM gossip and the ring. The worker that fans out across shards is a Schemaless worker. The app node that pings its peers is a Ringpop member. They can live in one company and still be two drawings.

What they traded

Choice What they gained What they spent
JSON cells, no schema enforcement A new field is a new cell, not an alter of the trips table The store will not reject a bad or unknown field
Append a higher ref, keep the old cell History of a value stays, and a retry of the same cell is the same write Readers must pick the highest ref, and old cells remain until something else deletes them
Async minions, reads on the master The master acknowledges without waiting, and the client sees its write A minion read can lag, and a master crash can lose the tail that never reached a minion
Hinted handoff when the master is down The write is still accepted Until the handoff lands, the write is not on the master
Eventually consistent secondary indexes The cell write does not wait for the index A lookup by a non-primary field can miss a write the master already has
Gossip plus a hash ring, no consensus service Workers decide ownership locally and quickly Two workers can briefly disagree on the owner

The third and fourth rows are easy to collapse into “the write is durable.” It is durable on the master once the master has it, and the client sees it because the client reads the master. It is durable on a minion only after asynchronous replication. It is durable on a fallback only as the fallback’s copy until hinted handoff finishes. Say which node has the bytes. “The write succeeded” is not yet “three copies have it.”

These notes do not describe garbage collection of old cells. Do not add a compaction story. The published fact is that a new version is a new cell with a higher ref key.

What to steal

Steal the diagnosis before you steal the store. If adding a column to the hot table is an outage, stop rewriting that table in place. An append-only versioned cell is one answer. A boring expand-and-contract migration on an ordinary table is the smaller answer, and it is the one a five-person team should reach for first. Schemaless exists because the trips table had already lost that fight. Your table has not, until it has.

Steal read-from-primary when the next screen must show the write. Steal an idempotent write so the client library can retry the same cell instead of applying a second version. Steal a breaker in front of a shard that is already sick, so one fan-out does not spend every worker. Steal the habit of saying the secondary index lags.

If the application tier needs “who owns this key” and you have decided not to run a coordination service for that lookup, a consistent-hash ring plus a membership gossip is a known shape. Say the agreement is approximate. Say FarmHash and a tree only if you are retelling Ringpop. In your own design, “consistent hash with virtual nodes” is the whole sentence, and adding a node moves about 1/N of the keys.

Leave the fleet. Four thousand ninety-six shards and three roles each is 12,288 database roles in a typical configuration of their trip store. You start with one master. You pick the shard key, the row key, before you pick a shard count. You do not adopt Ringpop as a database, and you do not drop schemas because a company with this trips table dropped schema enforcement inside this store. The JSON cell is a response to an alter that was taking them down. A misspelled field with no database to catch it is the bill, and a five-person team pays that bill on the first week if it copies the store and not the pain.

In five minutes, draw two boxes with air between them. Schemaless: cell equals row key, column name, ref key; higher ref is a new cell; MySQL; 4096 shards; master plus two minions; read the master; hinted handoff; idempotent retry; breaker; indexes lag. Ringpop: SWIM ping and ping-req; consistent-hash ring; virtual nodes; FarmHash; red-black tree; approximate agreement; not a database. If the listener merges them, split them again before you talk about anything else.

Check yourself

What three values identify a Schemaless cell, and what does a new version write?

A row key, a column name, and a ref key. The payload is JSON, and the store does not enforce a schema. A new version is a new cell with a higher ref key. The previous cell remains. Reading the latest value is reading the cell with the highest ref for that row and column. A retry is idempotent when it resends that same identity. A higher ref is a different version on purpose.

Why do Schemaless reads default to the master, and what can a read of a minion miss?

Replication to the minions is asynchronous, so a minion can be behind a write the master has already acknowledged. The default read goes to the master so the client sees its write. A minion read can return the previous cell for as long as that lag lasts. If the master dies before a minion stored the tail, asynchronous replication can lose that tail. That is the general rule. It is not a specific Uber incident in these notes.

The master for a shard is down. Where can a write go, and what is true of that write before handoff finishes?

The write can be accepted onto another node and handed off later, the same idea as hinted handoff. The client is not refused. Until the handoff lands, the write is on the fallback node rather than on the master. A later read that assumes the master already has it is assuming the handoff has finished. The two minions and the async replication are the steady-state copies. The fallback path is the master-down path.

A teammate says Ringpop shards trips across 4096 MySQL nodes using SWIM. Which facts did they merge, and how do you split them?

4096 is a typical Schemaless shard count. Each of those shards is MySQL, with a master and two minions. SWIM-style gossip, ping and ping-req, is how Ringpop members notice joins and deaths. The ring, virtual nodes, FarmHash, and the red-black tree are how an application process finds the owner of a key. Ringpop is not the trip store and it is not those MySQL shards. Say the two names, then say which box stores the cell.

A Ringpop ping to a peer fails. What is ping-req for, and what can two workers still disagree about?

Ping-req asks another member to ping the suspect. A single failed ping might be the asker’s own broken link. A failure from a second member is how the group decides the suspect is dead. Joins spread by the same gossip. Until that gossip reaches everyone, two workers can disagree on membership and therefore on who owns a key. The agreement is approximate and fast. It is not the outcome of a consensus round trip, which is the service they chose not to put on this lookup.

Based on: Uber Engineering writeup of Schemaless; Uber Engineering writeup of Ringpop.