04.3.3 · Skill in Data engineering

Databases and persistence

Choosing and using storage systems that fit the data and access patterns.

Relational databases cover most products well, with document, key-value and search stores for specific needs. Knowing indexes, transactions and query plans prevents most performance problems. Simple, boring storage choices are usually the right ones.

Your status · Databases and persistence

Saved in this browser. Sign in to keep it on every device.It colours this skill's tile on your map.

Why it matters for a product engineer

Most products are well served by one relational database, with a document, key-value or search store for specific needs. Knowing indexes, transactions and query plans prevents most performance problems. Simple, well-known storage choices are usually right, because the team already knows how they fail and how to fix them.

How to do it

  1. Start with one relational database, such as Postgres, unless you have a clear reason not to.
  2. Add an index for each frequent query, and check the plan to see that it is used.
  3. Use transactions for changes that must happen together, such as an order and its payment.
  4. Add caching, read replicas or a specialised store only when a measurement shows the need.
  5. Back up automatically, and test a restore. A backup you have never restored is a hope.
  6. Watch the limits that come with growth: table size, writes per second and maintenance jobs.

Common mistakes

  • A new database technology for each feature.
  • No indexes until the product is slow in production.
  • Backups that nobody has restored.
  • Splitting the database across many servers years before the load needs it.

Example

In 2020 Figma ran on one Postgres database, on the largest instance AWS offered. As the database stack grew almost 100 times, the team added caching and read replicas, then split related tables into about a dozen databases. Only when single tables reached several terabytes did it start horizontal sharding, and it kept the option to roll back.

Timeline

How Figma scaled Postgres, one step at a time

  1. 2020One Postgres databaseDecisionOn AWS's largest physical instance
  2. By end of 2022Caching, read replicas, a dozen databasesShippedRelated tables, such as files or organisations, moved together
  3. Late 2022Single tables too largeProblemSeveral terabytes and billions of rows
  4. Nine months laterHorizontal sharding on the same PostgresResultWith a way back to the unsharded setup

Each step came when a measured limit was near, not before. The team stayed on technology it already knew how to run.

ReportedFigma engineering blog, 14 Mar 2024

Sources: Use the index, Luke (Markus Winand) ↗ · Designing data-intensive applications (Martin Kleppmann) ↗ · How Figma's databases team lived to tell the scale (Sammy Steele, Figma) ↗

Resources

2
  • bookGo deeper
    Use the index, Luke ↗
    Markus Winand · free online book

    SQL indexing explained for developers, across the major databases. It prevents most slow queries.

  • bookGo deeper
    Designing data-intensive applications ↗
    Martin Kleppmann · 600 pages

    The standard book on data systems, consistency and scale. Read it when a product's data starts to hurt.

Tools

1