DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones Build AI Agents That Are Ready for Production
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
Build AI Agents That Are Ready for Production

Just dropped: New 2026 “Cloud-Native Foundations” Trend Report. See how teams are tackling complexity, cost & reliability.

AI can investigate. Engineers still decide. See how both work together across incident response in this DZone + Datadog webinar on Oct. 29.

Related

  • Accelerate Innovation by Shifting Left FinOps: Part 4
  • Batch Processing Large Data Sets with Spring Boot and Spring Batch
  • Jakarta Batch in Practice: Reliable Chunk-Oriented Processing for Enterprise Workloads
  • Beyond Batch: Engineering Enterprise Systems for Real-Time Decisioning

Trending

  • Beyond HTTP Handoffs: Build Durable Agent-to-Agent Services With Temporal Nexus
  • Building Time-Series Applications With Java and InfluxDB
  • Git Blame Isn’t Enough: Building Verifiable Provenance for AI-Generated Code
  • Meta Wants to Run Your Business With AI — Microsoft and Salesforce Have a New Rival
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Batch Processing in Go

Batch Processing in Go

Batching is commonly used to split a large amount of work into smaller chunks for optimal processing. Here, look at two ways to batch process in Go.

By 
Priyanka Nawalramka user avatar
Priyanka Nawalramka
·
Jan. 05, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
24.5K Views

Join the DZone community and get the full member experience.

Join For Free

Batching is a common scenario developers come across to basically split a large amount of work into smaller chunks for optimal processing. Seems pretty simple, and it really is. Say we have a long list of items we want to process in some way. A pre-defined number of them can be processed concurrently. I can see two different ways to do it in Go.

The first way is by using plain old slices. This is something most developers have probably done at some point in their careers. Let's take this simple example:

Go
 
func main() {
	data := make([]int, 0, 100)
	for n := 0; n < 100; n++ {
		data = append(data, n)
	}
	process(data)
}

func processBatch(list []int) {
	var wg sync.WaitGroup
	for _, i := range list {
		x := i
		wg.Add(1)
		go func() {
			defer wg.Done()
			// do more complex things here
			fmt.Println(x)
		}()
	}
	wg.Wait()
}

const batchSize = 10

func process(data []int) {
	for start, end := 0, 0; start <= len(data)-1; start = end {
		end = start + batchSize
		if end > len(data) {
			end = len(data)
		}
		batch := data[start:end]
		processBatch(batch)
	}
	fmt.Println("done processing all data")
}

The data to process is a plain list of integers. To keep things simple, we just want to print all of them,10 concurrently at most. To achieve this, we loop over the list, divide it into chunks of batchSize = 10, and process each batch serially. Short and sweet, and does what we want.

The second approach uses a buffered channel, similar to what's described in this post on concurrency. Let's look at the code first.

Go
 
func main() {
	data := make([]int, 0, 100)
	for n := 0; n < 100; n++ {
		data = append(data, n)
	}
	batch(data)
}

const batchSize = 10

func batch(data []int) {
	ch := make(chan struct{}, batchSize)
	var wg sync.WaitGroup
	for _, i := range data {
		wg.Add(1)
		ch <- struct{}{}
		x := i
		go func() {
			defer wg.Done()
			// do more complex things here
			fmt.Println(x)
			<-ch
		}()
	}
	wg.Wait()
	fmt.Println("done processing all data")
}

This example uses a buffered channel of size 10. As each item is ready to be processed, it tries to send to the channel. Sends are blocked after 10 items. Once processed, it reads from the channel, thereby releasing from the buffer. Using a struct{}{} saves us some space, because whatever is sent to the channel never gets used.

As the author of the post points out, here we're exploiting the properties of a buffered channel to limit concurrency. One might argue this is not really batching, but rather it's concurrent processing with a threshold. I would totally agree. Regardless, it gets the job done and the code is a tad simpler.

Is it any better than slices? Probably not. As for speed, I timed the execution of both programs, and they ran pretty close. These examples are far too simple to see any significant difference in runtime. Channels in general are slower and more expensive than slices. Since there is no meaningful data being passed between the goroutines, it's probably a wasted effort. So why would I do it this way? Well, I like simple code, but that might not be enough of a reason. If the cost of serial processing of each batch outweighs the cost of using a channel, it might be worth consideration!

Processing Batch processing

Opinions expressed by DZone contributors are their own.

Related

  • Accelerate Innovation by Shifting Left FinOps: Part 4
  • Batch Processing Large Data Sets with Spring Boot and Spring Batch
  • Jakarta Batch in Practice: Reliable Chunk-Oriented Processing for Enterprise Workloads
  • Beyond Batch: Engineering Enterprise Systems for Real-Time Decisioning

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook