Storing Billions of Chat Messages: Data Model and Database Choice

Choose a database and schema for chat messages: Cassandra-style wide-column stores, partitioning by conversation, time buckets, inboxes and retention.

Published November 27, 2026

A busy chat platform writes hundreds of thousands of messages per second, and the dominant read is "the latest N messages in this conversation" or "everything after sequence X". That access pattern points strongly toward wide-column or key-value stores partitioned by conversation. This article walks through the data model, why relational databases struggle at this scale, and how to avoid hot partitions.

The concept

Chat data has unusual properties: extremely high write volume, almost no updates, reads concentrated on recent messages, and queries always scoped to one conversation. Cassandra, ScyllaDB and HBase are built for exactly this: append-heavy writes distributed across many nodes, with rows inside a partition kept sorted by a clustering key. Discord, for example, publicly described moving its messages from MongoDB to Cassandra and later to ScyllaDB for these reasons.

The partition key is the conversation (and often a time bucket), and the clustering key is the message sequence or time-ordered ID in descending order. Fetching the latest 50 messages becomes a single-partition sequential read. Very active conversations would grow unbounded partitions, so bucketing by time (for example, per conversation per 10 days) keeps each partition a manageable size.

Database options

StoreStrengths for chatWeaknesses
Cassandra / ScyllaDBMassive write throughput, partition per conversation, linear scalingNo joins, limited ad-hoc queries
HBaseSorted rows, strong consistency per rowHeavier ops (HDFS, ZooKeeper)
PostgreSQL / MySQL (sharded)Familiar, transactionsManual sharding, write scaling effort
DynamoDBManaged, partition + sort key fits wellCost at very high write volume, item size limits

Partitioning by conversation

messages table(u42:u77, bucket 11a)(g-981, bucket 11a)120 › 119 › 118 …5521 › 5520 …
Each partition holds one conversation's messages for a time bucket, sorted newest first.

Schema

Two tables cover most queries: messages per conversation, and a per-user conversation list (the inbox screen) updated on each message.

CREATE TABLE messages (
  conversation_id text,
  bucket          int,            -- e.g. days since epoch / 10
  message_id      timeuuid,       -- time-ordered, unique
  sender_id       bigint,
  ciphertext      blob,
  media_ref       text,
  PRIMARY KEY ((conversation_id, bucket), message_id)
) WITH CLUSTERING ORDER BY (message_id DESC)
  AND default_time_to_live = 2592000;   -- 30 days if you expire delivered messages

CREATE TABLE user_conversations (
  user_id         bigint,
  last_message_at timestamp,
  conversation_id text,
  last_preview    text,
  unread_count    int,
  PRIMARY KEY (user_id, last_message_at, conversation_id)
) WITH CLUSTERING ORDER BY (last_message_at DESC);

Why time buckets matter

Partition by conversation onlyhot partitions grow foreverConversation + 10-day bucketbounded size
Relative maximum partition size for a very active group (illustrative).

Practical examples

Load the latest page of a conversation

Start with the current bucket and walk back if fewer than 50 rows.

SELECT message_id, sender_id, ciphertext FROM messages
WHERE conversation_id = 'u42:u77' AND bucket = 2026113
LIMIT 50;

Sync after reconnect

Fetch everything newer than the client's last known message id.

SELECT message_id, sender_id, ciphertext FROM messages
WHERE conversation_id = 'u42:u77' AND bucket = 2026113
  AND message_id > 5f1d9a40-cb6e-11f0-8000-000000000000;

Deterministic 1:1 conversation id

Both participants compute the same id.

def dm_id(a: int, b: int) -> str:
    lo, hi = sorted((a, b))
    return f"u{lo}:u{hi}"

Common mistakes

  • Using a single relational table with a global auto-increment id for all messages at huge scale.
  • Unbounded partitions for very active conversations, causing slow reads and repair problems.
  • Updating an unread counter row on every message in a way that creates write contention; use counters or compute on read.
  • Storing large media blobs in the message table instead of object storage.
  • Choosing a store whose query model does not match "latest N in a conversation".

Key takeaways

  • Chat is append-heavy and conversation-scoped: wide-column stores fit naturally.
  • Partition by conversation plus a time bucket; cluster by time-ordered message id.
  • Keep a separate per-user conversation list for the inbox screen.
  • Store media in object storage and keep only references in messages.

FAQ

Should the server keep messages after delivery?

It is a product decision. WhatsApp keeps them only until delivered (and in optional encrypted backups); Telegram, Slack and Discord keep full history server-side, which needs far more storage.

How do you search messages?

With end-to-end encryption, search happens on the device. Without it, messages are indexed into a search engine such as Elasticsearch or OpenSearch asynchronously.

Why timeuuid instead of a counter?

A timeuuid is unique without coordination and sorts by time, which suits distributed writes. Per-conversation sequence numbers can be added for gap detection.

More Architecture articles