PredictivTrend
Backend Engineer
A streaming-first AI stock-analysis backend — Express + Cloudflare Workers pull Polygon.io market data and stream Mistral/OpenAI performance reports to the browser over SSE, with abort propagation and backpressure-safe buffering.
stack
01 · Requirement Analysis
Retail users wanted understandable stock narratives — performance summaries plus directional reads — without raw terminals. The product needed trustworthy, fast-readable reports, and they had to render live rather than after a long server-side wait.
02 · Architecture Discovery
Chose a microservices-inspired split: an Express backend plus two Cloudflare Workers — a Polygon Worker for market data and an AI Worker for Mistral/OpenAI generation. Adopted a streaming-first flow where the controller opens an SSE connection, the service pulls OHLC from the Polygon Worker, then proxies the AI token stream straight through to the browser. Validated retry/backoff and abort semantics end-to-end.
03 · Software Architecture
A TypeScript Express 5 API where prediction controllers immediately set SSE (better-sse) headers; a predictiv service orchestrates the data pull (Polygon Worker, via axios-retry) and an AI stream passthrough (Mistral/OpenAI Worker emitting standard SSE chunks). Workers run on Cloudflare; MongoDB/Mongoose handles persistence; helmet, express-rate-limit and Zod validation harden every edge; the API is deployed to Render under predictable /api routes.
04 · Development Principles
Leak-proof abort propagation — a client disconnect drives an AbortController through Express and Axios all the way to the worker and halts the model mid-stream to save compute; axios-retry with exponential backoff under a strict 5s timeout; Mistral streaming bound in a CAF async generator wrapped with async-retry; and native Web Streams backpressure (enqueue only when controller.desiredSize > 0) to prevent buffer bloat.
05 · Impact
Reports render token-by-token in the browser with perceived instant latency, resilient to worker stutter, and cost-controlled — a closed tab instantly halts the underlying model call, so abandoned sessions don't burn AI credits.