Support
A help desk on Gmail that cannot open two tickets for one conversation
The same client email lands in a dozen mailboxes. Here is how one conversation became exactly one ticket, enforced by the database.
The problem
The team ran client support in Freshdesk. I replaced it with a help desk inside our own platform, built on the shared help address in Gmail. A client writes in, a ticket opens, the right person answers.
The first version kept breaking. Senders writing from personal addresses were dropped. Sync could skip mail for good. Then in late May it started opening pairs of duplicate tickets, one to three seconds apart.
Each break had been fixed on its own. What was missing was a model of what a ticket actually is. On an active thread the same message can land in seven to eleven mailboxes, and every copy was a chance to open another ticket.
Why the old key failed
The pipeline recognised a message by a fingerprint of its content: sender, subject and timestamp. That is not unique per message and not stable across mailbox copies. The duplicates came from the timestamp being written in two formats.
// Two copies of the same email, read through two code paths:
fingerprint('ana@client.com', 'Site is down', '2026-05-29T14:03:11+00:00')
fingerprint('ana@client.com', 'Site is down', '2026-05-29 14:03:11+00')
// Different strings, so two events, so two tickets.The obvious fix was to make the fingerprint consistent. I rejected it. It is a weaker version of an identifier that email already provides.
The decision
A duplicate ticket for one conversation becomes impossible, not merely unlikely.
Every email carries a Message-ID, and every reply names the message that started the thread. So a conversation has a natural key: the Message-ID at the root of the reply chain. The opening email and every reply to it resolve to the same key.
function conversationKey(msg: MessageHeaders): string | null {
// A reply that lists its ancestors: the first entry is the root.
if (msg.references?.[0]) return keyFor(msg.references[0]);
// No parent at all: this message starts the conversation.
if (!msg.inReplyTo) return keyFor(msg.messageId);
// A parent but no ancestor list: the caller walks up to the real root.
// Keying on the nearest parent would split one conversation in two.
return null;
}
const keyFor = (rootMessageId: string) => normalize(rootMessageId);Notice what is not in the key: the client. That was the original defect. Two copies of one email could resolve to different clients and each open a ticket.
Let the database enforce it
I considered a lock and rejected it. A lock is still coordination, and one missed call site silently reopens the race. A unique index has no call sites to miss.
CREATE UNIQUE INDEX one_live_ticket_per_conversation
ON tickets (conversation_key)
WHERE conversation_key IS NOT NULL
AND NOT is_deleted;The two conditions are deliberate. Ordinary tasks have no key and are left alone. A ticket that was merged away is soft-deleted, so it leaves the index and never blocks the ticket it was merged into.
When two workers race, one insert wins and the other fails. The loser does not retry or give up. It links its message to the ticket that won.
try {
return await tickets.create({ conversationKey: key, ...fields });
} catch (error) {
if (!isUniqueViolation(error)) throw error;
const existing = await tickets.findLiveByConversation(key);
return await tickets.linkMessage(existing.id, message);
}Rolling it out
- Merge the duplicates already in production. There were three conversations with two tickets.
- Backfill the key onto existing tickets.
- Only then create the index. Two live tickets sharing a key would make it fail to build.
The first ticket decides which client a conversation belongs to, and anyone can correct it. Two tickets for one conversation is silent and expensive. A ticket under the wrong client is visible and one click to fix.
What changed
- Before: five pairs of duplicate tickets in a few days. After: the duplicate check returns zero rows.
- About 99% of replies carry the reply chain the key depends on.
- Gmail push delivers mail in seconds, with a twenty-minute reconcile as a backstop for anything push misses.
What I can't tell you from the record: how many tickets it handles, or response times. Those were never written down.