Updated Nov 27, 20266 min read

Preview Environments for Microservices: One Environment per Pull Request

What breaks when a pull request needs a preview of several services at once, from version skew and shared queues to per-service databases and cost, how teams build per-PR environments by hand, and how Encore deploys every service in a pull request together.

Illustration for Preview Environments for Microservices: One Environment per Pull Request

A preview environment for one service is a solved problem: build the branch, deploy it, post the URL on the pull request. With five services it gets harder, since a change to orders only makes sense if the inventory service it calls and the notifications service that reads its events run compatible versions.

This guide covers what goes wrong in multi-service previews, the ways teams build them by hand, and how Encore creates one environment per pull request containing every service.

What breaks when previews span several services?

ProblemWhat to decide
Version skewThe preview calls the changed service, and every dependency runs at staging's older version
Shared queuesEvents published by the preview are consumed by staging subscribers, and the other way round
DatabasesEvery service that owns a database needs its own copy, with migrations applied
Service addressesEach service must find the others inside the same preview, not staging
CostProvisioning grows with the number of services and the number of open pull requests
CleanupEverything created for a pull request has to be removed when it closes
Schedules and webhooksCron jobs and third-party callbacks usually point at one environment

The shared queue is the quiet failure, since a preview that publishes order-placed to a topic that staging also subscribes to will trigger staging emails from a test order, which is why the isolation has to cover queues and not only compute and databases.

What are the options for building them by hand?

Teams usually pick one of three shapes, and a namespace per pull request on Kubernetes isolates services with namespaces, so a pipeline templates the manifests for every service into pr-72, creates the databases and deletes the namespace on close. A stack per pull request on ECS or Cloud Run does the same with services and load balancer rules, and the GitHub Actions preview guide walks through that on Cloud Run. Docker Compose per pull request on a shared host is the cheapest and the least like production.

All three need the same pieces: a build for each service, a way to hand each one the addresses and credentials of the others, a database per service seeded from somewhere, and a cleanup job that survives failed runs. Preview databases are the hardest to get right, and Neon's branching makes them quick to create when the database is Postgres on Neon. The result works, and it is a pipeline that someone keeps maintaining as services are added.

How does Encore create a preview environment for every service?

Encore brings infrastructure from code, so a pull request environment is built from the declarations in the app. Once the app is connected to GitHub, preview environments are hosted by Encore, not in your AWS or GCP account. The Encore Platform provisions an environment for each pull request, named after it, so PR 72 becomes pr:72 with the API at https://pr72-$APP_ID.encr.app, and destroys it when the pull request is merged or closed.

Application code passes through Encore and infrastructure guardrails. Deployed environments provision resources in your own AWS or GCP account, and preview environments are hosted by Encore.

Every service in the app is deployed from the pull request's commit, so there is no version skew between them, and the databases and topics belong to that environment. Calls between services are typed function calls generated from the code, so an incompatible change fails at compile time rather than in the preview.

Set up the app

Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

~
$ encore app create shop --example=ts/hello-world$ cd shop && mkdir -p orders inventory/migrations notifications

Each folder also needs an encore.service.ts that exports new Service("<name>"). Three services make the smallest useful example: orders calls inventory directly and publishes an event, inventory owns a database, and notifications subscribes to the event:

// inventory/inventory.ts import { api, APIError } from "encore.dev/api"; import { SQLDatabase } from "encore.dev/storage/sqldb"; const db = new SQLDatabase("inventory", { migrations: "./migrations" }); export const reserve = api( { expose: false, method: "POST", path: "/inventory/reserve" }, async ({ sku, qty }: { sku: string; qty: number }): Promise<{ remaining: number }> => { // The WHERE clause stops two orders from taking the last unit. const row = await db.queryRow<{ stock: number }>` UPDATE stock SET stock = stock - ${qty} WHERE sku = ${sku} AND stock >= ${qty} RETURNING stock`; if (!row) throw APIError.failedPrecondition("not enough stock"); return { remaining: row.stock }; }, );
-- inventory/migrations/1_create_stock.up.sql CREATE TABLE stock ( sku TEXT PRIMARY KEY, stock INTEGER NOT NULL CHECK (stock >= 0) ); INSERT INTO stock (sku, stock) VALUES ('sku-1', 10);
// orders/orders.ts import { api } from "encore.dev/api"; import { Topic } from "encore.dev/pubsub"; import { inventory } from "~encore/clients"; export interface OrderPlaced { sku: string; qty: number; } export const orderPlaced = new Topic<OrderPlaced>("order-placed", { deliveryGuarantee: "at-least-once", }); export const place = api( { expose: true, method: "POST", path: "/orders" }, async (req: OrderPlaced): Promise<{ remaining: number }> => { const { remaining } = await inventory.reserve(req); await orderPlaced.publish(req); return { remaining }; }, );
// notifications/notifications.ts import { Subscription } from "encore.dev/pubsub"; import { orderPlaced } from "../orders/orders"; new Subscription(orderPlaced, "confirm-order", { handler: async ({ sku, qty }) => { console.log("order confirmation queued", sku, qty); }, });

encore run starts all three services, Postgres for inventory and the topic in one process on a laptop, so curl -X POST localhost:4000/orders exercises the whole chain before the pull request exists. The services docs and API calls docs cover how services call each other, and the Pub/Sub docs cover subscriptions and retries.

Push the branch and open the pull request, and the preview environment appears with its own copy of the database, since a database can be created empty with migrations or branched from staging through Neon in the dashboard settings. Secrets can be given separate values for preview environments with encore secret set --type preview, and cron jobs do not run in preview environments, so scheduled work is tested by calling its endpoint. A frontend developer points the client at the preview with encore gen client --env=pr:72 --lang=typescript shop, and an AI coding agent that opens a pull request gets a running environment for the reviewer without anyone assembling a pipeline.

Best fit: Teams with several services in one repository that want every pull request to get an isolated environment with all services, databases and topics, without maintaining the pipeline that builds it.

How to choose

Stay with a namespace or stack per pull request when you already run Kubernetes or ECS pipelines and need previews inside your own network on day one. Choose Encore when the services live in one repository and the preview should be hosted by Encore from the code, so adding a service or a topic changes the preview without touching a workflow file.

Frequently asked questions

How do you create a preview environment for microservices?

Deploy every service at the pull request's commit into an isolated environment with its own databases and queues, point the services at each other inside it, and destroy it when the pull request closes. Doing it by hand takes a CI workflow that builds each service, a namespace or stack per pull request and cleanup logic.

Do you need to deploy every service for each pull request?

No. You can deploy only the changed service and point it at shared staging dependencies, which is cheaper, but the preview then shares queues and data with other work. Deploying all services from one commit keeps the versions consistent and the environment isolated, at the cost of provisioning more per pull request.

How do preview environments handle databases for many services?

Each service that owns a database needs its own copy in the preview, either an empty database with migrations applied or a branch of a staging database. Database branching makes the copy fast and gives the preview realistic data.

How do I get a preview environment per pull request for a multi-service Encore app?

Connect the app to GitHub in the Encore Platform. Each pull request then gets its own environment named after it, such as pr:72, with every service, database and topic deployed from that commit, and the environment is destroyed when the pull request is merged or closed.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders