Skip to content

Gourav01dev/TaskFlow-API

Repository files navigation

TaskFlow API – Refactored and Optimized (Developer Notes)

🔍 Core Issues Identified

  1. Multiple DB Calls in several service methods (findOne, remove, etc.).

  2. No caching layer, causing repeated DB hits for identical queries.

  3. Unoptimized filtering — previously done in-memory, not at the DB level.

  4. Improper error handling and inconsistent repository usage.

  5. Inconsistent field naming (due-date vs dueDate) causing DTO mismatches.

  6. JWT secret misconfiguration and environment variable inconsistency.

  7. Missing transactional control for job queue integration.


⚙️ Architectural Improvements

  • Redis caching integration (for query-heavy endpoints like findAll, getStats).

  • QueryBuilder-based pagination & filtering (no in-memory filtering).

  • Transactional job enqueueing — queue jobs added after successful commit to avoid stale tasks.

  • Optimized repository pattern usage to reduce redundant queries.

  • Consistent DTO naming and schema alignment for dueDate.

  • JWT strategy cleanup — environment variable renamed to JWT_SECRET.

  • Centralized error handling using NestJS NotFoundException and InternalServerErrorException.


🚀 Performance & Security Enhancements

  • Added Redis caching (via @nestjs/cache-manager) to minimize DB load.

  • Cache invalidation handled automatically after create/update/delete operations.

  • Background job retries with exponential backoff using BullMQ.

  • All DB operations wrapped in TypeORM transactions for data integrity.

  • JWT secret stored securely via environment variable (JWT_SECRET).

  • Removed duplicate DB queries and replaced with optimized QueryBuilder operations.


🧩 Key Technical Decisions

Decision Rationale
Redis caching Reduces DB calls for read-heavy endpoints.
QueryBuilder for filtering Enables efficient DB-level filtering & pagination.
Transactions + BullMQ Ensures data consistency before job enqueue.
Consistent DTO schema Prevents mismatches between code & DB schema.
Centralized logging Helps trace background job or DB failures.

⚖️ Trade-offs

  • Slight overhead for maintaining cache invalidation logic.

  • Redis adds infrastructure complexity but provides significant performance gain.

  • Transactions may introduce minimal latency, but ensure data consistency.


🧱 Files Modified & Summary

File Description
src/modules/tasks/tasks.service.ts Major refactor — added Redis cache, optimized DB calls, transactional queue integration.
src/modules/tasks/dto/task-filter.dto.ts Added pagination & filtering support.
src/modules/tasks/dto/create-task.dto.ts Fixed naming conflict (dueDate field).
src/modules/tasks/tasks.controller.ts Updated endpoints to align with new service structure.
src/modules/tasks/task-processor.service.ts Handles background jobs with retry/backoff.
src/modules/schedule-tasks/schedule-tasks.module.ts Imported TypeOrmModule & registered Task entity.
src/auth/jwt.strategy.ts Changed env variable to JWT_SECRET.

🧭 Developer Notes

  • After every new service or module creation, register it in its corresponding controller.

  • Maintain consistent DTO validation and naming across all modules.

  • Verify Redis caching using redis-cli monitor or by adding console.log for cache hits/misses.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published