Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

delayqueue

> 数据库
Open source

Simple, reliable, installation-free distributed delayed delivery message queue in Go. 简单、可靠、免安装的分布式延时投递消息队列

391 stars0 likes2 views
WebsiteGitHub

About

DelayQueue

中文版

DelayQueue is a message queue supporting delayed/scheduled delivery based on redis. It is designed to be reliable, scalable and easy to get started.

Core Advantages:

  • Guaranteed at least once consumption
  • Auto retry failed messages
  • Works out of the box, Config Nothing and Deploy Nothing, A Redis is all you need.
  • Natively adapted to the distributed environment, messages processed concurrently on multiple machines . Workers can be added, removed or migrated at any time
  • Support Redis Cluster or clusters of most cloud service providers. see chapter Cluster
  • Easy to use monitoring data exporter, see Monitoring

Install

DelayQueue requires a Go version with modules support. Run following command line in your project with go.mod:

bash
go get github.com/hdt3213/delayqueue

if you are using github.com/go-redis/redis/v8 please use go get github.com/hdt3213/delayqueue@redisv8

Get Started

…

go func consumer() { queue := NewQueue("test", redisCli, cb) queue.StartConsume() }

func producer() { publisher := NewPublisher("test", redisCli) publisher.SendDelayMsg(strconv.Itoa(i), 0) }


## Intercept/Delete Messages

```go
msg, err := queue.SendScheduleMsgV2(strconv.Itoa(i), time.Now().Add(time.Second))
if err != nil {
	panic(err)
}
result, err := queue.TryIntercept(msg)
if err != nil {
	panic(err)
}
if result.Intercepted {
	println("interception success!")
} else {
	println("interception failed, message has been consumed!")
}

SendScheduleMsgV2 and SendDelayMsgV2 return a structure which contains message tracking information.Then passing it to TryIntercept to try to intercept the consumption of the message.

If the message is pending or waiting to consume the interception will succeed.If the message has been consumed or is awaiting retry, the interception will fail, but TryIntercept will prevent subsequent retries.

TryIntercept returns a InterceptResult, which Intercepted field indicates whether it is successful.

Options

Consume Function

go
func (q *DelayQueue)WithCallback(callback CallbackFunc) *DelayQueue

WithCallback set callback for queue to receives and consumes messages callback returns true to confirm successfully consumed, false to re-deliver this message.

If there is no callback set, StartConsume will panic

go
queue := NewQueue("test", redisCli)
queue.WithCallback(func(payload string) bool {
	return true
})

Logger

go
func (q *DelayQueue)WithLogger(logger Logger) *DelayQueue

WithLogger customizes logger for queue. Logger should implemented the following interface:

go
type Logger interface {
	Printf(format string, v ...interface{})
}

Concurrent

go
func (q *DelayQueue)WithConcurrent(c uint) *DelayQueue

WithConcurrent sets the number of concurrent consumers

Polling Interval

go
func (q *DelayQueue)WithFetchInterval(d time.Duration) *DelayQueue

WithFetchInterval customizes the interval at which consumer fetch message from redis

Timeout

go
func (q *DelayQueue)WithMaxConsumeDuration(d time.Duration) *DelayQueue

WithMaxConsumeDuration customizes max consume duration

If no acknowledge received within WithMaxConsumeDuration after message delivery, DelayQueue will try to deliver this message again

Max Processing Limit

go
func (q *DelayQueue)WithFetchLimit(limit uint) *DelayQueue

WithFetchLimit limits the max number of unack (processing) messages

Hash Tag

go
UseHashTagKey()

UseHashTagKey add hashtags to redis keys to ensure all keys of this queue are allocated in the same hash slot.

If you are using Codis/AliyunRedisCluster/TencentCloudRedisCluster, you should add this option to NewQueue: NewQueue("test", redisCli, cb, UseHashTagKey()). This Option cannot be changed after DelayQueue has been created.

WARNING! CHANGING(add or remove) this option will cause DelayQueue failing to read existed data in redis

see more: https://redis.io/docs/reference/cluster-spec/#hash-tags

Default Retry Count

go
WithDefaultRetryCount(count uint)  *DelayQueue

WithDefaultRetryCount customizes the max number of retry, it effects of messages in this queue

use WithRetryCount during DelayQueue.SendScheduleMsg or DelayQueue.SendDelayMsg to specific retry count of particular message

go
queue.SendDelayMsg(msg, time.Hour, delayqueue.WithRetryCount(3))

Nack Redelivery Delay

go
WithNackRedeliveryDelay(d time.Duration) *DelayQueue

WithNackRedeliveryDelay customizes the interval between redelivery and nack (callback returns false) But if consumption exceeded deadline, the message will be redelivered immediately.

Script Preload

go
(q *DelayQueue) WithScriptPreload(flag bool) *DelayQueue

WithScriptPreload(true) makes DelayQueue preload scripts and call them using EvalSha to reduce communication costs. WithScriptPreload(false) makes DelayQueue run scripts by Eval commnand. Using preload and EvalSha by Default

Customize Prefix

go
queue := delayqueue.NewQueue("example", redisCli, callback, UseCustomPrefix("MyPrefix"))

All keys of delayqueue has a smae prefix, dp by default. If you want to modify the prefix, you could use UseCustomPrefix.

Monitoring

We provides Monitor to monitor the running status.

go
monitor := delayqueue.NewMonitor("example", redisCli)

Monitor.ListenEvent can register a listener that can receive all internal events, so you can use it to implement customized data reporting and metrics.

The monitor can receive events from all workers, even if they are running on another server.

go
type EventListener interface {
	OnEvent(*Event)
}

// returns: close function, error
func (m *Monitor) ListenEvent(listener EventListener) (func(), error) 

The definition of event could be found in events.go.

Besides, We provide a demo that uses EventListener to monitor the production and consumption amount per minute.

The complete demo code can be found in example/monitor.

go
type MyProfiler struct {
	List  []*Metrics
	Start int64
}

func (p *MyProfiler) OnEvent(event *delayqueue.Event) {
	sinceUptime := event.Timestamp - p.Start
	upMinutes := sinceUptime / 60
	if len(p.List) <= int(upMinutes) {
		p.List = append(p.List, &Metrics{})
	}
	current := p.List[upMinutes]
	switch event.Code {
	case delayqueue.NewMessageEvent:
		current.ProduceCount += event.MsgCount
	case delayqueue.DeliveredEvent:
		current.DeliverCount += event.MsgCount
	case delayqueue.AckEvent:
		current.ConsumeCount += event.MsgCount
	case delayqueue.RetryEvent:
		current.RetryCount += event.MsgCount
	case delayqueue.FinalFailedEvent:
		current.FailCount += event.MsgCount
	}
}

func main() {
	queue := delayqueue.NewQueue("example", redisCli, func(payload string) bool {
		return true
	})
	start := time.Now()
	// IMPORTANT: EnableReport must be called so monitor can do its work
	queue.EnableReport() 

	// setup monitor
	monitor := delayqueue.NewMonitor("example", redisCli)
	listener := &MyProfiler{
		Start: start.Unix(),
	}
	monitor.ListenEvent(listener)

	// print metrics every minute
	tick := time.Tick(time.Minute)
	go func() {
		for range tick {
			minutes := len(listener.List)-1
			fmt.Printf("%d: %#v", minutes, listener.List[minutes])
		}
	}()
}

…

go
func (m *Monitor) GetPendingCount() (int64, error) 

GetPendingCount returns the number of which delivery time has not arrived.

go
func (m *Monitor) GetReadyCount() (int64, error)

GetReadyCount returns the number of messages which have arrived delivery time but but have not been delivered yet

go
func (m *Monitor) GetProcessingCount() (int64, error)

GetProcessingCount returns the number of messages which are being processed

Cluster

If you are using Redis Cluster, please use NewQueueOnCluster

go
redisCli := redis.NewClusterClient(&redis.ClusterOptions{
    Addrs: []string{
        "127.0.0.1:7000",
        "127.0.0.1:7001",
        "127.0.0.1:7002",
    },
})
callback := func(s string) bool {
    return true
}
queue := NewQueueOnCluster("test", redisCli, callback)

If you are using transparent clusters, such as codis, twemproxy, or the redis of cluster architecture on aliyun, tencentcloud, just use NewQueue and enable hash tag

go
redisCli := redis.NewClient(&redis.Options{
    Addr: "127.0.0.1:6379",
})
callback := func(s string) bool {
    return true
}
queue := delayqueue.NewQueue("example", redisCli, callback, UseHashTagKey())

More Details

Here is the complete flowchart:

  • pending: A sorted set of messages pending for delivery. member is message id, score is delivery unix timestamp.
  • ready: A list of messages ready to deliver. Workers fetch messages from here.
  • unack: A sorted set of messages waiting for ack (successfully consumed confirmation) which means the messages here is being processing. member is message id, score is the unix timestamp of processing deadline.
  • retry: A list of messages which processing exceeded deadline and waits for retry
  • garbage: A list of messages reaching max retry count and waits for cleaning

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Godelay-queuedelayed-jobgolangmessage-queue

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库