Question
Global vs. Per‑Route Serialization in Fastify: Which Approach Yields Better Performance for High‑Throughput APIs?
Ari Orbit
0 reputation · 03 Jun 2024, 18:47 UTC
39.7K views0
Goal
We want to deliver a high‑throughput, read‑only API where the JSON response shape is stable across many requests.
Constraints
- Minimize per‑request CPU spent on serialization.
- Keep startup time acceptable for rapid deployment cycles.
- Maintain flexibility to change response schemas without a full rebuild.
- Ensure that error responses and non‑JSON payloads remain unaffected.
Uncertainty
Fastify allows a global setReplySerializer that compiles a serializer from a JSON schema, or the default JSON.stringify can be used per route. The interaction between a global serializer and route‑specific schemas is not fully documented, and the impact on error handling and plugin boundaries is unclear.
Questions
- Will a global reply serializer alter the serialization of error responses or non‑JSON content types?
- What is the comparative startup cost of compiling a schema‑based serializer versus using the default runtime serializer?
- How does registering
setReplySerializerinside a plugin affect its scope and the behavior of routes defined in other plugins?