Skip to content

perf(microservices): parse message patterns only when they may be json - #18277

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
OmBhandwaldar:perf/microservices-string-patterns
Oct 25, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
OmBhandwaldar:perf/microservices-string-patterns

Conversation

@OmBhandwaldar

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

getRouteFromPattern() calls JSON.parse() on each incoming pattern (server.ts#L460-L470). TCP, Redis, NATS, RMQ and Kafka look up handlers through it (Kafka calls it once more when no handler matches the topic exactly). For a string pattern like 'user.created', and every Kafka topic is one, JSON.parse() throws a SyntaxError on every message and the catch block falls back to the string. Creating that error with its stack trace is most of what the lookup costs. It's also the exception the debugger stopped on in #4066.

Server CPU time per message, from process.cpuUsage() over all handled messages. The client runs in its own process and keeps 100 send('ping', { id }) requests in flight, the handler returns its payload. 3 rounds per transport, before and after alternating, M5, Node 24.16:

before after
TCP 5.4 / 5.7 / 5.6 µs 3.1 / 3.0 / 3.2 µs
NATS 10.1 / 10.5 / 11.8 µs 6.9 / 7.3 / 7.3 µs
Redis 16.9 / 17.3 / 18.4 µs 14.5 / 14.6 / 13.7 µs

On TCP that goes from ~197k to ~345k messages/s, but only because the handler does nothing.

What is the new behavior?

JSON.parse() is skipped when the pattern can't be JSON, i.e. when (after whitespace) it doesn't start with {, [, ", - or a digit and isn't true, false or null. The catch block would have used the string in that case anyway, so the route stays the same. I compared the old and new routes for ~1M generated strings (whitespace including BOM and U+2028, partial literals, numbers, quotes, brackets) and a few non-string values, with no difference.

Checking only for {, like ServerMqtt does, wouldn't be enough here: numbers, literals and quoted strings are parsed today and some route differently ('"ping"' routes to ping).

An object pattern lookup gets ~10-16 ns slower (the regex test, ~210 ns before), a string pattern lookup goes from ~1.7 µs to ~12 ns.

Added unit tests for patterns that are parsed, ones that aren't, and ones that look like JSON but aren't. The microservices integration tests pass locally with Redis, NATS, MQTT and RabbitMQ.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

getRouteFromPattern() ran JSON.parse() on every incoming pattern, which
throws for a string pattern like "user.created" (or any kafka topic)
before the catch block falls back to the string. Skip the parse when the
pattern can't be json.
@kamilmysliwiec
kamilmysliwiec merged commit 11e1466 into nestjs:master Oct 25, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants