DEV Community

zisam shaikh
zisam shaikh

Posted on

Building a Zero-Dependency Key-Value Engine in 5 Languages (Rust, Go, TypeScript, Swift, Kotlin)

Modern distributed software is polyglot. A cloud backend might have microservices written in Go, low-latency data pipelines in Rust, web APIs in TypeScript/Node, iOS clients in Swift, and Android/JVM services in Kotlin.

Yet, when working across different language ecosystems, caching semantics are wildly fragmented:

  • Eviction policies behave differently ($O(1)$ LRU vs probabilistic vs FIFO).
  • Concurrency models handle mutex contention and clock drift inconsistently.
  • Dependency bloat drags dozens of third-party packages into otherwise clean microservices.

To solve this, we engineered KVDock natively across 5 languages with zero external dependencies in core and unified memory/concurrency semantics.

Here is what we learned building high-performance key-value engines across Rust, Go, TypeScript, Swift, and Kotlin.


1. Rust (kvdock-rs): Monotonic Clocks & Zero Lock Contention

In systems programming, relying on wall-clock time (SystemTime) for TTL eviction is dangerous because NTP clock synchronizations or leap seconds can cause erratic cache expirations.

In kvdock-rs, we enforce TTL using monotonic Instant:

use kvdock_rs::{KVDock, MemoryAdapter};
use std::time::Duration;

#[tokio::main]
async fn main() {
    let adapter = MemoryAdapter::new(10_000);
    let dock = KVDock::new(adapter).with_default_ttl(Duration::from_secs(60));

    // Single-flight stampede coalescing in Rust
    let session = dock.get_or_set("user:session:101", || async {
        fetch_user_from_db().await
    }).await;
}
Enter fullscreen mode Exit fullscreen mode
  • Concurrency: Thread-safe KVDock<A> backed by read-write locks for sub-microsecond concurrent reads.
  • Eviction: True $O(1)$ LRU with doubly-linked intrusive pointers.

2. Go (kvdock-go): Generics & Singleflight Mutexes

Go 1.18+ generics revolutionized type-safe caching without interface{} type assertions:

package main

import (
    "context"
    "time"
    "github.com/zisam-glitch/kvdock-go"
)

type UserSession struct {
    UserID string
    Role   string
}

func main() {
    dock := kvdock.New(kvdock.WithMemoryAdapter(10000), kvdock.WithTTL(60 * time.Second))

    // Generic type-safe get or set
    val, err := kvdock.GetOrSet[UserSession](dock, context.Background(), "user:101", func() (UserSession, error) {
        return queryUserFromDB()
    })
}
Enter fullscreen mode Exit fullscreen mode
  • Concurrency: Combines atomic operational telemetry with internal single-flight deduplication to ensure concurrent Goroutines requesting the same missing key coalesce into a single execution.

3. TypeScript (kvdock): Zero Dependencies & Universal Runtimes

JavaScript runtimes are now fragmented across Node.js, Bun, Deno, and Cloudflare Workers:

import { KVDock, TieredDock } from "kvdock";
import { MemoryAdapter } from "kvdock/adapters/memory";
import { RedisAdapter } from "kvdock/adapters/redis";

const dock = new TieredDock({
  l1: new MemoryAdapter({ maxSize: 10_000 }), // In-memory L1 (< 0.05ms)
  l2: new RedisAdapter({ url: process.env.REDIS_URL }), // Distributed L2
  l1TTL: 15_000
});
Enter fullscreen mode Exit fullscreen mode
  • Key Innovation: Zero runtime dependencies in core. Tree-shakeable dual ESM/CJS exports with single-flight promise coalescing that reduces database stampedes by 99.9%.

4. Swift (kvdock-swift): Swift 6 Concurrency & Actor Isolation

In modern Swift, data races are caught at compile-time using structured concurrency:

import KVDockSwift

let dock = await KVDock(
    adapter: MemoryAdapter(capacity: 10_000),
    defaultTTL: .seconds(60)
)

// Actor-isolated thread-safe access
let user = try await dock.getOrSet("user:profile") {
    try await fetchUserProfile()
}
Enter fullscreen mode Exit fullscreen mode
  • Uses ContinuousClock for nanosecond monotonic TTL expiration.

5. Kotlin (kvdock-kt): Access-Order LRU & Coroutines

On the JVM and Android, coroutine compatibility and low memory overhead are paramount:

import com.kvdock.KVDock
import com.kvdock.adapters.MemoryAdapter
import kotlin.time.Duration.Companion.minutes

val dock = KVDock(MemoryAdapter(maxSize = 10_000), defaultTTL = 1.minutes)

val order = dock.getOrSet("order:7788") {
    fetchOrderFromDatabase()
}
Enter fullscreen mode Exit fullscreen mode

The Polyglot Ecosystem on GitHub

Explore the source code, benchmarks, and architectures:

⭐ Star the repos & follow @zisam-glitch for deep dives into low-latency systems architecture!

Top comments (1)

Collapse
 
mp3sss31 profile image
mp3sss •

Monotonic clocks for TTL are a detail most cache implementations get wrong - wall-clock based expiry means an NTP step or a laptop suspend can expire or extend entries arbitrarily, and in Electron we see the same bug in another disguise: timers and cache TTLs drift across the main/renderer boundary because each side uses its own clock base. Standardizing on one monotonic source for expiry (and persisting an offset if you must restore across restarts) removes a whole class of flaky-cache bugs. The zero-dependency constraint is also a good stress test: when you can't pull a cache library, you re-derive the eviction and concurrency semantics and end up with fewer surprises than most wrappers ship.