Skip to content
keshav
Work
01 — Active · 2025

Letterly

asynchronous correspondence across the world

PostgreSQL · Redis · Workers · Next.jsDesign, backend, everythingSource ↗

Problem

Modern chat pretends distance doesn't exist. Letterly does the opposite: it reintroduces the delay. Letters travel in real time across real timezones — a note written in Bangalore arrives in Lisbon hours later, because that's when it would actually arrive. The question was how to build a messaging system whose core feature is not being instant.

Thinking

The delivery delay is not a bug to hide, it's the product. So the delay had to be simulated honestly: a scheduler computes an arrival time from geographic distance and a routing model, then the letter is simply not released until then.

That pushed everything toward a queue-shaped architecture. Write path stays thin — validate, persist, enqueue, acknowledge. All the interesting work happens out of band, where it can be retried, observed, and reasoned about.

I chose Postgres as the single source of truth and Redis only for what Redis is genuinely good at: scheduled job sets and short-lived presence. One durable store, one ephemeral one. Anything more felt like résumé-driven infrastructure.

Architecture

client
Next.js app
edge
APIAuth
core
PostgresRedis ZSET
workers
DispatchArrivalMail transport
Writers go straight to Postgres; Redis holds the scheduled set; workers drain it as letters become deliverable.

Engineering

Data flow
A letter is written, validated, inserted into Postgres, and pushed onto a Redis sorted set scored by its computed arrival timestamp. The write path never touches transport. A dispatch worker pops due entries in a loop, marks them in-flight, and hands them to the arrival worker.
Persistence
Postgres owns every state transition. Redis is disposable: the scheduled set can be rebuilt from the letters table at any moment, which means losing Redis degrades latency, never data.
Concurrency
Workers claim letters with a compare-and-swap update (pending → in-flight) guarded by a visibility timeout. A crashed worker's letters are reaped after the timeout expires and re-enqueued — at-least-once delivery, with dedupe on the client via letter IDs.
Caching & failure modes
Mailbox reads cache briefly at the edge; nothing else needs caching because reads are cheap and correctness matters more. Duplicate delivery, double-dispatch under timeout expiry, and clock skew between app and worker servers were all failure modes I had to design against explicitly.

What broke

  • Dispatch delivered letters twice

    The first version popped from the ZSET, then sent, then marked — a crash between send and mark caused duplicates. Moving to claim-then-send with a visibility timeout fixed it, at the cost of reintroducing rare duplicates the client now dedupes.

  • Timezone math was wrong for weeks

    I treated arrival time as a UTC offset calculation and ignored DST. A test letter scheduled for spring-forward arrived an hour early. The fix was boring and correct: compute everything in UTC, convert only at the boundary for display.

  • The 'simple' polling loop melted a small instance

    Polling the ZSET every 500ms with blocking reads starved the same box that ran Postgres. Separating worker and database onto different processes — even on one machine — fixed p99 latency immediately.

What I'd change

  • Move dispatch to a proper partitioned queue once letter volume justifies it — the ZSET is a single point of contention.
  • Idempotency keys on the write path instead of client-side dedupe.
  • An event log (outbox pattern) so mail transport, notifications, and future features all consume the same history.