# Backend Architecture: APIs, Data, and Authorization

Canonical page: https://get-vaastu.vercel.app/guides/backend-architecture

Use Vaastu's backend architecture skill to plan APIs, authorization, domain rules, database operations, retries, and concurrency with your AI coding agent.

## Start with one trusted operation

Use Vaastu for a backend architecture decision when ownership, authorization, data consistency, or recovery is unclear. Keep the existing language, framework, transport, and persistence unless a concrete requirement justifies changing them. A backend-only service does not need a frontend or a monorepo to use the skill.

Consider an illustrative resource update: an authenticated actor changes an allowed field on a record within their authorized scope. Two actors may edit concurrently, and a request may be retried after its response is lost. Specify the intended behavior before adding layers. This is a design exercise, not a verified service.

## Keep adapters thin and operations coherent

The transport adapter parses the request, obtains verified identity, invokes the owning feature operation, and maps the result. The operation enforces resource access and business rules. Scoped queries and writes remain owned by the feature or a justified persistence capability.

These are responsibilities rather than mandatory controller, service, and repository files. Separate complex rules for independent testing or isolate an external provider behind an adapter when that clarifies behavior. Keep lower-level modules independent of routes and avoid a generic abstraction for every table.

## Protect the resource rather than only the route

Validate runtime input and use verified identity. A supplied actor ID, tenant, role, or owner field is not authority. Scope both reads and writes to the permitted resource and explicitly choose which fields may change.

Test a permitted update alongside another user's known record ID, a different tenant where relevant, missing identity, malformed values, and protected-field edits. Stable errors should support callers without exposing database exceptions or private resource details.

## Make the update correct under concurrency

Choose the domain policy for competing edits: a version precondition, conditional write, lock, or other database-supported mechanism. A read followed by a write inside one application function does not automatically prevent a race. Use transactions and constraints for changes that must succeed or fail together.

Where a repeated operation could create another consequential effect, define durable idempotency scoped to the actor, resource, and operation. Bind the key to a payload fingerprint and retain the result for the supported retry window. Matching retries recover the original outcome; a different payload using the same key conflicts.

## Define failures before promising external work

Give provider calls bounded timeouts and retries consistent with their semantics. A database transaction generally cannot roll back a remote side effect. If committed data must reliably trigger downstream work, define an atomic handoff or explicit reconciliation and compensation.

Add a worker only when independent execution or recovery is required. Document delivery semantics, duplicate processing, retry limits, failure ownership, and authorization context. Use the existing logging and health setup to make a representative failure diagnosable without exposing secrets.

## Choose between a modular monolith and microservices

A modular monolith can keep backend features in one deployment while giving each feature explicit ownership and public operations. Separate API handlers, business rules, and persistence responsibilities where the operation benefits from that separation. Clean architecture does not require every request to pass through an identical stack of interfaces.

Consider microservices when independent deployment, team ownership, runtime isolation, or measured load creates a concrete need. Define the new service's contracts, authorization context, timeouts, and recovery before extracting it. Compare that cost with keeping a clear module boundary in the existing application, and verify the affected cross-service workflow if extraction proceeds.

## Give your coding agent a backend acceptance contract

Replace the placeholders with the real operation, permissions, invariant, and expected conflict behavior. This prompt authorizes a scoped implementation, not an infrastructure migration.

```text
Read the installed Vaastu SKILL.md and its backend architecture guidance.
Inspect the existing transport, authentication, feature ownership, and persistence.

Implement [trusted operation] with the current stack.
Actor/resource access: [who may do what to which records].
Domain invariant: [condition that must remain true].
Concurrency and retry policy: [expected conflict and duplicate behavior].

Keep adapters thin and add layers only for concrete responsibilities.
Verify permitted, malformed, denied, concurrent, and partial-failure cases as applicable.
Preserve existing contracts and unrelated behavior; report environment and unrun checks.
```

## Verify persistence and trust boundaries directly

Use focused tests for complex domain rules, adapter integration tests for validation and errors, and representative database tests for constraints, permissions, transactions, and concurrent updates. Simulate a committed operation whose response is lost when the retry contract matters.

Separate source review, mocked behavior, real persistence checks, hosted settings, and operational acceptance. For release work, verify migration compatibility and recovery with the project's deployment process. Tidy modules and passing import checks alone do not establish production correctness.

## References

- [Vaastu backend reference](https://github.com/KOUSHIKG04/scalable-architecture-skill/blob/main/skills/vaastu/references/backend-architecture.md)
- [Vaastu integration reference](https://github.com/KOUSHIKG04/scalable-architecture-skill/blob/main/skills/vaastu/references/frontend-backend-integration.md)
- [Connect backend and client responsibilities](/guides/frontend-backend-architecture)

## Related architecture guides

- [Plan Frontend and Backend Architecture with AI](https://get-vaastu.vercel.app/guides/frontend-backend-architecture)
- [Review and Fix Architecture with an AI Coding Agent](https://get-vaastu.vercel.app/guides/review-existing-architecture)

[Architecture protocol](https://get-vaastu.vercel.app/docs)
[Source repository](https://github.com/KOUSHIKG04/scalable-architecture-skill)
