tellus
How-to guides

Roll out a new message shape

Change the shape of a message in a rolling upgrade: the new release encodes the old shape for old nodes and upcasts it when it arrives.

In a rolling upgrade, nodes of the old and of the new release run side by side in one cluster. Old code cannot be taught a new shape of a message, so the new release bridges both directions. It encodes the old shape for nodes of the old release, and it upcasts the old shape it receives: it decodes it and converts it into the new one. For that, every payload carries the schema version of its shape, and every handshake carries the application version of its node, if one is configured.

The examples on this page come from two nodes. The first one runs version 2.0.0 of the application, whose greeting carries a language. The legacy node runs version 1.0.0, whose greeting does not. Each node asks the greeter of the other one. The program plays both nodes and needs the cluster-dev feature, as in the clustered actors tutorial. Messages that travel introduces Schema and Manifest, which this page builds on.

Version the message

tellus/examples/how_to/roll_out_a_new_message_shape.rs
#[derive(Serialize, Deserialize)]
struct Greeting {
    name: String,
    language: String,
    reply_to: ReplyTo<String>,
}

impl Schema for Greeting {
    const VERSION: SchemaVersion = SchemaVersion::new(1);

    fn encode(&self, codec: Codec, peer: &Peer) -> Result<(SchemaVersion, Vec<u8>), CodecError> {
        if peer.app_version().is_none_or(|version| version.major < 2) {
            let view = GreetingV0View {
                name: &self.name,
                reply_to: &self.reply_to,
            };
            return Ok((SchemaVersion::new(0), codec.encode(&view)?));
        }
        Ok((Self::VERSION, codec.encode(self)?))
    }

    fn decode(
        codec: Codec,
        schema_version: SchemaVersion,
        payload: &[u8],
    ) -> Result<Self, CodecError> {
        if schema_version == SchemaVersion::new(0) {
            let LegacyGreeting { name, reply_to } = codec.decode(payload)?;
            return Ok(Greeting {
                name,
                language: "en".to_string(),
                reply_to,
            });
        }
        if schema_version == Self::VERSION {
            return codec.decode(payload);
        }
        Err(CodecError::make_decoding(UnsupportedSchemaVersion::new::<
            Self,
        >(schema_version)))
    }
}

impl Manifest for Greeting {
    const MANIFEST: &'static str = "greeting";
}

#[derive(Serialize)]
struct GreetingV0View<'a> {
    name: &'a str,
    reply_to: &'a ReplyTo<String>,
}

Greeting is the message in the shape of version 2.0.0. Version 1.0.0 sends it without the language, as schema version 0, the default. So Greeting sets VERSION to 1, and its manifest stays greeting.

tellus calls encode when a greeting is told to an actor on another node, before it is queued, with the Peer it is for. Peer::app_version is the application version that node announced at its handshake. For a peer whose major version is below 2, encode writes the old shape as schema version 0, and the language is dropped. GreetingV0View borrows the fields the old shape has. They keep the order of the old shape, since the codec, postcard, encodes fields by position. Any other peer gets the current shape. The old shape may have no place for a value, a new enum variant for instance. Then the encode override returns an error made with CodecError::make_encoding. A tell then becomes a dead letter on the sending node, and an ask fails as AskError::NotEncodable.

app_version is None before the first handshake with the peer has completed, and for a peer which announced no version. is_none_or treats None like a peer of version 1.0.0. An override treats None as the oldest version it still encodes, since a message in a newer shape is a dead letter at an older peer. Before the first handshake has completed, a greeting hence crosses in the old shape, even between two nodes of version 2.0.0. A greeting in German then arrives with the language en, which the upcast fills in.

decode gets the schema version a payload carries. It decodes version 0 as a LegacyGreeting and upcasts it to a Greeting with the language en. Version 1 decodes as it is, and every other version fails with UnsupportedSchemaVersion. The example holds both releases in one program, so decode uses the legacy node's own type. In an application, version 2.0.0 keeps a struct of the old shape, deriving Deserialize, only to decode it.

Greet in both releases

The greeter of version 2.0.0 gets every greeting in the new shape, whichever shape crossed the wire:

tellus/examples/how_to/roll_out_a_new_message_shape.rs
struct Greeter;

impl Actor for Greeter {
    type Message = Greeting;
    type Timers = Untimed;
    type Stash = Unstashed;
    type State = ();
    type Error = !;

    fn init(&self, _: &ActorContext<Self::Message>) -> Result<Self::State, Self::Error> {
        Ok(())
    }

    fn receive(
        &self,
        _: &ActorContext<Self::Message>,
        incoming: Incoming<Self::Message>,
        state: Self::State,
    ) -> Result<Control<Self::State>, Self::Error> {
        let Incoming::Message(Greeting {
            name,
            language,
            reply_to,
        }) = incoming
        else {
            unreachable!("the greeter watches no actor, hence never gets a terminated signal")
        };

        reply_to.reply(format!("Hello, {name} ({language})"));
        Ok(Control::Continue(state))
    }
}

It answers with the name and the language and keeps running. The answer, a String, has one shape forever. A reply type of the application evolves like Greeting, since its encode gets the node which asked as its peer. The legacy node's greeter is the code of version 1.0.0, unchanged:

tellus/examples/how_to/roll_out_a_new_message_shape.rs
struct LegacyGreeter;

impl Actor for LegacyGreeter {
    type Message = LegacyGreeting;
    type Timers = Untimed;
    type Stash = Unstashed;
    type State = ();
    type Error = !;

    fn init(&self, _: &ActorContext<Self::Message>) -> Result<Self::State, Self::Error> {
        Ok(())
    }

    fn receive(
        &self,
        _: &ActorContext<Self::Message>,
        incoming: Incoming<Self::Message>,
        _: Self::State,
    ) -> Result<Control<Self::State>, Self::Error> {
        let Incoming::Message(LegacyGreeting { name, reply_to }) = incoming else {
            unreachable!("the greeter watches no actor, hence never gets a terminated signal")
        };

        reply_to.reply(format!("Hello, {name}"));
        Ok(Control::Stop)
    }
}

#[derive(Serialize, Deserialize)]
struct LegacyGreeting {
    name: String,
    reply_to: ReplyTo<String>,
}

impl Schema for LegacyGreeting {}

impl Manifest for LegacyGreeting {
    const MANIFEST: &'static str = "greeting";
}

The empty Schema impl of LegacyGreeting keeps the defaults: it encodes its one shape as schema version 0 and decodes version 0 only. Its manifest is greeting too. A key names its message type by the manifest, so both greeters are registered under the same key. The legacy greeter answers once and stops.

Announce the application version

tellus/examples/how_to/roll_out_a_new_message_shape.rs
fn start_endpoint(addr: SocketAddr, app_version: AppVersion) -> anyhow::Result<()> {
    let transport = QuicTransport::dev(addr).context("dev QUIC transport")?;
    let config = EndpointConfig {
        app_version: Some(app_version),
        ..EndpointConfig::new(addr)
    };
    cluster::start_endpoint(config, transport).context("remoting endpoint")
}

EndpointConfig::app_version is the version of the application a node runs. The endpoint announces it at every handshake, and on the other side encode gets it as Peer::app_version. AppVersion is semver's Version. With the serde feature, a config file gives it as a string, such as app_version: "2.0.0". The default, None, announces nothing.

Ask across releases

tellus/examples/how_to/roll_out_a_new_message_shape.rs
async fn first_node() -> anyhow::Result<()> {
    let addrs = free_addrs(2)?;
    let mut legacy_node = spawn_legacy_node(addrs[1], &addrs)?;
    start_endpoint(addrs[0], AppVersion::new(2, 0, 0))?;
    let greeter = ActorSystem::new(Greeter);
    cluster::register(&Key::new(GREETER_KEY), greeter.root()).context("registering the greeter")?;
    bootstrap(addrs.clone()).await?;

    let legacy_greeter = find_greeter(addrs[1]).await?;
    let answer = legacy_greeter
        .ask(ASK_TIMEOUT, |reply_to| Greeting {
            name: "Ann".to_string(),
            language: "de".to_string(),
            reply_to,
        })
        .await
        .context("asking the legacy greeter")?;

    let status = timeout(EXIT_TIMEOUT, legacy_node.wait())
        .await
        .context("the legacy node did not exit within the timeout")?
        .context("awaiting the legacy node's exit")?;
    anyhow::ensure!(status.success(), "the legacy node failed: {status}");
    println!("The legacy node answered: {answer}");

    greeter.stop();
    greeter
        .terminated()
        .await
        .context("awaiting the greeter's termination")
}

The first node starts the legacy node as a second process of this program, with legacy-node, its address and the seeds as arguments. It runs version 2.0.0 and registers its greeter under the key greeter. Once bootstrap has made it a member, find_greeter polls receptionist::lookup_at at the legacy node's address every 100 ms, until the registration has arrived. The first node asks that greeter with a greeting in German. The legacy node announced 1.0.0, so the greeting crosses in the old shape. The first node prints the answer once the legacy node has exited, and stops its greeter.

tellus/examples/how_to/roll_out_a_new_message_shape.rs
async fn legacy_node() -> anyhow::Result<()> {
    let (addr, seeds) = node_args()?;
    start_endpoint(addr, AppVersion::new(1, 0, 0))?;
    let greeter = ActorSystem::new(LegacyGreeter);
    cluster::register(&Key::new(GREETER_KEY), greeter.root())
        .context("registering the legacy greeter")?;
    bootstrap(seeds.clone()).await?;

    let current_greeter = find_greeter(seeds[0]).await?;
    let answer = current_greeter
        .ask(ASK_TIMEOUT, |reply_to| LegacyGreeting {
            name: "Bob".to_string(),
            reply_to,
        })
        .await
        .context("asking the current greeter")?;
    println!("The current node answered: {answer}");

    let leave = cluster::leave_on_terminated(greeter);
    let departure = timeout(GREETED_TIMEOUT, leave)
        .await
        .context("not greeted within the timeout")?
        .context("leaving the cluster")?;
    anyhow::ensure!(
        departure.is_acknowledged(),
        "departure not acknowledged by {:?}, timed out for {:?}",
        departure.unacknowledged(),
        departure.timed_out()
    );
    Ok(())
}

The legacy node runs version 1.0.0, registers its greeter and asks the greeter at the first seed, the first node's address. It prints the answer at once. cluster::leave_on_terminated then waits until the legacy greeter has stopped after its one greeting, and leaves the cluster. The example treats a missing acknowledgment of the departure as an error.

What to keep in mind

One rollout is enough. The new release bumps VERSION, decodes the old version and encodes the old shape for nodes of the old release, so it can be rolled out node by node. A rollback brings old nodes back, and the same encode serves them. A later release serves them too, as long as it keeps encoding the old shape. Without an encode override, it takes two rollouts: first a release which decodes both shapes but sends the old one, then one which sends the new one. The cluster demo rolls its five nodes between versions 1.0.0 and 2.0.0 with an encode override like this one, one node at a time.

A node drops a payload of a version it does not decode. On the legacy node, that is every version but 0. Such a payload is a dead letter on the receiving node, so nothing is decoded in the wrong shape. The dead-letter warning names the type and both versions, and an ask fails as AskError::NoReply. The connection stays open, and the sender's later messages are delivered as usual.

Every change to a shape bumps VERSION. Since postcard encodes by position, the schema version is the only reliable check on a changed shape. Two fields of one type swapped, or a field whose type changed, can decode silently wrong. A change inside a field of an application type bumps the message's VERSION.

decode decodes a payload once. Decoding a ReplyTo claims the reply's destination, so a decode which tries one shape and then another can leave the asker without a reply. Match on the schema version and decode once.

The manifest never changes. A type renamed in the new release has to keep its manifest. Otherwise a lookup omits the actors which the other release registered. Where only those hold the name, the lookup fails with ReceptionistError::TypeMismatch.

For the contract behind all of this, see Rolling upgrades in the internals documentation. Evolve events upcasts stored events the same way.

Run it

tellus/examples/how_to/roll_out_a_new_message_shape.rs
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    init_tracing();

    match env::args().nth(1).as_deref() {
        Some(LEGACY_NODE_ARG) => legacy_node().await,
        _ => first_node().await,
    }
}

From a clone of the tellus repository:

cargo run --quiet -p tellus --features cluster-dev --example how_to_roll_out_a_new_message_shape
The current node answered: Hello, Bob (en)
The legacy node answered: Hello, Ann

The first line is the legacy node's. Its greeting reached the first node, the current one, in the old shape, and the upcast added the language en. The second line is the first node's. Its German greeting reached the legacy node in the old shape, without the language. The legacy node prints as soon as it has its answer. The first node prints only after the legacy node has exited, so the legacy node's line always comes first. tellus logs to stderr; RUST_LOG=tellus=debug shows what both nodes do meanwhile.