# Procedures: the six kinds, typed errors, cancellation
A procedure is an effect. Whatever its kind, its handler returns an `Effect<_, E, DynEnv>`, so
the capabilities it needs come from the environment, its typed errors are part of its type, and
it can be tested with the test executor like any other effect. This chapter shows the low-level
`RpcGroup` API that `#[rpc::service]` generates against; each kind has one registration method
and one handler shape (module `id_effect_rpc::handler`).
| query | `RpcGroup::query` | `Effect<O, E, DynEnv>` from `I` |
| mutation | `RpcGroup::mutation` | `Effect<O, E, DynEnv>` from `I` |
| server stream | `RpcGroup::server_stream` | `Effect<Stream<O, E, DynEnv>, E, DynEnv>` from `I` |
| client stream | `RpcGroup::client_stream` | `Effect<O, E, DynEnv>` from a `Stream<I, E, DynEnv>` |
| two-way stream | `RpcGroup::bidi` | `Effect<Duplex<I, O, E>, E, DynEnv>` |
| subscription | `RpcGroup::subscription` | `Effect<Subscription<O, E>, E, DynEnv>` from `I` and a cursor |
Inputs and outputs cross the wire through a `WireSchema`: `WireSchema::of::<T>()` for a
`#[derive(Schema)]` type, or `WireSchema::new(schema)` for any Schema whose wire type is JSON-like.
```rust
use id_effect::{DynEnv, Never, Stream, fail, schema, succeed};
use id_effect_rpc::error_union::{RpcErrorStatus, RpcErrorUnion};
use id_effect_rpc::{Procedure, RpcGroup, RpcServer, WireSchema};
use axum::http::StatusCode;
use serde_json::{Value, json};
enum TodoError {
NotFound { id: i64 },
}
impl RpcErrorStatus for TodoError {
fn status(&self) -> StatusCode {
StatusCode::NOT_FOUND
}
}
impl RpcErrorUnion for TodoError {
fn to_wire(&self) -> Value {
match self {
Self::NotFound { id } => json!({"_tag": "NotFound", "id": id}),
}
}
}
let mut group = RpcGroup::new();
group.query(
Procedure::query("Todo.title").public(),
WireSchema::new(schema::i64::<()>()),
WireSchema::new(schema::string::<()>()),
|id: i64, _env: &mut DynEnv| {
if id == 1 { succeed("milk".to_owned()) } else { fail(TodoError::NotFound { id }) }
},
);
group.server_stream(
Procedure::server_stream("Todo.count").public(),
WireSchema::new(schema::i64::<()>()),
WireSchema::new(schema::i64::<()>()),
|n: i64, _env: &mut DynEnv| succeed::<_, Never, _>(Stream::from_iterable(1..=n)),
);
group.client_stream(
Procedure::client_stream("Todo.sum").max_items(1_000).public(),
WireSchema::new(schema::i64::<()>()),
WireSchema::new(schema::i64::<()>()),
A mutation called with `Idempotency-Key: <key>` runs once: a retry with the same key and payload
gets the stored result (and `idempotency-replayed: true`), the same key with another payload is a
`409`. Keys belong to the caller: an authenticated principal (`RpcPrincipal`, or your own
`RpcServer::with_principal`) has its own; anonymous callers are scoped by client IP and need
keys of at least 16 characters. The default `MemoryIdempotencyStore` keeps results for 24 hours,
at most 100,000 keys; `IdempotencyStore` is the seam for a shared store.