If you come from a framework like Laravel, you will be quite familiar with dispatching jobs onto a queue and then consuming that job via a queue worker.
Usually in Golang, we tend to just use goroutines that can run jobs in the background without needing Redis or some external service, and this works fine for a fair amount of use cases however there'll come a time when you need to distribute work across multiple servers and need some kind of retry mechanism.
This is just a basic example of how you would go about setting up a Redis queue in Golang, it's not meant to be a complete solution but should give you a good idea of how to go about building your own job queue system.
Firstly you'll need to pull in a Redis Go driver:
github.com/go-redis/redis
To set up a Redis client:
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
Password: "",
DB: 0,
})
Next, to add data to a queue:
err = client.LPush("queuename", "some data").Err()
if err != nil {
log.Fatal(err)
}
Finally, to read the data from your queue:
item, err := client.LPop("queuename").Result()
Top comments (0)