The in-app timeline is what a user sees when they open the notification list. It has to stay correct when Kafka delivers the same event twice. Newest items go first, the unread count has to be instant, and old items have to clean themselves up. This post covers both halves: how duplicates get rejected, and how the DynamoDB table is shaped.
Part A: Idempotency
Kafka promises at-least-once delivery, not exactly-once. After a restart, a rebalance or a retry, the same event can arrive again. Without protection the user sees duplicates and the unread badge drifts upward.
A deterministic notification ID
The service builds the notification ID from the event's eventId. It hashes the eventId with SHA-256 and keeps the first 32 hex characters. The same event always gives the same ID, so a duplicate is recognisable without a lookup. Events with no eventId or no userId are skipped and logged.
One atomic transaction
Storing a notification is a single DynamoDB transaction with three parts:
- A marker item for that notification ID, with a condition that it must not already exist.
- The notification item itself.
- An increment of the user's unread counter.
If the marker is already there, DynamoDB cancels the whole transaction. The service reads that as "duplicate", logs it and returns without an error. The counter update sits in the same transaction, so a duplicate can't bump the unread count a second time.
Why it holds up
- There's no race window. The guard and the write succeed or fail together, so two concurrent copies can't both get through.
- It works across time. The marker lives as long as the notification does, so a late retry still gets caught.
- Real errors still surface. Only a failed marker condition counts as a duplicate. Any other failure is counted, logged and rethrown, so Kafka's error handling takes over.
Where the guarantee stops
This protects the stored timeline: one event gives at most one stored notification and a correct unread count. It doesn't make push and email exactly-once. After the store step the listener still sends the push or email, even if the store said "duplicate". So external sends are at-least-once.
There's also a catch in how the delivery status is updated afterwards. It uses the sort key returned by the store step, and that key includes a timestamp taken when the notification was built. On a duplicate, the key that comes back is the new one, not the one that was stored. The status update then points at an item that doesn't exist, and a DynamoDB update creates a missing item instead of failing. The result is a stray item holding only that key and a delivery status.
From reading the code, I'd expect that stray item to show up in the user's timeline and never expire, because it has no time-to-live. Meanwhile the real notification's status doesn't get updated. I haven't reproduced it, so treat it as a known gap. Returning the stored key on a duplicate, or skipping the status update when the store reports one, would close it.
Part B: Table design
One partition per user
Everything for a user lives under the partition key USER#<userId>, and the sort key tells the items apart:
-
NOTIF#<reverseTimestamp>#<notificationId>is a notification. -
METAis a per-user summary: unread count, last notification time, last "read all" time. -
DEDUP#<notificationId>is the idempotency marker.
Reading a timeline is one query against one partition.
Newest first without sorting
DynamoDB returns items in sort-key order. The timestamp in the sort key is stored reversed (a maximum value minus the creation time), so newer items come first and nothing has to be sorted.
The unread list: a sparse index
Unread notifications carry two extra attributes that feed a secondary index, GSI1. Read ones don't have those attributes, so they aren't in the index at all. Marking a notification as read removes the attributes and the item drops out of the index. That means the unread query only ever touches unread items, however long the history gets.
The unread counter
The count sits on the META item, so reading it is a single key lookup.
- Storing a notification increments it, in the same transaction as the write.
- Marking one as read decrements it, with a condition that it must be above zero, so it can't go negative.
- "Mark all as read" updates only the
METAitem (count to zero, time recorded). It doesn't rewrite every notification, so it costs the same for 5 notifications or 5,000.
Pagination without leaking internals
DynamoDB's "last evaluated key" gets encoded into an opaque, URL-safe token. Clients send it back unchanged to get the next page and never see a key name. The table design can change later without breaking the API.
Expiry
Each notification has a time-to-live of 30 days from creation, plus a random extra of up to 24 hours. The random part spreads deletions across the day instead of bunching them up. User preferences are in a separate table.
Where this leaves us
Deterministic IDs and an atomic conditional write give the timeline solid duplicate protection. The key design makes the common reads cheap: one query for the timeline, a sparse index for unread, one lookup for the count.
Top comments (2)
This is the part most teams learn the hard way: the dedup marker has to live in the same atomic write as the notification itself, or a retry lands in the gap between them. I liked that you called out where the guarantee stops, too — the timeline can be exactly-once while delivery status still drifts, and that stray-item case is exactly the kind of thing that only shows up in production. In B2B work we see the same pattern with inbound documents: a deterministic key plus a conditional write beats any amount of retry tuning after the fact.
Thanks, that's the bit I'd have liked to know earlier too. Retries don't create the duplicate by themselves. The gap between "wrote the marker" and "wrote the notification" does.
Your inbound documents example is the same shape. The part that took us a while to get right is what goes into the key. If it comes from the business identity of the thing (sender, document number, version) and not from the delivery attempt, a replay from a different path still collapses into one write. If it includes anything attempt-specific, like a request ID or timestamp, you've rebuilt the duplicate problem with extra steps.
On the drift, we ended up treating delivery status as something to reconcile, not something to guarantee. A small sweep that compares the timeline against provider receipts catches the strays without making the write path any heavier.
Curious whether you expire your dedup keys, or keep them for the life of the document?