Rails performance tooling · Engineering case study
NPlusInsight
Turning repeated SQL from log noise into source-level Rails diagnostics and concrete remediation guidance.
Rails logs can show that a query repeated, but the most expensive part of performance work is often the gap between evidence and cause. Which application line triggered the queries? Which relationship is being loaded lazily? What preload shape fixes the entire access path rather than one visible symptom?
NPlusInsight was built to close that gap. It detects N+1 query patterns during a request, attributes them to application source, infers the Active Record relationships involved, and presents the evidence with a candidate eager-loading or strict-loading remediation.
Detection pipeline
From runtime notifications to an actionable finding.
The engine subscribes to Rails’ `sql.active_record` instrumentation while a request is processed. It excludes cached queries, schema activity, transactions, and configured noise; normalizes literals in each remaining SELECT; and groups statements with the same structural shape.
- Capture the first relevant application stack frame.
- Extract tables from FROM and JOIN clauses.
- Normalize strings and numbers into a comparable SQL shape.
- Keep groups that meet the configured repetition threshold.
- Inspect Active Record reflections to infer association paths.
- Render a model tree and combined preload suggestion.
Findings produced by the same source line are combined. If a serializer lazily reaches both `post.comments` and `post.likes`, the result is one coherent report and a combined suggestion such as `Post.includes(:comments, :likes)`.
Feedback loop
The fix should be closer than the log line.
A small page indicator reports whether the current HTML response produced findings. Opening it reveals the surrounding source, grouped SQL patterns, query counts and duration, affected models, and remediation options without leaving the page. A mounted dashboard provides a broader process-local history.
“Good diagnostics should not merely announce a problem. They should preserve enough context for an engineer to make the next decision.”
The interface is intentionally evidence-first. NPlusInsight does not rewrite application code automatically, because custom scopes, polymorphic associations, and intentional repetition still require engineering judgment.
Operational design
Useful in development, explicit everywhere else.
The gem is environment-neutral: teams can enable it in development, test, staging, or a controlled production environment. Configuration supports path and SQL exclusions, a repetition threshold, bounded event retention, optional response injection, and a mode that raises on detection for targeted CI coverage.
- Privacy-aware storage Findings remain in process memory and are never sent to an external service.
- Clear deployment boundaries Documentation calls out source exposure, multi-process isolation, buffering, and access control.
- Rails-native integration An engine and middleware provide automatic mounting without application route boilerplate.
- Heuristics with humility Suggested fixes are presented as candidates, not infallible transformations.
The takeaway
Performance tooling is communication tooling.
NPlusInsight turns low-level runtime signals into a narrative an engineer can act on: where the work happened, what repeated, how the models connect, and what change is worth testing next.