Skip to content
Ashwani Kumar
All work
Status: In production (employer)Professional experienceAlpaca · 2025 – present

Reliable financial transfer workflows

Durable, idempotent workflows that move brokerage accounts — thousands of assets and their tax lots — between institutions through size-limited external APIs.

A generalized engineering case study. It describes the class of problem and common patterns, not Alpaca's internal systems, code or data.

Solution architecture

Scroll sideways to see the full diagram.

Transfer workflow, simplifiedEvents start one durable workflow per transfer. Large payloads are planned into bounded batches; each batch is an idempotent, retryable activity whose progress is committed to PostgreSQL. Components: Clearing-house events, Event ingestion, Transfer workflow, Batch planner, Batch activities, External systems, PostgreSQL, Metrics & logs.Temporal workflowstart / signalcommit progressClearing-house eventsACATS transfer messagesEvent ingestiondedupe · idempotent startTransfer workflowone per transfer · explicit statesBatch plannersized to request limitsBatch activitiesretry · idempotency keyExternal systemscustody · cost basisPostgreSQLstate · batch ledgerMetrics & logsstuck-transfer alerts
Events start one durable workflow per transfer. Large payloads are planned into bounded batches; each batch is an idempotent, retryable activity whose progress is committed to PostgreSQL.
  • Service
  • Workflow
  • Data
  • External

The business problem

When a customer moves a brokerage account, the transfer arrives as a series of industry events (ACATS, via the clearing house) and progresses through many states over several days. One transfer can contain thousands of positions, and each position can carry a long history of tax lots that has to be reported accurately for cost basis.

What needed to be built

Services that turn a stream of transfer events into reliable, resumable workflows — batching very large payloads for external systems and recording progress so nothing is processed twice.

Engineering approach

Every transfer has to move forward exactly once even though the work is long-running and spread across external parties. Downstream APIs cap request sizes, any call can fail or time out, and events can arrive late or twice. A naive retry can duplicate positions; a lost step can strand an account mid-transfer.

  1. 01

    Ingest clearing-house events through a service that deduplicates them and starts or signals one durable workflow per transfer, keyed by the transfer identifier so repeated events are harmless.

  2. 02

    Model the transfer lifecycle as explicit workflow states, so every transition is recorded and a process can resume after a deploy, crash or long external wait.

  3. 03

    Split asset and tax-lot payloads into bounded batches sized to each external API's limits, and run every batch as its own activity with a retry policy and an idempotency key.

  4. 04

    Persist transfer and batch progress in PostgreSQL so retries continue from the last committed batch rather than starting over.

  5. 05

    Use timeouts for waits on external parties and surface stuck or failing transfers through metrics and structured logs instead of discovering them from customer reports.

What this means for your business

The same patterns — durable workflows, idempotent steps, bounded batches, explicit retries — are what make an AI agent safe to run against real business systems. An agent that calls your APIs needs exactly this engineering underneath it.

Discuss a project like this