Skip to content

About

No description, website, or topics provided.

Resources

Stars

45 stars

Watchers

1 watching

Forks

Repository files navigation

Vanilla Kotlin

A reference implementation of Kotlin microservices built without an application framework. No Spring, no Micronaut — dependencies are wired together explicitly with constructor and function injection, on top of http4k, JDBI, and Kafka.

The goal is to show what these services look like when nothing is hidden behind annotations or classpath scanning: every dependency, route, and configuration value is visible in code.

Background and rationale:

Layout

Libraries (libs/)

Module Purpose
common Shared models, the VanillaApp lifecycle interface, Jackson setup, extensions
db JDBI setup and repositories, Flyway-backed migrations
http4k Server builder DSL, health checks, CORS and logging filters
kafka Producer, consumer, and the actor-pattern KafkaTransformer
metrics OpenTelemetry metrics publishing
rocksdb Embedded RocksDB store with configurable column families and compaction
client OkHttp-based HTTP client with retry and telemetry interceptors
pipeline Composable Flow-based source/processor/sink pipelines (kafka, postgres, rocksdb)

Applications (apps/)

App Purpose
api REST service with OpenAPI documentation and health checks
bulk-inserter Consumes Kafka messages and batch-inserts them into PostgreSQL
kafka-transformer Transforms and republishes messages between Kafka topics
outbox-processor Reads the transactional outbox table and publishes to Kafka
rocks-db-api Kafka-fed read API backed by embedded RocksDB
reducer Kafka source topic → RocksDB → Kafka sink topic reducer, built on libs/pipeline

Each application is a thin composition root: it loads a Config, wires concrete implementations into the SAM interfaces defined by libs/*, and starts a VanillaApp. Business logic lives in libs/* and in each app's own service classes, not in the composition root.

Module dependencies

flowchart TD
    api --> common
    api --> db
    api --> http4k
    api --> client
    api --> metrics
    bulk-inserter --> common
    bulk-inserter --> db
    bulk-inserter --> kafka
    kafka-transformer --> common
    kafka-transformer --> kafka
    kafka-transformer --> client
    outbox-processor --> common
    outbox-processor --> db
    outbox-processor --> kafka
    rocks-db-api --> common
    rocks-db-api --> kafka
    rocks-db-api --> rocksdb
    rocks-db-api --> http4k
    reducer --> common
    reducer --> http4k
    reducer --> pipeline
    pipeline --> common
    pipeline --> kafka
    pipeline --> db
    pipeline --> rocksdb
    db --> common
    kafka --> common
    kafka --> metrics
    client --> common
    http4k --> common
    metrics --> common
    rocksdb --> common

    subgraph apps
        api
        bulk-inserter
        kafka-transformer
        outbox-processor
        rocks-db-api
        reducer
    end

    subgraph libs
        common
        db
        http4k
        kafka
        metrics
        rocksdb
        client
        pipeline
    end
Loading

Stack

  • Kotlin 2.4, JVM toolchain 25
  • Gradle 9.6 with a centralized version catalog (gradle/libs.versions.toml)
  • http4k for HTTP servers and clients
  • Apache Kafka for messaging
  • PostgreSQL with JDBI and Flyway
  • RocksDB for embedded key-value storage
  • OpenTelemetry for metrics
  • Docker Compose for local Postgres/Kafka

Getting started

Requires Java 25 and Docker.

docker-compose up -d
./gradlew :db-migration:flywayMigrate
./gradlew build
./gradlew :apps:api:run

The API listens on http://localhost:8080, with a health check at /health. Other applications run the same way, substituting the module name (:apps:bulk-inserter:run, etc.).

Testing

./gradlew test                # everything
./gradlew :libs:kafka:test    # a single module

Unit tests use function injection instead of a mocking framework — see testing.md for the unit/integration split. Integration tests exercise real Postgres and Kafka via docker-compose.yml.

Building

./gradlew build              # compile, test, lint everything
./gradlew :apps:api:build    # build a distribution for one app
./gradlew spotlessApply      # apply ktlint formatting
./gradlew dependencyUpdates  # report on outdated dependencies in the version catalog

./gradlew :apps:api:build produces a runnable distribution at apps/api/build/install/api, which a container image can copy in directly:

FROM eclipse-temurin:25-jre-jammy
COPY apps/api/build/install/api /app
WORKDIR /app
CMD ["./bin/api"]

Examples

HTTP server (libs/http4k)

val server = buildServer(port = 8080) {
    routeHandlers {
        +getFavoriteThingsRoute(favoriteThingsService.getFavoriteThings)
    }

    healthMonitors {
        +object : HealthMonitor {
            override val name = "database"
            override fun check() = databaseHealthCheck()
        }
    }
}

server.start()

Kafka producer (libs/kafka)

val producer = KafkaProducer<String>(
    config = KafkaProducer.Config(broker = "localhost:9092", topic = "my-topic"),
    publishTimerMetric = metrics::publishTimerMetric,
).start()

producer.send(KafkaOutputMessage(key = "message-key", value = "hello"))

JDBI repository with function injection (libs/db)

fun interface FindAllFavoriteThings {
    operator fun invoke(): List<FavoriteThing>
}

class FavoriteThingRepository(private val jdbi: Jdbi) {
    val findAll = FindAllFavoriteThings {
        jdbi.withHandle<List<FavoriteThing>, Exception> { handle ->
            handle.createQuery("SELECT * FROM favorite_thing")
                .mapTo(FavoriteThing::class.java)
                .list()
        }
    }
}

A composition root then injects repository.findAll directly into a service constructor rather than the whole FavoriteThingRepository — see functional-injection.md.

Application configuration

Configuration is a plain data class loaded by Hoplite, not annotation-driven:

data class Config(
    val db: DbConfig,
    val http: HttpConfig,
    val metrics: OtelMetrics.Config,
) {
    data class HttpConfig(
        val client: ClientConfig,
        val server: ServerConfig,
    ) {
        data class ClientConfig(
            val thing: ThingGateway.Config,
            val connectionConfig: ConnectionConfig,
            val retryConfig: RetryInterceptor.Config,
        )

        data class ServerConfig(val port: Int, val host: String)
    }
}

About

No description, website, or topics provided.

Resources

Stars

45 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages