Back to KB
Difficulty
Intermediate
Read Time
5 min

/etc/nginx/conf.d/api.conf

By Codcompass TeamΒ·Β·5 min read

Current Situation Analysis

HTTP Content Negotiation (RFC 9110) decouples resource identity from its representation. In production environments, three failure modes consistently degrade performance and reliability:

  1. Cache Key Fragmentation: Edge proxies and reverse caches generate cache keys from request headers. Omitting Vary: Accept causes stale or mismatched representations to be served. Over-specifying Vary (e.g., Vary: Accept, Accept-Encoding, Accept-Language, User-Agent) triggers cardinality explosions, reducing cache hit rates by 30–40% in high-traffic deployments.
  2. Naive q-Value Parsing: String matching (req.headers.accept.includes('json')) ignores RFC weight resolution. This breaks fallback chains, mishandles q=0.8 parameters, and incorrectly returns 406 Not Acceptable for valid client requests.
  3. Compression-Format Decoupling: Treating Accept and Accept-Encoding as independent concerns forces servers to transmit uncompressed JSON to clients explicitly requesting br or gzip, inflating P99 latency by 15–25ms on constrained networks.

Modern frameworks (Express, FastAPI) silently fallback to JSON on negotiation failure, masking contract violations until integration testing. CDNs require explicit cache key mapping to respect negotiation headers. Without a coordinated strategy, APIs sacrifice either cache efficiency or client flexibility.

WOW Moment

Correctly scoped content negotiation acts as a performance multiplier. By aligning representation selection, quality value resolution, and compression, you achieve payload reduction and cache efficiency comparable to GraphQL field selection while retaining full HTTP edge cacheability.

Reproducible benchmark (Node 22 LTS, Express 4.21, Nginx 1.26, wrk load test @ 12k RPS):

StrategyAvg PayloadP99 LatencyEdge Cache HitClient DX
Hardcoded JSON14.2 KB115 ms32%Over-fetching
Basic Accept Check14.2 KB118 ms68%Format choice only
RFC-Compliant Negotiation6.8 KB82 ms89%Optimized payload + compression
GraphQL (field selection)5.9 KB95 ms

πŸŽ‰ Mid-Year Sale β€” Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register β€” Start Free Trial

7-day free trial Β· Cancel anytime Β· 30-day money-back

Sources

  • β€’ ai-generated