Service Discovery
Clients need to know which instances of a service are running now. In this recipe, each instance registers itself under a key with an expiration, like a Redis key with a TTL. The instance must refresh the key, or the key expires. This means that crashed instances disappear automatically. Clients and workers then discover healthy endpoints with one read. You don't need a separate registry.
Use one bucket for each service.
services/{service-name}/{instance-id}
These keys register two instances of a payments service and one instance of a search service.
services/payments/node-a
services/payments/node-b
services/search/node-c
Register an Instance
Use an expiration so the instance disappears if the process dies or stops refreshing its heartbeat.
set `services/payments/node-a` '{"host":"10.0.1.15","port":8080,"version":"1.2.3","zone":"us-east-1a"}' ex 30000 norev
With NOREV, only the current registration is stored. Leave it off if you need historical membership reads.
Register the instance from a client.
- .NET
- TypeScript
await client.SetKeyValue(
"services/payments/node-a",
"""{"host":"10.0.1.15","port":8080,"version":"1.2.3","zone":"us-east-1a"}""",
expiryTime: 30000,
flags: KeyValueFlags.SetNoRevision,
durability: KeyValueDurability.Persistent
);
await client.setNoRevision(
"services/payments/node-a",
'{"host":"10.0.1.15","port":8080,"version":"1.2.3","zone":"us-east-1a"}',
{
expiry: 30_000,
durability: "persistent"
}
);
Refresh the Heartbeat
Refresh the key periodically, before the expiration passes.
- .NET
- TypeScript
await client.ExtendKeyValue(
"services/payments/node-a",
TimeSpan.FromSeconds(30),
KeyValueDurability.Persistent
);
await client.extend("services/payments/node-a", 30_000, {
durability: "persistent"
});
If instance metadata can change on heartbeat, write the full value again with the same expiration.
- .NET
- TypeScript
await client.SetKeyValue(
"services/payments/node-a",
currentMetadataJson,
expiryTime: 30000,
flags: KeyValueFlags.SetNoRevision,
durability: KeyValueDurability.Persistent
);
await client.setNoRevision("services/payments/node-a", currentMetadataJson, {
expiry: 30_000,
durability: "persistent"
});
Discover Instances
Read all instances in the service bucket.
get by bucket `services/payments`
Read the instances from a client.
- .NET
- TypeScript
List<KahunaKeyValue> instances = await client.GetByBucket(
"services/payments",
KeyValueDurability.Persistent
);
foreach (KahunaKeyValue instance in instances)
Console.WriteLine($"{instance.Key}: {instance.ValueAsString()}");
const instances = await client.getByBucket("services/payments", {
durability: "persistent"
});
for (const instance of instances) {
console.log(`${instance.key}: ${instance.valueAsString()}`);
}
Audit Historical Membership
To inspect which instances were visible at a previous point in time, use an AS OF read.
get by bucket `services/payments` as of 1718392012345
You can also use a client snapshot parameter.
- .NET
- TypeScript
List<KahunaKeyValue> previous = await client.GetByBucket(
"services/payments",
KeyValueDurability.Persistent,
snapshotMs: 1718392012345
);
const previous = await client.getByBucket("services/payments", {
durability: "persistent",
snapshotMs: 1718392012345
});
Do not use NOREV for registrations when historical membership is required. No-revision writes keep only the latest value and skip the archived revision records needed by historical reads.
Choosing Durability
Use persistent durability when discovery state must survive node restarts and remain available during failover.
Each persistent registration and each persistent heartbeat, whether set or extend, is a Raft-replicated write. With many instances and short heartbeat intervals, that write load adds up.
Use ephemeral durability when registrations are high-volume and short-lived, and running services can rebuild them. Ephemeral heartbeats are stored only in the memory of the partition leader. They aren't replicated, and this makes them cheaper. However, a Kahuna node restart or a leader change drops all registrations on that partition, and memory eviction can also drop them. The instances then disappear from discovery until they write their registration again. Refresh ephemeral registrations with a full eset, and don't use only an extend. A dropped key then comes back on the next heartbeat. Ephemeral keys also cannot be watched. See Durability Levels.
eset `services/payments/node-a` '{"host":"10.0.1.15","port":8080}' ex 30000
eget by bucket `services/payments`
Operational Notes
- Set the expiration to several heartbeat intervals. One interval isn't enough.
- Use no-revision writes for high-churn persistent registrations when only the current membership matters.
- Store enough metadata for clients to make routing decisions in one lookup.
- Keep service membership under a shared bucket so reads are consistent and partition-local.
- For large ordered registries, use key-range sharding. A bucket is always on one partition.