Home / Companies / WorkOS / Blog / Post Details
Content Deep Dive

Query caching using Nest.js and Typeorm

Blog post from WorkOS

Post Details
Company
Date Published
Author
Ryan Cooke
Word Count
2,015
Language
English
Hacker News Points
-
Summary

Engineers at a company identified inefficiencies in their API code, where the same SQL query was executed multiple times during a single HTTP request, leading to unnecessary database load. To address this, they implemented a request-scoped in-memory query cache within their database adapter, which would store query results during the lifecycle of an HTTP request, reducing redundant database queries. Using NestJS and TypeORM, they encountered initial challenges due to TypeORM's caching limitations and NestJS's request-scoping complexities. By leveraging nestjs-cls for continuation-local storage and extending TypeORM's repository behavior, they managed to maintain cache integrity without extensive code rewrites. This optimization resulted in a 30% reduction in database queries during testing, demonstrating a significant performance improvement without the need for a large-scale refactor, thus allowing the team to focus on product development amidst increasing service loads.