Idempotent social publishing: preventing duplicate posts when provider APIs time out

6 min readReliability engineeringView as Markdown

Preventing duplicate social posts requires a deterministic idempotency key per publish intent, per-target delivery isolation, and a dedicated unresolved state whenever a social API times out after receiving a write. Never auto-retry an ambiguous write without reconciling provider status first.

1. Why naive HTTP retries cause double-posting

Social networks frequently accept a publish request, commit the post to the user's feed, and then drop the TCP connection or return a 504 Gateway Timeout before sending the external post ID back. A naive retry loop immediately posts the same caption and video a second time.

In multi-platform posts, the failure mode compounds: LinkedIn succeeds in 900 ms while X times out at 15 seconds. Retrying the whole draft would duplicate LinkedIn.

2. Per-target delivery guards and the five outcome states

PubNio processes each target destination independently, with an atomic delivery check to guard against duplicate publishes. Every target settles into one of five explicit states:

  • published: Provider confirmed live publication and returned an external post ID and URL.
  • provider_draft: Media was intentionally uploaded to a provider draft inbox (such as TikTok inbox mode).
  • failed: Provider rejected the request cleanly before publishing; fix the typed IssueCode and retry safely.
  • unresolved: Network timeout or ambiguous upstream response; do not blindly create a new post, reconcile first.
  • cancelled: Scheduled delivery was cancelled before dispatch.
idempotent-publish.ts · @pubnio/sdk
// Safe to replay from a webhook worker, queue consumer, or AI agent
const job = await ws.posts.publish(draft.postId, {
  expectedRevision: draft.revision,
  idempotencyKey: `campaign-${campaignId}-v${draft.revision}`,
});

// Poll or listen via signed webhook for per-target states
const status = await ws.posts.status(job.postId);
for (const target of status.targets) {
  console.log(target.provider, target.outcome, target.externalUrl);
}