DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
MediumDSA

Implement a Worker Pool Pattern in Go

235 views

Problem Statement

At Uber, we process millions of ride requests daily. Each request involves geocoding, pricing calculations, and driver matching. Implementing these as sequential operations would be disastrously slow. Design and implement a Worker Pool that processes jobs concurrently with a fixed number of workers.

Requirements

Implement a worker pool with the following specifications:

  • A fixed number of worker goroutines (configurable)
  • A jobs channel to receive work
  • A results channel to send completed work
  • Graceful shutdown when all jobs are processed

Example Implementation

package main

import (
    "fmt"
    "sync"
)

type Job struct {
    ID      int
    Payload string
}

type Result struct {
    JobID  int
    Output string
}

func worker(id int, jobs <-chan Job, results chan<- Result, wg *sync.WaitGroup) {
    defer wg.Done()
    for job := range jobs {
        // Process the job
        output := fmt.Sprintf("Worker %d processed: %s", id, job.Payload)
        results <- Result{JobID: job.ID, Output: output}
    }
}

func main() {
    numWorkers := 5
    numJobs := 100
    
    jobs := make(chan Job, numJobs)
    results := make(chan Result, numJobs)
    
    var wg sync.WaitGroup
    
    // Start workers
    for w := 1; w <= numWorkers; w++ {
        wg.Add(1)
        go worker(w, jobs, results, &wg)
    }
    
    // Send jobs
    for j := 1; j <= numJobs; j++ {
        jobs <- Job{ID: j, Payload: fmt.Sprintf("job-%d", j)}
    }
    close(jobs)
    
    // Wait and close results
    go func() {
        wg.Wait()
        close(results)
    }()
    
    // Collect results
    for result := range results {
        fmt.Println(result.Output)
    }
}

Real-World Scenarios

  • Image Processing Pipeline: Resize/compress thousands of user uploads
  • API Aggregation: Fetch data from multiple microservices concurrently
  • Log Processing: Parse and index high-volume log streams

Follow-up Questions

  1. How would you handle job failures and retries?
  2. How do you limit memory usage with very large job queues?
  3. How would you implement priority queues for jobs?

Sample Test Cases

Case 1
Input
workers=3, jobs=[1,2,3,4,5]
Expected Output
All jobs processed by 3 workers concurrently
Case 2
Input
workers=1, jobs=[1,2,3]
Expected Output
Jobs processed sequentially by single worker

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
235
Likes
0
Solutions
0
Comments
0

Category

Backend Engineering

Languages

Go