RPC boundaries with id_effect (@effect/rpc parity)

Effect.ts @effect/rpc ties Schema, Layer, and HTTP so typed contracts cross process boundaries. In Rust there is no single blessed stack; this chapter documents patterns and the crate id_effect_rpc (crates/id_effect_rpc, tier integration since 0.12) for RPC-shaped HTTP on top of id_effect_axum and id_effect::schema. Since 0.12 RPC is id_effect's first-party call surface (ADR 0014); the whole picture — procedure kinds, the contract, transports — is One call surface.

Choosing a wire stack (tonic, tarpc, HTTP+JSON)

ApproachStrengthsTrade-offs
tonic + ProtobufMature gRPC ecosystem, streaming, codegen from .protoSeparate schema language; protobuf ↔ Effect error mapping is manual at generated boundaries
tarpcRust-native service traits, pluggable transportsFewer batteries than tonic for cross-language clients; still need an explicit error wire type
HTTP + JSON + SchemaSame Schema as domain validation; easy to debug; fits AxumNo streaming parity on day one; discipline required for versioning and error envelopes

Recommendation for id_effect today: use HTTP + JSON for public APIs where you already host Axum, declare and validate request input with the typed routes of id_effect_axum::route (or its ValidJson extractor), which answer invalid input with an RFC 9457 Problem, and return structured failures with id_effect_rpc::RpcError. Add gRPC (tonic) when you need IDL-first codegen or cross-language streaming at scale.

Effect, R, and errors at the edge

  • Keep Effect<A, E, R> for domain logic; E stays rich inside the process.
  • At the Axum route, map E into RpcError or a route::Problem; schema failures are already a 422 application/problem+json from the typed route or extractor — clients see a stable wire contract, not internal enums.
  • Propagate x-correlation-id (read or generate with id_effect_rpc::correlation) and attach it to responses so logs and traces line up across hops.

Tracing and OpenTelemetry

Use id_effect_rpc::span::rpc_request_span for a stable rpc.request span name and fields (http.method, http.route, rpc.operation, correlation.id). When your binary installs a subscriber with an OpenTelemetry layer (OpenTelemetry), these fields map cleanly to semantic conventions without id_effect_rpc depending on OTEL crates.

The crate: id_effect_rpc

PieceRole
Procedure / RpcGrouptagged procedures, each public() or guard(g) (an undecided one does not compile); handlers Fn(A, &mut DynEnv) -> Effect<B, RpcError, DynEnv>
RpcServerPOST /rpc as a declared typed route of id_effect_axum::route::Router; per-call guards; unknown tag → 404 Problem
RpcClientcall(..) → an Effect over any R containing HttpClientService (feature client)
contract, wire, codecthe serde service contract, the 0.12 wire envelope and frames, JSON codec and negotiation
RpcError / RpcEnvelopeJSON body + HTTP status + IntoResponse for Axum
correlation, spanx-correlation-id and tracing helpers for RPC-shaped requests
openapi, versioning, streamOpenAPI 3.0 emission, version negotiation, NDJSON stream chunks

Each handler's effect runs as one request fiber (id_effect_axum::run_with_env). A typed RpcError becomes a failure envelope; a defect or interrupt becomes RpcError::internal (HTTP 500, the panic message is not sent).

use id_effect::{DynEnv, succeed};
use id_effect_platform::http::{ReqwestHttpClient, http_client_env};
use id_effect_rpc::{Procedure, RpcClient, RpcGroup, RpcServer};

let mut group = RpcGroup::new();
group.register(Procedure::query("Greeter.greet").public(), payload, success, &["message", "n"],
    |(name, n), _env: &mut DynEnv| succeed((format!("Hello, {name}!"), n)));
let app = RpcServer::new(group).mount_default().with_state(DynEnv::new());

let out = RpcClient::new("http://127.0.0.1:8080")
    .call("Greeter.greet", input, &payload_schema, &["name", "n"], &success_schema)
    .provide_all(http_client_env(ReqwestHttpClient::default_client()))
    .block_on() // or TokioRuntime::current()?.run(..).await in async code
    .into_result()?;

API index: cargo doc -p id_effect_rpc --open.

Example: Axum + Schema + RpcError

cargo run -p id_effect_rpc --example 010_json_greet

It accepts POST /greet with JSON {"name": string, "enthusiasm": i64}, validates with Schema, rejects bad enthusiasm with RpcError::invalid_argument, and echoes x-correlation-id. 020_rpc_tracing adds the span helpers.

Further reading