trellis

Deployment Strategies & Resource Management

Trellis is designed to be flexible, running either as an embedded library inside your Go application or as a standalone service (Sidecar/MCP). Choosing the right strategy depends on your isolation requirements and traffic patterns.

1. Embedded (Library)

In this mode, Trellis runs inside your Host process. This offers the lowest latency and simplest deployment but shares memory resources with your application.

Memory Provisioning Formula

Since Go processes share a single Heap, you must account for Trellis’s memory footprint to prevent OOM (Out of Memory) kills.

Estimated RAM Usage:

Total RAM = App Base + (Concurrent Sessions * Session Overhead)

Where:

The Input Buffer Risk

If a user sends a large input (e.g., 10MB text), that memory is allocated in your process. Multiplied by 1000 users, this causes Memory Starvation.

Mitigation: Input Sanitization Trellis enforces a strict input limit to make this variable predictable.

Example: If you set TRELLIS_MAX_INPUT_SIZE=65536 (64KB) and expect 100 concurrent sessions: RAM Spike Risk = 100 * 64KB = 6.4MB. This is safe and predictable.

Goroutine Management (Stdin Pump)

When using TextHandler (Interactive Mode) in a long-running application:

[!CAUTION] Avoid recreating TextHandler on os.Stdin repeatedly.

The TextHandler starts a persistent background goroutine (Pump) to safely read from the input stream. This goroutine runs until the stream is closed. Since os.Stdin is typically never closed until process exit:

  1. Leak Risk: Creating new Handlers for every request will leak goroutines.
  2. Mitigation (Automatic): The Runner memoizes the Handler. Best Practice: Instantiate Runner ONCE and reuse it across multiple .Run() calls.

2. Sidecar (Standalone Service)

For high-traffic or multi-tenant environments (e.g., Kubernetes Pods), you may want to isolate Trellis to protect your main application from resource contention.

Architecture

Run Trellis as a separate container or process using trellis serve:

Benefits

Observability & Security

Trellis emits structured logs (slog compatible) for security events. Monitor these to detect attacks or misconfiguration.

Security Events

Example Log:

{"time":"...","level":"WARN","msg":"Input Rejected","size":10500,"limit":4096,"reason":"input exceeds maximum allowed size"}

Alerting Strategy: Configure your APM (Prometheus/Datadog) to alert if Input Rejected counts spike, as this may indicate a DoS attempt or a legitimate need to increase TRELLIS_MAX_INPUT_SIZE.