時系列単位のTTLが一斉に揮発してThundering Herdを引き起こしていたので、TTLの値にジッターを付加することにした。ランダム要素はいらなくて、ある系列は常に一定の時間間隔でTTL切れとなるようにしつつ、起点となる時刻をずらす。
ある系列のjitterは次のように計算する。文字列を数値に射影する必要があるので、まず文字列に対して一意の数値を出力するfnvハッシュ関数で数値IDを取得し、IDをTTL値で割り算した結果の剰余をジッターとする。揺らぎがあるわけではないのでジッターとは呼ばないかもしれない。
```go
package main
import (
"fmt"
"hash/fnv"
"math"
"unsafe"
)
func getMetricID(metricName string) uint64 {
h := fnv.New64a()
h.Write([]byte(metricName))
return h.Sum64()
}
func main() {
eseconds := 600
name := "cpu.user;hostname=host_1;region=ap-northeast-1"
id := getMetricID(name)
ttl := math.Mod(*(*float64)(unsafe.Pointer(&id)), *(*float64)(unsafe.Pointer(&eseconds)))
fmt.Println(*(*int32)(unsafe.Pointer(&ttl)))
}
```
[https://play.golang.org/p/UU_qtWG7MJn](https://play.golang.org/p/UU_qtWG7MJn)
[https://github.com/yuuki/xtsdb/commit/23abb647444849a02449b461eb4f17ef0d79c22e](https://github.com/yuuki/xtsdb/commit/23abb647444849a02449b461eb4f17ef0d79c22e)
itchynyさんの時間軸シャーディングを思い出した。
[負荷を均すための『時間軸シャーディング』という考え方 - プログラムモグモグ](https://itchyny.hatenablog.com/entry/2017/10/10/100000)