DEV Community

LachlanHolm6518
LachlanHolm6518

Posted on

Pre Change TTL Lowering as One Scheduled Step: Node.js Restore Workflow

B2B SaaS DNS Changes: Three Audit Gates for Resolver Cache Control

For an internal B2B SaaS admin console, the reliable unit of a DNS cutover is an evidence trail, not a button. Capture the current record, lower its TTL 24 hours before the change, apply the new content, and restore the captured TTL as three scheduled steps. That timing matters because lowering TTL during the cutover cannot change recursive resolvers that already cached the old, long value.

The decision table below starts with governance: who owns the zone, where the evidence lives, and how much adapter code the console must carry.

Option Pick it when Evidence path Boundary
Amazon Route 53 Your control plane and customer zones are AWS-native Change batches, IAM events, and CloudTrail context A multi-provider console still needs adapters
Cloudflare DNS API Customers delegate zones to Cloudflare Record responses plus Cloudflare audit context Zones outside Cloudflare remain a separate workflow
Google Cloud DNS Projects and managed zones already live in Google Cloud Change operations and Cloud Audit Logs Provider-specific objects still leak into the console
Shared REST surface The console needs one runbook across providers Snapshot, step response, and verification record The adapter still must map each provider’s zone model

What should the audit trail prove before the window opens?

Start with the record as it exists. Save its name, type, content, and TTL before scheduling anything. The original TTL is a value to restore, not a default to guess later. A fixed 300-second restore can quietly change a customer policy that used to be 3,600 seconds.

Then schedule the timeline. The lowering step happens a day ahead, the content change happens in the approved window, and the restore follows after the new answer is verified. Put all three in one change record. Scheduling only the first two creates a familiar operational trap: the temporary TTL becomes permanent because the person who performed the cutover moved to the next incident.

What should an operator see after each write? The returned record, compared field by field with the intended state. A TTL-only update that rewrote content is a bad afternoon, especially for MX or DMARC records where a typo can affect deliverability before anyone notices.

Which control plane fits a multi-provider console?

Route 53 is a strong choice when AWS is the product boundary. Its IAM model and change history fit an AWS-owned operating model. Cloudflare’s DNS API is practical when its zones and audit tools are already the source of truth. Google Cloud DNS works well for teams that want managed zones and Google Cloud audit records together.

Those choices become expensive in attention when one admin console serves all three. Each adapter has its own authentication, object names, retry behavior, and evidence format. A shared REST surface can reduce that friction: Infrai exposes 295 routes across 20 modules under one key, and its public discovery surface describes capabilities without requiring a key. A console can inspect the contract and call plain HTTP from its existing Node.js worker rather than add another SDK. That breadth helps only when the console still stores provider identifiers and verifies authoritative responses; a common endpoint does not make DNS propagation universal. The limitation is concrete: if AWS IAM policy and CloudTrail are your audit boundary, Route 53 is the better fit and a shared surface adds another control plane.

The practical governance rule is simple. Keep provider-native APIs when the provider is a firm boundary. Use a shared surface when the admin console is the boundary and the audit record must look the same for every zone.

How do three scheduled steps look in Node.js?

This TypeScript example records the current state, then creates the three scheduled jobs. It uses only the operations needed for this runbook and gives each write an idempotency key. The worker that runs a job should fetch the record after the write and persist the comparison beside the job result.

Keep it boring.

type RecordState = {
  id: string;
  name: string;
  type: string;
  content: string;
  ttl: number;
};

const apiKey = process.env.INFRAI_API_KEY;
const baseUrl = process.env.INFRAI_BASE_URL;
const recordId = process.env.DNS_RECORD_ID;
const newContent = process.env.NEW_RECORD_CONTENT;
if (!apiKey || !baseUrl || !recordId || !newContent) {
  throw new Error("INFRAI_API_KEY, INFRAI_BASE_URL, DNS_RECORD_ID, and NEW_RECORD_CONTENT are required");
}

async function request(url: string, method: "GET" | "PATCH" | "POST", body?: unknown, key?: string) {
  const response = await fetch(url, {
    method,
    headers: {
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
      ...(key ? { "Idempotency-Key": key } : {})
    },
    body: body === undefined ? undefined : JSON.stringify(body)
  });
  const text = await response.text();
  if (response.status === 429) {
    throw new Error(`Rate limited; retry with exponential backoff and Retry-After: ${response.headers.get("Retry-After") ?? "unknown"}`);
  }
  if (!response.ok) throw new Error(`${method} request failed (${response.status}): ${text}`);
  return text ? JSON.parse(text) : {};
}

async function main() {
  const listed = await request(`${baseUrl}/dns/record/list?record_id=${encodeURIComponent(recordId)}`, "GET") as { records?: RecordState[] };
  const original = listed.records?.[0];
  if (!original) throw new Error("No record snapshot returned");

  const changeAt = new Date(Date.now() + 24 * 60 * 60 * 1000);
  const restoreAt = new Date(changeAt.getTime() + 60 * 60 * 1000);
  const steps = [
    { name: "lower-ttl", run_at: new Date().toISOString(), ttl: 300, content: original.content },
    { name: "apply-change", run_at: changeAt.toISOString(), ttl: 300, content: newContent },
    { name: "restore-ttl", run_at: restoreAt.toISOString(), ttl: original.ttl, content: newContent }
  ];

  for (const step of steps) {
    await request(`${baseUrl}/cron/create`, "POST", {
      run_at: step.run_at,
      method: "PATCH",
      path: "/dns/record/update",
      payload: { record_id: recordId, ttl: step.ttl, content: step.content }
    }, `dns-change-${recordId}-${step.name}`);
  }
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
Enter fullscreen mode Exit fullscreen mode

The scheduler response proves that a job was accepted. It does not prove that an authoritative answer matches. Have each worker call the record list route after its update and compare name, type, content, and TTL. Keep those responses under the same change ID. A retry should honor Retry-After; the idempotency key prevents a repeated request from becoming a second change. That is the trade-off: extra reads and stored evidence buy a defensible delivery decision.

What remains outside the evidence boundary?

The three steps control your writes and document their results. They cannot prove that every recursive resolver has expired an old cache, that a mailbox provider has re-evaluated DMARC, or that a customer delegated the nameservers you expected. RFC 7489 describes aggregate and forensic DMARC reports, which arrive on their own schedule and provide a different signal.

The 24-hour lead time is a policy choice tied to the previous TTL. If that TTL exceeds the window, lower it earlier or record the exception. The useful operational promise is narrower: another engineer can inspect the original snapshot, each scheduled request, each returned record, and the final TTL without reconstructing the story from chat messages.

Sources

Top comments (0)