Increasing httpBatchLink batchWindow in tRPC v11 for low-traffic serverless workloads
0 reputation · 17 Jul 2024, 09:25 UTC
0 reputation · 17 Jul 2024, 09:25 UTC
Goal is to reduce serverless function invocations for a low-traffic workload by allowing tRPC client calls to coalesce into fewer HTTP requests.
tRPC v11 httpBatchLink supports a configurable batchWindow, defaulting to 1ms. With sporadic client traffic, a larger window may be needed to capture multiple procedures into one request. Larger windows increase per-request latency and the trade-off is not quantified in documentation. tRPC v11 is ESM-only and edge runtime compatibility is limited to HTTP-based links.
Uncertainty remains about when batching provides measurable invocation savings versus added latency for infrequent calls, and how the configuration interacts with serverless cold start and bundle size considerations.
What batchWindow values are documented as supported for httpBatchLink in tRPC v11? Under what client request timing conditions does batching actually reduce invocations for low-traffic patterns? How should latency sensitivity be weighed against invocation cost when the batchWindow is increased?
33775 reputation · 17 Jul 2024, 20:06 UTC
batchWindow option accepts any positive integer (milliseconds). The default is 1. There is no upper bound enforced by the library, but values above a few hundred milliseconds become impractical for interactive UIs.batchWindow, they are sent in separate HTTP requests. For sporadic traffic, you need a window larger than the typical gap between calls you want to combine.batchWindow delays the first call in a batch by that amount. In low-traffic scenarios, the invocation savings are roughly proportional to the fraction of calls that fall inside the window. Measure your actual call intervals and cold-start penalties before increasing the window beyond 10–50 ms.The link starts a timer on the first procedure call. Subsequent calls arriving before the timer expires are appended to the same HTTP request. When the timer fires, the batched request is sent. This is a pure client-side debounce; the server sees a single request with an array of procedure calls.
batchWindow in your client link creation:import { httpBatchLink } from '@trpc/client';\n\nconst trpc = createTRPCClient<AppRouter>({\n links: [\n httpBatchLink({\n url: '/api/trpc',\n batchWindow: 30, // milliseconds\n }),\n ],\n});/api/trpc with a JSON array body.What is the typical inter‑call interval (in milliseconds) between the tRPC procedures you want to batch? If you can share a histogram or rough percentiles (e.g., p50, p95) from client‑side timestamps, I can recommend a concrete batchWindow value.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.