MosaicMosaic

Think from the response up.

Composable backend orchestration for Kotlin

Start with what your endpoint returns. Fetch independent data in parallel and reuse results when more than one part of the response needs them.

Mosaic works with the HTTP framework you already use.

Run the product-page example

Runtime 1.1.0 · Apache 2.0
ProductPageTiles.kt
val ProductPageTile by singleTile {
val product = composeAsync(SelectedProductTile)
val offer = composeAsync(ProductOffersTile, source(ProductId))
val availability = composeAsync(AvailabilityTile)
val reviews = composeAsync(ReviewsTile)
val recommendations = composeAsync(RecommendationsTile)
ProductPage(
product = product.await(),
price = offer.await().price,
availability = availability.await(),
reviews = reviews.await(),
recommendations = recommendations.await(),
)
}
Three branches share one customer lookupPricing, Delivery, and Recommendations all share one customer lookup through CustomerTile.PricingDeliveryRecommendationsCustomerTile
Three branches, one customer lookup per request.
Think from the response up, not the database down.Follow the composition

Start from what
the endpoint returns.

A product page needs a price, availability, reviews, and recommendations. Each part asks for the data it needs.

The endpoint stays small even when the graph behind it grows.

Combine backend API calls
ProductPageTiles.kt
val RecommendationIdsTile by singleTile {
val product = composeAsync(SelectedProductTile)
val customer = composeAsync(CustomerTile)
source<RecommendationService>().getRecommendations(
product = product.await(),
postalCode = customer.await().postalCode,
)
}

Independent work stays independent.

composeAsync starts each branch before you await its result. Use compose when the next step needs that value.

Compose concurrent work

Ask twice.
Execute once.

Pricing needs to know whether the shopper is a member. Delivery and recommendations need their postal code. Each asks for the customer, but Mosaic fetches it only once per request.

Shared work and cache identity
ProductPageTiles.kt
val CustomerTile by singleTile {
source<CustomerService>().getCustomer(source(CustomerId))
}

A branch can join a lookup already in progress or reuse its completed result.

Batch naturally.

A MultiTile puts keyed work behind one reusable dependency. Overlapping requests reuse equal keys; newly pending keys can join a batch before execution starts.

Ready work is never intentionally delayed. Exact batch boundaries depend on scheduling.

Work with MultiTile
ProductPageTiles.kt
val ProductsTile by multiTile { ids: Set<String> ->
source<CatalogService>().getProducts(ids)
}
First callheadphones-123
Second callheadphones-123 reusedcase-456 fetched

Sequential calls in one Mosaic. Only the new key reaches the fetch block.

Choose bulk, per-key, or chunked fetching

Your composition
is architecture.

The optional analysis plugin turns Tile relationships into a graph. Keep the code as the model, instead of maintaining a second diagram by hand.

Product-page Tile dependencies: pricing, availability, and recommendation lookup all reach CustomerTile.
View full-size graph. Selected dependencies from the product-page mosaicGraph report. Static dependencies, not runtime timing or batch boundaries.

See what
actually executed.

Add Mosaic execution spans to your OpenTelemetry traces. Dependency links reveal shared producers and batch callers; cache hits create no new spans.

val applicationCanvas = canvas {
tracing { openTelemetry }
}

Your application supplies OpenTelemetry, sampling, export, and shutdown.

Trace Tile execution

Test response logic
without booting the world.

Replace dependency Tiles with fixture values and leave the real response composition running. Test inputs, failures, and virtual-time delays in isolation.

mosaicBuilder()
.withMockTile(ProductsTile, products)
.withMockTile(PricesTile, prices)
.withMosaic {
assertEquals(ProductOffersTile, ids, offers)
}

Inside runTest; products, prices, and expected offers are fixture values.

Test your compositions

Measured cost.
Visible tradeoffs.

Mosaic handles orchestration you would otherwise write yourself. The published application benchmark compares it with equivalent optimized Kotlin doing the same downstream work.

For the service-backed aggregate graph at 800 RPS, Mosaic added about 115 µs of CPU/request. Both implementations had median HTTP latency of about 21.12 ms.

Read the evidence and limitations
Additional CPU per request
WorkloadMosaic CPU overhead
Light / batching+14 to +21 µs
Aggregate graph+48 to +115 µs
Sibling coalescing+61 to +78 µs
CPU-heavy+6 to +9 µs

Ranges show additional CPU used by Mosaic versus direct Kotlin across measured profiles and rates, using rounded paired medians. CPU cost is not HTTP latency.

Bring your HTTP framework.

The same product-page composition runs behind Spring Boot, Ktor, and Micronaut.
Keep routing and HTTP concerns in your framework. Compose the response with Mosaic.

Runnable framework examples. No published framework adapter is required.

Run an example

Build the response.
Compose the rest.

Add Mosaic to your Kotlin project.
Your first Tile is a few lines away.

Get started