How it works
The sender submits a client-generated idempotency key, a token identifying one logical send, with the conversation ID and text. The chat service validates membership, assigns a server message ID and position, persists the record, and acknowledges acceptance. Only then should the sender show a durable “sent” state. A retry with the same key must not create a second message.
Follow one send. Ana’s phone sends “lunch?” with key k-71. The service stores it as message 1042, position 88, but the acknowledgement is lost in a dead zone. Her phone retries k-71, and the service finds the existing row and returns message 1042 without writing another. Ben’s laptop, connected to a gateway, receives a live push. Ben’s phone is asleep, so it gets a notification, wakes, and fetches everything after position 87.
The delivery worker checks each recipient device’s live route. It pushes to connected devices and may ask the notification service to wake inactive ones. Push notifications are hints; clients retrieve history from storage to fill gaps. Apple’s APNs, for instance, keeps only the most recent notification per app for an offline device, so earlier ones can vanish.
Distinguish accepted, delivered to a device, and read by a person. These acknowledgements may arrive later or never. A network timeout after persistence is ambiguous to the sender, which is why idempotency and client-side reconciliation matter. The cost is a lookup on every send and keys retained for a few days; a unique index on conversation and key covers both.
The server saves a message but its acknowledgement is lost. The sender retries. What should the service return?
Look up the original idempotency key and return the existing message ID and status. A new row would duplicate the message. The receiver can still retrieve the persisted record even if the sender never saw the first acknowledgement.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Persist before claiming durable acceptance.
- Retries and offline catch-up need stable IDs and idempotency; push is not the source of truth.