Notion System Design
Hundreds of billions of blocks in application-sharded Postgres
By Ankur NayakReviewed
How Notion stores every paragraph, heading and to-do as a 'block' row in Postgres: sharded by workspace ID into 480 logical shards, routed in application code, re-sharded from 32 to 96 machines with zero downtime, and streamed through change data capture into a Kafka, Hudi and S3 data lake.
Open in Studio· edit a copy- Storage
- 200B+ blocks (2024)
- Logical shards
- 480
- Postgres machines
- 32 → 96
- Block growth
- 2x every 6–12 months
- Upserts that are updates
- 90%
Requirements
Functional
- Store pages as trees of blocks: text, headings, to-dos, databases, embeds
- Load any page in a workspace quickly
- Let many members edit pages in a shared workspace
- Feed analytics, search and AI features from the same data
Non-functional
- Keep growing as blocks double every 6–12 months
- Add database capacity without downtime
- Avoid Postgres maintenance stalls (VACUUM, transaction ID wraparound)
- Keep analytics data fresh without loading the primary databases
In depth
Everything is a block
In Notion, every piece of content is a block: a paragraph, a heading, a to-do, an image, a page, a database row. Blocks nest to form pages, and each block is a row in Postgres. That flexible model is why Notion feels like building with Lego, and it is also why its database grows so fast. Notion's engineering team has written three detailed posts about keeping up: sharding Postgres in 2021, re-sharding it in 2023, and building a data lake on top of it in 2024.
By 2021 Notion had more than 20 billion block rows; by 2024, more than 200 billion, with block data doubling every 6 to 12 months.
Why shard: a database that couldn't keep up
Until 2021 Notion ran on a single, very large Postgres database. Two maintenance problems made that untenable. VACUUM, the process Postgres uses to reclaim space from dead rows, began to stall consistently, so the database couldn't recover disk space. Worse, the database was heading toward transaction ID wraparound, a safety mechanism in which Postgres stops processing all writes to avoid corrupting data. That would have stopped writes for everyone.
The answer was to split the data across many smaller databases.
Choosing the shard key
The deciding fact: each block belongs to exactly one workspace. So Notion partitioned all data reachable from the block table by workspace ID. A page and all its blocks live together on one shard, page loads never need cross-shard queries, and transactions stay on one database. See database sharding.
Notion chose to implement its own partitioning and route queries in application code, with a single source of truth for mapping a workspace ID to its shard, rather than adopt sharding middleware.
Logical shards on physical machines
The layout has two levels. There are 480 logical shards, each a Postgres schema with its own copy of the tables. In 2021 they were spread over 32 physical databases, 15 schemas on each.
Creating far more logical shards than machines is the important trick. Adding capacity later doesn't mean splitting rows; it means moving whole schemas to new machines. And 480 divides evenly in many ways.
The first migration: double writes and dark reads
Moving a live product onto 32 new databases followed the classic online migration recipe:
- Double writes: an audit log table recorded every write to the tables being migrated, and a process applied those writes to the new databases.
- Backfill: a script copied the existing data; running on all 96 CPUs of an m5.24xlarge instance, it took about three days.
- Dark reads: for a while, the application fetched data from both old and new databases and compared results. This added latency, but built confidence that the switch would be seamless.
- Switch-over: Notion took the product down for five minutes of scheduled maintenance to cut over.
The Great Re-shard: 32 to 96 machines
By 2023 the shards were running hot again. Some exceeded 90% CPU at peak, many were close to their provisioned disk bandwidth, and the PgBouncer connection poolers were hitting connection limits. Notion tripled the fleet to 96 machines, moving from 15 schemas per database to 5.
This time the data was copied with Postgres logical replication: three publications on each existing database, covering five schemas each, copied history and then continuously applied new changes to the new databases. Tuning cut the time to synchronise from three days to 12 hours.
Connection pooling needed rework too. About 100 PgBouncer instances could each open several connections to every shard; adding three times as many databases would have multiplied connections past their limits. Notion split PgBouncer into four groups, each in front of 24 databases.
Dark reads came back, scoped to queries returning up to five rows, and showed near 100% equivalence. Cutover was done one shard at a time: pause traffic for the database in PgBouncer, let replication finish, repoint the routes, resume. Notion reported no perceived user downtime, and afterwards CPU and IOPS sat around 20% at peak.
The data lake
Postgres serves the product, but analytics and newer features need all the data in one place. Notion's 2024 post describes a data lake built in 2022 from Debezium change-data-capture connectors that stream changes from Postgres into Kafka, Apache Hudi that writes them into S3, and Spark for processing. Hudi uses copy-on-write tables with upserts; because 90% of Notion's upserts are updates to existing blocks, the update-heavy workload shaped those choices. The block table lands in the lake within about two hours.
The trade-offs
Notion's design is a case for plain Postgres, sharded carefully. Application-level routing gives full control, but every query must carry the workspace ID and cross-workspace queries are hard. Many logical shards per machine make growth a matter of moving schemas, at the cost of more schemas to manage. Workspace as the shard key keeps pages together and transactions local, but concentrates very large workspaces on one shard. And zero-downtime migrations take months of tooling: replication, comparisons and cutover scripts.
Compare with Figma, which also sharded Postgres, using a query proxy and views, and with Slack, which chose Vitess.
What to take into an interview
- Pick a shard key that matches ownership: here, every block belongs to one workspace
- Create many more logical shards than machines so you can grow by moving them
- Migrate online: double write or replicate, backfill, dark-read, then cut over
- Watch database maintenance (VACUUM, transaction IDs), not just CPU
- Plan connection pooling as part of sharding
- Stream changes with CDC into a lake instead of querying production for analytics
Components
Notion App
Client. Web, desktop or mobile client reading and editing pages.
API Servers
API gateway. Serve page loads and edits. Every query for block data includes the workspace ID.
Shard Routing
Service. Application logic maps workspace ID → logical shard → physical database, with a single source of truth for the mapping.
Why: Every block belongs to exactly one workspace, so routing by workspace keeps a page's blocks together on one shard.
Building block: Database Sharding
PgBouncer
Load balancer. Connection pooling in front of the databases. In 2023 about 100 PgBouncer instances were split into 4 groups so each handled fewer databases and stayed within connection limits.
Postgres
480 shardsSQL database. 480 logical shards, each a Postgres schema, spread across physical databases: 32 machines with 15 schemas each in 2021, 96 machines with 5 each after the 2023 re-shard.
Building block: Database Sharding, Online Migrations
Existing Database
SQL database. One of the original 32 machines, holding 15 logical shards (schemas).
Logical Replication
Stream. Three Postgres publications per existing database, five schemas each, copy history and stream new changes. Syncing dropped from 3 days to 12 hours.
New Databases
SQL database. Three new machines per old one, each taking 5 of its schemas.
Dark Read Checks
Monitoring. Compare results from the old and new databases for queries returning up to 5 rows; near 100% equivalence before cutover.
PgBouncer Cutover
Load balancer. Pause traffic to the old database, let replication catch up, repoint the routes, resume.
Debezium CDC
Worker. Change data capture connectors read every change from the Postgres shards and publish them to Kafka.
Kafka
Stream. Streams the incremental changes from all 480 shards.
Building block: Message Queue & Event Log
Data Lake
Hudi on S3Data warehouse. Apache Hudi writes the changes from Kafka into S3 as copy-on-write tables with upserts; the block table lags by up to about two hours.
Spark Processing
Batch processing. Processes the lake's data for analytics and product features.
Request flows
Load a page
- A user opens a page in their workspace.
- The API knows the workspace ID and maps it to one of 480 logical shards.
- The logical shard maps to the physical database holding it, reached through PgBouncer. All the page's blocks are in one schema on one machine: no cross-shard queries.
- The blocks come back and the page renders.
Changes reach the data lake
- A user edits a block; Debezium captures the change from Postgres.
- The change is published to Kafka.
- Hudi upserts it into the S3 data lake; 90% of upserts are updates to existing blocks.
- Spark jobs process the lake for analytics and product features.
Failure scenarios
What if: vACUUM can't keep up on one big database?
Before 2021 Notion ran one large Postgres database. VACUUM began to stall consistently, unable to reclaim space, and transaction ID wraparound loomed: a safety mechanism where Postgres stops all writes. Sharding into many smaller databases fixed both.
User impact: Risk of the whole product stopping writes.
- Shard by workspace ID
- Many small databases instead of one huge one
What if: shards run out of headroom?
By 2023 some shards exceeded 90% CPU at peak, others neared their disk bandwidth limits, and PgBouncer hit connection limits. Notion tripled the fleet to 96 machines; afterwards CPU and IOPS sat around 20% at peak.
User impact: Slower page loads at peak without the re-shard.
- Re-shard with logical replication
- Split PgBouncer into groups
Trade-offs
Application-level sharding
Route queries to shards in application code rather than adopt a distributed database or sharding middleware.
- Pros
- Full control over routing and migrations
- Keeps plain Postgres
- Cons
- Every query must carry the workspace ID
- Cross-workspace queries are hard
Many logical shards per machine
Create 480 logical shards up front and place several on each machine.
- Pros
- Adding machines moves whole schemas; no rows are split
- 480 divides evenly in many ways
- Cons
- More schemas to manage per machine
Workspace ID as the shard key
Put everything reachable from a block on the shard of its workspace.
- Pros
- A page and its blocks live together
- Transactions stay on one shard
- Cons
- Very large workspaces concentrate on one shard
Zero-downtime migrations
Copy data while live, verify with dark reads, then switch quickly.
- Pros
- 2023's re-shard caused no perceived downtime
- Cons
- Months of tooling: replication, comparisons, cutover scripts
Frequently asked questions
How does Notion store data?
Every piece of content, from a paragraph to a to-do, is a block stored as a row in Postgres. Each block belongs to exactly one workspace, and the data is sharded by workspace ID into 480 logical shards. By 2024 Notion had more than 200 billion blocks.
Why did Notion shard Postgres?
Its single Postgres database was struggling: VACUUM stalled and couldn't reclaim space, and the database risked transaction ID wraparound, where Postgres stops accepting writes to protect data. Splitting it into many smaller databases solved both problems.
How did Notion re-shard without downtime?
In 2023 it moved from 32 to 96 machines. Postgres logical replication copied five schemas to each new machine, dark reads compared results between old and new, and PgBouncer briefly paused traffic for each cutover. Notion saw no perceived user downtime.
Why 480 logical shards?
Having many more logical shards than machines means adding capacity only moves whole shards. 480 divides evenly into many machine counts, which is how Notion went from 32 machines with 15 schemas each to 96 machines with 5 each.