Skip to content

Grain lifecycle overview

Applies to: Orleans 8.0, Orleans 9.0, Orleans 10.0

Orleans grains use an observable lifecycle (see Orleans Lifecycle) for ordered activation and deactivation. This allows grain logic, system components, and application logic to start and stop in an ordered manner during grain activation and collection.

The predefined grain lifecycle stages are as follows:

public static class GrainLifecycleStage
{
public const int First = int.MinValue;
public const int SetupState = 1_000;
public const int Activate = 2_000;
public const int Last = int.MaxValue;
}

While Orleans uses the grain lifecycle during grain activation, grains aren’t always deactivated during some error cases (such as silo crashes). Therefore, applications shouldn’t rely on the grain lifecycle always executing during grain deactivations.

Applies to: Orleans 10.0, Orleans 9.0

Memory-based activation shedding automatically deactivates grain activations when the silo is under memory pressure. This helps prevent out-of-memory conditions by intelligently removing grains from memory based on their activity patterns.

When enabled, Orleans monitors memory usage and begins deactivating grains when usage exceeds the configured threshold:

  1. Orleans polls memory usage at a configurable interval (default: 5 seconds)
  2. When memory usage exceeds MemoryUsageLimitPercentage, Orleans begins deactivating grains
  3. Orleans prioritizes deactivating older and less-recently-used grains first
  4. Deactivation continues until memory usage falls below MemoryUsageTargetPercentage

Configure memory-based activation shedding using GrainCollectionOptions:

builder.Configure<GrainCollectionOptions>(options =>
{
// Enable memory-based activation shedding
options.EnableActivationSheddingOnMemoryPressure = true;
// Memory usage percentage (0-100) at which grain collection triggers
options.MemoryUsageLimitPercentage = 80; // default: 80
// Target memory usage percentage to reach after collection
options.MemoryUsageTargetPercentage = 75; // default: 75
// How often to poll memory usage
options.MemoryUsagePollingPeriod = TimeSpan.FromSeconds(5); // default: 5s
});
OptionTypeDefaultDescription
EnableActivationSheddingOnMemoryPressureboolfalseEnable automatic deactivation under memory pressure.
MemoryUsageLimitPercentageint80Memory usage percentage at which shedding begins. Valid range: 0-100.
MemoryUsageTargetPercentageint75Target memory usage percentage after shedding. Valid range: 0-100.
MemoryUsagePollingPeriodTimeSpan5 secondsHow often to check memory usage.
CollectionAgeTimeSpan15 minutesMinimum time a grain must be idle before eligible for collection.
CollectionQuantumTimeSpan1 minuteHow often idle grain collection runs.
  • Set thresholds conservatively: Leave headroom between MemoryUsageTargetPercentage and MemoryUsageLimitPercentage to avoid oscillation
  • Monitor collection metrics: Track grain deactivations to understand the impact on your workload
  • Consider grain importance: Critical grains can extend their lifetime using timers with KeepAlive = true or by receiving periodic calls
  • Test under load: Verify behavior under production-like memory conditions

Application logic can participate in a grain’s lifecycle in two ways:

Applies to: Orleans 8.0, Orleans 7.0

Applies to: Orleans 3.x

A grain always participates in its lifecycle, so application logic can be introduced by overriding the participate method.

public override void Participate(IGrainLifecycle lifecycle)
{
base.Participate(lifecycle);
lifecycle.Subscribe(
this.GetType().FullName,
GrainLifecycleStage.SetupState,
OnSetupState);
}

In the preceding example, Grain<T> overrides the Grain.Participate method to tell the lifecycle to call its OnSetupState method during the GrainLifecycleStage.SetupState stage of the lifecycle.

Applies to: Orleans 8.0, Orleans 7.0

Components created during a grain’s construction can also participate in the lifecycle without adding any special grain logic. Since Orleans creates the grain’s context (IGrainContext), including its lifecycle (IGrainContext.ObservableLifecycle), before creating the grain, any component injected into the grain by the container can participate in the grain’s lifecycle.

Applies to: Orleans 3.x

Components created during a grain’s construction can also participate in the lifecycle without adding any special grain logic. Since Orleans creates the grain’s activation context (IGrainActivationContext), including its lifecycle (IGrainActivationContext.ObservableLifecycle), before creating the grain, any component injected into the grain by the container can participate in the grain’s lifecycle.

The following component participates in the grain’s lifecycle when created using its factory function Create(...). This logic could exist in the component’s constructor, but that risks adding the component to the lifecycle before it’s fully constructed, which might not be safe.

Applies to: Orleans 8.0, Orleans 7.0

public class MyComponent : ILifecycleParticipant<IGrainLifecycle>
{
public static MyComponent Create(IGrainContext context)
{
var component = new MyComponent();
component.Participate(context.ObservableLifecycle);
return component;
}
public void Participate(IGrainLifecycle lifecycle)
{
lifecycle.Subscribe<MyComponent>(GrainLifecycleStage.Activate, OnActivate);
}
private Task OnActivate(CancellationToken ct)
{
// Do stuff
}
}

Applies to: Orleans 3.x

public class MyComponent : ILifecycleParticipant<IGrainLifecycle>
{
public static MyComponent Create(IGrainActivationContext context)
{
var component = new MyComponent();
component.Participate(context.ObservableLifecycle);
return component;
}
public void Participate(IGrainLifecycle lifecycle)
{
lifecycle.Subscribe<MyComponent>(GrainLifecycleStage.Activate, OnActivate);
}
private Task OnActivate(CancellationToken ct)
{
// Do stuff
}
}

By registering the example component in the service container using its Create(...) factory function, any grain constructed with the component as a dependency has the component participate in its lifecycle without requiring any special logic in the grain itself.

Applies to: Orleans 8.0, Orleans 7.0

services.AddTransient<MyComponent>(sp =>
MyComponent.Create(sp.GetRequiredService<IGrainContext>());

Applies to: Orleans 3.x

services.AddTransient<MyComponent>(sp =>
MyComponent.Create(sp.GetRequiredService<IGrainActivationContext>());
public class MyGrain : Grain, IMyGrain
{
private readonly MyComponent _component;
public MyGrain(MyComponent component)
{
_component = component;
}
}

Applies to: Orleans 8.0, Orleans 9.0, Orleans 10.0

Grain migration allows grain activations to move from one silo to another while preserving their in-memory state. This enables load balancing and cluster optimization without losing grain state. The migration process involves:

  1. Dehydration: Serializing the grain’s state on the source silo before deactivation
  2. Transfer: Sending the serialized state to the target silo
  3. Rehydration: Restoring the grain’s state on the new activation before OnActivateAsync is called

Migration can occur in several scenarios:

  • Application-initiated: A grain can request migration by calling this.MigrateOnIdle()
  • Activation repartitioner: Automatically collocates frequently communicating grains to optimize call locality (experimental)
  • Activation rebalancer: Distributes activations to balance load across silos (experimental)

Grains can participate in migration by implementing IGrainMigrationParticipant:

public interface IGrainMigrationParticipant
{
void OnDehydrate(IDehydrationContext dehydrationContext);
void OnRehydrate(IRehydrationContext rehydrationContext);
}

Option 1: Implement IGrainMigrationParticipant directly

Section titled “Option 1: Implement IGrainMigrationParticipant directly”

For grains with custom in-memory state that should be preserved during migration:

public class MyMigratableGrain : Grain, IMyGrain, IGrainMigrationParticipant
{
private int _cachedValue;
private string _sessionData;
public void OnDehydrate(IDehydrationContext dehydrationContext)
{
// Save state before migration
dehydrationContext.TryAddValue("cached", _cachedValue);
dehydrationContext.TryAddValue("session", _sessionData);
}
public void OnRehydrate(IRehydrationContext rehydrationContext)
{
// Restore state after migration
rehydrationContext.TryGetValue("cached", out _cachedValue);
rehydrationContext.TryGetValue("session", out _sessionData);
}
}

Option 2: Use Grain<TState> or IPersistentState<T>

Section titled “Option 2: Use Grain<TState> or IPersistentState<T>”

Grains using Grain<T> or IPersistentState<T> automatically support migration. Their state is serialized and restored without additional code:

// Automatic migration support via Grain<TState>
public class MyStatefulGrain : Grain<MyGrainState>, IMyGrain
{
public Task UpdateValue(int value)
{
State.Value = value;
return WriteStateAsync();
}
}
// Automatic migration support via IPersistentState<T>
public class MyOtherGrain : Grain, IMyGrain
{
private readonly IPersistentState<MyGrainState> _state;
public MyOtherGrain(
[PersistentState("state", "storage")] IPersistentState<MyGrainState> state)
{
_state = state;
}
}

A grain can request migration to a new silo:

public class MyGrain : Grain, IMyGrain
{
public Task RequestMigration()
{
// Request migration when the grain becomes idle
this.MigrateOnIdle();
return Task.CompletedTask;
}
}

You can also specify a preferred target silo using placement hints:

public Task MigrateToSilo(SiloAddress targetSilo)
{
RequestContext.Set(IPlacementDirector.PlacementHintKey, targetSilo);
this.MigrateOnIdle();
return Task.CompletedTask;
}

Use the ImmovableAttribute to prevent automatic migration by the repartitioner or rebalancer:

// Prevent all automatic migration
[Immovable]
public class MyImmovableGrain : Grain, IMyGrain { }
// Prevent only repartitioner migration
[Immovable(ImmovableKind.Repartitioner)]
public class MyPartiallyImmovableGrain : Grain, IMyGrain { }

The ImmovableKind enum provides these options:

ValueDescription
RepartitionerWon’t be migrated by the activation repartitioner
RebalancerWon’t be migrated by the activation rebalancer
AnyWon’t be migrated by any automatic process (default)

The following grain types cannot be migrated:

  • Client grains
  • System targets
  • Grain services
  • Stateless worker grains