Most projects do not die from bad code — they die from tangled dependencies. Separate the layers early: UI, API, domain logic, data access and infrastructure. I have inherited four production systems that ignored this, and every one had the same symptom: a simple feature requiring changes in seven places because business rules lived inside button handlers, API routes and database triggers simultaneously.
Layers with a direction
The rule that fixes this is dependency direction: UI depends on the API, the API depends on domain logic, domain logic depends on data access abstractions, and nothing depends upward. A React component never imports a database client; a database migration never knows about a screen. When a new developer can draw the dependency arrows without crossing lines, the architecture is healthy.
In practice with Next.js and NestJS this means modular boundaries with explicit contracts. Each domain module owns its routes, services and data access, exposing only typed interfaces. Shared code goes through a documented common package, not through reaching into another module internals. The first time you feel tempted to import across modules directly is the moment to define the interface instead.
Queues and workers from the first background job
Add a queue and a worker the moment you see background work, even trivial jobs. It keeps request paths fast and makes future scaling a configuration change instead of a rewrite. Email sending is the classic first candidate — it looks synchronous until the provider has a slow day and every signup times out. Notifications, report generation, image processing and AI calls all belong behind the queue from the start.
My standard setup is a durable queue with retries, a dead-letter queue for poison messages, and idempotent workers that can safely re-run. Every job logs its input, attempts and outcome. This sounds like ceremony until the first incident, when the difference between we replayed the failed jobs and we lost Tuesday afternoon is the entire business.
- Separate UI, API, domain, data and infrastructure with inward-pointing dependencies
- Put every background task behind a queue with retries and a dead-letter queue
- Version the database with migrations and review every schema change like code
- Centralize auth, roles and audit logging instead of scattering checks
- Containerize, automate deploys and monitor from the first production release
Data, auth and deployment discipline
Treat the database schema as code: migrations in version control, reviewed like any pull request, with rollback tested before it is needed. Add indexes deliberately for measured queries rather than preemptively, and keep reporting reads off the transactional database once they grow — a read replica or a scheduled materialization saves more incidents than any amount of query tuning.
Centralize authentication and authorization in one layer with explicit roles, and log who did what for anything consequential. Scattered permission checks drift out of sync; a single policy module stays auditable. On deployment, containerize everything, keep environment configuration explicit, automate the pipeline so releases are boring, and monitor error rates, queue depth and database health from day one — observability added after the first outage is always more expensive.
Deployment matters as much as code: containerize, keep a repeatable setup with Nginx, process management and SSL, and monitor from day one. Systems built this way do not just survive year two — they welcome it, because each layer can evolve without dragging the others along. If you are planning such a system, my development services scope exactly this layering up front, and the AI automation guide shows how workers fit the pattern.
