Cluster
Background jobs, file depot, distributed locks, scheduled workers, and key-value storage.
The cluster module provides server-side infrastructure that goes beyond request handling.
Everything is accessible through cluster — a facade available in any handler.
Background jobs
Queue work for asynchronous execution with typed input/output and retry policies.
class SendWelcomeEmail(
backgroundJobs: Lazy<BackgroundJobs>,
) : BackgroundJobs.Handler<SendWelcomeEmail.Input, SendWelcomeEmail.Output>(
backgroundJobs, kType()
) {
data class Input(val userId: String, val email: String)
data class Output(val sent: Boolean)
override val jobType = "send-welcome-email"
override suspend fun execute(job: BackgroundJobQueued, data: Input): Output {
mailService.send(data.email, "Welcome!")
return Output(sent = true)
}
}
// Queue a job from any handler:
welcomeEmailHandler.queue(
data = SendWelcomeEmail.Input(userId = "123", email = "user@example.com"),
retryPolicy = BackgroundJobRetryPolicy.LinearDelay(delayInMs = 5000, maxTries = 3),
) File depot
Abstract file storage with pluggable backends. Same API for local filesystem and AWS S3.
val repo = cluster.depot.getRepo("uploads")
// Upload
repo?.putFile("photos/avatar.jpg", imageBytes)
// List
val files = repo?.listItems("photos/")
// Download
val content = cluster.depot.getContent(DepotUri("uploads", "photos/avatar.jpg")) Distributed locks
Coordinate work across server instances with database-backed global locks.
// Acquire lock, run work, release automatically
cluster.locks.global.lock("invoice-generation", timeout = 10.seconds) {
generateMonthlyInvoices()
}
// Try to acquire (returns null on timeout)
val result = cluster.locks.global.tryToLock("resource", timeout = 5.seconds) {
doWork()
} Workers
Scheduled background tasks that run at defined intervals.
class CleanupWorker : Worker {
override val id = "cleanup-old-sessions"
override val shouldRun = Worker.Every.hours(1)
override suspend fun execute(state: StateProvider) {
sessionRepo.deleteOlderThan(Duration.ofDays(30))
}
} Key-value storage
Typed persistent storage and volatile caching with TTL policies.
// Persistent storage
val category = RandomDataStorage.category<UserPrefs>("prefs")
cluster.storage.randomData.save(category, "user-123", prefs)
val loaded = cluster.storage.randomData.load(category, "user-123")
// Cache with TTL
val cacheCategory = RandomCacheStorage.category<PricingData>(
"pricing",
RawCacheDataModel.Policy.ServeCacheAndRefresh(ttl = 3600, refreshAfter = 1800)
)
val pricing = cluster.storage.randomCache.getOrPut(cacheCategory.bind("EUR")) {
fetchPricingFromApi()
} All cluster features use the same pluggable storage backends as auth —
Karango or Monko. Jobs, locks, files, and cache entries are all persisted to your database of choice.