DEV Community

Alex Spinov
Alex Spinov

Posted on

Encore Has a Free Backend Framework That Auto-Generates Infrastructure

Most backend frameworks make you configure infrastructure separately. Encore reads your code and automatically provisions databases, caches, pub/sub, and cron — locally and in the cloud.

How It Works

You write business logic. Encore figures out infrastructure.

// Define an API endpoint
import { api } from "encore.dev/api";
import { SQLDatabase } from "encore.dev/storage/sqldb";

const db = new SQLDatabase("users", { migrations: "./migrations" });

export const getUser = api(
  { method: "GET", path: "/users/:id", expose: true },
  async ({ id }: { id: string }) => {
    const user = await db.queryRow`SELECT * FROM users WHERE id = ${id}`;
    return user;
  }
);
Enter fullscreen mode Exit fullscreen mode

Encore sees SQLDatabase and automatically:

  • Creates a PostgreSQL database locally (Docker)
  • Provisions Cloud SQL/RDS in production
  • Runs migrations
  • Manages connection pooling

Pub/Sub (Auto-Provisioned)

import { Topic, Subscription } from "encore.dev/pubsub";

interface OrderEvent {
  orderId: string;
  total: number;
}

const orderTopic = new Topic<OrderEvent>("orders", { deliveryGuarantee: "at-least-once" });

// Publisher
export const createOrder = api({ method: "POST", path: "/orders" }, async (data) => {
  const order = await saveOrder(data);
  await orderTopic.publish({ orderId: order.id, total: order.total });
  return order;
});

// Subscriber
new Subscription(orderTopic, "send-email", {
  handler: async (event) => {
    await sendOrderConfirmation(event.orderId);
  },
});
Enter fullscreen mode Exit fullscreen mode

Locally: uses in-memory pub/sub
In cloud: automatically uses GCP Pub/Sub or AWS SNS/SQS

Caching

import { CacheCluster, CacheKeyspace } from "encore.dev/storage/cache";

const cluster = new CacheCluster("main", { evictionPolicy: "allkeys-lru" });
const userCache = new CacheKeyspace<User>(cluster, { keyPattern: "user/:id" });

export const getUser = api({ method: "GET", path: "/users/:id" }, async ({ id }) => {
  // Check cache first
  const cached = await userCache.get(id);
  if (cached) return cached;

  const user = await db.queryRow`SELECT * FROM users WHERE id = ${id}`;
  await userCache.set(id, user, { ttl: 3600 });
  return user;
});
Enter fullscreen mode Exit fullscreen mode

Locally: Redis in Docker. Cloud: ElastiCache/Memorystore. Automatic.

Cron Jobs

import { CronJob } from "encore.dev/cron";

new CronJob("cleanup", {
  title: "Clean expired sessions",
  schedule: "0 * * * *", // Every hour
  endpoint: cleanupSessions,
});

const cleanupSessions = api({ method: "POST", path: "/internal/cleanup" }, async () => {
  await db.exec`DELETE FROM sessions WHERE expires_at < NOW()`;
});
Enter fullscreen mode Exit fullscreen mode

Local Development Dashboard

encore run
# Opens dashboard at localhost:9400
Enter fullscreen mode Exit fullscreen mode

The dashboard shows:

  • API documentation (auto-generated)
  • Service architecture diagram
  • Request tracing
  • Database queries
  • Pub/sub messages
  • Cron job history

Deploy

git push encore main
# Encore Cloud provisions everything automatically
Enter fullscreen mode Exit fullscreen mode

Or self-host: Encore generates Terraform + Docker for any cloud.


Need backend architecture? I build APIs and data infrastructure. Email spinov001@gmail.com or check my Apify tools.

Top comments (0)