Architectural Best Practices & Production
This chapter covers:
- Adhere to Thin Controller / Fat Service conventions;
- Tune database connection pool sizes, index prefixes, and cursor pagination;
- Enforce production security policies and secrets hygiene;
- Deploy Foundry applications with multi-stage Dockerfiles and Docker Compose.
1. Architectural Rules
Section titled “1. Architectural Rules”- Thin Controller / Fat Service: Keep handlers restricted to deserialization, validation, and serialization. Keep domain rules inside
logic/. - Explicit Validation: Always call
payload.validate()?before business logic execution. - Compound Indexes: Always make
system_idthe first column in multi-tenant composite indexes (IndexDef). - Cursor Pagination: For large tables, avoid deep
OFFSET. UseWHERE id > $last_id LIMIT $n.
2. Docker Containerization
Section titled “2. Docker Containerization”Multi-Stage Dockerfile
Section titled “Multi-Stage Dockerfile”FROM rust:1.85-alpine AS builderWORKDIR /appRUN apk add --no-cache musl-dev openssl-dev pkgconfigCOPY Cargo.toml Cargo.lock ./COPY src ./srcRUN cargo build --release
FROM alpine:3.20WORKDIR /appRUN apk add --no-cache ca-certificates libgccCOPY --from=builder /app/target/release/my-app /app/serverCOPY .env.example /app/.envEXPOSE 8080CMD ["/app/server"]3. Production Migration Strategy
Section titled “3. Production Migration Strategy”- Development & CI:
AUTO_MIGRATE=truefor zero-friction schema creation. - Production:
AUTO_MIGRATE=false. Export offline migration scripts viafoundry ddl --db <type>intomigrations/init.sqlfor DBA auditing and controlled deployment.