PRODUCTION BACKEND SYSTEM

Multi-Platform Publishing & Distributed Concurrency Backend

Building core backend microservices for multi-tenant content publishing and synchronization — handling OAuth token concurrency races, atomic job reservations, and idempotent multi-channel dispatches across 340+ production commits.

Role Backend Developer (Core Contributor — Publishing & Media)
System Type Live Production Microservice
Timeline 09/2025 – Present
Stack NestJS 11 · Fastify · Drizzle · PostgreSQL · Docker

1. The Problem & Technical Context

The team required an automated, reliable system to schedule, format, transcode, and publish multi-media content across multiple third-party social media providers (Facebook, YouTube, TikTok, Telegram) through a unified REST/OAuth gateway (Zernio integration).

In early prototypes, high concurrency and external API flakiness caused operational bottlenecks:

  • OAuth Token Refresh Stampede: When an access token expired, multiple concurrent background workers attempted to exchange the refresh token simultaneously, causing invalidation of active session tokens and cascade authentication failures.
  • Duplicate Dispatch on Network Retry: A brief network timeout during third-party publishing could cause worker retries, leading to identical marketing videos being published 2 or 3 times to user channels.
  • Desynchronized Scheduled State: Scheduled posts would occasionally remain in a "PENDING" or "PROCESSING" state indefinitely if a worker pod restarted mid-flight.
My Role & Direct Contributions (Core Modules)

Over 340+ Git commits, I designed and implemented the publishing pipeline, social account integration modules, media transcoding fallback workflows, and authored 30+ Jest unit/integration test suites covering concurrency and failure edge cases.

2. System Architecture & Data Flow

The microservice is built with NestJS 11 on top of the high-throughput Fastify adapter, utilizing Drizzle ORM with PostgreSQL for type-safe schema migrations and atomic SQL transactions.

API Client / Cron HTTP / Triggers NestJS Publishing Core Single-Flight Token Mutex Atomic Job Claiming (SQL) HMAC Webhook Validator PostgreSQL Drizzle ORM Zernio / Providers FB, YT, TikTok
Figure 1.1: Multi-Platform Publishing Data Pipeline & Concurrency Control

3. Key Engineering Decisions & Trade-Offs

Decision 1: In-Memory Single-Flight Mutex for OAuth Token Refresh

Problem When 10 scheduled posts for the same social account fire simultaneously upon token expiry, 10 parallel refresh calls hit the provider. Providers with refresh-token rotation invalidate all requests except the first, locking out the user.
Decision Engineered an in-memory SingleFlightMutex service. The first incoming request initiates the async HTTP token refresh and stores the pending Promise. All 9 subsequent concurrent requests join and await the exact same Promise without making additional network calls.
Trade-off Works per Node.js instance. In multi-pod horizontal scaling, a Redis distributed lock (Redlock) or database row-level locking (SELECT ... FOR UPDATE) is used as a secondary fence.

Decision 2: Atomic Job Claiming via PostgreSQL SKIP LOCKED

Problem Multiple worker instances running concurrent cron ticks frequently picked up the same pending publish tasks, causing duplicate publishing dispatches.
Decision Used SQL SELECT ... FOR UPDATE SKIP LOCKED inside an atomic transaction. A worker locks and transitions rows to 'PROCESSING' atomically, allowing concurrent workers to immediately skip to unreserved records with zero lock contention.
Trade-off Ties queue semantics to PostgreSQL instead of a dedicated broker (RabbitMQ/Kafka), but eliminated the operational overhead of managing extra infrastructure while handling our throughput needs reliably.

Decision 3: Multi-Layer Video Transcoding & Fallback Pipeline

Problem Certain video codecs (e.g. ProRes, H.265, uncompressed MOV) from client uploads were rejected by third-party social endpoints (TikTok/Instagram require strict H.264/AAC MP4).
Decision Built a modular pipeline: (1) Fast FFprobe metadata inspection → (2) Direct pass-through if compatible → (3) Fallback transcoding queue if dimensions/codecs violate platform constraints.
Trade-off Transcoding increases server CPU utilization, mitigated by spawning background FFmpeg worker processes with concurrency limits.

4. Resilient Publishing State Machine Implementation

src/modules/publishing/publish.service.ts
// Single-Flight Mutex pattern for OAuth token refresh deduplication @Injectable() export class TokenManagerService { private inFlightRefreshes = new Map<string, Promise<string>>(); async getValidToken(accountId: string): Promise<string> { const existingRefresh = this.inFlightRefreshes.get(accountId); if (existingRefresh) { // Coalesce duplicate callers into the single in-flight promise return existingRefresh; } const refreshPromise = this.executeTokenRefresh(accountId).finally(() => { this.inFlightRefreshes.delete(accountId); }); this.inFlightRefreshes.set(accountId, refreshPromise); return refreshPromise; } }

5. Verification & Testing Strategy

To ensure production zero-regression, I authored over 30 Jest test suites encompassing unit tests for services and integration tests with a live test database container.

  • Mutex Deduplication Tests: Verified that firing 20 concurrent getValidToken() calls results in exactly 1 HTTP network invocation.
  • Idempotency Validation: Verified that duplicate webhook payloads with matching idempotency keys return 200 OK without duplicate DB inserts.
  • Reconcile Sweep Tests: Simulated crashed workers and verified that stale tasks older than 15 minutes are reset to FAILED or re-queued gracefully.
What I Would Improve Next (Engineering Reflection)

If scaling to 100,000+ posts/hour, I would transition the PostgreSQL SKIP LOCKED queue to a dedicated distributed message broker like BullMQ (Redis) or Apache Kafka with partitioned partition keys by account ID to ensure horizontal FIFO scheduling.

Additionally, implementing OpenTelemetry distributed tracing across the worker lifecycle would provide granular p99 latency insights into third-party API response degradation.