Ringslice: A Hook-Driven Ring Buffer for Go

author avatar

Toru Maesaka

Founder and Engineer

Ring buffer, or circular buffer, is a classic data structure. The concept is simple: a fixed-size buffer that wraps around when full, overwriting the oldest data. This circular design makes ring buffers memory-efficient, and perhaps most importantly, predictable. Today, we’re introducing Ringslice, a reactive hook-driven Go ring buffer, designed for production workloads.

In modern software, ring buffers shine in workloads that care about recency over completeness. Web applications are full of these workloads, yet data is often persisted when it does not need to be, potentially accumulating privacy concerns.

Reactive Buffer Management

Most buffer implementations stop at storage. Ringslice goes further with lifecycle hooks designed for production workloads, turning the buffer into a reactive component.

OnBeforeAdd allows the buffer to validate or reject data before it occupies a slot. OnRotate fires when the write index wraps, giving programmatic insight into the sliding window. OnFlush ensures that when the buffer is manually cleared, existing data can be gracefully processed or persisted elsewhere.

ring := ringslice.New[string](128)

ring.OnBeforeAdd(func(value string) bool {
    return len(value) > 0
})

ring.OnRotate(func(values []string) {
    fmt.Println("lap complete")
})

ring.OnFlush(func(values []string) {
    for _, v := range values {
        db.Insert(v)
    }
})

ring.Add("apple")
ring.Add("banana")

Ringslice is also type-safe through Go generics and supports iter.Seq for idiomatic range iteration, meaning you can loop over the buffer just like any other Go range expression.

for v := range ring.All() {
    fmt.Println(v)
}

For pointer-based circular lists and round-robin traversal, Go’s standard container/ring package remains the right tool.

In Production: Chrono News

Ringslice is used in production at Chrono to power a sliding window of content signals on Chrono News, a newswire for Japanese-speaking creator communities. This transient approach allows the platform to surface emerging trends in real time without the need for persistent activity logs.

Available To Everyone

Ringslice is open source and available on GitHub under the MIT license, consistent with all software Chrono publishes. To get started, run go get github.com/chronohq/ringslice, or browse the documentation. Contributions are welcome, and if Ringslice finds a place in your stack, we’d love to hear about it.

Enjoy the article? Get updates by email. No spam.