Use Case GuidesMulti-Function Architecture

Multi-Function Architecture

As your application grows, a single monolithic worker may become difficult to maintain. Aerostack supports a multi-function architecture (also known as “monorepo-lite”) that allows you to split your logic into multiple independent functions while sharing code effortlessly.

The Core Concept

In a multi-function project, you have one main entry point and multiple auxiliary functions (e.g., Auth Hooks, Background Jobs, Webhook Processors). All these functions reside in the same project directory and can share a shared/ folder.

Sharing Code with the shared/ Folder

By placing your common logic, TypeScript types, and utility functions in a root-level shared/ directory, you ensure that every function in your project has access to a single source of truth.

Example: Shared Types

// shared/types.ts
export interface User {
  id: string;
  email: string;
}
 
// api/index.ts
import { User } from '../shared/types';
// ... use User type

Technical Benefits

  1. Reduced Cold Starts: Small, focused functions have smaller bundle sizes, leading to faster startup times.
  2. Independent Scaling: You can scale your user-facing API differently from your heavy background processors.
  3. Modular Deployment: Aerostack manages the orchestration, allowing you to deploy parts of your system independently if needed.
  4. RPC Communication: Functions can communicate with each other using high-speed Service Bindings.

Configuration in aerostack.toml

The aerostack.toml file is where you define your project’s function map:

[main]
path = "src/index.ts"
 
[[functions]]
name = "auth-hook"
path = "functions/auth-hook.ts"
route = "/auth/*"
 
[[functions]]
name = "email-worker"
path = "functions/email.ts"
# No route means it's internal-only (accessible via RPC)

Getting Started Faster: Use the multi-func template during initialization to see this architecture in action: aerostack init my-project --template=multi-func