Skip to main content

Embedded Kahuna Node

EmbeddedKahunaNode starts a single-node Kahuna engine inside the current .NET process. It is useful for integration tests, local tools, and applications that need Kahuna's transaction engine without running the ASP.NET server, Kestrel, REST, or external gRPC endpoints.

using System.Text;
using Kahuna;
using Kahuna.Shared.KeyValue;
using Kommander.Time;

await using var node = new EmbeddedKahunaNode(new()
{
Storage = "memory",
WalStorage = "memory",
InitialPartitions = 1
});

await node.StartAsync();
await node.WaitForLeaderForKeyAsync("tenant/table/key-a");

byte[] value = Encoding.UTF8.GetBytes("value-a");

await node.Kahuna.LocateAndTrySetKeyValue(
transactionId: HLCTimestamp.Zero,
key: "tenant/table/key-a",
value: value,
compareValue: null,
compareRevision: -1,
flags: KeyValueFlags.Set,
expiresMs: 0,
durability: KeyValueDurability.Persistent,
cancellationToken: CancellationToken.None
);

API

public sealed class EmbeddedKahunaNode : IAsyncDisposable
{
public IKahuna Kahuna { get; }
public IRaft Raft { get; }

public EmbeddedKahunaNode(
EmbeddedKahunaOptions options,
ILoggerFactory? loggerFactory = null
);

public Task StartAsync(CancellationToken cancellationToken = default);
public Task<string> WaitForLeaderForKeyAsync(string key, CancellationToken cancellationToken = default);
public ValueTask DisposeAsync();
}

Options

OptionDefaultDescription
NodeNameembedded-1Logical node name.
NodeId1Raft node identifier.
HostlocalhostMust be a concrete host, not *.
Port0Unused by in-memory communication, but still part of the Raft configuration.
InitialPartitions1Number of Raft partitions.
StoragememoryKey/value storage backend: memory, sqlite, or rocksdb.
StoragePathemptyStorage directory for persistent backends.
StorageRevisiongeneratedStorage revision name.
WalStoragememoryRaft WAL backend: memory, sqlite, or rocksdb.
WalPathemptyWAL directory for persistent backends.
WalRevisiongeneratedWAL revision name.
LocksWorkersEnvironment.ProcessorCountLock worker count.
KeyValueWorkersEnvironment.ProcessorCountKey/value worker count.
BackgroundWriterWorkers1Background persistence worker count.
DefaultTransactionTimeout5000Default transaction timeout in milliseconds.
ScriptCacheExpiration1 minuteHow long parsed scripts stay cached.
RevisionsToKeepCached100Number of key revisions to keep cached in memory.
CacheEntryTtl5 minutesMaximum age of cached entries before they become eviction candidates.
CacheEntriesToRemove1000Maximum number of cache entries removed per eviction pass.
CollectionInterval60 secondsInterval for cache collection and eviction checks.
MaxEntriesPerActor50000Maximum cached entries per actor before collection pressure applies.
MaxBytesPerActor268435456Approximate maximum cached bytes per actor before collection pressure applies.
CollectBatchMax1000Maximum number of entries considered in a collection batch.
RevisionRetention16Number of revisions retained for in-memory revision history.
LruSampleSize5Number of candidates sampled for LRU-style eviction decisions.
LruSampleScanMax256Maximum number of entries scanned while building an LRU sample.
MetadataTrimInterval4Collection cycle interval for metadata trimming. 0 disables metadata trimming.
DirtyObjectsWriterDelay1000Delay between dirty object writer flush passes, in milliseconds.
ReadIOThreads8Number of Raft read I/O threads.
WriteIOThreads8Number of Raft write I/O threads.
HttpSchemehttps://HTTP scheme used by Raft REST communication.
HttpAuthBearerTokenemptyBearer token sent with Raft REST communication.
HttpTimeout5Raft REST request timeout in seconds.
HttpVersion2.0HTTP protocol version used by Raft REST communication.
HeartbeatInterval500 msLeader heartbeat interval.
RecentHeartbeat100 msRecent-heartbeat window.
VotingTimeout1500 msVote wait timeout.
CheckLeaderInterval250 msLeader check interval.
TimerInitialDelay2500 msInitial delay before Raft timers start.
UpdateNodesInterval5000 msNode registry update interval.
StartElectionTimeout500Minimum election timeout in milliseconds.
EndElectionTimeout1500Maximum election timeout in milliseconds.
StartElectionTimeoutIncrement100Minimum election timeout increment in milliseconds.
EndElectionTimeoutIncrement200Maximum election timeout increment in milliseconds.
SlowRaftStateMachineLog50Slow state-machine operation log threshold in milliseconds.
SlowRaftWALMachineLog25Slow WAL state-machine operation log threshold in milliseconds.
CompactEveryOperations1000Number of committed operations between automatic Raft WAL compaction checks.
CompactNumberEntries50Number of Raft WAL entries removed per compaction batch.
MaxEntriesPerCompaction5000Maximum Raft WAL entries processed per compaction run.

Notes

  • The embedded node uses in-memory Raft and inter-node communication.
  • StartAsync joins the single-node cluster and waits for leaders for the configured partitions.
  • WaitForLeaderForKeyAsync waits for the partition that owns a specific key.
  • Use distinct StoragePath and WalPath values when using sqlite or rocksdb.
  • Always dispose the node with await using or DisposeAsync so Raft leaves the cluster and file-backed resources are released.