Repository navigation
perf(microservices): parse message patterns only when they may be json - #18277
Merged
kamilmysliwiec merged 1 commit intoOct 25, 2026
Merged
kamilmysliwiec merged 1 commit into
kamilmysliwiec merged 1 commit into
Conversation
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.
4 of 12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
getRouteFromPattern()callsJSON.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 aSyntaxErroron 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 100send('ping', { id })requests in flight, the handler returns its payload. 3 rounds per transport, before and after alternating, M5, Node 24.16: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'ttrue,falseornull. 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
{, likeServerMqttdoes, wouldn't be enough here: numbers, literals and quoted strings are parsed today and some route differently ('"ping"'routes toping).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?
Other information