Below the Fold

A designer’s path to creative technologist. Thirty days, ten minutes each, on how software actually gets made.

A table with a new column A grid drawn as a table standing on legs, with a new red column added past its edge and nothing holding it up.

Databases

After today, you can read a simple data model, explain what an index is for, and say why adding a field can take two weeks.

Dip your toes 1 min

A database is where a product keeps the things it has to remember. In Slack, every message you scroll back to, every channel name, and every person lives in one. It sits on computers Slack rents or runs, not in the app on your phone. The data sits in tables, like spreadsheets with strict rules about what each column can hold. Each message stores its channel’s ID, a unique label, instead of the channel’s name, so renaming a channel changes one row. That strictness keeps data clean, but it makes change slow. Adding one new field to a table with billions of messages can take a couple of weeks, and old messages start out blank.

The full lesson 10 min

Why this exists

Scroll up in a busy Slack channel and you can read messages from years ago. Two people post at the same moment, and both messages appear, in the same order, for everyone in the channel. Rename the channel, and every old message still sits in it under the new name. The app on your phone does none of this: delete it and reinstall it, and everything is still there.

Most of that lives in a database on Slack’s servers. A database is built for three jobs that ordinary files do badly. It keeps data safe when a machine fails, and it finds one item among billions without reading them all. And it stays correct when thousands of requests write at the same moment. A spreadsheet can hold a list, but it can’t do those three things for millions of people at once.

There’s a fourth job that matters most to you. A database enforces a shape: every message belongs to a channel, every channel has a name, every timestamp is a real time. That shape decides what the product can show quickly, what it can’t show at all, and how hard each one is to change. Most of the “why is this so hard?” moments in a roadmap trace back to it.

What it is

Day 6 introduced the database as the part of the system that remembers. Most products keep their core data in a relational database, which stores everything in tables. A table is a set of rows that all describe one kind of thing. Each row is one item, like one message. Each column is one fact about it, like the text or the time it was sent.

Slack is a well-documented case. In a 2020 post, Slack’s engineers described MySQL as the storage for all of its data. MySQL is one of the most widely used relational databases. A simplified version of Slack’s data would have a table for channels, a table for messages, and a table for users, among many others.

The tables connect through IDs. Every channel gets a unique ID when it’s created. Every message stores the ID of its channel, not the channel’s name. Show a message, and the backend looks up its channel by that ID.

A messages table and a channels tableTwo tables. The messages table has columns id, channel_id, and text, with three rows: M1 in C2, New mocks are up; M2 in C2, Looks good to me; M3 in C3, Ship it Friday? The channels table has columns id and name, with three rows: C1 general, C2 design, and C3 launches. An arrow runs from the channel_id column header to the id column header of the channels table.MessagesChannelsidchannel_idtextidnameM1C2New mocks are upM2C2Looks good to meM3C3Ship it Friday?C1generalC2designC3launchesEach message points to its channel by ID
Figure 1. Two tables behind a Slack channel, joined by the channel’s ID. Look at the channel_id column. Each message stores only its channel’s ID, so renaming a channel changes one row, not every message in it.

That one choice explains a lot of product behavior. Renaming a channel is instant, because the name lives in exactly one row. Engineers call this normalization: keeping each fact in one place. Copy the name into every message instead, and a rename means rewriting millions of rows, with a window where some messages show the old name.

The rules for all of this live in the schema, the blueprint of the database. It lists the tables, the columns in each, the type of value each column holds, and which columns point to other tables. The schema is strict on purpose: a message can’t be saved without a channel ID. A timestamp column won’t accept the word “yesterday” in place of a time. Those rules catch bad data at the door, before it can spread through the product.

The schema is also a promise to every piece of code that reads the table. A messages table might be read by the code that serves the apps, by search, by notifications, and by analysts’ reports. Change what a column is called or what it means, and every one of those readers has to change with it. That’s the real reason schemas move slowly. The database could make the change in seconds, but the code around it can’t.

The backend reads data by sending a query. A typical one asks for every message in channel C2. Without help, the database answers by reading every message it has and keeping the ones that match. That’s fine for a hundred rows. It’s hopeless for billions.

An index fixes that. An index is a sorted lookup the database keeps alongside a table, like the index at the back of a book. An index on channel_id lists every channel ID in order, each with the rows that belong to it. The database looks up C2, then jumps straight to those rows. Most tables have several indexes, one for each common way the product looks things up.

Finding rows with and without an indexA query asks for every message in channel C2. Without an index, the database reads all ten rows of the messages table. With an index on channel, it looks up C2, finds rows 3, 6, and 9, and reads only those three rows.Query: every message in channel C2Without an indexThe database reads every row and keeps the matchesC1C3C2C1C1C2C3C1C2C3Reads 10 of 10 rowsWith an indexIt looks up C2 first, then reads only those rowsIndex on channelC1: rows 1, 4, 5, 8C2: rows 3, 6, 9C3: rows 2, 7, 1012345678910C1C3C2C1C1C2C3C1C2C3Reads 3 of 10 rows
Figure 2. The same query answered by reading every row, and by looking it up in an index first. Look at the index. It turns reading every row into reading only the three that match, and the gap grows with every row the table adds.

Indexes aren’t free. Every new message has to be added to every index on the messages table, so each index makes writing slower and takes storage. Teams add indexes for the questions the product asks often, and skip the rest. A missing index often shows up as a page that was fast with test data and slow in production, where the table is far bigger. That’s why a new way to sort or filter a list can turn into backend work.

Not every database uses tables. Some store whole documents, and some store simple pairs of keys and values. Most products still keep their core records in tables. There’s more here about choosing between kinds of databases; you don’t need it yet.

How it shows up in your work

Every piece of information in your mockups comes from a column somewhere, and one screen often needs several tables. Stitching rows from different tables together in one query is called a join. Your Slack channel view is a join: the channel’s name, each message’s text, and each author’s name and photo all live in different tables. When a design puts a new fact next to existing ones, ask whether it lives in the same table or needs another join.

One screen, three tablesA wireframe of a Slack channel named design, with two messages. Lines connect the channel name to the channels table, an author’s name to the users table, and a message’s text to the messages table.# designAnaSamNew mocks are upLooks good to meTodayChannelsUsersMessagesName, topicName, photoText, time, author ID
Figure 3. A simplified Slack channel view, with three of its pieces traced to the tables they come from. Look at the users table. The author’s name isn’t stored with the message, so showing it takes a second lookup.

Sorting and filtering come next. “Sort by most reactions” sounds like a frontend control. If there’s no index that fits, and no stored count of reactions per message, the database has to count them on every request. Engineers might need a new index, a new column, or both. Bringing the idea up in a design review, before the ticket is written, turns a surprise into a plan.

Names are the third. The schema’s names tend to outlive the product’s names, because renaming a table is risky. Slack’s product says “workspace,” but its developer docs still describe a workspace as a team, in method names like team.info. When your team renames a concept in the interface, ask what it’s called in the schema. Engineers will keep translating between the two names for years, and so will you.

The one thing people get wrong

How hard can one new field be? On a screen, it’s one more label and one more value. In the database, it’s a change to the schema of a table that might hold billions of rows and serve live traffic every second. That change happens through a migration: a script, kept in the repository, that changes the schema in a recorded way. It runs in order in every environment, from a laptop to staging to production.

Then there are the rows that already exist. A new column starts empty for every old message. Filling it in for those rows is called a backfill, and on a big table it’s the slowest part. It has to run in small batches, so the database keeps answering users while it works. Meanwhile, the code has to cope with both shapes: new rows that have the field, and old rows that don’t yet.

Size changes the risk, too. On a big table, some schema changes make the database rebuild the whole table. Some databases block new writes to a table while that happens. For Slack, a blocked messages table would mean nobody can send a message. So teams use careful tools instead, like gh-ost, which GitHub built and shared to change MySQL tables by working on a copy in the background.

The timeline of adding one fieldSeven steps on a two-week timeline: write the migration, review and test it, run it on staging, run it on production, write the field for new rows, backfill old rows, and screens start reading it. The backfill bar is the longest, running from the start of week two to near its end.Illustrative timingWrite the migrationReview and test itRun it on stagingRun it on productionWrite the field for new rowsBackfill old rowsScreens start reading itWeek 1Week 2
Figure 4. The steps between “add a field” and users seeing it, on a large table. Look at the backfill. Filling in years of old rows takes longer than any other step, and it runs while the product is live.

That’s where the two weeks go. Each step is short on its own, but they have to happen in order, and some wait on others. Undoing a migration is harder than undoing a code change, because data written in the new shape doesn’t un-write itself. Bigger moves take longer still: Slack moved its data onto Vitess, a system that spreads MySQL across many machines. That move began in 2017 and was close to done three years later.

This changes what you design. During the backfill, some rows have the field and some don’t, so the screen needs a blank or fallback state. Sometimes the best answer is to skip the backfill: show the new field only on messages sent after launch. That call belongs in the design conversation, not only the engineering one. Ask early whether old rows can be filled in at all, because sometimes the information was never recorded.

Ask an engineer this

“Which table in our database is the hardest to change, and what makes it hard?”

Ask before you plan a feature that adds or changes stored data, not after the designs are approved. Most engineers can name one right away, along with the reason: it’s huge, many services read it, or its old rows are messy. Follow up on whether migrations on it need special care, like running at quiet hours or in small batches. It also helps to know how long the last change to that table took from start to finish. By then you’ll know which parts of your product are cheap to change and which ones deserve a conversation first.

Terms from today

Table
A set of rows in a database that all describe one kind of thing, like messages or channels, and share the same columns.
Schema
The blueprint of a database, listing its tables, their columns, the type of value each column holds, and how tables point to each other.
Index
A sorted lookup kept alongside a table that lets the database find matching rows without reading every row.
Migration
A script, kept in the repository, that changes a database’s schema in a recorded way and runs in order in every environment.
Backfill
Filling in a new or changed column for the rows that already existed before the change.
Next

Day 17: Auth

Who are you, what can you do. Sessions, tokens, roles, and why hiding a button protects nothing.

16

Databases

All 30 days