Skip to content

A Go library that allows you to slash your data into micro-batches, and perform operation on them once they are flushed.

License

Notifications You must be signed in to change notification settings

audipasuatmadi/go-microbatch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

2c5c188 · Mar 26, 2025

History

8 Commits
Mar 25, 2025
Mar 15, 2025
Mar 10, 2025
Feb 27, 2025
Feb 27, 2025
Mar 15, 2025
Mar 15, 2025
Mar 15, 2025
Mar 15, 2025
Feb 27, 2025
Feb 27, 2025
Mar 15, 2025
Mar 15, 2025

Repository files navigation

Go Microbatch

Microbatch mechanism written in Go. Could be useful in use cases such as:

  • Handling frequent real-time data from IoT devices where you have to batch-insert them to your OLAP data store of choice for performance.
  • Handling frequent user messages to LLM, where multiple messages of user sent at a short-time is treated as one message for efficiency without destroying semantic meaning.
  • Many others.

Installation

go get github.com/audipasuatmadi/go-microbatch

The Idea of Microbatching

Microbatching groups multiple incoming data into one batch of data. This means less operation for the data, opting for throughput and sacrificing latency. Thus allows us to have less operations and faster performance on a large amount of data.

image

Other than for performance benefit, in nowadays, it allows for a unique use case in Large-Language Models. As user tend to send multiple messages from messaging platform such as Whatsapp, we surely don't want each message to be treated as individual message such as the picture below. image

Usage

Example Usage

func main() {
    // Initialize the microbatch
    mb := microbatch.New[string](microbatch.NewParams{
        // When MaxSize is reached, it will flush the batch.
        MaxSize:       int32(10),
        // Or when the interval is reached, it will flush the batch.
        FlushInterval: 3 * time.Second,
    })

    messages := []string{
        "Hello",
        "How are you?",
    }

    go simulateSendToLLM(mb)

    // Simulate how people type in message platforms, where people
    // type multiple messages which semantically related as one message.
    mb.Add(context.Background(), messages[0])
    fmt.Printf("\n> %s\n", messages[0])
    time.Sleep(2 * time.Second)
    mb.Add(context.Background(), messages[1])
    fmt.Printf("\n> %s\n", messages[1])
}

func simulateSendToLLM(mb microbatch.Microbatch[string]) {
    for {
        // This will block until the batch is flushed.
        data := mb.ReadData(context.Background())
        // You got all the data at once, can do whatever you wish with it.
        fmt.Printf("\nsending \"%s\" to LLM\n> ", strings.Join(data, " | "))
    }
}

Contributing

This project is "very" far from perfect. Every contribution is really appreciated.

About

A Go library that allows you to slash your data into micro-batches, and perform operation on them once they are flushed.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages