AlgoViz

Sharding & replication

medium 30 min

Scale a database past one machine by splitting rows over shards, and keep it available by copying every shard.

Where it shows up

The same pattern appears across everyday software.

  • Notion

    Shards its block data by workspace id: 480 logical shards as PostgreSQL schemas, 15 on each of 32 databases. The many divisors of 480 make later growth simpler.

  • Slack workspace co-location, and its limit

    Slack first kept all data of a workspace on one shard, so the app needed one connection. Large customers made hot spots, and Slack moved to Vitess, where tables can use other keys.

  • Vitess, born at YouTube

    Started in 2010 to grow YouTube's MySQL. It sits in front of MySQL, routes queries to shards and can reshard. GitHub has used it to move groups of tables between clusters.

  • Redis Cluster

    Splits keys over 16,384 slots. Hash tags force chosen keys into the same slot so that operations on several keys can work.

  • MongoDB

    A background balancer is on by default and moves ranges between shards when they differ enough. Since version 5.0 a collection can be given a new shard key.

  • Shard numbers inside ids

    An id can carry its logical shard in a few bits next to time and sequence. Instagram described 64-bit PostgreSQL ids with 13 bits for the logical shard.

  • Figma

    Groups related tables into colos that share a shard key and layout, and separates logical sharding in the application from the physical move.