What Happens After You Click Send?
From one screen to another.
before taking off, we first have to get the motivation on why something as simple as sending a message needs more than just two screens. supposed we have a really simple chat application where user A wanna send “u there?” to user B.
from our perspective, we type the message, click send, then wait until it shows up on the other person's screen. the whole thing could happen in less than a second. but looking at what our software actually has to do, there are several steps happening between those two screens.
for the sake of our argument, we are gonna build a small browser-based chat application. this is one possible design, and we are not trying to explain the exact architecture of WhatsApp or any other particular product.
the first architecture seems straightforward. user A sends a request to our server, the server stores the message inside a database, then forwards it to user B. be that as it may, a few questions already came up. what happens if user B is offline? what if our server receives the message but the reply never reaches user A? and how do we know the message actually arrived?
therefore, we can start by separating the journey into a few parts.
- How the message leaves our screen
- How our server accepts and stores it
- How the other person receives it
- What happens when one of those steps fails
What happens on our screen first
supposed we already type “u there?” and click send. our application could immediately put that message into the conversation, even before the server replies. this is usually called an optimistic update.
the idea is to make the interface feel responsive. but since we have not received a confirmation yet, the message should still have a pending state. showing a message inside our own chat window does not tell us whether the server has stored it.
at this point, our client also creates an identifier for this send attempt. suppose the value is msg_1234. along with the text, we include the conversation_id and this identifier in the request. our server can use the authenticated session to determine who is sending it, instead of trusting whatever sender_id the client provides.
we are gonna come back to the identifier later, because it becomes useful when our connection starts acting up.
How the message reaches our server
our browser needs a connection to the server. if there is no existing connection, it first needs to resolve the hostname and establish the connection required by the protocol. but we do not necessarily repeat all of that for every message, since a chat application can keep a connection open.
one option is to send messages through ordinary HTTPS requests and use a separate mechanism for receiving updates. another option is WebSocket, which lets the client and server send messages over a connection that stays open. for the rest of this example, suppose we use a secure WebSocket connection. [ 1 ]
this gives our server a way to send a new message to user B without waiting for B to ask whether anything has changed. be that as it may, having an open connection does not tell us whether our application has saved a particular message. we still have to define our own application-level acknowledgement.
also, the word secure here refers to protecting the connection using TLS. it does not automatically mean we have end-to-end encryption where only the participants can read the message. that would require another design decision beyond the delivery flow we are discussing here. [ 1 ]
Before we say the message was sent
when the request arrives, our server has to check a few things. is the session valid? does this user belong to the conversation? is the message within the size limit? are we allowing this user to send at this rate?
these checks matter because a request reaching our server does not automatically make it a request that we should accept.
suppose everything is valid. our server stores the message with its conversation, sender, and identifier. in this design, we only reply with an accepted acknowledgement after the database transaction commits successfully under our chosen durability configuration.
therefore, the pending message on user A's screen can now move into a sent state. what we mean by sent here is that our service has accepted and stored it. we have not said that user B received it, or that user B has read it.
this might sound like a small distinction, but without defining it we could end up showing a successful status at a point where the message can still disappear before it is stored.
What if the reply never reaches us?
suppose our server successfully saves msg_1234. right after that, the connection drops before user A receives the acknowledgement. looking from the server, the message exists. looking from the client, the send attempt is still uncertain.
our first instinct might be to retry. but if every retry creates a new message, user B could end up receiving “u there?” two or three times even though we only clicked send once.
this is where our identifier comes back. when retrying the same send operation, the client sends the same identifier. our server keeps a uniqueness rule for that identifier within the sender's scope, and can return the result of the earlier accepted operation instead of inserting another message. it should also reject reuse of the same identifier with different content. [ 2 ]
this behaviour is called idempotency. we are making repeated attempts at the same operation produce the same intended effect. therefore, we can retry an uncertain request without treating every attempt as a new message. [ 2 ]
be that as it may, idempotency still needs actual storage and coordination on the server. simply attaching a random identifier to the request does not solve anything unless our server uses it to detect repeated operations.
Getting the message to the other screen
suppose user B is online and connected to the same server. we could look up B's connection and send the saved message through it. for a small application, this could be enough to get started.
but before we say everything is done, there is another failure that we have to consider. what happens if our server saves the message, then crashes before forwarding it?
if the only delivery plan lives in the memory of that server, the database has our message but nothing is making sure it reaches user B. one approach is to store the message and a pending delivery event inside the same database transaction. a worker can later read the event and attempt delivery. this is the idea behind the transactional outbox pattern. [ 3 ]
the worker can forward the event through a broker when we have several servers. but introducing a broker does not remove all the failure cases. the worker could publish an event and fail before recording that it finished, so another attempt can publish it again. the receiving side still needs to recognise duplicate events using the message identifier. [ 3 ]
therefore, we have to think about both saving the message and remembering the work that still needs to happen after it is saved.
Sent, delivered, and read
by now we have a few different points where we could show a check mark. to keep the meaning clear, suppose our application uses these definitions:
- Sent: our service has accepted and stored the message.
- Delivered: at least one recipient device has acknowledged receiving the message.
- Read: the recipient client has reported that the conversation was viewed up to this message.
these are product decisions for our example. another application could define the states differently, especially when one account has multiple devices.
the important part is that the status comes from an event that we can actually observe. a successful write to a socket is not the same event as the recipient application acknowledging a message. and a read receipt tells us what the client reported, rather than proving someone understood the text.
therefore, if we wanna show a delivered state, user B's client needs to send a receipt after receiving the message. our server records that receipt and forwards the update to user A. if we also support read receipts, we need another update when B views the conversation.
What happens when user B is offline?
looking back at our first architecture, we were assuming that both users are connected at the same time. but in reality user B could be asleep, have no signal, or simply close the app.
our message should still exist in the database. when B reconnects, the client can ask for messages after its last known conversation position. the server retrieves the missing records and the client combines them with what it already has, ignoring identifiers that are already present.
this also helps if the live connection drops briefly while the application is open. instead of assuming the connection delivered everything, we give the client a way to catch up from stored history.
we could additionally send a push notification to let B know that something is waiting. but our design should still retrieve the actual conversation from stored messages when the app opens. notification delivery can be delayed depending on the device and its state. [ 4 ]
therefore, our live connection handles the immediate updates, while stored history gives us somewhere to recover from when those updates are missed.
Keeping the conversation in order
another problem occurs when several messages are being sent around the same time. suppose user A sends “wait” and then “nvm”. we probably do not want B's conversation to permanently show them in the opposite order.
we could start by sorting on timestamps. but if those timestamps come from different devices, their clocks might not agree. even server timestamps alone do not define a tie-breaker when two events have the same value.
for this example, we can have an authoritative writer assign a monotonically increasing sequence inside each conversation. the client can then use that sequence to place the committed messages in order. pending messages can stay in a temporary position until their committed position arrives.
be that as it may, this means writes within the same conversation need coordination. when we distribute the work across servers, we have to preserve that per-conversation ordering decision. we are choosing an ordering rule for the product, rather than discovering a perfect global order of everything that every user did.
When one server is no longer enough
given time, our application starts to grow. suppose user A is connected to server 1 while user B is connected to server 2. the database still has the message, but server 1 cannot directly use a connection that only exists inside server 2.
we now need a way to route the delivery event to the server that owns B's connection. a shared connection registry and a broker are possible pieces of that design. when B reconnects somewhere else, that routing information also needs to change.
and since components can fail, the worker and the receiving server need acknowledgement and retry rules. a broker acknowledgement only covers its own part of the journey. it does not automatically tell us that the final recipient device has received the chat message. [ 5 ]
this is also where the earlier decisions start to help us. duplicate handling makes retries less dangerous. stored history lets a reconnecting client catch up. the outbox keeps pending work recoverable after a server crashes.
Conclusion
looking back at the simple thing that we started with, clicking send asks our software to do more than move some text between two screens. we have to accept the request, save the message, find the recipient, and keep track of what actually happened along the way.
be that as it may, we do not have to introduce every component from the beginning. a small application could start with one server and one database, as long as the meaning of its acknowledgements and its recovery behaviour are clear.
the problems become more visible when the network drops, a recipient goes offline, or our application grows across multiple servers. therefore, the question is not only how fast the message arrives when everything is fine, but how our software behaves when part of that journey does not go as planned.
with that being said, the little check mark beside our message carries quite a lot of decisions behind it. understanding those decisions helps us build a conversation that still makes sense when the connection does not.