Skip to content

Guide · Open source

How to use Orleans: grains, install and a first example

Updated · written with AI from the project's own documentation and checked against our catalogue data

Orleans is an open-source framework from dotnet that lets you build distributed applications in C# and .NET, such as services that run across several servers or in the cloud. This guide explains its core idea, the grain, and walks through the runtime, the client, persistence and transactions. After reading it you will understand how grains are modeled and how a small example is structured.

Orleansdotnet/orleans · ★ 10.9k · MIT

Quick start

  1. 1Confirm your .NET environment. Orleans is built for .NET. The README excerpt does not specify a required .NET version, so check the project documentation before you start.
  2. 2Get the source. The README shows how to clone the repository with git clone https://github.com/dotnet/orleans. Browse the code and samples there.
  3. 3Define a grain interface. Create an interface that inherits from a grain key type such as IGrainWithStringKey and declare the methods callers may invoke.
  4. 4Implement the grain class. Write a class that derives from Grain and implements your interface. Keep state in fields while the grain is active.
  5. 5Host the grains in a silo. Run a silo to host your grains. For a cluster, run several silos that coordinate with each other.
  6. 6Call the grain from a client. Use the client library to get a grain reference by key with GetGrain and await its methods.
  7. 7Add persistence when needed. For state that must survive restarts, follow the Grain Persistence documentation and call state.WriteStateAsync() to save changes.

What Orleans is and who it is for

Orleans extends familiar .NET ideas such as objects, interfaces, async/await and try/catch to environments with multiple servers. The project describes itself as a cross-platform framework that scales from a single on-premises server to globally distributed applications in the cloud.

It suits C# and .NET developers building scalable backend services, teams that model many independent entities such as devices or users as stateful objects, and developers moving from single-server apps to distributed systems. The README does not describe other language bindings, so it is aimed at .NET projects.

Understand grains, the core building block

The fundamental building block in an Orleans application is a grain. A grain combines a user-defined identity, behavior and state. Its identity is a user-defined key, so a grain is always available for invocation whether or not it is currently loaded in memory.

Grains are invoked through strongly-typed interfaces (contracts), either by other grains or by external clients such as web frontends. Each grain is an instance of a class that implements one or more of these interfaces. The runtime activates grains on demand and removes unused ones from memory, so you can write code as if all grains were always in memory.

  • Stable identity: the key that the caller uses to reach the grain
  • Statefulness: state can be kept in memory while the grain is active
  • Managed lifecycle: the runtime activates, deactivates and places grains

Model an entity with an interface and a grain class

The README's IoT example models each device as a grain that acts as a digital twin of the physical device. A simple thermostat is described by an interface that inherits from IGrainWithStringKey, and a separate interface can expose control operations on the same grain.

A single implementation class, ThermostatGrain, derives from Grain and implements both interfaces. Note that the README says this example does not persist its state; a fuller persistence example is linked from the docs.

public interface IThermostat : IGrainWithStringKey { Task<List<Command>> OnUpdate(ThermostatStatus update); }

Call a grain from a client

Web frontends and other external clients call grains using the Orleans client library, which manages network communication. A client obtains a grain reference by key and then invokes its methods like any other async call.

Clients can also be co-hosted in the same process as silos for simplicity.

var thermostat = client.GetGrain<IThermostat>(id); return await thermostat.OnUpdate(update);

Know how silos and the cluster fit together

The main component of the Orleans runtime is the silo, which hosts grains. Typically a group of silos runs as a cluster for scalability and fault tolerance. Silos coordinate to distribute work and to detect and recover from failures.

The silo also provides runtime services such as timers, reminders (persistent timers), persistence, transactions and streams. Orleans is compatible with .NET Standard 2.0 and above and runs on Windows, Linux and macOS, in full .NET Framework or .NET Core.

Use persistence and distributed transactions

Grains can hold several named persistent data objects, for example a profile and an inventory, and each can be stored in a different storage system. While a grain is running, its state is kept in memory; calling state.WriteStateAsync() ensures the backing store is updated.

Grains can also have transactional state. Multiple grains can take part in ACID transactions regardless of where their state is stored. These transactions are distributed and decentralized, with no central transaction manager, and use serializable isolation.

What people use it for

  • Modeling each IoT device as a digital twin that holds its latest readings in memory
  • Representing users or accounts as stateful objects that clients query over the network
  • Running backend services that scale from one server to a cluster of silos
  • Coordinating changes across several grains with distributed ACID transactions

Frequently asked questions

Is Orleans free to use?

Orleans is released under the MIT licence, as stated in the project listing. Check the licence file in the repository for the full terms.

What do I need to install Orleans?

The project is distributed as packages and requires .NET. The README excerpt does not specify a .NET version, so consult the official docs for requirements.

Which languages does Orleans support?

Orleans is a C# and .NET framework. The README does not describe bindings for other languages.

What is a grain in Orleans?

A grain is the basic unit of an Orleans application. It is an object with a stable identity, behavior and optional state that the runtime can activate, move and deactivate as needed.

How does Orleans compare to Akka.NET, Proto.Actor or Dapr?

These are comparable projects in the same general space of distributed .NET or actor-style systems. This page does not benchmark them, so evaluate each against your own requirements.

Does Orleans handle persistence and transactions?

Yes. Grains can have persistent state stored in any storage system and can take part in distributed ACID transactions with serializable isolation.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Orleans suits C# and .NET teams building scalable backend services, especially when many independent entities such as devices or users need to be stateful objects. It is a poor fit for non-.NET languages, small single-server apps with no scaling needs, or teams that want to control every low-level detail of their cluster.

Install options & downloads