NestJS + TypeORM: Managing In‑Flight Requests After a Database Restore
0 reputation · 07 Nov 2025, 15:22 UTC
0 reputation · 07 Nov 2025, 15:22 UTC
Goal: Verify that a NestJS service using TypeORM can resume normal operation after a database backup has been restored without processing requests that were in flight at the moment of the dump.
Constraints: The backup is performed externally (e.g., pg_dump) and the application is restarted against the restored instance; TypeORM does not retain knowledge of transactions that were open when the backup was taken, so any HTTP handler or background job that had begun but not completed may encounter stale data or violate constraints when it resumes.
34775 reputation · 07 Nov 2025, 19:31 UTC
TypeORM and NestJS are stateless regarding external database events. Because a pg_dump or similar restore happens outside the application layer, TypeORM cannot natively detect if a specific request was "in-flight" during the backup window. Furthermore, since the application is restarted against the restored instance, all in-memory request contexts are wiped; requests cannot "resume" in the same process—they must be retried by the client or a queue manager.
To manage consistency and prevent the processing of stale or duplicate requests after a restore, implement the following patterns:
The application cannot detect a "backup window" retrospectively. Instead, you must ensure that any request that was partially processed before the dump is not duplicated after the restore. Implement Idempotency Keys (passed in headers by the client):
To minimize the number of in-flight requests that are interrupted during the restore process, use NestJS shutdown hooks to implement a drain period. This prevents the "hard kill" of transactions.
// main.ts async function bootstrap() { const app = await NestFactory.create(AppModule); // Enable shutdown hooks to handle SIGTERM app.enableShutdownHooks(); await app.listen(3000); }When the orchestration layer (e.g., Kubernetes) sends a
SIGTERM, NestJS will stop accepting new connections and attempt to finish active requests before the process exits, ensuring fewer requests are left in an ambiguous state during the database downtime.Implementation Summary
| Goal | Mechanism | Responsibility |
|---|---|---|
| Detect In-Flight Requests | Idempotency Keys / Request Logs | Application Layer |
| Prevent Stale Writes | Database Transactions (ACID) | TypeORM / Postgres |
| Minimize Interruptions | enableShutdownHooks() |
NestJS Framework |
SIGTERM; verify the request completes before the process terminates.Diagnostic Detail Needed: Are these in-flight requests primarily synchronous HTTP calls or asynchronous background jobs (e.g., BullMQ)? The strategy for replaying failed background jobs differs significantly from HTTP request retries.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 07 Nov 2025, 18:07 UTC
NestJS shutdown hooks and TypeORM’s connection-close API are version-dependent. TypeORM v0.2 exposes Connection.close(), while v0.3 uses DataSource.destroy(); minor releases may alias close(). Calling the wrong method during SIGTERM can leave the pool open or throw before the drain finishes. Confirm the installed NestJS and TypeORM versions, then close the data source only after the HTTP layer stops accepting new requests. Verify by sending SIGTERM while a long-running TypeORM query executes and confirming the process exits only after the hook timeout or request completion.