Stateless vs stateful execution

When to use this

You need to choose between independent request-style calls (stateless) and object lifetimes, callbacks, and events (stateful) — especially for edge clients and load-balanced services.

Prerequisites

Stateless vs stateful Graft execution compared


Stateless execution

Characteristics:

  • Each call is independent
  • No server-side object retained between calls
  • Maps to static methods and simple DTOs
  • Scales horizontally without session stickiness

Interface pattern:

public static class OrderQueries
{
    public static OrderDto GetOrder(string orderId) => /* ... */;
}

GraftConfig:

GraftConfig.stateless = true;
GraftConfig.stateless = true;

Best for: edge clients (tutorial #1), MCP tools, read-heavy APIs.


Stateful execution

Characteristics:

  • Object instances live on the receiver
  • Properties and methods on returned objects may trigger additional round-trips
  • Callbacks, events, and delegates work over full-duplex Hypertube sessions
  • Load balancers may need session stickiness

Interface pattern:

public class ShoppingCart
{
    public void AddItem(string sku) => /* ... */;
    public decimal GetTotal() => /* ... */;
}
const cart = new ShoppingCart();
await cart.addItem("SKU-1");
const total = await cart.getTotal();

GraftConfig: default stateless = false.

Best for: rich domain models, interactive workflows, service-to-service with long-lived objects (tutorial #3).


Alpha note: DTOs and statefulness

In Alpha, complex object parameters/returns can make calls effectively stateful (session stickiness required). Beta will improve stateless DTO passing. See Alpha limitations.


Comparison

AspectStatelessStateful
ScalingEasy horizontal scaleSticky sessions or single instance
Edge / browserRecommendedPossible but heavier
MCPRequired patternNot projected to MCP
GraftConfig.statelesstruefalse (default)
Interface styleStatic methodsInstance classes

Verify it works

  • Stateless: parallel calls from many clients without shared state bugs
  • Stateful: same object instance behaves consistently across method calls on one session

See also

Practiced in Quick Start tutorials #1 and #3