Kandra

Getting started

Five minutes: install, one entity, one query, against a real (local) ScyllaDB instance. This page intentionally skips the reasoning behind every decision — for that, go build the full app in /tutorial, which explains every line as it's written. If you haven't read /foundations yet, do that first; nothing below will feel arbitrary once you have.

Note

You need JDK 21 or newer and Docker for a local ScyllaDB instance. Start from an empty Gradle Kotlin project; the build file below is complete. Every dependency is explained in /tutorial/02-project-setup.

1. Run a local ScyllaDB

docker-compose.yml
services:
  scylla:
    image: scylladb/scylla:latest
    ports:
      - "9042:9042"
    command: --smp 1 --memory 750M --overprovisioned 1 --api-address 0.0.0.0
docker compose up -d

2. Add the dependencies

build.gradle.kts
plugins {
    kotlin("jvm") version "2.3.21"
    kotlin("plugin.serialization") version "2.3.21"
    id("com.google.devtools.ksp") version "2.3.12"
    application
}
 
application {
    mainClass.set("com.example.ApplicationKt")
}
 
repositories {
    mavenCentral()
}
 
dependencies {
    val kandraBom = platform("com.kandraorm.kandra:kandra-bom:0.5.2")
    implementation(kandraBom)
    ksp(kandraBom)
    implementation("com.kandraorm.kandra:kandra-ktor")
    ksp("com.kandraorm.kandra:kandra-codegen")
 
    implementation("io.ktor:ktor-server-core:3.6.0")
    implementation("io.ktor:ktor-server-netty:3.6.0")
    implementation("io.ktor:ktor-server-content-negotiation:3.6.0")
    implementation("io.ktor:ktor-serialization-kotlinx-json:3.6.0")
    implementation("org.slf4j:slf4j-simple:2.0.9")
}
  • The BOM pins every Kandra module to one version. It has to be added to ksp as well as implementation: Gradle applies a platform per configuration, so without ksp(kandraBom) the versionless kandra-codegen line can't be resolved.
  • kandra-core and kandra-runtime don't need to be listed explicitly — kandra-ktor brings both in at compile time (not just at runtime), so your code can import the annotations from kandra-core and the repositories from kandra-runtime with no extra dependency line. Versions before 0.4.8 needed both listed explicitly; see kandra#152 if you're pinned to an older one.
  • kandra-codegen runs at build time through KSP and generates a type-safe UserTable for the query DSL.
  • Ktor 3.x and Kotlin 2.3+. Since 0.5.0 Kandra is built and tested against Ktor 3.6, and CI also runs the Ktor-facing modules against Ktor 3.5.2 and the latest published 3.x (kandra#160). Ktor 2.x is no longer supported; if you're on it, stay on Kandra 0.4.x (group ke.co.coinx.kandra).

/tutorial/02-project-setup explains each dependency in depth, including the dependency-injection modules (kandra-koin, kandra-kodein).

3. Define an entity

User.kt
package com.example
 
import io.kandra.core.annotations.*
import java.time.Instant
import java.util.UUID
 
@ScyllaTable("users")
data class User(
    @PartitionKey val userId: UUID,
    @LookupIndex(tableSuffix = "by_email", consistency = LookupConsistency.BATCH)
    val email: String,
    val displayName: String,
    @CreatedAt val createdAt: Instant = Instant.EPOCH,
)

@PartitionKey decides which node(s) this row lives on — see /foundations if "why this column" isn't obvious yet. @LookupIndex generates and maintains a second table, users_by_email, so "find by email" doesn't need a full scan.

4. Install the plugin

Application.kt
package com.example
 
import io.kandra.core.KandraAuth
import io.kandra.ktor.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.routing.*
 
fun main() {
    embeddedServer(Netty, port = 8080, module = Application::module).start(wait = true)
}
 
fun Application.module() {
    install(ContentNegotiation) { json() }
    install(Kandra) {
        contactPoints = "localhost:9042"
        keyspace = "quickstart"
        localDatacenter = "datacenter1"
        autoCreateKeyspace = true
        replicationStrategy = ReplicationStrategy.SimpleStrategy(replicationFactor = 1, disableTablets = true)   // see the note below
        schemaMode = SchemaMode.AUTO_CREATE
        register(User::class)
        auth { provider = KandraAuth.static("cassandra", "cassandra") } // local dev only, see below
    }
    routing { userRoutes() }
}

schemaMode = SchemaMode.AUTO_CREATE runs CREATE TABLE IF NOT EXISTS for every registered entity at startup — right for a quickstart, not what you'd run in a production deploy pipeline (see /philosophy/schema-safety).

`disableTablets = true` — current ScyllaDB images need it for SimpleStrategy

scylladb/scylla:latest creates new keyspaces with tablets enabled, and a tablets keyspace cannot use SimpleStrategy. Without the replicationStrategy line above, this app fails at startup with InvalidConfigurationInQueryException: SimpleStrategy doesn't support tablet replication while it tries to CREATE KEYSPACE. disableTablets = true appends AND tablets = {'enabled': false} to that statement. It is ScyllaDB-only (Cassandra rejects the option) and is for local single-node dev; a real cluster should provision its keyspace ahead of time or use NetworkTopologyStrategy, which needs no flag.

The auth block isn't optional, even for an unauthenticated local cluster

AuthConfig.provider defaults to KandraAuth.fromEnv(), and install(Kandra) calls it unconditionally while building the driver session — there's no "skip auth for local dev" default. Without the override above, this app throws KandraAuthException at startup unless SCYLLA_USERNAME/SCYLLA_PASSWORD are set in the environment, even though the docker-compose ScyllaDB above has no authentication enabled. KandraAuth.static(...) is fine for local dev; KandraAuth.fromEnv() (the default) is the right choice once there's a real cluster with real credentials. The tutorial uses it with exported environment variables; see /tutorial/04-plugin-install.

5. Save and query

Routes.kt
package com.example
 
import io.kandra.ktor.kandra
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.Serializable
import java.util.UUID
 
@Serializable
data class CreateUserRequest(val email: String, val displayName: String)
 
@Serializable
data class UserResponse(val userId: String, val email: String, val displayName: String)
 
fun User.toResponse() = UserResponse(userId.toString(), email, displayName)
 
fun Route.userRoutes() {
    val userRepo = application.kandra.suspendRepository<User>()
 
    post("/users") {
        val req = call.receive<CreateUserRequest>()
        val user = User(userId = UUID.randomUUID(), email = req.email, displayName = req.displayName)
        userRepo.save(user)
        call.respond(HttpStatusCode.Created, user.toResponse())
    }
 
    get("/users/by-email/{email}") {
        val email = call.parameters["email"]!!
        val user = userRepo.find { UserTable.email eq email }
        if (user == null) call.respond(HttpStatusCode.NotFound) else call.respond(user.toResponse())
    }
}

Run it and try it:

./gradlew run
curl -X POST localhost:8080/users -H 'Content-Type: application/json' \
  -d '{"email":"ada@example.com","displayName":"Ada"}'
curl localhost:8080/users/by-email/ada@example.com

The routes use small request and response classes instead of serializing User directly, because UUID and Instant need custom serializers that would distract from the point of this page. Tutorial page 03 shows how to serialize entities directly. application.kandra.suspendRepository<User>() is the plain way to get a typed repository; the tutorial wires repositories through Koin instead.

UserTable is generated by kandra-codegen from the User entity — a compile-time-checked reference to the email column, not a magic string. userRepo.find { UserTable.email eq email } resolves through the users_by_email lookup table Kandra generated in step 3, not a full scan of users.

That's the whole loop: an annotated entity, a plugin install, a type-safe query. Nothing here explains why each piece is shaped this way — that's what the rest of the site is for.

Tip

Next: /tutorial builds a real multi-user app — a todo list — from data modeling through shipping, with every decision explained as it's made. It's deliberately more thorough than this page; that's the point.

Using Kandra with Claude Code

Kandra ships Claude Code skills as an installable plugin: a project-wide overview, a kandra-setup skill for adding Kandra to a build, and one exhaustive skill per module (kandra-core, kandra-runtime, kandra-ktor, kandra-koin, kandra-kodein, kandra-codegen, kandra-test, kandra-multidc, kandra-migrate, kandra-jakarta). Each documents every public class and function in its module, including footguns the compiler won't catch — such as update(old, new) taking two entities.

/plugin marketplace add Dev-Pasaka/kandra
/plugin install kandra@kandra

Claude Code then loads the relevant skill automatically while you define entities, call repositories, configure install(Kandra), wire DI, write tests, or author a migration.