Skip to content

Commit 26bbcc9

Browse files
committed
cache: enable protection against thundering herd problem by default. The protection may be disabled by setting negative grace_time
1 parent daf9de4 commit 26bbcc9

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Chproxy, is an http proxy for [ClickHouse](https://ClickHouse.yandex) database.
1313
- All the limits may be independently set for each input user and for each per-cluster user.
1414
- May delay request execution until it fits per-user limits.
1515
- Per-user [response caching](#caching) may be configured.
16-
- Protection from [thundering herd](https://en.wikipedia.org/wiki/Cache_stampede) problem may be enabled on response caches.
16+
- Response caches have built-in protection against [thundering herd](https://en.wikipedia.org/wiki/Cache_stampede) aka `dogpile effect`.
1717
- Evenly spreads requests among cluster nodes using `least loaded` + `round robin` technique.
1818
- Monitors node health and prevents from sending requests to unhealthy nodes.
1919
- Supports automatic HTTPS certificate issuing and renewal via [Let’s Encrypt](https://letsencrypt.org/).
@@ -353,7 +353,8 @@ caches:
353353
# for the cached response during this grace duration.
354354
# This is known as protection from `thundering herd` problem.
355355
#
356-
# By default `thundering herd` protection is disabled.
356+
# By default `grace_time` is 5s. Negative value disables the protection
357+
# from `thundering herd` problem.
357358
grace_time: 20s
358359

359360
- name: "shortterm"

cache/cache.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ func New(cfg config.Cache) (*Cache, error) {
107107
}
108108

109109
graceTime := cfg.GraceTime
110-
if graceTime <= 0 {
110+
if graceTime == 0 {
111+
// Default grace time.
112+
graceTime = 5*time.Second
113+
}
114+
if graceTime < 0 {
115+
// Disable protection from `dogpile effect`.
111116
graceTime = 0
112117
}
113118

config/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ expire: <duration>
6767
# for the cached response during this grace duration.
6868
# This is known as protection from `thundering herd` problem.
6969
#
70-
# By default `thundering herd` protection is disabled.
70+
# By default `grace_time` is 5s. Negative value disables the protection
71+
# from `thundering herd` problem.
7172
grace_time: <duration>
7273
```
7374

config/config.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,6 @@ func (c *Cache) UnmarshalYAML(unmarshal func(interface{}) error) error {
467467
if c.MaxSize <= 0 {
468468
return fmt.Errorf("`cache.max_size` must be specified for %q", c.Name)
469469
}
470-
if c.GraceTime < 0 {
471-
return fmt.Errorf("`cache.grace_time` cannot be negative for %q", c.Name)
472-
}
473470
return checkOverflow(c.XXX, fmt.Sprintf("cache %q", c.Name))
474471
}
475472

config/testdata/full.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ caches:
3333
# for the cached response during this grace duration.
3434
# This is known as protection from `thundering herd` problem.
3535
#
36-
# By default `thundering herd` protection is disabled.
36+
# By default `grace_time` is 5s. Negative value disables the protection
37+
# from `thundering herd` problem.
3738
grace_time: 20s
3839

3940
- name: "shortterm"

0 commit comments

Comments
 (0)