Skip to content

Commit

Permalink
cache: enable protection against thundering herd problem by default…
Browse files Browse the repository at this point in the history
…. The protection may be disabled by setting negative `grace_time`
  • Loading branch information
valyala committed Nov 5, 2017
1 parent daf9de4 commit 26bbcc9
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Chproxy, is an http proxy for [ClickHouse](https://ClickHouse.yandex) database.
- All the limits may be independently set for each input user and for each per-cluster user.
- May delay request execution until it fits per-user limits.
- Per-user [response caching](#caching) may be configured.
- Protection from [thundering herd](https://en.wikipedia.org/wiki/Cache_stampede) problem may be enabled on response caches.
- Response caches have built-in protection against [thundering herd](https://en.wikipedia.org/wiki/Cache_stampede) aka `dogpile effect`.
- Evenly spreads requests among cluster nodes using `least loaded` + `round robin` technique.
- Monitors node health and prevents from sending requests to unhealthy nodes.
- Supports automatic HTTPS certificate issuing and renewal via [Let’s Encrypt](https://letsencrypt.org/).
Expand Down Expand Up @@ -353,7 +353,8 @@ caches:
# for the cached response during this grace duration.
# This is known as protection from `thundering herd` problem.
#
# By default `thundering herd` protection is disabled.
# By default `grace_time` is 5s. Negative value disables the protection
# from `thundering herd` problem.
grace_time: 20s

- name: "shortterm"
Expand Down
7 changes: 6 additions & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ func New(cfg config.Cache) (*Cache, error) {
}

graceTime := cfg.GraceTime
if graceTime <= 0 {
if graceTime == 0 {
// Default grace time.
graceTime = 5*time.Second
}
if graceTime < 0 {
// Disable protection from `dogpile effect`.
graceTime = 0
}

Expand Down
3 changes: 2 additions & 1 deletion config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ expire: <duration>
# for the cached response during this grace duration.
# This is known as protection from `thundering herd` problem.
#
# By default `thundering herd` protection is disabled.
# By default `grace_time` is 5s. Negative value disables the protection
# from `thundering herd` problem.
grace_time: <duration>
```
Expand Down
3 changes: 0 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,6 @@ func (c *Cache) UnmarshalYAML(unmarshal func(interface{}) error) error {
if c.MaxSize <= 0 {
return fmt.Errorf("`cache.max_size` must be specified for %q", c.Name)
}
if c.GraceTime < 0 {
return fmt.Errorf("`cache.grace_time` cannot be negative for %q", c.Name)
}
return checkOverflow(c.XXX, fmt.Sprintf("cache %q", c.Name))
}

Expand Down
3 changes: 2 additions & 1 deletion config/testdata/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ caches:
# for the cached response during this grace duration.
# This is known as protection from `thundering herd` problem.
#
# By default `thundering herd` protection is disabled.
# By default `grace_time` is 5s. Negative value disables the protection
# from `thundering herd` problem.
grace_time: 20s

- name: "shortterm"
Expand Down

0 comments on commit 26bbcc9

Please sign in to comment.