Where should chat messages go, when there are billions of them and every conversation is read newest first?
Applications like this: chat apps, support conversations, comments on a document. This page is written the way I write a database design doc: the workload first, then where each kind of data goes and why, then how it grows, what breaks, and what I rejected.
| Dimension | This application |
|---|---|
| Writes | High and steady. Every message is an insert. |
| Reads | The latest page of one conversation, very often. |
| Read to write | Roughly balanced. |
| Consistency | Per conversation, in order. Across conversations, loose. |
| Latency | Delivery feels instant: tens of milliseconds to store. |
| Growth | Fast and forever, unless you set retention. |
These incidents are why the choices below matter. Each one is written up with its numbers and sources.
Solid arrows: the application writes there. Dashed arrows: a copy fed from the source of truth (change data capture, an outbox, or a cache fill), which can be rebuilt from it.
| Data | Where it goes | Why |
|---|---|---|
| Messages | PostgreSQL with time partitions, then Cassandra or ScyllaDB | One conversation, newest first, is a range read on one partition. LSM stores take heavy insert rates well. |
| Conversations and members | The relational database | Few rows, many relationships, permissions. |
| Online status and typing | Redis with a short TTL | Changes every few seconds and does not need to survive a restart. |
| Attachments | Object storage | Large and immutable. |
How each family of store behaves under traffic: Database families.
-- PostgreSQL CREATE TABLE messages ( conversation_id bigint NOT NULL, sent_at timestamptz NOT NULL, id bigint NOT NULL, sender_id bigint NOT NULL, body text, PRIMARY KEY (conversation_id, sent_at, id) ) PARTITION BY RANGE (sent_at); -- Cassandra or ScyllaDB at scale CREATE TABLE messages ( conversation_id bigint, bucket int, sent_at timeuuid, sender_id bigint, body text, PRIMARY KEY ((conversation_id, bucket), sent_at) ) WITH CLUSTERING ORDER BY (sent_at DESC);
Step through one message from the sender to the recipient, and back when they scroll. Press Play, or use the arrows.
Pick a stage, then throw something at it. The numbers are a model to show the shape of each change, not a measurement; the measured numbers are in the lab notebooks.
| What happens | What you see | What I do |
|---|---|---|
| One huge group chat | A hot partition and slow reads | Bucket by time inside the conversation. |
| Deleting old messages with DELETE | Bloat and long-running deletes | Drop whole partitions, or use TTLs in the wide-column store. |
| Unread counts computed on every read | COUNT(*) over a conversation on every app open | Keep a per-member last-read marker and a counter. |
Cost. Storage grows forever. Set retention, or move old partitions to cheaper storage.
| Stage | Where it runs |
|---|---|
| Where I would start | Managed relational with time partitions |
| When it grows | Managed Cassandra-compatible service, or self-managed Cassandra or ScyllaDB on VMs |
For a team without a dedicated database team. Who does which job in each option, and the full-cost comparison: Where to run the database.
The back-of-the-envelope numbers I would want before choosing anything. Move the assumptions to match your application. The link keeps your numbers, so a colleague sees the same ones.
Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.