3 places
Page 5 of 8 · Posts and friends

Posts, likes and friends work the same way in both models

Every write is local to its writer; a delete marker and a cleanup worker keep links right.

Scenario 1

Ana posts and Ben likes: every write is local to its writer

Ana's post has one owner: Ana. Ben's like has one owner: Ben. So both are shared rows, in both models.

Press play and watch where Ben's like is written:

Ben in US West likes Ana's post from Singapore
Ben's app
US West
US West database
Singapore database
Ana's app
Singapore

1/3US West checks its copy of the post, then writes Ben's like. About 0.3 ms.

Ben never waits for an ocean. His write lands at home, and the copy travels behind him.

A comment works the same way. The comment is Ben's row, written in US West.

Scenario 2

Ana deletes the post: a marker travels and workers clean up

Ben's like points at Ana's post across 2 regions. That is a weak ref. So the delete uses the 5 parts.

Watch the marker leave Singapore and the worker act in US West:

Ana deletes a post that Ben liked
Ana's app
Singapore
Singapore database
US West database
Cleanup worker
US West

1/4One transaction in Singapore removes the post and writes the delete marker.

In the test the post was clean in all 3 regions after 197 ms. Measured, simulated distance Of 200 likes that raced a delete, none stayed. Measured, simulated distance

Scenario 3

The like counter is the one social place where the models could differ

A counter is one number that many people change. Read literally, the 3-place rule sends it to the global database.

That would make every like from Singapore wait 187 ms. A like is the cheapest tap in the app. It should not cross an ocean.

The better way works in both models. Each region counts the like rows it already holds as copies.

Counting copies keeps the like local.
Way to count likesWait for the person who likesProblem
One counter row in the global databaseUp to 187 msSlow tap; many people fight over one row
Count the copied like rows in each region0.3 msThe count can lag by 0.1 seconds

Counting copies wins. This is guard rule 2 at work: a like has one owner, so it stays shared. Not tested

Scenario 4

A friendship is two own rows, one written by each person

Ben asks Ana to be friends. Nobody else writes to that friendship. So no global row is needed.

The steps are the same in both models:

  1. Ben asks
    US West writes Ben's request row. Ana sees the copy about 90 ms later.
  2. Ana accepts
    Singapore writes Ana's friend row. US West sees the copy and writes Ben's friend row.
  3. Ben unfriends
    US West deletes Ben's row and writes a delete marker. Singapore's worker removes Ana's row.
  4. Friends again
    A new request starts at step 1, even while the old marker still exists.

Each step is one local transaction. The link between the 2 rows is a weak ref.

The tests found one real fault here. A request sent in the first 0.1 seconds after an unfriend was lost, in 25 of 50 tries. Measured, simulated distance

The fix is small. The app answers "already friends" until the old row is gone. The caller then asks again. After the fix, 100 of 100 tries ended as friends. Measured, simulated distance