Join our Newsletter — 33% off our NHI Course

Why pair request scope and invalidation in TypeORM caching?

 

(@nhi-mgmt-group)
Member Moderator
Joined: 2 years ago
Posts: 20230
Topic starter  

TL;DR: Duplicate database reads fell by about 30% in WorkOS's NestJS and TypeORM backend because the cache stayed tied to each request, so repeated lookups could reuse the same result. Request-scoped caching still needs immediate write invalidation to keep repeated reads from serving stale state.

Editorial analysis by NHI Mgmt Group, based on content published by WorkOS: “Query caching using Nest.js and Typeorm”.

Key questions

Q: Why does request-scoped caching reduce staleness risk compared with a shared cache?

A: A request-scoped cache expires naturally when the HTTP request ends, so it cannot leak older state into a later request.

Q: What are the signs that query duplication is hurting backend performance?

A: Look for repeated reads of the same user, entitlement, or environment data inside one request path, especially when logs show the same SQL firing multiple times during one page load.

Q: How should teams implement request-scoped query caching without rewriting every query?

A: Use a request-local cache provider that sits under the database adapter, so repeated reads can reuse the same query result automatically.

Practitioner guidance

  • Define the request boundary first Place any read cache inside request-local storage so repeated helper calls can reuse the same result without creating a shared cross-request state layer.
  • Invalidate on every write path Clear the cache immediately after save, update, or delete operations so a request never serves state that changed earlier in the same lifecycle.
  • Measure at the database layer Use PostgreSQL query statistics or equivalent backend telemetry to compare real query counts before and after the cache is enabled.

Bottom line: Repeated database reads inside a single request can create avoidable load even when application code is already well structured.

Glossary terms: Request-Scoped Cache, Continuation-Local Storage, Query Result Cache, Request-Scope Invalidation

Explore further

View Full Forum →  |  NHI Foundation Course →  |  Our Services →  |  Read the full analysis →


This topic was modified 1 week ago by NHI Mgmt Group

   
Quote
(@mr-nhi)
Member Moderator
Joined: 6 months ago
Posts: 20712
 

Request-scoped caching is a workload-shaping control, not a data-governance control. The article shows a common backend pattern: many helpers ask the same question about the same state during one request. That is not an access-control failure, but it is a performance and consistency pressure point that identity-aware platforms often underestimate. The practitioner lesson is to design for repeated state access without turning the application into a shared-state cache machine.

A question worth separating out:

Q: What should teams do when a cache improves performance but might hide fresh writes?

A: Treat write paths as invalidation points and clear the request cache before any read can reuse stale state. If the application needs consistent reads after mutation, the cache must be bounded to the request and coupled to the repository layer so freshness always wins over reuse.

👉 Read our full editorial: Request-scoped query caching cuts duplicate database reads by 30%



   
ReplyQuote
Share:

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.