Skip to content

Orleans silo lifecycle

Orleans starts and stops runtime components through an ordered observable lifecycle. Startup advances from the lowest stage to the highest. Shutdown runs the same stages in reverse.

Most application code should use IHostedService or BackgroundService. Implement ILifecycleParticipant<T> with ISiloLifecycle only when a component must run at a precise point inside the Orleans runtime lifecycle.

ServiceLifecycleStage defines these stages:

StageValuePurpose
Firstint.MinValueEarliest lifecycle stage
RuntimeInitialize2000Initialize the runtime
RuntimeServices4000Start core networking and runtime services
RuntimeStorageServices6000Initialize runtime storage services
RuntimeGrainServices8000Start grain-facing runtime services
AfterRuntimeGrainServices8100Run after grain runtime services
ApplicationServices10000Start application-level services
ValidateInitialConnectivity19900Validate connectivity before becoming active
GrainDirectoryShutdown19997Coordinate grain-directory shutdown
GrainDeactivation19998Deactivate grains during shutdown
BecomeActive19999Internal transition immediately before active
Active20000Silo is active and accepts workload
Lastint.MaxValueFinal lifecycle stage

Some constants intentionally share or closely bracket values because their startup and shutdown semantics differ. Treat the named constants as ordering contracts; don’t copy their numeric values into application code.

Register a singleton that implements ILifecycleParticipant<T> for ISiloLifecycle:

public sealed class CacheLifecycleParticipant
: ILifecycleParticipant<ISiloLifecycle>
{
private readonly IApplicationCache _cache;
public CacheLifecycleParticipant(IApplicationCache cache)
{
_cache = cache;
}
public void Participate(ISiloLifecycle lifecycle)
{
lifecycle.Subscribe<CacheLifecycleParticipant>(
ServiceLifecycleStage.ApplicationServices,
cancellationToken => _cache.StartAsync(cancellationToken),
cancellationToken => _cache.StopAsync(cancellationToken));
}
}
public static void RegisterLifecycleParticipant(ISiloBuilder siloBuilder)
{
siloBuilder.Services.AddSingleton<CacheLifecycleParticipant>();
siloBuilder.Services.AddSingleton<
ILifecycleParticipant<ISiloLifecycle>>(
services =>
services.GetRequiredService<CacheLifecycleParticipant>());
}

The start callback must complete before Orleans advances to the next stage. On shutdown, the stop callback receives the host shutdown cancellation token.

HighestCompletedStage and LowestStoppedStage expose lifecycle progress for diagnostics.

Fail startup when a required dependency can’t initialize. For optional or continuously retrying work, start a hosted background service after Orleans instead of blocking a lifecycle stage indefinitely.

The Orleans.Runtime.SiloLifecycleSubject logger category reports participants, timing, and errors by stage. Set LogLevel to Information while diagnosing startup ordering or slow shutdown. Lifecycle callbacks should log the external operation they are waiting for and honor cancellation.

For implementation details, see Orleans lifecycle. For a simpler one-time callback, see Background services and startup tasks. For host termination behavior, see Shut down Orleans silos.