0
0
NestJSframework~8 mins

Migrations in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Migrations
MEDIUM IMPACT
Migrations affect the initial page load speed and backend response time by managing database schema changes efficiently.
Applying database schema changes during app startup
NestJS
await connection.runMigrations(); // run only pending migrations
Runs only incremental changes, reducing startup time and avoiding full schema rebuild.
📈 Performance Gainreduces backend startup delay to under 100ms, improving LCP
Applying database schema changes during app startup
NestJS
await connection.synchronize(true); // auto-sync schema on every app start
This triggers full schema synchronization on every startup, causing delays and potential data loss.
📉 Performance Costblocks backend response for 500ms+ on each start, increasing LCP
Performance Comparison
PatternBackend DelayUser Request ImpactLCP ImpactVerdict
Full schema sync on startupHigh (500ms+)Blocks startupHigh[X] Bad
Incremental migrations on startupLow (under 100ms)No block on requestsLow[OK] Good
Migrations during user requestsHighBlocks user inputHigh[X] Bad
Migrations before requestsLowNo block on user inputLow[OK] Good
Rendering Pipeline
Migrations run on the backend before serving content, affecting the time to first byte and thus the browser's ability to start rendering.
Backend Processing
Network Response
First Paint
⚠️ BottleneckBackend Processing delays due to heavy migration operations
Core Web Vital Affected
LCP
Migrations affect the initial page load speed and backend response time by managing database schema changes efficiently.
Optimization Tips
1Avoid full schema synchronization on every app start to reduce backend startup delay.
2Run migrations incrementally and outside of user request paths to prevent blocking user input.
3Schedule migrations during deployment or startup to improve Largest Contentful Paint (LCP).
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of running full schema synchronization on every app startup?
AIt improves user input responsiveness.
BIt reduces bundle size significantly.
CIt blocks backend startup, increasing time to first byte and LCP.
DIt decreases network latency.
DevTools: Network and Performance panels
How to check: Use Performance panel to record backend response time and Network panel to check time to first byte (TTFB).
What to look for: Look for long backend processing times before first byte and delayed first paint indicating migration delays.