← Building blocks

Keep the server out of the content

Security & abusestaff

Users want messages the provider cannot read.

Say it like this

The provider must be unable to read messages, so clients encrypt with keys the server never holds and the server becomes a router and store for ciphertext, which I say up front costs server-side search, moderation and easy multi-device sync.

Basic solution

Encrypt on the client with keys the server never holds; the server routes and stores ciphertext only.

Signal protocolX3DHDouble Ratchet

Public keys through the server, secrets never

Devices publish public keys; a sender derives a shared secret from them and encrypts on the device. The server sequences, stores and pushes ciphertext exactly as it would plaintext, and can never read it.

How it works

  1. Each device generates a long-term identity key pair and a batch of one-time pre-keys, which are single-use public keys published in advance so someone can start a conversation while the device is offline. The public halves are uploaded to the server; the private keys never leave the device.
  2. To start a conversation, Alice's device fetches Bob's public identity key and one of his pre-keys from the server and combines them with her own keys to derive a shared secret (the X3DH handshake). The server handed over only public material, so it learns nothing about the secret.
  3. Each message is encrypted with a fresh key produced by a ratchet (the Double Ratchet): a function that advances the key with every message sent and mixes in new key material on every round trip. Compromising one message key therefore exposes neither earlier nor later messages.
  4. The server receives ciphertext plus routing metadata (recipient, timestamp, sequence number) and does exactly what the plaintext system did: it assigns sequence numbers, stores the message in the recipient's inbox, and pushes it to connected devices. It cannot read the body.
  5. Verification guards against the server swapping keys. Users compare a safety number, a hash of both parties' identity keys, through another channel, and the app shows a warning in the conversation whenever a contact's key changes.

What the server still knows

  • Who talks to whom, when, how much, and from which device. Metadata is not hidden by E2EE, and it is worth saying so explicitly.

Worked example

Chat app

  • 500M users, 500K messages/s. The server pipeline is unchanged in shape: sequence, inbox write, push. The body is now ciphertext of similar size plus a 16-byte authentication tag and a small header: under 5% overhead on a 200-byte message.
  • Key material per device: one identity key, one signed pre-key, 100 one-time pre-keys of 32 bytes each. About 4 KB per device; 1B devices is 4 TB of public keys, a small key-value store. Pre-keys are consumed on first contact and replenished by the client; the server alerts the client when fewer than 20 remain.
  • Group chats: with a sender-key scheme, the sender encrypts once per message with a group key distributed to each member pairwise. A 256-member group costs 255 pairwise key deliveries once per key rotation (on membership change), then one encryption per message, so fan-out stays at 256 ciphertext deliveries, same as plaintext.
  • Multi-device: each device is a separate endpoint with its own keys. A message to Bob is encrypted separately for each of Bob's 3 devices, so recipient fan-out triples per device count, not per message count: 500K/s × 1.3 recipients × 2.5 devices ≈ 1.6M encryptions/s, spread across clients, not servers.
  • Encryption cost on the client: AES-GCM on a phone handles tens of MB/s; a 200-byte message is microseconds. The ratchet step is an HMAC, also microseconds. Battery impact is dominated by the network, not the crypto.
  • What the product loses: server-side search (the client indexes locally), link previews generated by the server, spam classification on content (moved to metadata and user reports), and server-restorable history (backups must be client-encrypted with a user-held key).

Variants and when each wins

Signal protocol (X3DH + Double Ratchet)

  • Pairwise encryption with a fresh key per message and asynchronous first contact through pre-keys. It wins for consumer messaging because it gives forward secrecy and recovery after a compromise, and it is the standard.

Transport encryption only (TLS) with server-side plaintext

  • Messages are encrypted on the wire but the server reads them. It wins when the product needs search, moderation, link previews, cloud history and easy multi-device; it offers no protection against the provider, insiders or a server breach, which is where most enterprise chat sits.

Sender keys for groups

  • Each sender has one symmetric key per group, distributed to members pairwise, and encrypts each message once with it. It wins for large groups by keeping fan-out at one ciphertext per member; forward secrecy is weaker within a group epoch and keys rotate on membership change.

MLS (Messaging Layer Security)

  • An IETF standard that uses a tree of keys so a membership change costs logarithmic rather than linear work. It wins for very large groups and interoperability; it is newer and more complex to implement.

Recommendation: Signal protocol for one-to-one and small groups, sender keys or MLS for large groups, and TLS-only when the product genuinely needs server-side content features and users accept that.

Failure modes

Key substitution by the server

  • Nothing visible happens, which is the problem: the server could hand Alice a key it controls and read everything. It happens because clients trust the key directory. Show safety numbers users can verify out of band, publish keys to a transparency log, and warn loudly in the conversation when a key changes.

Lost device means lost history

  • A new phone starts with an empty chat list. It happens because history lived only on the old device and the server holds ciphertext it cannot re-encrypt for the new keys. Offer client-side encrypted backups protected by a user-held passphrase or recovery key, and explain the trade-off clearly.

Pre-key exhaustion

  • New conversations with a popular account fail or fall back to a weaker handshake. It happens because one-time pre-keys are consumed by new contacts faster than the client replenishes them. Keep a last-resort signed pre-key, have the server tell the client when the count is low, and rate limit pre-key fetches.

Metadata exposure

  • The provider still knows the social graph. It happens because routing a message requires knowing the recipient. Minimise how long metadata is retained, consider sealed sender designs where the sender identity is encrypted too, and document honestly what is and is not hidden.

Multi-device inconsistency

  • A message appears on the phone but not on the laptop. It happens because encryption for one device failed or that device's key set was stale. Use per-device delivery receipts, keep the device list synchronised and signed by the account, and re-encrypt when the device list changes.

Interviewer follow-ups

Answer out loud before you open each one.

What do you lose on the server side with end-to-end encryption?

Any feature that needs plaintext: search, link previews, content-based spam and abuse detection, server-generated summaries, and the ability to restore history to a new device. Each moves to the client or becomes a client-encrypted backup. Metadata-based abuse signals and user reports replace content scanning.

How do you support a user with three devices?

Each device has its own key pair and is listed under the account with a signature from an existing device. Senders encrypt a message once per recipient device. Adding a device requires an existing device to approve and sign it, so the server cannot silently add one.

How does group messaging stay affordable with E2EE?

With sender keys, each member distributes one symmetric key to every other member pairwise, once, and then encrypts each message once with it. Fan-out stays at one ciphertext per member, the same as plaintext. Membership changes rotate the key, which is why very large groups move to MLS, where that rotation is logarithmic.

What is forward secrecy and why does the ratchet matter?

Forward secrecy means a key compromised today cannot decrypt messages from yesterday. The ratchet derives a new key for every message and discards the old one, and mixes in fresh Diffie-Hellman material on every round trip so even a compromised current state heals once both parties exchange messages.

Connections

Pairs with
Push to the browser in real time · Deliver to users who are offline · Find which server holds a user · Protect data and secrets · Terminate TLS at the edge
Pulls against
Catch spam and abuse · Full-text and fuzzy search · Reach devices through platform push
Used in
Chat App (WhatsApp / Messenger)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon DynamoDBKey directoryFirestore, BigtableCosmos DBScyllaDB, Apache Cassandra
Amazon EC2 Auto ScalingChat serviceCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon KeyspacesInbox (ciphertext)BigtableManaged Instance for Apache CassandraApache Cassandra, ScyllaDB