<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: kevindev</title>
    <description>The latest articles on DEV Community by kevindev (@kevindev27).</description>
    <link>https://dev.to/kevindev27</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4012755%2F59157d97-36db-4340-addc-f6c6776820fb.png</url>
      <title>DEV Community: kevindev</title>
      <link>https://dev.to/kevindev27</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kevindev27"/>
    <language>en</language>
    <item>
      <title>Circuit Breakers for Email Verification APIs</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Tue, 24 Nov 2026 05:25:43 +0000</pubDate>
      <link>https://dev.to/kevindev27/circuit-breakers-for-email-verification-apis-25e1</link>
      <guid>https://dev.to/kevindev27/circuit-breakers-for-email-verification-apis-25e1</guid>
      <description>&lt;p&gt;Email verification is often treated as a single API call: create a token, call a provider, and tell the user to check the inbox. That model breaks when the provider slows down or returns intermittent errors. A request timeout can turn into a retry storm, duplicate messages, and a registration flow that is impossible to explain.&lt;/p&gt;

&lt;p&gt;A circuit breaker gives the delivery dependency an explicit failure policy. It does not make an email provider reliable. It keeps provider trouble from becoming application-wide trouble, while preserving enough state to resume delivery safely.&lt;/p&gt;

&lt;p&gt;This pattern is useful whether the recipient uses a normal mailbox or a &lt;a href="https://tempmailso.com" rel="noopener noreferrer"&gt;use and throw email&lt;/a&gt; for a short-lived workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why verification email delivery needs a circuit breaker
&lt;/h2&gt;

&lt;p&gt;A circuit breaker separates provider rejection, lost responses, and slow responses into explicit states:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Closed:&lt;/strong&gt; requests flow normally and failures are counted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Open:&lt;/strong&gt; new delivery attempts fail fast for a cooldown period.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Half-open:&lt;/strong&gt; one or a few probe attempts test whether the dependency recovered.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The user-facing API should not expose provider details. Return a documented status such as &lt;code&gt;202 Accepted&lt;/code&gt; for queued work or &lt;code&gt;503&lt;/code&gt; when the service cannot accept more work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model delivery states separately from account state
&lt;/h2&gt;

&lt;p&gt;Do not store “email sent” as a boolean on the user row. Account activation and delivery are related, but they have different lifecycles. A verification request can be pending while the circuit is open, and a previously delivered message can still be valid after a later resend fails.&lt;/p&gt;

&lt;p&gt;A compact delivery record might contain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;verification_deliveries&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;request_key&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;token_digest&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;attempts&lt;/span&gt; &lt;span class="kt"&gt;integer&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;next_attempt_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;last_error_code&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;updated_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;request_key&lt;/code&gt; is an idempotency boundary. If the client retries after losing a response, the server can return the existing delivery instead of creating a second token and job. Keep the raw token out of this table; store a digest.&lt;/p&gt;

&lt;p&gt;Draw &lt;a href="https://dev.to/bitheirstake/email-test-fixtures-need-a-trust-boundary-map-5dn8"&gt;a trust boundary map for email fixtures&lt;/a&gt; first so log-safe metadata is clear.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implement bounded retries in a Node.js worker
&lt;/h2&gt;

&lt;p&gt;The worker needs two limits: a maximum number of attempts for one delivery and a maximum time for the circuit to remain open. Exponential backoff with jitter prevents every worker from retrying on the same second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;nextDelay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1_000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.75&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isRetryable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"ETIMEDOUT"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
    &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"PROVIDER_5XX"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
    &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"RATE_LIMITED"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On a retryable error, increment &lt;code&gt;attempts&lt;/code&gt;, set &lt;code&gt;next_attempt_at&lt;/code&gt;, and release the job. On a permanent error, mark the delivery as failed and stop. On a timeout after the provider may have accepted the request, do not blindly create a new token; first use the request key or provider idempotency key to make the next attempt replay-safe.&lt;/p&gt;

&lt;p&gt;The circuit can be process-local in a small service, but a multi-instance deployment needs shared state. Otherwise one instance can stop sending while the others continue. Use a short lease for the half-open probe so multiple workers cannot all declare recovery at once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Persist attempts and cooldowns in PostgreSQL
&lt;/h2&gt;

&lt;p&gt;PostgreSQL is useful here because the state transition can be made atomic. A worker should claim a due row and update its attempt metadata in one transaction, rather than reading a row and hoping another worker does not claim it first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;candidate&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;verification_deliveries&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next_attempt_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;next_attempt_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;
  &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="no"&gt;SKIP&lt;/span&gt; &lt;span class="no"&gt;LOCKED&lt;/span&gt;
  &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;verification_deliveries&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'sending'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updated_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;candidate&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;candidate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the breaker is open, the worker should avoid claiming unlimited rows and immediately putting them back. Either pause polling for the cooldown or claim only a small batch for visibility. This keeps the queue honest and avoids a busy loop that looks like useful work in metrics.&lt;/p&gt;

&lt;p&gt;Record counters for open transitions, probes, permanent failures, retries, and delivery latency. Do not log tokens, complete message bodies, or recipient addresses by default. A label like tem email or temp mailid does not make data safe; contents and retention rules matter more than the fixture name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the failure path
&lt;/h2&gt;

&lt;p&gt;Unit tests can cover the state machine, but integration tests should exercise the boundary between the API, queue, database, and provider adapter. Test at least these cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a timeout after the provider accepted the request&lt;/li&gt;
&lt;li&gt;repeated &lt;code&gt;429&lt;/code&gt; responses followed by recovery&lt;/li&gt;
&lt;li&gt;two workers claiming the same delivery&lt;/li&gt;
&lt;li&gt;a client retry with the same idempotency key&lt;/li&gt;
&lt;li&gt;a permanent rejection that must not be retried&lt;/li&gt;
&lt;li&gt;a half-open probe racing with another worker&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Run these scenarios with a fake provider that controls response timing. Keep the test inbox isolated by build and make the provider response part of the evidence. Teams working in cloud pipelines can review guidance on &lt;a href="https://dev.to/jasonmills94/isolate-email-tests-in-docker-ci-on-aws-3pc5"&gt;isolated email tests in Docker CI&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Assert on state transitions and request counts, not only on the final email; one inbox message can hide multiple provider calls.&lt;/p&gt;

&lt;h2&gt;
  
  
  Q&amp;amp;A: common circuit-breaker decisions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Should an open circuit reject every verification request?
&lt;/h3&gt;

&lt;p&gt;Usually it should accept a bounded request if the queue has capacity, then report that delivery is delayed. Reject when more work would breach a queue or retention limit.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical implementation checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Give each logical verification request an idempotency key.&lt;/li&gt;
&lt;li&gt;Store delivery state separately from account activation.&lt;/li&gt;
&lt;li&gt;Bound attempts, queue age, and provider timeouts.&lt;/li&gt;
&lt;li&gt;Add jittered backoff and a shared circuit state for multiple instances.&lt;/li&gt;
&lt;li&gt;Use a lease for half-open probes.&lt;/li&gt;
&lt;li&gt;Claim PostgreSQL jobs with row locking and &lt;code&gt;SKIP LOCKED&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Test lost responses, duplicate workers, rate limits, and recovery.&lt;/li&gt;
&lt;li&gt;Redact tokens and personal data before logs or CI artifacts are created.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A circuit breaker is a small state machine, but it changes the reliability contract of the whole verification flow. Once delivery has explicit states, bounded work, and replay-safe requests, provider failures become a manageable system condition instead of a mystery scattered across API logs and user reports.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>restapi</category>
      <category>node</category>
      <category>postgres</category>
    </item>
    <item>
      <title>Dead-Letter Queues for Email Verification Webhooks</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Mon, 23 Nov 2026 17:23:32 +0000</pubDate>
      <link>https://dev.to/kevindev27/dead-letter-queues-for-email-verification-webhooks-4b96</link>
      <guid>https://dev.to/kevindev27/dead-letter-queues-for-email-verification-webhooks-4b96</guid>
      <description>&lt;p&gt;Email verification is often treated as a small REST API feature: create a token, send a message, and accept the callback. In production, the callback is a distributed-systems boundary. Providers retry, requests arrive out of order, and a valid message can become useless after its token expires.&lt;/p&gt;

&lt;p&gt;A dead-letter queue gives the service somewhere explicit to put deliveries that cannot be completed safely. It also makes failures inspectable without turning every retry into another chance to activate an account incorrectly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why verification webhooks need a dead-letter path
&lt;/h2&gt;

&lt;p&gt;The webhook handler should not try to solve every problem synchronously. A provider may send a duplicate event, a downstream database can be briefly unavailable, or the message can refer to a verification token that has already expired. Treating all of those cases as HTTP 500 creates noisy retries. Treating all of them as HTTP 200 loses evidence.&lt;/p&gt;

&lt;p&gt;The useful split is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Retryable:&lt;/strong&gt; temporary database failure, timeout, or a dependency returning a transient error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terminal:&lt;/strong&gt; malformed payload, unknown event type, invalid signature, or an expired token.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reviewable:&lt;/strong&gt; a validly signed event whose business meaning is ambiguous, such as a token already consumed by another request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The endpoint can acknowledge receipt after it durably records the event. Processing then happens from a queue or worker. This keeps the public API small and makes the failure policy visible in code.&lt;/p&gt;

&lt;p&gt;For test environments, the same boundary matters when a disposable temporary email is used as a fixture. A &lt;a href="https://dev.to/bitheirstake/privacy-budgets-for-browser-email-tests-31gh"&gt;privacy budget for browser email tests&lt;/a&gt; should cover message retention and inspection, not only the mailbox itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model the delivery and processing states
&lt;/h2&gt;

&lt;p&gt;Do not represent delivery with a single boolean such as &lt;code&gt;processed&lt;/code&gt;. A small state machine is easier to query and safer to operate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;received -&amp;gt; processing -&amp;gt; completed
                    \-&amp;gt; retryable
                    \-&amp;gt; dead_lettered
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Store the provider event ID, event type, received timestamp, attempt count, next attempt time, and the last error. Keep the original payload in a restricted column or object store with a retention limit. Signature material and access tokens should be redacted before logs are emitted.&lt;/p&gt;

&lt;p&gt;The provider event ID is the first idempotency key. If a sender does not provide one, derive a stable key from the verified event attributes, but document the collision tradeoff. A request ID alone is not enough because a retry normally has a new transport request ID.&lt;/p&gt;

&lt;p&gt;It is also worth separating the delivery record from the verification result. A delivery can be completed while the token is rejected because it was expired. That is a business outcome, not necessarily a failed webhook.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use PostgreSQL as the idempotency boundary
&lt;/h2&gt;

&lt;p&gt;The worker needs an atomic claim. PostgreSQL can provide it without a distributed lock:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;verification_events_provider_id_idx&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;verification_events&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provider_event_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;verification_events&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'processing'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;locked_until&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kt"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'2 minutes'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;attempt_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;attempt_count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;verification_events&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'received'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'retryable'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;next_attempt_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;locked_until&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;locked_until&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;next_attempt_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
  &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="no"&gt;SKIP&lt;/span&gt; &lt;span class="no"&gt;LOCKED&lt;/span&gt;
  &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The unique index turns duplicate delivery into a harmless conflict. The lease prevents a crashed worker from holding an event forever. Keep the lease shorter than the provider's retry horizon and renew it only if processing genuinely can take longer.&lt;/p&gt;

&lt;p&gt;If completing the verification and recording an application event must be atomic, do both in one database transaction. For a separate message broker, use an outbox row in the same transaction, then publish the outbox record asynchronously. This is simpler to reason about than hoping two independent writes finish together.&lt;/p&gt;

&lt;p&gt;The service should also define what happens when a verification link is used twice. A consumed token can produce a stable &lt;code&gt;already_verified&lt;/code&gt; result. It should not be reactivated merely because a webhook was delivered again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implement bounded retries in Node.js
&lt;/h2&gt;

&lt;p&gt;Retry policy belongs in the worker, not in the HTTP controller. Use bounded exponential backoff with jitter, and cap both the delay and the total attempts. For example, the delay can grow from seconds to minutes while a separate maximum age prevents an old event from being processed after its business meaning has gone stale.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;nextAttempt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1_000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;jitter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;jitter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function only schedules work; it does not decide whether an error is retryable. Make that decision from typed errors or explicit result codes. A signature failure, malformed JSON, or expired token is terminal; a connection reset or lock timeout can be retried.&lt;/p&gt;

&lt;p&gt;A Node.js worker should claim one event, process it with a timeout, and update the row in a &lt;code&gt;finally&lt;/code&gt; path. If it dies, the lease expires and another worker can claim the event. Track lease recoveries because they can indicate a slow dependency or memory problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Review failures without replaying blindly
&lt;/h2&gt;

&lt;p&gt;A dead-letter queue is an operational inbox, not a second normal queue. Give each record a reason code, payload fingerprint, first-seen time, and a link to the relevant request trace. A review tool can offer three actions: mark resolved, replay after a human decision, or discard after retention requirements are met.&lt;/p&gt;

&lt;p&gt;Replay must create a new attempt record while retaining the original outcome. Never edit history so that a later success hides the initial failure. For privacy-sensitive fixtures, &lt;a href="https://dev.to/bitheirstake/privacy-aware-email-verification-boundaries-42b4"&gt;privacy-aware email verification boundaries&lt;/a&gt; are a useful reminder that debugging access should be narrower than application access.&lt;/p&gt;

&lt;p&gt;When a test suite needs a &lt;a href="https://tempmailso.com" rel="noopener noreferrer"&gt;throwaway email generator&lt;/a&gt;, make its mailbox lifecycle explicit in the fixture. A failed cleanup should not cause a worker to keep polling an inbox forever. Also, “tempail mail” is a common search typo, but it should remain plain text rather than becoming a misleading link.&lt;/p&gt;

&lt;h2&gt;
  
  
  A production checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Verify webhook signatures before inserting the event.&lt;/li&gt;
&lt;li&gt;Enforce a unique provider event ID.&lt;/li&gt;
&lt;li&gt;Separate transport acknowledgement from business processing.&lt;/li&gt;
&lt;li&gt;Classify errors as retryable, terminal, or reviewable.&lt;/li&gt;
&lt;li&gt;Use a lease and &lt;code&gt;SKIP LOCKED&lt;/code&gt; for concurrent workers.&lt;/li&gt;
&lt;li&gt;Cap attempts, event age, payload retention, and log exposure.&lt;/li&gt;
&lt;li&gt;Make token consumption atomic and replay-safe.&lt;/li&gt;
&lt;li&gt;Track dead-letter volume, retry age, lease recovery, and completion latency.&lt;/li&gt;
&lt;li&gt;Keep a manual replay trail with an operator and reason.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main design decision is not whether to add a queue. It is deciding which outcomes are safe to repeat and which must stop. Once that contract is explicit, a REST API, PostgreSQL transaction, and small Node.js worker can make email verification predictable even when providers behave unpredictably.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>restapi</category>
      <category>node</category>
      <category>postgres</category>
    </item>
    <item>
      <title>Bounded Polling for Email Verification APIs</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Mon, 23 Nov 2026 08:23:35 +0000</pubDate>
      <link>https://dev.to/kevindev27/bounded-polling-for-email-verification-apis-40fa</link>
      <guid>https://dev.to/kevindev27/bounded-polling-for-email-verification-apis-40fa</guid>
      <description>&lt;p&gt;Email verification tests often fail for a reason that has nothing to do with the account being verified. The application sends a message, the test checks a temp mailbox immediately, and the test concludes that delivery is broken. A retry is added, then another retry, until the polling loop becomes an unbounded source of time and load.&lt;/p&gt;

&lt;p&gt;The better boundary is a polling budget. The test should define how long it is willing to wait, how often it will check, and what evidence it will preserve when the message does not arrive. This post shows a bounded Node.js poller and the REST API decisions around it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why email polling needs a budget
&lt;/h2&gt;

&lt;p&gt;Message delivery is asynchronous. The request that creates a verification challenge may finish before the delivery worker has accepted the job, and the worker may finish before the inbox exposes the message. Treating these stages as one synchronous operation makes failures hard to classify.&lt;/p&gt;

&lt;p&gt;An unbounded loop has three problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A stuck provider can keep a CI worker busy until the job-level timeout.&lt;/li&gt;
&lt;li&gt;Many parallel tests can turn frequent inbox checks into avoidable load.&lt;/li&gt;
&lt;li&gt;A final failure contains little information about whether the message was late, rejected, or never requested.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The polling policy should therefore be explicit: a deadline, a delay schedule, and a correlation value. The deadline is a test concern. It should not silently change the production verification token expiry or the REST API request timeout.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separate delivery latency from API timeout
&lt;/h2&gt;

&lt;p&gt;Keep the API responsible for accepting a verification request and returning a durable identifier. Keep the inbox adapter responsible for observing delivery. This separation lets a test distinguish “the API rejected the request” from “the API accepted it, but no matching message was visible before the deadline.”&lt;/p&gt;

&lt;p&gt;For example, the request can return a &lt;code&gt;202 Accepted&lt;/code&gt; response with a verification job ID. The response body should not promise that an email is already readable. A worker can attach the same ID to structured logs and, where supported, to provider metadata.&lt;/p&gt;

&lt;p&gt;I have found correlation IDs especially usefull when a test suite runs several accounts at once. &lt;a href="https://dev.to/kevindev27/correlation-ids-for-reliable-email-verification-apis-37dh"&gt;Correlation IDs for reliable verification APIs&lt;/a&gt; covers the logging side of this boundary. The poller should record the ID, inbox identifier, attempt count, and final reason, but never the full token or message contents.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implement a bounded Node.js poller
&lt;/h2&gt;

&lt;p&gt;The adapter below uses a deadline rather than a fixed number of attempts. That matters because a short delay schedule and a long delay schedule should not accidentally create different timeout behavior when the code is changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;waitForVerification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;readMessages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;inbox&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;subjectPattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;timeoutMs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;delaysMs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4_000&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timeoutMs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;readMessages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inbox&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;subjectPattern&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;delay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;delaysMs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;delaysMs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"verification_message_timeout"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"VERIFICATION_MESSAGE_TIMEOUT"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a few deliberate constraints here. The adapter receives &lt;code&gt;readMessages&lt;/code&gt; instead of importing a provider client, so it can be tested with a small fake. The last delay is reused, but the deadline still wins. If a message arrive just after the deadline, the test fails consistently instead of waiting forever.&lt;/p&gt;

&lt;p&gt;In production-like integration tests, make &lt;code&gt;readMessages&lt;/code&gt; filter by the run-specific inbox and correlation value. A broad “latest message” query is a race: another test can consume the result, and the failure will be flakey. Also make message parsing separate from polling. A malformed message should be reported as a parse failure, not mistaken for missing delivery.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the REST API contract observable
&lt;/h2&gt;

&lt;p&gt;The API that starts verification should return enough information for the test harness to follow one request without exposing secrets. A useful response shape is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"verification_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ver_123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"queued"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"correlation_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"test-run-42"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The verification endpoint can then expose a small status vocabulary such as &lt;code&gt;queued&lt;/code&gt;, &lt;code&gt;sent&lt;/code&gt;, &lt;code&gt;consumed&lt;/code&gt;, and &lt;code&gt;expired&lt;/code&gt;. Do not make the test infer state from arbitrary HTTP text or from a dashboard that is eventually consistent.&lt;/p&gt;

&lt;p&gt;The frontend may model the same lifecycle explicitly; &lt;a href="https://dev.to/ryanlee91/react-email-verification-as-a-state-machine-5d3d"&gt;the frontend verification state machine&lt;/a&gt; is a useful companion pattern. This distinction are important: the UI state is not proof that a message was delivered, and inbox visibility is not proof that a user consumed the token.&lt;/p&gt;

&lt;p&gt;When a poll times out, include the verification ID and correlation ID in the test failure. Include the number of attempts and the last observed message timestamp too. Never include the raw verification URL in CI output, because logs are often retained longer then the temporary inbox itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test slow, duplicate, and missing messages
&lt;/h2&gt;

&lt;p&gt;The poller deserves tests for both timing and classification:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A matching message appears on the first read and returns immediately.&lt;/li&gt;
&lt;li&gt;A matching message appears after several empty reads and is still found before the deadline.&lt;/li&gt;
&lt;li&gt;No message appears, and the error code is &lt;code&gt;VERIFICATION_MESSAGE_TIMEOUT&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A message from another test run is ignored.&lt;/li&gt;
&lt;li&gt;A duplicate matching message is handled according to a documented rule, usually the earliest message for the current verification ID.&lt;/li&gt;
&lt;li&gt;The inbox adapter rejects malformed provider data without retrying forever.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use a fake clock or an injected sleep function for unit tests. Then add a smaller integration test against the real provider path. The fake should verify the deadline logic; the integration test should verify authentication, filtering, and message parsing. A fixture named &lt;code&gt;tepm mail com&lt;/code&gt; or &lt;code&gt;tempail mail&lt;/code&gt; can be useful when checking input validation, but it should remain ordinary test data rather than a special production branch.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Give every verification request a correlation ID.&lt;/li&gt;
&lt;li&gt;Return a durable verification ID from the REST API.&lt;/li&gt;
&lt;li&gt;Set a total polling deadline, not only a retry count.&lt;/li&gt;
&lt;li&gt;Keep inbox reading behind an injectable adapter.&lt;/li&gt;
&lt;li&gt;Filter messages by the test run and verification ID.&lt;/li&gt;
&lt;li&gt;Record attempts and failure reasons without logging secrets.&lt;/li&gt;
&lt;li&gt;Distinguish API rejection, delivery timeout, parse failure, and token expiry.&lt;/li&gt;
&lt;li&gt;Test the poller with a fake clock and the adapter with a real integration path.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A temp mailbox is useful test infrastructure, but it is still an asynchronous dependency. Once the poll budget and state contract are explicit, slow delivery becomes a classified test result instead of a random failure. The service stays responsible for its API state, while the test harness stays responsible for observing delivery in a controlled way.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>restapi</category>
      <category>node</category>
      <category>testing</category>
    </item>
    <item>
      <title>Replay-Safe Verification Tokens in PostgreSQL</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Mon, 23 Nov 2026 05:23:12 +0000</pubDate>
      <link>https://dev.to/kevindev27/replay-safe-verification-tokens-in-postgresql-35pa</link>
      <guid>https://dev.to/kevindev27/replay-safe-verification-tokens-in-postgresql-35pa</guid>
      <description>&lt;p&gt;Email verification looks like a small feature: create a token, send a message, and mark the account as verified when the user clicks it. In a real REST API, the click can arrive twice, arrive after expiry, or race with a retry from a mobile client. If the endpoint treats every request as a fresh command, the database state becomes hard to explain.&lt;/p&gt;

&lt;p&gt;This post describes a replay-safe design for verification tokens in PostgreSQL. The same boundary is useful when a disposable email account is used in an integration test: the test should be able to repeat a request and still get a predictable result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a token endpoint needs a replay policy
&lt;/h2&gt;

&lt;p&gt;A verification token is a capability with a short lifetime. It should normally produce one successful transition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;issued -&amp;gt; consumed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Several other outcomes are possible:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The token does not exist.&lt;/li&gt;
&lt;li&gt;The token has expired.&lt;/li&gt;
&lt;li&gt;Another request already consumed it.&lt;/li&gt;
&lt;li&gt;Two requests tried to consume it at nearly the same time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These outcomes should not be hidden behind a generic 500 response. A client may safely retry a network timeout, but it needs to know whether the first request actually succeeded. An OAuth flow has a similar concern; &lt;a href="https://dev.to/sophiax99/oauth-state-needs-a-replay-boundary-fkf"&gt;an OAuth replay boundary&lt;/a&gt; is a useful related pattern.&lt;/p&gt;

&lt;p&gt;The important decision is to make the state transition authoritative in the database, not in application memory. A process restart, a second API instance, or a slow queue worker should not change the rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model the verification as a database state transition
&lt;/h2&gt;

&lt;p&gt;Keep the token record separate from the user record. This gives the API an audit-friendly place to record when a token was issued, consumed, and rejected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;email_verification_tokens&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;token_digest&lt;/span&gt; &lt;span class="kt"&gt;BYTEA&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;expires_at&lt;/span&gt; &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;consumed_at&lt;/span&gt; &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;email_verification_tokens_user_idx&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;email_verification_tokens&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Store a digest rather than the raw token. The raw value belongs in the link sent to the user, while a database leak should not immediately turn every unexpired link into a usable credential. The digest column also gives us a stable lookup key.&lt;/p&gt;

&lt;p&gt;Do not use a boolean such as &lt;code&gt;is_verified&lt;/code&gt; on the token alone. A timestamp tells us when the transition happened and makes support investigations less guessy. The user account can still have its own verified state, updated in the same transaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use PostgreSQL to claim a token once
&lt;/h2&gt;

&lt;p&gt;The consume operation should check validity and claim the row in one statement. A conditional update is often simpler and safer than selecting first and updating later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;email_verification_tokens&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;consumed_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;token_digest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;consumed_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;expires_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumed_at&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this returns one row, the request won the claim. If it returns zero rows, the API can perform a bounded follow-up lookup to distinguish expired, unknown, and already-consumed states. That lookup is informational; it must not be used to decide whether the token is valid.&lt;/p&gt;

&lt;p&gt;Then update the user inside the same transaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;email_verified_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;COALESCE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email_verified_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;COALESCE&lt;/code&gt; makes the user update tolerant of an already verified account, while the token claim remains one-time. This is a small but important distinction: verification of the account can be idempotent even though consumption of a particular token is not repeatable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the REST API response deterministic
&lt;/h2&gt;

&lt;p&gt;A useful endpoint contract might be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /v1/email-verifications/consume
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Return a success response only when the transaction claimed the token and updated the account. For a second click, use a stable conflict response such as &lt;code&gt;409&lt;/code&gt; with a machine-readable code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"verification_token_unavailable"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"reason"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"consumed_or_expired"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Avoid exposing whether an arbitrary token belongs to a real account. For unknown, expired, and consumed tokens, a deliberately similar response prevents the endpoint from becoming an account-enumeration oracle. The exact status code can vary with your product contract, but it should be documented and tested.&lt;/p&gt;

&lt;p&gt;If the client times out after the commit, a retry may receive the conflict response even though the first request succeeded. That is why the client should refresh account state after an ambiguous result. A request ID and structured logs make this diagnosis much less painfull.&lt;/p&gt;

&lt;p&gt;For test environments, isolate inboxes and credentials by run. &lt;a href="https://dev.to/jasonmills94/aws-ci-cd-needs-run-scoped-email-test-data-4m49"&gt;Run-scoped email test data&lt;/a&gt; helps prevent one build from consuming another build's token. A fixture named &lt;code&gt;temp gamil com&lt;/code&gt; should be treated as ordinary test input, not as a special case in production validation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test concurrency and expiry
&lt;/h2&gt;

&lt;p&gt;The most valuable tests are not only happy-path tests. Add cases for:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A valid token is consumed once and verifies the user.&lt;/li&gt;
&lt;li&gt;The same token is submitted twice; exactly one request claims it.&lt;/li&gt;
&lt;li&gt;Two concurrent requests use the same digest; one gets success and one gets the documented conflict.&lt;/li&gt;
&lt;li&gt;An expired token cannot be claimed, even if the application clock has not yet cleaned it up.&lt;/li&gt;
&lt;li&gt;A malformed token does not cause an expensive unbounded database query.&lt;/li&gt;
&lt;li&gt;A transaction rollback leaves both the token and user unchanged.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Run the concurrency case against a real PostgreSQL instance. Mocks rarely reproduce row locks, transaction isolation, or the timing around &lt;code&gt;RETURNING&lt;/code&gt;. Add a short comment to the test explaining the expected winner behavior; otherwise a future refactor may accidentally turn the race into two successes.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Hash verification tokens before storing them.&lt;/li&gt;
&lt;li&gt;Put expiry and consumption checks in the conditional update.&lt;/li&gt;
&lt;li&gt;Update token and account state in one transaction.&lt;/li&gt;
&lt;li&gt;Return a stable, documented error for replay and expiry.&lt;/li&gt;
&lt;li&gt;Avoid leaking account existence through token errors.&lt;/li&gt;
&lt;li&gt;Test concurrent requests against PostgreSQL.&lt;/li&gt;
&lt;li&gt;Log a request ID, token record ID, and outcome without logging the raw token.&lt;/li&gt;
&lt;li&gt;Keep email delivery separate from the database claim operation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The design is not complicated, but the ordering matters. Let PostgreSQL own the one-time state transition, let the REST API expose a predictable contract, and let retries be an expected behavior instead of an incident. That gives backend services a verification flow that remains understandable under duplicate clicks, slow networks, and parallel workers.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>restapi</category>
      <category>postgres</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Transactional Outbox for Email Verification APIs</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Sun, 22 Nov 2026 14:24:14 +0000</pubDate>
      <link>https://dev.to/kevindev27/transactional-outbox-for-email-verification-apis-3ge7</link>
      <guid>https://dev.to/kevindev27/transactional-outbox-for-email-verification-apis-3ge7</guid>
      <description>&lt;p&gt;Email verification looks like a two-step feature: create a user, then send a message. In a real backend, it is a distributed workflow. The database transaction can commit while the mail provider is unavailable, or a worker can send the same message twice after a timeout.&lt;/p&gt;

&lt;p&gt;A transactional outbox gives the workflow a durable handoff. The API records the verification state and the email command in one PostgreSQL transaction. A separate relay delivers the command and records the result. This keeps the REST API responsive while making failures visible and recoverable.&lt;/p&gt;

&lt;h2&gt;
  
  
  The delivery problem starts inside the database transaction
&lt;/h2&gt;

&lt;p&gt;Consider an endpoint such as &lt;code&gt;POST /v1/email-verifications&lt;/code&gt;. It may need to create a token, associate it with a user, and request an email. If the handler sends email before committing the transaction, the message can point at data that never commits. If it commits first and sends afterward, a process crash leaves a user waiting forever.&lt;/p&gt;

&lt;p&gt;Use a clear ownership boundary:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The API owns verification state.&lt;/li&gt;
&lt;li&gt;PostgreSQL owns the atomic change to that state.&lt;/li&gt;
&lt;li&gt;The outbox owns the request to deliver a message.&lt;/li&gt;
&lt;li&gt;A relay owns communication with the provider.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That boundary is easier to reason about than a request handler doing all four jobs. It gives operations a record of what should happen, even when delivery status are temporarily unknown.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separate verification state from email delivery
&lt;/h2&gt;

&lt;p&gt;The verification record should answer whether a token is valid. It should not depend on whether the last provider request returned a response. A simple model might contain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;email_verifications&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;token_hash&lt;/span&gt; &lt;span class="kt"&gt;bytea&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;expires_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;consumed_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;email_outbox&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;verification_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;event_key&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="kt"&gt;jsonb&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;available_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;sent_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;attempts&lt;/span&gt; &lt;span class="kt"&gt;integer&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;last_error&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The API insert both rows in one transaction. The &lt;code&gt;event_key&lt;/code&gt; can be derived from the verification ID and message purpose, so a retry of the request does not create a second logical email. The outbox rows is a command, not proof that a message was delivered.&lt;/p&gt;

&lt;p&gt;For links that grant access to a flow, the token should be stored as a hash and consumed with a conditional update. A &lt;a href="https://dev.to/sophiax99/tenant-bound-magic-links-for-saas-invites-5g84"&gt;tenant-bound magic link for SaaS invites&lt;/a&gt; shows why the token's scope matters: the recipient, tenant, and action should not be left implicit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use an outbox row as the handoff contract
&lt;/h2&gt;

&lt;p&gt;The relay claim available rows in small batches. PostgreSQL can support this with &lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt;, or the application can use a lease column with an expiry time. The important detail is that claiming a row is not the same as marking it sent.&lt;/p&gt;

&lt;p&gt;The relay then calls the provider with a provider-side idempotency key when that feature exists. After a successful response, it sets &lt;code&gt;sent_at&lt;/code&gt;. If the request times out, it increments &lt;code&gt;attempts&lt;/code&gt;, stores a safe error summary, and schedules a later attempt. This makes retries boring, which is a good thing.&lt;/p&gt;

&lt;p&gt;There is still a small uncertainty window: the provider may accept a message and the relay may crash before recording &lt;code&gt;sent_at&lt;/code&gt;. Provider idempotency, a stable event key, or a downstream deduplication rule is needed to handle that case. An outbox does not create exactly-once delivery by itself; it creates a durable and inspectable at-least-once process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the REST API retry-safe
&lt;/h2&gt;

&lt;p&gt;Clients and load balancers retry requests for ordinary reasons. The endpoint should accept an idempotency key, or derive a safe request identity from the user and verification purpose. A repeated request can then return the existing verification record without issuing a new token each time.&lt;/p&gt;

&lt;p&gt;The verification endpoint should make the state transition conditional:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;email_verifications&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;consumed_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;token_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;consumed_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;expires_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only one transaction should be able to change an unused token to consumed. Email delivery can be duplicated in a rare provider failure, but successful verification must not be duplicated. A &lt;a href="https://dev.to/sophiax99/oauth-test-tokens-need-a-trust-boundary-204"&gt;trust boundary for OAuth test tokens&lt;/a&gt; is a useful related reminder: test inboxes and message links are inputs to authentication, not authorities that define it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Relay, observe, and recover without double verification
&lt;/h2&gt;

&lt;p&gt;Track the outbox lifecycle with fields that support action: available, leased, sent, failed, and dead-lettered. Keep raw tokens, full message bodies, and provider credentials out of logs. Record a verification ID, event key, attempt count, and a redacted provider response instead.&lt;/p&gt;

&lt;p&gt;When a worker crash happens after send but before acknowledgement, the next worker should retry using the same event key. Alert on age and repeated failures, not only on process health. A queue that is technically running can still be quietly stuck.&lt;/p&gt;

&lt;p&gt;For manual staging checks, a &lt;a href="https://tempmailso.com" rel="noopener noreferrer"&gt;disposable temporary email&lt;/a&gt; inbox can be useful for inspecting a non-production message, but never send real credentials or customer data there. Seeded test data sometimes contains strings like tamp mail com; treat them as ordinary input and do not infer trust from the address.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical implementation checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Insert verification state and the outbox command in one PostgreSQL transaction.&lt;/li&gt;
&lt;li&gt;Give each logical message a stable, unique event key.&lt;/li&gt;
&lt;li&gt;Make token consumption conditional and single-use.&lt;/li&gt;
&lt;li&gt;Lease work with an expiry so a dead worker does not hold it forever.&lt;/li&gt;
&lt;li&gt;Use provider idempotency when available, while still designing for duplicates.&lt;/li&gt;
&lt;li&gt;Keep retries bounded and move persistent failures to a reviewable dead-letter state.&lt;/li&gt;
&lt;li&gt;Expose delivery age, attempts, and last failure without exposing secrets.&lt;/li&gt;
&lt;li&gt;Test the crash windows: before commit, after commit, after provider acceptance, and before acknowledgement.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Questions worth asking before shipping
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Does the outbox guarantee one email?&lt;/strong&gt; No. It guarantees that the request is durable. Exactly-once delivery requires cooperation from the provider or a deduplicating boundary.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can the API wait for the email provider?&lt;/strong&gt; It can, but that couples user-facing latency and availability to a downstream service. The outbox lets the API acknowledge durable work instead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What should a user see when delivery is delayed?&lt;/strong&gt; Return a stable verification state and allow a controlled resend. A resend should create a new event only when policy allows it, not on every network retry.&lt;/p&gt;

&lt;p&gt;The design is small, but the tradeoff is clear: PostgreSQL commits the user-visible state, the outbox records the delivery intent, and the relay handles an unreliable boundary. That separation makes email verification easier to retry, audit, and maintain as the service grows.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>restapi</category>
      <category>postgres</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Lease Expiry for REST Email Verification</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Sun, 22 Nov 2026 08:23:49 +0000</pubDate>
      <link>https://dev.to/kevindev27/lease-expiry-for-rest-email-verification-3o9a</link>
      <guid>https://dev.to/kevindev27/lease-expiry-for-rest-email-verification-3o9a</guid>
      <description>&lt;p&gt;Email verification is often implemented as a short sequence: create a token, send a message, and mark the user as verified when the token comes back. That model works until a worker retries, a user asks for a second email, or two API requests arrive at nearly the same time.&lt;/p&gt;

&lt;p&gt;The failure mode is simple: a verification flow look like one request in the product UI, but it is a distributed workflow in the backend. The REST API, PostgreSQL, queue, email provider, and browser all observe different parts of it.&lt;/p&gt;

&lt;p&gt;This is why I prefer a lease-based design. A lease gives one verification attempt a bounded period of authority. It also gives the service a clear answer to a common question: which token is allowed to change authentication state right now?&lt;/p&gt;

&lt;h2&gt;
  
  
  The failure mode: verification is not one request
&lt;/h2&gt;

&lt;p&gt;Consider this sequence:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The user submits an address.&lt;/li&gt;
&lt;li&gt;The API creates a verification record.&lt;/li&gt;
&lt;li&gt;A worker sends an email.&lt;/li&gt;
&lt;li&gt;The user clicks a link.&lt;/li&gt;
&lt;li&gt;The API consumes the token.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each step can be retried. A client can retry step one after a timeout, even though the first transaction committed. A queue can redeliver the send job. The user can click an old email after requesting a new one. Without an explicit state model, the service starts treating timing as business logic.&lt;/p&gt;

&lt;p&gt;The first email are no longer authoritative after a new verification attempt is created. That rule should live in the database, not only in a conditional inside a controller.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model the lease explicitly
&lt;/h2&gt;

&lt;p&gt;A verification attempt can have these fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;email_verification_attempts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;token_digest&lt;/span&gt; &lt;span class="kt"&gt;bytea&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'sent'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'consumed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'expired'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'revoked'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;lease_expires_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;consumed_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The token itself should be random and shown only to the user. Store a digest, just as you would for a password reset token. &lt;code&gt;lease_expires_at&lt;/code&gt; is the important part here: it limits the time during which this attempt can be consumed.&lt;/p&gt;

&lt;p&gt;When a new attempt is issued, revoke the previous pending attempt for that user. The transition should be deliberate, not an accidental side effect of a unique constraint. A partial index can still protect the invariant that only one active lease exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;one_active_verification_per_user&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;email_verification_attempts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'sent'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In practice, create the new attempt and revoke the old one in the same transaction. If the operation fails, the user should not be left with half of a new workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make PostgreSQL own the state transition
&lt;/h2&gt;

&lt;p&gt;The verification endpoint should perform one atomic update. It can hash the supplied token, select the matching row, and update it only when the row is still active:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;email_verification_attempts&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'consumed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumed_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;token_digest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'sent'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;lease_expires_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this returns no row, the endpoint can return a generic invalid-or-expired response. Do not reveal whether the token existed, belonged to another user, or had already been consumed.&lt;/p&gt;

&lt;p&gt;The transaction that consumes the token should also update the user’s authentication state, or record an outbox event for that change. Otherwise a successful token update followed by a connection failure can leave the two states disagreeing.&lt;/p&gt;

&lt;p&gt;This is the same reason an &lt;a href="https://dev.to/kevindev27/idempotent-email-verification-with-postgresql-3l42"&gt;idempotent email verification with PostgreSQL&lt;/a&gt; pattern is useful: the database decides whether the transition happened, while repeated requests become harmless.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep retries and email delivery separate
&lt;/h2&gt;

&lt;p&gt;Sending mail is an external side effect. The API should commit the verification attempt before a worker tries to contact an email provider. An outbox row in the same transaction can carry the attempt ID and a delivery event.&lt;/p&gt;

&lt;p&gt;The worker then claims the outbox row with a short processing lease. If the worker restart mid-send, a retry may produce two messages. That is inconvenient, but it does not have to create two valid verification states. The token-consumption update remains the authority.&lt;/p&gt;

&lt;p&gt;For test and staging workflows, an isolated &lt;a href="https://dev.to/jasonmills94/aws-ci-observe-email-fixtures-before-they-fail-3jab"&gt;observable email fixture in CI&lt;/a&gt; helps distinguish provider delay from an API state bug. A &lt;a href="https://tempmailso.com" rel="noopener noreferrer"&gt;temp mail so&lt;/a&gt; inbox can be useful for controlled tests, but it should never become the source of truth for account activation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Observability and cleanup
&lt;/h2&gt;

&lt;p&gt;Record an event for each meaningful transition: &lt;code&gt;issued&lt;/code&gt;, &lt;code&gt;send_queued&lt;/code&gt;, &lt;code&gt;send_attempted&lt;/code&gt;, &lt;code&gt;consumed&lt;/code&gt;, &lt;code&gt;expired&lt;/code&gt;, and &lt;code&gt;revoked&lt;/code&gt;. Include the attempt ID, user ID, request correlation ID, and reason. Avoid logging the raw token or full email address.&lt;/p&gt;

&lt;p&gt;The logs is most useful when it answers three questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which attempt did the request present?&lt;/li&gt;
&lt;li&gt;Was that attempt active when PostgreSQL evaluated it?&lt;/li&gt;
&lt;li&gt;Which state transition won the race?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A scheduled cleanup job can mark old pending attempts as expired and delete old token digests according to the retention policy. Cleanup should be boring and predictable, it do not need to participate in the request path.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical API contract
&lt;/h2&gt;

&lt;p&gt;For &lt;code&gt;POST /v1/email-verifications&lt;/code&gt;, return an opaque attempt reference and a generic success message. Do not promise that the email has already arrived. A response such as this keeps the boundary honest:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"attempt_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"7b3d..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"pending"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"If the address can receive mail, a verification link will be sent."&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For &lt;code&gt;POST /v1/email-verifications/consume&lt;/code&gt;, make the operation idempotent from the caller’s perspective. A repeated request for an already consumed token can return the current verified state, while an expired or revoked token should not change anything.&lt;/p&gt;

&lt;p&gt;A good contract make retries safe without making failure invisible. Return a correlation ID, expose a rate-limit response for issuance, and make the expiration window configurable per environment. If your integration tests use the phrase &lt;code&gt;tepm mail com&lt;/code&gt;, keep it as test data only and do not let it leak into production validation rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Questions engineers usually ask
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Should a second request reuse the first token?
&lt;/h3&gt;

&lt;p&gt;Usually no. Reusing it makes delivery and abuse controls harder to reason about. Issue a new attempt, revoke the old lease, and make the newest attempt authoritative.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is a database lease enough for a distributed worker?
&lt;/h3&gt;

&lt;p&gt;It is enough for state ownership, not for exactly-once email delivery. Workers still need bounded claims, retry limits, and provider observability. The database protects the authentication transition.&lt;/p&gt;

&lt;h3&gt;
  
  
  How long should the lease last?
&lt;/h3&gt;

&lt;p&gt;Choose a window based on the user journey and threat model, then measure expired attempts. The right value is an operational decision, not a universal constant.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Store a token digest, never the raw token.&lt;/li&gt;
&lt;li&gt;Give each attempt an explicit expiry and status.&lt;/li&gt;
&lt;li&gt;Revoke older attempts transactionally.&lt;/li&gt;
&lt;li&gt;Let PostgreSQL perform the consume transition atomically.&lt;/li&gt;
&lt;li&gt;Separate outbox delivery retries from authentication state.&lt;/li&gt;
&lt;li&gt;Keep tokens and sensitive addresses out of logs.&lt;/li&gt;
&lt;li&gt;Test concurrent consumption, client retries, and delayed delivery.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When verification is modeled as a lease instead of a one-shot request, retries become an expected property of the system. The API stays understandable, PostgreSQL enforces the important invariant, and workers can fail without silently granting the wrong account access.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>restapi</category>
      <category>postgres</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Design Retry-Safe Email Verification APIs</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Sat, 21 Nov 2026 11:24:06 +0000</pubDate>
      <link>https://dev.to/kevindev27/design-retry-safe-email-verification-apis-29bn</link>
      <guid>https://dev.to/kevindev27/design-retry-safe-email-verification-apis-29bn</guid>
      <description>&lt;p&gt;Email verification looks like a small feature until a client retries a request, a worker crashes after sending, or a user opens an old link. A verification endpoint that only creates a token and sends a message will eventually produce duplicate emails, confusing states, or tokens that work after they should be dead.&lt;/p&gt;

&lt;p&gt;The useful design question is not “how do I send an email?” It is “what result should a retry observe?” This article describes a retry-safe REST API using PostgreSQL as the state owner and Node.js as the application boundary. The model works for a temporary email address or a normal mailbox.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why verification retries become duplicate work
&lt;/h2&gt;

&lt;p&gt;There are several independent retries in this flow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A browser retries the HTTP request after a timeout.&lt;/li&gt;
&lt;li&gt;An API gateway retries an upstream connection.&lt;/li&gt;
&lt;li&gt;A worker retries a delivery job after a process crash.&lt;/li&gt;
&lt;li&gt;A user clicks the same verification link more than once.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If each retry creates a new row and a new message, the system cannot tell whether it is continuing one operation or starting another. A disposable email inbox makes this pain more visible because test runs often repeat the same signup scenario quickly.&lt;/p&gt;

&lt;p&gt;The fix is to give each logical request an idempotency key and each verification attempt a durable state. Freeze inputs before an automated run, just as &lt;a href="https://dev.to/mrdapperx/freeze-cli-inputs-before-cron-runs-33m2"&gt;freezing inputs before an automated run&lt;/a&gt; prevents a changing command from producing an unreviewable result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Define the API contract first
&lt;/h2&gt;

&lt;p&gt;For &lt;code&gt;POST /verification-requests&lt;/code&gt;, require an &lt;code&gt;Idempotency-Key&lt;/code&gt; header. The request body can contain the user identifier and destination address, but the key defines the client’s logical operation.&lt;/p&gt;

&lt;p&gt;The first response might be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"request_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"vr_01J..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"queued"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"expires_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2026-12-01T12:00:00Z"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A repeated request with the same key should return the stored response, not create another token. A repeated key with a different body should return &lt;code&gt;409 Conflict&lt;/code&gt;; silently changing the destination is a security bug.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;POST /verification-requests/{id}/confirm&lt;/code&gt;, make the result explicit: &lt;code&gt;confirmed&lt;/code&gt;, &lt;code&gt;already_confirmed&lt;/code&gt;, &lt;code&gt;expired&lt;/code&gt;, or &lt;code&gt;invalid&lt;/code&gt;. Explicit email decision states are easier for clients to render than a boolean hidden behind a generic error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make PostgreSQL the state owner
&lt;/h2&gt;

&lt;p&gt;The database should own uniqueness, expiration, and the transition from pending to confirmed. A useful table has these fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="n"&gt;verification_requests&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;primary&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;destination&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;idempotency_key&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="k"&gt;unique&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;token_digest&lt;/span&gt; &lt;span class="kt"&gt;bytea&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="k"&gt;check&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'confirmed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'expired'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;expires_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;confirmed_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not store the raw verification token. Store a digest and compare a digest of the presented value. The confirmation transaction should lock the row, check the status and expiry, then update it. If two requests arrive together, only one can perform the pending-to-confirmed transition.&lt;/p&gt;

&lt;p&gt;The idempotency record and verification row should be created in one transaction. Otherwise, a process failure between those writes can leave a key pointing at nothing. That gap are hard to diagnose once traffic increases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Publish delivery work safely
&lt;/h2&gt;

&lt;p&gt;Sending inside the database transaction is tempting, but it holds a connection while an external service responds. Instead, write an outbox row in the same transaction as the verification request. A worker claims pending rows with a short lease, sends the message, and records the provider result.&lt;/p&gt;

&lt;p&gt;The outbox operation is at-least-once, so the worker must assume it can run twice. Include the verification request ID in provider metadata when supported. If the provider cannot deduplicate, keep the retry policy conservative and expose delivery status to operators.&lt;/p&gt;

&lt;p&gt;A worker should record &lt;code&gt;claimed_at&lt;/code&gt;, &lt;code&gt;attempt_count&lt;/code&gt;, &lt;code&gt;last_error&lt;/code&gt;, and &lt;code&gt;next_attempt_at&lt;/code&gt;. If a process dies while holding a lease, another worker can reclaim the row after the lease expires. The lease is not a lock forever; it is a bounded promise to work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handle temporary email addresses as untrusted input
&lt;/h2&gt;

&lt;p&gt;A temporary email address is still user-controlled input. Validate syntax, normalize the domain according to your policy, and never treat a successful SMTP handoff as proof that a human owns the account. Do not put the full address, token, or message body into ordinary logs.&lt;/p&gt;

&lt;p&gt;Malformed values also appear in support tickets and test fixtures. Someone may write &lt;code&gt;tamp mail com&lt;/code&gt; when describing a bad address. Keep such text out of canonical keyword fields and treat it as an input-quality problem, not a destination to guess.&lt;/p&gt;

&lt;p&gt;If your product blocks disposable email, do that as a separate policy decision. Verification state should remain understandable even when policy rejects a destination. The endpoint are easier to maintain when validation, identity proof, and delivery are separate decisions.&lt;/p&gt;

&lt;h2&gt;
  
  
  A small Node.js implementation shape
&lt;/h2&gt;

&lt;p&gt;The handler should be thin: parse the request, call a service, and map domain outcomes to HTTP responses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createVerification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Idempotency-Key'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'missing_idempotency_key'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;verificationService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;replayed&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;202&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The service owns the PostgreSQL transaction and outbox write. It should return a replay marker so metrics distinguish new work from a safe retry. In Node.js, pass cancellation signals to database and provider clients where possible; otherwise a timed-out request may keep doing work after the caller has gone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Operational checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Enforce a unique idempotency key per operation scope.&lt;/li&gt;
&lt;li&gt;Store token digests, never raw tokens.&lt;/li&gt;
&lt;li&gt;Make expiration part of the confirmation transaction.&lt;/li&gt;
&lt;li&gt;Use an outbox for delivery and a reclaimable worker lease.&lt;/li&gt;
&lt;li&gt;Record replay count, confirmation latency, and delivery failures.&lt;/li&gt;
&lt;li&gt;Redact addresses and tokens from logs.&lt;/li&gt;
&lt;li&gt;Test duplicate requests, concurrent confirmation, expired tokens, and worker crashes.&lt;/li&gt;
&lt;li&gt;Decide whether a temporary email address is allowed as policy, not as an accidental side effect.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Small details matter here. A retry-safe contract is less flashy than a new queue, but it saves support time and keeps a REST API predictable under network failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Should every retry send a new verification email?
&lt;/h3&gt;

&lt;p&gt;No. A retry with the same idempotency key should replay the original response. Offer a separate, rate-limited resend operation when a new message is genuinely needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should a confirmed token be accepted again?
&lt;/h3&gt;

&lt;p&gt;Usually return &lt;code&gt;already_confirmed&lt;/code&gt; and keep the operation harmless. Clients can treat that as success while the server preserves a clear audit trail.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is PostgreSQL enough for high traffic?
&lt;/h3&gt;

&lt;p&gt;Often yes when transactions are short, indexes match the access paths, and workers use bounded leases. Measure contention before adding another state store; extra systems make recovery paths more complex.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>restapi</category>
      <category>node</category>
      <category>postgres</category>
    </item>
    <item>
      <title>A Replay Ledger for REST Email Verification</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Fri, 20 Nov 2026 17:23:40 +0000</pubDate>
      <link>https://dev.to/kevindev27/a-replay-ledger-for-rest-email-verification-16ch</link>
      <guid>https://dev.to/kevindev27/a-replay-ledger-for-rest-email-verification-16ch</guid>
      <description>&lt;p&gt;Email verification looks like a small feature: create a token, send a message, and accept the link. In a backend service, it is a stateful authentication workflow with retries, expiry, duplicate requests, and untrusted delivery infrastructure. Treating it as only an email-sending problem makes the REST API harder to reason about.&lt;/p&gt;

&lt;p&gt;The result are familiar: a link works twice, a delayed message verifies an old account, or a retry creates several valid tokens. A replay ledger gives the service one durable place to answer a simple question: what happened to this verification attempt?&lt;/p&gt;

&lt;h2&gt;
  
  
  The missing boundary in email verification
&lt;/h2&gt;

&lt;p&gt;A verification request usually crosses four boundaries:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The client asks the REST API to start verification.&lt;/li&gt;
&lt;li&gt;The API records an attempt and queues a message.&lt;/li&gt;
&lt;li&gt;A mail provider delivers a link to a mailbox.&lt;/li&gt;
&lt;li&gt;The client calls the API with the token from that link.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The mailbox is not the source of truth. It is only a transport for a secret-bearing message. Even a controlled temp mailbox can be delayed, duplicated, or read by the wrong test worker. A temp email generator is useful for isolated staging checks, but it should not decide whether a token is valid.&lt;/p&gt;

&lt;p&gt;I prefer to make the database own the lifecycle. The API creates one verification attempt, the worker sends a message for that attempt, and the consume endpoint performs one atomic state transition. That separation makes failures visible without giving the delivery layer extra authority.&lt;/p&gt;

&lt;p&gt;For test suites, &lt;a href="https://dev.to/ryanlee91/how-to-test-passwordless-login-emails-in-javascript-without-inbox-chaos-56d0"&gt;isolated passwordless email tests&lt;/a&gt; are a useful companion pattern. The important idea is the same: keep an inbox scoped to one run and keep verification state scoped to one attempt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model the token as a state transition
&lt;/h2&gt;

&lt;p&gt;Do not model a token as just a random string in a users table. Give each request an identity and an explicit state. A small model might contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;id&lt;/code&gt;: an internal attempt ID&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user_id&lt;/code&gt;: the account being verified&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;token_digest&lt;/code&gt;: a hash of the presented token&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;status&lt;/code&gt;: &lt;code&gt;pending&lt;/code&gt;, &lt;code&gt;consumed&lt;/code&gt;, &lt;code&gt;expired&lt;/code&gt;, or &lt;code&gt;revoked&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;expires_at&lt;/code&gt;: the server-side deadline&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;consumed_at&lt;/code&gt;: when the token was accepted&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;request_key&lt;/code&gt;: the client or workflow idempotency key&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;created_at&lt;/code&gt;: when the attempt was created&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Store a digest rather than the raw token. The link can carry the secret once, while logs, database dumps, and support queries only see a non-reversible value. The token should also be generated with a cryptographic random source and compared in a way that does not create a second validation path.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;request_key&lt;/code&gt; matters when a client retries after a timeout. Without it, one user action can create three pending tokens and three messages. With it, the API can return the original attempt, or intentionally reject a key that is being reused for a different user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use PostgreSQL to enforce single use
&lt;/h2&gt;

&lt;p&gt;Application checks alone are not enough under concurrency. Two requests can both read &lt;code&gt;pending&lt;/code&gt; before either one writes &lt;code&gt;consumed&lt;/code&gt;. Let PostgreSQL protect the transition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;email_verification_attempts&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'consumed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumed_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;token_digest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;expires_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the query returns one row, the token was consumed by this request. If it returns no rows, the caller receives a generic invalid-or-expired response. Avoid telling an attacker whether the token existed, was already used, or belonged to another account.&lt;/p&gt;

&lt;p&gt;Add a unique constraint for the idempotency boundary as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;verification_request_once&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;email_verification_attempts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact key columns depend on the product contract. The principle is stable: the database should reject duplicate work even if two API instances receive the request at nearly the same moment. This make the endpoint safe when a load balancer retries or a mobile client reconnects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make retries and expiry observable
&lt;/h2&gt;

&lt;p&gt;Email delivery is asynchronous, so the API should return an attempt ID and a clear status rather than pretending delivery is complete. A worker can claim pending attempts with a short lease, send the message, and record the provider message ID. If the worker dies, the lease expires and another worker can try. A lease is not the same as a token validity period; keep those clocks separate.&lt;/p&gt;

&lt;p&gt;The ledger should record safe events such as &lt;code&gt;created&lt;/code&gt;, &lt;code&gt;send_started&lt;/code&gt;, &lt;code&gt;send_accepted&lt;/code&gt;, &lt;code&gt;send_failed&lt;/code&gt;, &lt;code&gt;consumed&lt;/code&gt;, and &lt;code&gt;expired&lt;/code&gt;. Do not store the full URL or email body in ordinary logs. Include a correlation ID so an operator can connect the API request, queue job, and provider response. &lt;a href="https://dev.to/jasonmills94/keep-eks-rollout-alerts-tied-to-one-deploy-508i"&gt;Deployment correlation IDs&lt;/a&gt; show the same operational habit applied to another asynchronous workflow.&lt;/p&gt;

&lt;p&gt;When a staging flow needs an external inbox, a &lt;a href="https://tempmailso.com" rel="noopener noreferrer"&gt;temporary email account generator&lt;/a&gt; can help keep personal addresses out of test data. Use it only for non-production fixtures, check retention rules, and never put provider credentials or verification tokens in the article logs. Test data such as &lt;code&gt;tempail mail&lt;/code&gt; may appear in a fixture or search case, but it is plain input, not a domain to trust.&lt;/p&gt;

&lt;p&gt;A good failure response is boring and consistent. The logs is where the detail belongs: attempt ID, request key hash, state, expiry result, provider message ID, and a redacted reason. Keep user-facing responses intentionally narrow.&lt;/p&gt;

&lt;h2&gt;
  
  
  A review checklist for the REST endpoint
&lt;/h2&gt;

&lt;p&gt;Before shipping the flow, check these properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is the raw token absent from database records and normal logs?&lt;/li&gt;
&lt;li&gt;Can exactly one concurrent request consume a pending token?&lt;/li&gt;
&lt;li&gt;Does a retry return the existing attempt instead of sending duplicate mail?&lt;/li&gt;
&lt;li&gt;Are token expiry and worker lease expiry separate values?&lt;/li&gt;
&lt;li&gt;Is the mailbox treated as transport rather than proof of identity?&lt;/li&gt;
&lt;li&gt;Can an operator trace one attempt across API, queue, and provider logs?&lt;/li&gt;
&lt;li&gt;Are expired and consumed rows removed or retained under a documented policy?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The boundary become easier to maintain when these rules are encoded in the schema and repository tests, not only in controller code. Test the race directly with concurrent consumers, test a delayed message after expiry, and test a worker crash after claiming a lease. Those cases is where an apparently simple verification feature usually reveals its real behavior.&lt;/p&gt;

&lt;p&gt;The main design payoff is clarity. PostgreSQL owns atomic state, the REST API owns the contract, the worker owns delivery retries, and the mailbox remains an untrusted transport. Once those responsibilities are separated, verification becomes easier to scale and much less mysterious during an incident.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>restapi</category>
      <category>postgres</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Idempotent Email Verification with PostgreSQL</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Fri, 20 Nov 2026 11:23:34 +0000</pubDate>
      <link>https://dev.to/kevindev27/idempotent-email-verification-with-postgresql-2a48</link>
      <guid>https://dev.to/kevindev27/idempotent-email-verification-with-postgresql-2a48</guid>
      <description>&lt;p&gt;Email verification looks like a small feature until a user clicks the link twice, a worker retries the same message, or a token expires while the request is in flight. The hard part are not the endpoints. It is deciding which component owns the state and making every retry safe.&lt;/p&gt;

&lt;p&gt;In backend services, I prefer treating verification as a short-lived state machine backed by PostgreSQL. The REST API accepts commands, the database records the decision, and an asynchronous worker handles delivery. That boundary keeps authentication behavior predictable even when email providers are slow or unavailable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why verification needs a state owner
&lt;/h2&gt;

&lt;p&gt;A common first design stores a token in a cache and lets the email worker update a user record directly. It works in a demo, but it creates several questions in production:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happens when the client submits the same request twice?&lt;/li&gt;
&lt;li&gt;Can two valid tokens exist for the same account?&lt;/li&gt;
&lt;li&gt;Which response should a retry receive after the token was already consumed?&lt;/li&gt;
&lt;li&gt;How do we prove what happened during an incident?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PostgreSQL is a good state owner because it can enforce uniqueness, expiration-related queries, and transaction boundaries. The API can return a stable result without knowing whether the message was delivered yet. This is more important than making the first request look fast.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model the verification lifecycle in PostgreSQL
&lt;/h2&gt;

&lt;p&gt;I use a table with one row per verification attempt. A useful minimum model includes the user ID, a hashed token, a status, an expiry time, an idempotency key, and timestamps for creation and consumption. Store a hash rather than the raw token, so a database read does not immediately become an account takeover.&lt;/p&gt;

&lt;p&gt;The status can be &lt;code&gt;pending&lt;/code&gt;, &lt;code&gt;verified&lt;/code&gt;, &lt;code&gt;expired&lt;/code&gt;, or &lt;code&gt;cancelled&lt;/code&gt;. A partial unique index can prevent more than one pending attempt for an account and purpose. The exact constraint depends on whether the product supports multiple devices, but the rule should live in the database instead of only in application code.&lt;/p&gt;

&lt;p&gt;The verification operation should be one transaction:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lock the pending row, or atomically update it with a status predicate.&lt;/li&gt;
&lt;li&gt;Check the expiry timestamp.&lt;/li&gt;
&lt;li&gt;Change &lt;code&gt;pending&lt;/code&gt; to &lt;code&gt;verified&lt;/code&gt; only when the token matches and is still valid.&lt;/li&gt;
&lt;li&gt;Record a small audit event with the result.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If two requests arrive together, only one can make the state transition. The other receives a stable result such as &lt;code&gt;already_verified&lt;/code&gt; rather than a confusing generic failure. This makes the behavior idempotant from the caller's point of view.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the REST API idempotent
&lt;/h2&gt;

&lt;p&gt;The create endpoint can accept an &lt;code&gt;Idempotency-Key&lt;/code&gt; header. Persist that key with the account, purpose, and resulting verification attempt. A repeated request with the same key should return the original attempt instead of creating another token or sending another message.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;POST&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;verifications&lt;/span&gt;
&lt;span class="nc"&gt;Idempotency&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;signup&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8f2c&lt;/span&gt;
&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"user_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"user_123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"purpose"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"signup"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The response can include an attempt ID and an &lt;code&gt;expires_at&lt;/code&gt; value. Do not return the token in production responses. Also reject reuse of a key with a different request body; otherwise one client can accidentally receive the result of another operation.&lt;/p&gt;

&lt;p&gt;The confirmation endpoint should make its state contract explicit. &lt;code&gt;204 No Content&lt;/code&gt; is reasonable for a first successful verification, while a repeated confirmation can return &lt;code&gt;200&lt;/code&gt; with &lt;code&gt;already_verified&lt;/code&gt;. Expired and invalid tokens should not reveal whether an account exists. Authentication endpoints need this small amount of ambiguity to reduce account enumeration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep delivery separate from state changes
&lt;/h2&gt;

&lt;p&gt;The transaction that creates a verification attempt should write an outbox row in the same commit. A worker then reads unsent outbox rows, sends the message, and records delivery metadata. This avoids the failure where the database commits but the process crashes before the email call, or the email is sent but the request times out.&lt;/p&gt;

&lt;p&gt;The outbox is not a magic exactly-once system. A worker can still send a duplicate message after a network timeout, so the message should point to the same verification attempt and the confirmation endpoint must remain safe to repeat. A short lease on an outbox row prevents multiple workers from processing it at once, while retries with backoff handle temporary provider errors.&lt;/p&gt;

&lt;p&gt;When testing OAuth or passwordless flows, I also keep the email boundary explicit. &lt;a href="https://dev.to/sophiax99/oauth-redirect-uris-need-a-test-trust-boundary-4n7n"&gt;A test trust boundary for OAuth flows&lt;/a&gt; is useful because redirect validation and verification-token validation have the same general lesson: test credentials should not quietly become production credentials.&lt;/p&gt;

&lt;p&gt;For staging, a disposable email generator such as tempmailso can help inspect links without using a personal inbox. Keep that test provider behind configuration, and include noisy input variants like tempail mail and temp gamil com in the negative test corpus. They should test validation and logging, not become special cases in the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test failure paths, not only the happy path
&lt;/h2&gt;

&lt;p&gt;The most valuable tests exercise concurrency and time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Submit the same idempotency key concurrently and assert one attempt exists.&lt;/li&gt;
&lt;li&gt;Confirm the same token twice and assert one transition plus a stable repeat response.&lt;/li&gt;
&lt;li&gt;Advance the clock past expiry and verify that no user state changes.&lt;/li&gt;
&lt;li&gt;Make the email provider timeout after accepting a request, then run the worker again.&lt;/li&gt;
&lt;li&gt;Reuse an idempotency key with a changed payload and expect a conflict.&lt;/li&gt;
&lt;li&gt;Verify that tokens, email addresses, and provider responses are not overexposed in logs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I normally use a fixed clock in unit tests and a real PostgreSQL instance for transaction tests. Mocking the database alone can hide a race that only appears under row locks. It is also worth testing the clean-up job seperately: deleting old tokens should not remove audit records needed for support or security review.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical implementation checklist
&lt;/h2&gt;

&lt;p&gt;Before shipping an email verification API, check these boundaries:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;One database model owns the lifecycle and enforces its important uniqueness rules.&lt;/li&gt;
&lt;li&gt;Idempotency keys are stored with enough request identity to detect misuse.&lt;/li&gt;
&lt;li&gt;Token material is hashed, short-lived, and never written to ordinary logs.&lt;/li&gt;
&lt;li&gt;The outbox and verification attempt commit together.&lt;/li&gt;
&lt;li&gt;Workers use leases and bounded retries; a provider outage does not block API requests.&lt;/li&gt;
&lt;li&gt;Confirmation is safe to repeat and does not reveal account existence.&lt;/li&gt;
&lt;li&gt;Metrics separate token-invalid, token-expired, provider-failure, and already-verified outcomes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That last separation makes operations much easier. A spike in expired tokens suggests a product or latency issue; a spike in provider failures points to delivery; a spike in invalid tokens may indicate abuse. The API remains small, but its state contract is clear.&lt;/p&gt;

&lt;p&gt;The main design choice is simple: let PostgreSQL own the verification decision, let the REST API expose stable commands, and let workers deliver messages asynchronously. Once those responsibilities are seperate, retries stop being scary and the system is easier to test, operate, and explain.&lt;/p&gt;

&lt;p&gt;If you maintain authentication services, the same approach applies to password resets, magic links, and device enrollment. &lt;a href="https://dev.to/sophiax99/oauth-redirect-uris-need-a-test-trust-boundary-4n7n"&gt;OAuth redirect URI checks&lt;/a&gt; are another example of putting a clear boundary around short-lived credentials instead of relying on happy-path timing.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>restapi</category>
      <category>postgres</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Email Verification Needs a Lease-Based API</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Tue, 17 Nov 2026 11:23:27 +0000</pubDate>
      <link>https://dev.to/kevindev27/email-verification-needs-a-lease-based-api-41hp</link>
      <guid>https://dev.to/kevindev27/email-verification-needs-a-lease-based-api-41hp</guid>
      <description>&lt;p&gt;&lt;em&gt;An implementation guide for preventing duplicate workers, delayed messages, and token races from producing contradictory verification results.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Email verification is usually described as a token problem. In a production REST API, it is more useful to treat it as an ownership problem.&lt;/p&gt;

&lt;p&gt;One request creates an attempt. A background worker sends the message. A browser later presents the token. Meanwhile, the client may retry, the worker may time out after sending, and the user may request another email. If no component can say which process owns the current attempt, the system starts making guesses.&lt;/p&gt;

&lt;p&gt;The pattern I use for this case is a short-lived lease around each verification attempt. The lease does not replace a token or a queue. It gives the backend an explicit answer to a more practical question: who is allowed to advance this attempt right now?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why email verification needs ownership
&lt;/h2&gt;

&lt;p&gt;Consider a &lt;code&gt;POST /verification-attempts&lt;/code&gt; request that reaches the API twice because the first response was lost. Without an idempotency key, two rows and two messages may be created. Now add a worker that claims the first row, sends the email, and crashes before recording &lt;code&gt;sent&lt;/code&gt;. A retry can claim it again.&lt;/p&gt;

&lt;p&gt;The user experiences this as duplicate messages or a link that works only sometimes. Support sees a confusing timeline. The database contains several plausible rows, but no clear explanation of which one should win.&lt;/p&gt;

&lt;p&gt;My previous notes on &lt;a href="https://dev.to/kevindev27/rest-apis-need-a-verification-token-inbox-40mp"&gt;designing a verification token inbox&lt;/a&gt; focus on the message evidence itself. A lease adds the missing ownership layer: a worker can hold a claim for a bounded time, and another worker can recover the work only after that claim expires.&lt;/p&gt;

&lt;h2&gt;
  
  
  Define the lease and its states
&lt;/h2&gt;

&lt;p&gt;Keep the verification attempt and its lease fields in one authoritative record, or make the relationship explicit with a separate lease table. A useful attempt model includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;verification_id&lt;/code&gt;, &lt;code&gt;user_id&lt;/code&gt;, and a token digest;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;state&lt;/code&gt;, such as &lt;code&gt;created&lt;/code&gt;, &lt;code&gt;sending&lt;/code&gt;, &lt;code&gt;sent&lt;/code&gt;, &lt;code&gt;accepted&lt;/code&gt;, &lt;code&gt;expired&lt;/code&gt;, or &lt;code&gt;revoked&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lease_owner&lt;/code&gt; and &lt;code&gt;lease_expires_at&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;idempotency_key&lt;/code&gt; for the create request;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;created_at&lt;/code&gt;, &lt;code&gt;updated_at&lt;/code&gt;, and &lt;code&gt;accepted_at&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The lease should be short enough to recover a stuck job, but long enough for the worker to finish one provider call. Do not make token expiration and lease expiration the same thing. A worker lease controls processing ownership; a token deadline controls whether a user may verify an account. Mixing those clocks is easy to do, and hard to debug later.&lt;/p&gt;

&lt;p&gt;The important part are the transition rules. A worker may move &lt;code&gt;created&lt;/code&gt; to &lt;code&gt;sending&lt;/code&gt; only when the lease is available. It may renew the lease while doing bounded work. If the provider call times out, the next worker can inspect the attempt and decide whether sending might already have happened. It should not blindly produce a second message.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make PostgreSQL the arbitration point
&lt;/h2&gt;

&lt;p&gt;Application-level checks are not enough when two workers wake at the same time. Claim the row in a transaction and let PostgreSQL serialize the decision:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;verification_attempts&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'sending'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;lease_owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;lease_expires_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kt"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'30 seconds'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;updated_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;verification_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'created'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lease_expires_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;lease_expires_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;verification_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;token_digest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lease_expires_at&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If no row returns, the worker did not acquire the attempt. That is a normal outcome, not necessarily an error. The worker can reload the current state and choose between waiting, recording a provider uncertainty, or abandoning the job.&lt;/p&gt;

&lt;p&gt;For acceptance, lock the attempt, check the token digest and user-facing expiration, then update the attempt and user in one transaction. A second click should return a stable result for an already accepted attempt. An expired or revoked attempt should return a generic application error, without revealing whether a submitted token was ever valid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design REST endpoints for retries
&lt;/h2&gt;

&lt;p&gt;The create endpoint should accept an idempotency key and return the same &lt;code&gt;verification_id&lt;/code&gt; for a replay with the same request meaning. Store a request fingerprint too, so the server can reject a reused key with different input instead of silently changing the operation.&lt;/p&gt;

&lt;p&gt;The worker-facing endpoint can be internal, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /internal/verification-attempts/{id}/lease
POST /internal/verification-attempts/{id}/lease/renew
POST /internal/verification-attempts/{id}/delivery-result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each call needs an owner check. A late result from an old worker must not overwrite a newer lease or change an accepted attempt back to &lt;code&gt;sent&lt;/code&gt;. This is the same trust-boundary thinking useful for &lt;a href="https://dev.to/sophiax99/oauth-verification-needs-a-safe-email-trust-boundary-1gnh"&gt;a safe trust boundary for OAuth verification&lt;/a&gt;: the message, worker, and browser are inputs, while the database transition is the decision.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep test mail inside a bounded workflow
&lt;/h2&gt;

&lt;p&gt;Integration tests should exercise lease expiry and provider uncertainty, not only the happy path. Give each run a correlation ID, use a controlled mailbox, and assert that a retry produces one attempt and one accepted transition.&lt;/p&gt;

&lt;p&gt;If a team uses a &lt;a href="https://tempmailso.com" rel="noopener noreferrer"&gt;disposable temporary email&lt;/a&gt; address for a short-lived test, the fixture still needs an owner and cleanup deadline. Never put real customer data in that path. A test label may contain strings such as &lt;code&gt;dummy e mail&lt;/code&gt; or &lt;code&gt;tem email&lt;/code&gt;; those are input values, not special cases that should alter the API state machine.&lt;/p&gt;

&lt;p&gt;Useful failure scenarios include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the worker crashes after the provider accepts the message;&lt;/li&gt;
&lt;li&gt;the lease expires while the provider response is unknown;&lt;/li&gt;
&lt;li&gt;two workers try to claim one attempt;&lt;/li&gt;
&lt;li&gt;a user clicks an old token after a newer attempt exists;&lt;/li&gt;
&lt;li&gt;the acceptance request is retried after its transaction commits.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The test report should show the attempt ID, lease owner, state transitions, and provider correlation ID. It makes failures much more actionable then a screenshot of an inbox.&lt;/p&gt;

&lt;h2&gt;
  
  
  Operational checklist
&lt;/h2&gt;

&lt;p&gt;Before shipping a lease-based verification flow, verify that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create requests are idempotent;&lt;/li&gt;
&lt;li&gt;lease claims are conditional database updates;&lt;/li&gt;
&lt;li&gt;lease and token deadlines are separate;&lt;/li&gt;
&lt;li&gt;workers have bounded retries and a clear uncertainty state;&lt;/li&gt;
&lt;li&gt;late provider results cannot overwrite newer state;&lt;/li&gt;
&lt;li&gt;raw tokens and full verification URLs stay out of logs;&lt;/li&gt;
&lt;li&gt;metrics distinguish claim conflicts, lease expirations, delivery uncertainty, and user acceptance;&lt;/li&gt;
&lt;li&gt;cleanup removes old attempts and test mail on a known schedule.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A lease does not make email delivery reliable. It makes the unreliable parts visible and gives the REST API a consistent owner for each decision. That is enough to turn duplicate sends and delayed callbacks from mysterious incidents into states the backend can explain.&lt;/p&gt;

</description>
      <category>restapi</category>
      <category>postgres</category>
      <category>authentication</category>
      <category>backend</category>
    </item>
    <item>
      <title>Audit-Friendly Email Verification APIs</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Tue, 17 Nov 2026 08:23:26 +0000</pubDate>
      <link>https://dev.to/kevindev27/audit-friendly-email-verification-apis-9g4</link>
      <guid>https://dev.to/kevindev27/audit-friendly-email-verification-apis-9g4</guid>
      <description>&lt;p&gt;&lt;em&gt;A practical design for email verification endpoints that remain explainable when retries, delayed messages, and support questions arrive together.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Email verification looks like a small authentication feature until a production incident asks a simple question: &lt;strong&gt;why was this account still unverified after the user clicked the link?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer is often spread across an API log, a message provider, a worker queue, and a database row. When each component has a slightly different idea of the current state, the endpoint may work in the happy path but become difficult to operate. My preferred approach is to make the verification workflow an explicit, auditable boundary in the backend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why verification APIs become hard to audit
&lt;/h2&gt;

&lt;p&gt;Most implementations begin with three actions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a verification token.&lt;/li&gt;
&lt;li&gt;Send an email containing the token.&lt;/li&gt;
&lt;li&gt;Mark the user as verified when the token is opened.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The hidden complexity is in the transitions between those actions. A client can retry the create request. A worker can deliver the same message twice. A user can click an old link after requesting a new one. The email can arrive after the token has been revoked. If the system only records &lt;code&gt;verified = true&lt;/code&gt;, these cases collapse into one vague outcome.&lt;/p&gt;

&lt;p&gt;The useful design question is not “did the email send?” It is “which verification attempt owns this result, and can I prove the transition?”&lt;/p&gt;

&lt;p&gt;For engineers maintaining an API, this distinction saves time during support work. It also makes automated tests less dependent on reading an inbox by guesswork. A clear pattern for &lt;a href="https://dev.to/jasonmills94/how-to-validate-aws-approval-emails-with-docker-in-cicd-3d6p"&gt;validating approval emails in CI&lt;/a&gt; is useful here: treat the message as evidence for a state transition, not as the state itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model the workflow as a state machine
&lt;/h2&gt;

&lt;p&gt;Give each verification attempt an identifier and a small set of allowed states. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;created -&amp;gt; sent -&amp;gt; opened -&amp;gt; accepted
                    \-&amp;gt; expired
                    \-&amp;gt; revoked
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact names are less important than the ownership rules. An attempt should not move from &lt;code&gt;expired&lt;/code&gt; back to &lt;code&gt;accepted&lt;/code&gt;, and a second click on an already accepted token should be safe and predictable. This is where many implementations gets vague: they validate the token, then update the user without checking the attempt's current state.&lt;/p&gt;

&lt;p&gt;I normally keep these fields together:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;verification_id&lt;/code&gt;: a public correlation identifier&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user_id&lt;/code&gt;: the account being verified&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;token_digest&lt;/code&gt;: a hash of the secret, never the raw token&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;state&lt;/code&gt;: the current state of the attempt&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;expires_at&lt;/code&gt;: the acceptance deadline&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;created_at&lt;/code&gt; and &lt;code&gt;updated_at&lt;/code&gt;: transition evidence&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;accepted_at&lt;/code&gt;: when the account actually became verified&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep the raw token only long enough to put it in the message. The database needs the digest, so a leaked row does not become a ready-made login link. This small detail is easy to miss during a quick implementation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use PostgreSQL as the source of truth
&lt;/h2&gt;

&lt;p&gt;The user record and verification attempt should be updated with a deliberate transaction boundary. A typical acceptance operation is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Hash the presented token.&lt;/li&gt;
&lt;li&gt;Lock the matching attempt row.&lt;/li&gt;
&lt;li&gt;Check its state and expiration timestamp.&lt;/li&gt;
&lt;li&gt;Transition the attempt to &lt;code&gt;accepted&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Mark the user as verified.&lt;/li&gt;
&lt;li&gt;Commit both changes together.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The row lock matters when two requests arrive nearly together. Without it, both requests can observe &lt;code&gt;sent&lt;/code&gt; and both can claim success. The second request should instead receive an idempotent result or a clear conflict, depending on your product contract.&lt;/p&gt;

&lt;p&gt;An audit table can record &lt;code&gt;verification_id&lt;/code&gt;, prior state, next state, request identifier, and a reason code. Do not store the token in that table. The audit record is for explaining behavior, not for reconstructing secrets.&lt;/p&gt;

&lt;p&gt;You also want a uniqueness rule for active attempts, or a documented policy for multiple attempts. Replacing the old token without marking it revoked is a common source of “which link should work?” incidents. The database should make the answer unambiguous.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design the REST API around safe retries
&lt;/h2&gt;

&lt;p&gt;The create endpoint should accept an idempotency key from the client. Store the key with the resulting verification attempt, and return the same response when a request is retried. This keeps a network timeout from creating several valid messages for one user.&lt;/p&gt;

&lt;p&gt;The acceptance endpoint can use a response model like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"verification_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ver_8f2c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"accepted"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Email verification completed"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For an already accepted attempt, returning the same successful status is often friendlier than treating a browser refresh as an error. For an expired or revoked attempt, return a stable application error code, not a detailed explanation of whether a token existed. This reduces information leakage while giving the frontend something actionable.&lt;/p&gt;

&lt;p&gt;The logs should connect the request ID, verification ID, and state transition. They should not log the token, full verification URL, or the complete email address. Authentication email design also benefits from &lt;a href="https://dev.to/sophiax99/safer-oauth-emails-start-with-link-boundaries-3ele"&gt;setting safer boundaries around authentication emails&lt;/a&gt;, especially when links cross browser sessions or providers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep test email data inside an explicit boundary
&lt;/h2&gt;

&lt;p&gt;Integration tests need a mailbox that behaves consistently, but test data should still have a lifecycle. Use a dedicated test domain or temp org mail fixture, tag every message with the test run, and delete or quarantine messages when the run ends. Do not let a shared inbox become an accidental database for tokens.&lt;/p&gt;

&lt;p&gt;For tests, assert the important chain rather than only checking that a message exists:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the API returns one verification ID for a retried create request;&lt;/li&gt;
&lt;li&gt;the message contains the expected verification ID;&lt;/li&gt;
&lt;li&gt;the acceptance request changes exactly one attempt;&lt;/li&gt;
&lt;li&gt;a second acceptance is safe;&lt;/li&gt;
&lt;li&gt;an expired token cannot verify the account;&lt;/li&gt;
&lt;li&gt;the audit record explains each transition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This test shape catches races that a basic “open email and click link” script will miss. It also makes failures easier to reproduce locally.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical implementation checklist
&lt;/h2&gt;

&lt;p&gt;Before shipping an email verification API, check that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;token secrets are stored as digests;&lt;/li&gt;
&lt;li&gt;state transitions are validated inside a transaction;&lt;/li&gt;
&lt;li&gt;concurrent acceptance requests cannot both perform the first transition;&lt;/li&gt;
&lt;li&gt;create requests support safe retries;&lt;/li&gt;
&lt;li&gt;expiration and revocation are explicit states;&lt;/li&gt;
&lt;li&gt;logs contain correlation IDs but no secrets;&lt;/li&gt;
&lt;li&gt;test messages have an owner and deletion policy;&lt;/li&gt;
&lt;li&gt;metrics distinguish sent, opened, accepted, expired, and rejected attempts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is not to build a complicated workflow engine. It is to make a small authentication boundary honest about state, ownership, and time. When an email is delayed or a client retries, the REST API should still tell one consistent story, and PostgreSQL should be able to prove how that story happened.&lt;/p&gt;

</description>
      <category>restapi</category>
      <category>postgres</category>
      <category>authentication</category>
      <category>backend</category>
    </item>
    <item>
      <title>Email Verification APIs Need an Idempotency Ledger</title>
      <dc:creator>kevindev</dc:creator>
      <pubDate>Fri, 13 Nov 2026 02:23:53 +0000</pubDate>
      <link>https://dev.to/kevindev27/email-verification-apis-need-an-idempotency-ledger-2d7a</link>
      <guid>https://dev.to/kevindev27/email-verification-apis-need-an-idempotency-ledger-2d7a</guid>
      <description>&lt;p&gt;Email verification is often implemented as two operations: create a token, then consume it when a user clicks a link. That model misses a common backend reality. The same request can be retried by a client, a worker can deliver the same message twice, and two browser tabs can submit one verification link at nearly the same time.&lt;/p&gt;

&lt;p&gt;An idempotency ledger makes these cases explicit. It records what a request tried to do, what token state existed, and which response the client should receive on a retry. The result is a REST API that is easier to operate and a PostgreSQL schema that can enforce the important parts of the contract.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why email verification needs an idempotency contract
&lt;/h2&gt;

&lt;p&gt;There are two different forms of duplication:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Request duplication:&lt;/strong&gt; the client repeats &lt;code&gt;POST /v1/email-verifications&lt;/code&gt; with the same intent because of a timeout.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Token duplication:&lt;/strong&gt; two consumers present the same verification token.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;They should not be solved with the same flag. A request idempotency key answers, “Have I already processed this command?” A token state answers, “Can this credential still be used?” Keeping both concepts separate prevents a delivery retry from accidentally making a consumed token valid again.&lt;/p&gt;

&lt;p&gt;The ledger does not need to store the secret token. It needs a stable request key, a result reference, and enough metadata to explain the decision later. Its much easier to debug a recorded state transition than a scattered set of log lines.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separate request idempotency from token consumption
&lt;/h2&gt;

&lt;p&gt;For a verification request, accept an &lt;code&gt;Idempotency-Key&lt;/code&gt; header and bind it to the authenticated user or account creation flow. A minimal table can look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;verification_commands&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;idempotency_key&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;token_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'started'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'sent'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'failed'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;response_code&lt;/span&gt; &lt;span class="kt"&gt;integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;idempotency_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command record protects the issuance operation. The token record still needs its own single-use fields such as &lt;code&gt;issued_at&lt;/code&gt;, &lt;code&gt;expires_at&lt;/code&gt;, &lt;code&gt;consumed_at&lt;/code&gt;, and &lt;code&gt;revoked_at&lt;/code&gt;. A client retry can return the original result without creating a second active token, while a second click can be rejected by the token update.&lt;/p&gt;

&lt;p&gt;Do not put the raw token in this ledger. Hash the token before storage, keep it out of logs, and keep the command response free of the secret. A free disposable email inbox may be appropriate for an isolated test account, but the fixture should never become a shortcut around production identity checks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use PostgreSQL as the concurrency boundary
&lt;/h2&gt;

&lt;p&gt;The important write must be conditional and atomic. A read followed by an unconditional update has a race: two API workers can both read an unused token and both mark the user as verified.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;email_verification_tokens&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;consumed_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;token_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;consumed_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;revoked_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;expires_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the statement returns one row, the request won the consume operation. If it returns no rows, the token is expired, revoked, already consumed, or unknown. Wrap the token update and the user verification update in one transaction when they must succeed together.&lt;/p&gt;

&lt;p&gt;For issuance, insert the idempotency key first and handle a unique-key conflict by loading the previous command. Store a request fingerprint too, so the same key cannot be reused with a different payload. The contract are small, but it closes an awkward class of retry bugs.&lt;/p&gt;

&lt;p&gt;Cleanup should be asynchronous. Delete old command and token records after the audit period, and index &lt;code&gt;expires_at&lt;/code&gt; for predictable cleanup work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design the REST API response states
&lt;/h2&gt;

&lt;p&gt;Clients need stable categories, not a new message for every internal reason. A practical response model is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;201 Created&lt;/code&gt; when a new verification command issued a message.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;200 OK&lt;/code&gt; with the original result when the same idempotency key is retried.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;409 Conflict&lt;/code&gt; when the key is reused with a different request fingerprint.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;400&lt;/code&gt; or &lt;code&gt;410&lt;/code&gt; for a token that is invalid or expired, without revealing account details.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For asynchronous delivery, return a command ID and let the worker update delivery status. A &lt;a href="https://dev.to/mrdapperx/a-run-receipt-for-disposable-email-tests-1ali"&gt;run receipt for disposable email tests&lt;/a&gt; is a useful pattern: record a durable outcome without placing message contents in logs.&lt;/p&gt;

&lt;p&gt;Avoid making every old link look successful. That hides replay attempts and gives clients no signal that they need a fresh verification email. The response should be safe to expose, while the internal ledger retains the reason and correlation ID.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test with isolated email fixtures
&lt;/h2&gt;

&lt;p&gt;End-to-end tests should create a fresh user, issue one command, and then deliberately replay it. Verify that the second issuance returns the original command instead of creating another active token. Run two consume requests concurrently and assert that only one changes &lt;code&gt;consumed_at&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If the test needs a temporary inbox, &lt;a href="https://tempmailso.com" rel="noopener noreferrer"&gt;tempmailso&lt;/a&gt; can be used as an isolated fixture source. Keep the mailbox address and message ID scoped to the test run, and delete or expire the fixture after the test. The old label &lt;code&gt;tepm mail com&lt;/code&gt; may exist in a fixture migration, while &lt;code&gt;tem email&lt;/code&gt; can appear in a manually entered case; neither spelling should affect application behavior.&lt;/p&gt;

&lt;p&gt;For Node.js suites, a &lt;a href="https://dev.to/ryanlee91/nodejs-email-tests-need-a-message-cursor-iee"&gt;message cursor in email tests&lt;/a&gt; helps distinguish a newly delivered message from an earlier one. Test delivery retries separately from token consumption. A passing mail assertion does not prove that the verification endpoint is replay-safe.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Should the idempotency key live forever?
&lt;/h3&gt;

&lt;p&gt;No. Retain it for the longest realistic client retry window plus an operational margin. Keep a compact audit record longer only when the product or compliance policy requires it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can a token be the idempotency key?
&lt;/h3&gt;

&lt;p&gt;Usually no. A token represents a credential, while an idempotency key represents a command. Combining them makes issuance, retries, and security rotation harder to reason about.&lt;/p&gt;

&lt;h3&gt;
  
  
  What if the worker sends the email twice?
&lt;/h3&gt;

&lt;p&gt;The recipient may see duplicate messages, but both messages should point to the same token or clearly expire older tokens according to the product rule. Delivery deduplication and token single-use enforcement are related, not identical.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Define separate states for command processing and token consumption.&lt;/li&gt;
&lt;li&gt;Require an idempotency key for retriable verification commands.&lt;/li&gt;
&lt;li&gt;Store a request fingerprint and reject conflicting key reuse.&lt;/li&gt;
&lt;li&gt;Hash tokens and use a conditional PostgreSQL update for consumption.&lt;/li&gt;
&lt;li&gt;Keep issuance, user verification, and token state changes transactional.&lt;/li&gt;
&lt;li&gt;Return stable, privacy-safe response categories.&lt;/li&gt;
&lt;li&gt;Test retries, concurrent clicks, expiry, revocation, and duplicate delivery.&lt;/li&gt;
&lt;li&gt;Add cleanup ownership and a bounded audit policy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This design keeps the happy path simple while making retries observable. The edge cases dont disappear, but they become database-enforced transitions that a backend team can test, measure, and safely operate.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>restapi</category>
      <category>postgres</category>
      <category>authentication</category>
    </item>
  </channel>
</rss>
