Skip to content

Architecture

A full-stack architecture that survives year two

November 14, 202511 min readRasel Hossain
Architecture

Quick answer

Systems survive year two through layering: UI, API, domain logic, data access and infrastructure stay separate with dependencies pointing inward. Add a queue and worker at the first background job, version the database with migrations, centralize auth, and containerize deployment with monitoring from day one.

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.

  1. Separate UI, API, domain, data and infrastructure with inward-pointing dependencies
  2. Put every background task behind a queue with retries and a dead-letter queue
  3. Version the database with migrations and review every schema change like code
  4. Centralize auth, roles and audit logging instead of scattering checks
  5. 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.

Written by

Rasel Hossain — author photo

Rasel Hossain

Full Stack Developer & AI Automation Engineer — 6+ years, 168 projects delivered on Fiverr, SaaS platforms and automation in production.

Published:

#Architecture#NestJS#PostgreSQL#DevOps

How to do it

  1. 1

    Draw the layers

    Define UI, API, domain, data and infrastructure boundaries with dependencies pointing inward only.

  2. 2

    Add the queue early

    Route background work through retries and dead-letter handling with idempotent workers.

  3. 3

    Discipline the data

    Version the schema with reviewed migrations and separate reporting load from transactions.

  4. 4

    Ship observably

    Containerize, automate deploys, and alert on errors, queues, database and certificates from release one.

Frequently asked questions

When should I add a queue to my project?

At the first background task — email, notifications, reports, file processing. Retrofitting a queue later means rewriting every call site under pressure. Starting with one keeps request paths fast and scaling a config change.

Monolith or microservices for a new SaaS?

A modular monolith with clear internal boundaries. It deploys simply, debugs easily, and splits into services later only where measured load demands it. Premature microservices multiply infrastructure work without business benefit.

How do I keep the database maintainable?

Migrations in version control, reviewed schema changes, deliberate indexes for measured queries, and reporting reads separated from transactional load as they grow. Never share a database between services without an explicit contract.

What monitoring is essential from day one?

Error rates with alerts, queue depth and dead-letter counts, database CPU and slow queries, disk and certificate expiry, and uptime checks on critical flows. Five dashboards that page a human beat fifty that nobody reads.

More articles

Related reading from the same areas — practical notes on shipping software.

View all articles

Liked the article?

Have a similar problem in your business? Let's talk about building the fix.

Start a project