1:1 and group messages, offline delivery, and why two tables beat one

Two Bigtable tables for a WhatsApp-style chat: history keyed by chat, delivery keyed by user.
Two Bigtable tables for a WhatsApp-style chat: history keyed by chat, delivery keyed by user.

Asha sends "Hi Ravi" while Ravi's phone is in his pocket on a train with no signal. Then she posts "Book hotel?" in the "Goa Trip" group, where Ravi is online and Meera is offline.

When Ravi and Meera reconnect, both expect every message, in order, exactly once. This is how I would design that for a WhatsApp-style chat app on GCP. It is an illustrative design, not WhatsApp's real architecture.

The problem

A chat app has two very different reads:

  1. "Open this chat and show the latest 50 messages."
  2. "I was offline. What did I miss, across all my chats?"

If messages sit in one relational table, both reads hit the same primary, and so do billions of append-only writes. A single Cloud SQL primary would struggle with that write volume. And if a group message is copied in full into every member's row, a 500-member group multiplies storage and writes by 500.

The solution

Phones keep a WebSocket to gateway pods on GKE; Memorystore maps users to pods, Bigtable stores messages and Cloud SQL stores groups.
Phones keep a WebSocket to gateway pods on GKE; Memorystore maps users to pods, Bigtable stores messages and Cloud SQL stores groups.

Connections. Each phone keeps a WebSocket open to a gateway pod on GKE, behind Cloud Load Balancing. Memorystore (Redis) holds a session map, such as ravi -> pod-3, with a TTL of about 60 seconds that each heartbeat refreshes. If a pod crashes, its entries expire by themselves.

Storage: two Bigtable tables, keyed by the read. Bigtable only looks up by row key or key range, so the key is the design.

Table Row key Holds Serves
messages chat_id#reverse_ts#message_id Sender, body (ciphertext), one status cell per recipient "Open this chat"
inbox user_id#ts#message_id Only a pointer to the messages row "What did I miss"

The reverse timestamp is 9999999999999 - time in ms, so the newest message sorts first and "latest 50" is one short prefix scan. A DM's chat_id is both user ids sorted, like dm_asha_ravi, so both sides find the same chat. The inbox uses the normal timestamp, so the oldest missed message is delivered first.

The messages table is keyed by chat with a reverse timestamp; the inbox table is keyed by user and holds only pointers.
The messages table is keyed by chat with a reverse timestamp; the inbox table is keyed by user and holds only pointers.

Small, relational data stays in Cloud SQL: users, groups and group membership. Cloud SQL holds who is in the group; Bigtable holds what was said.

1:1 flow. The chat service writes the message to Bigtable first, then acks Asha (one tick). Redis says Ravi is on pod-3, so pod-3 pushes it down his WebSocket. Ravi's app acks, and the status becomes delivered (two ticks).

Offline delivery. If Ravi is offline, the same messages row is written with status "sent", plus an inbox row such as ravi#1760950012000#m_003 pointing to it, and Pub/Sub triggers an FCM push. When Ravi reconnects, the order is: register his session in Redis first, so new live messages are not missed; then scan ravi# in the inbox in batches, oldest first; then delete each inbox row after his app acks. FCM is only a wake-up nudge. If it drops a push, nothing is lost, because the inbox is the source of truth.

While Ravi is offline the inbox row waits and FCM only nudges; on reconnect he registers first, scans his inbox oldest first, then deletes after each ack.
While Ravi is offline the inbox row waits and FCM only nudges; on reconnect he registers first, scans his inbox oldest first, then deletes after each ack.

Group flow: fan out pointers, not bodies. For "Book hotel?" in grp_42, the service looks up the members (cached in Redis, source Cloud SQL), writes the body once to messages, and writes one small pointer row per recipient into inbox. Ravi is online, so his pod pushes it. Meera is offline, so a worker sends FCM, and her inbox row waits. Opening the group is still one scan of grp_42#, whatever the group size.

A group message is written once to messages, with one small inbox pointer per recipient; online members get a push, offline ones wait in the inbox.
A group message is written once to messages, with one small inbox pointer per recipient; online members get a push, offline ones wait in the inbox.

Duplicates. Pub/Sub and retries are at-least-once. If Ravi's ack is lost, the server resends m_001, and his app ignores it because it has already seen that message_id. At-least-once delivery plus dedupe behaves like exactly-once.

Trade-offs and limits

  • Hot groups. Keys start with chat_id, so one very active group is one busy key range. Bigtable splits ranges to spread load, but extreme groups need more thought.
  • Very large groups. Fan-out on write means one inbox row per member. For very large groups I would switch to fan-out on read.
  • Ordering is per chat only. That is what users need, but there is no global order across chats.
  • Media stays off the chat path. The app uploads files to Cloud Storage with a signed URL and the message carries only the link, so a large video never passes through the chat pods.
  • Redis can be wrong. If a session entry is missing or stale, the safe default is to treat the user as offline. The message is already safe in Bigtable and the inbox.

Takeaway

Design the Bigtable row key around the read you need.

History keyed by chat, delivery keyed by user, body stored once.

Save first, then deliver, and dedupe by message id.