Cache-handler caches in Badger instead of Redis

1. The problem I’m having:

I’m trying to use caddyserver/cache-handler plugin with Redis. My plan is to add caching capability to https://saascustomdomains.com/

But, cache-handler is caching into Badger instead of Redis even though I provided correct Redis URL and port.

I discovered this by seeing error messages mentioning the key/value being too big for Badger. Also, nothing was being written into Redis.

Redis is running in the same container as Caddy.

At my laptop’s localhost, host.docker.internal:3000 in caddy.json, I run my Rails app that just serves a dummy page.

Proof that cache is working for favicon.ico:

Proof that bigger files are trying to be cached in Badger and failing:

2. Error messages and/or full log output:

Example log lines showing cache-handler is trying to use Badger:

{"level":"error","ts":"2024-05-20T13:46:11.337Z","logger":"http.handlers.cache","msg":"Impossible to set the key GET-https-localhost-/assets/application.js-72537f286efd77c87948232a36159f8a1fdc9b9b4eb84abbff80c7b2c4776c35.map into Badger, Value with size 2363173 exceeded 1048576 limit. Value:\n00000000  48 54 54 50 2f 30 2e 30  20 32 30 30 20 4f 4b 0d  |HTTP/0.0 200 OK.|\n00000010  0a 41 6c 74 2d 53 76 63  3a 20 68 33 3d 22 3a 34  |.Alt-Svc: h3=\":4|\n00000020  34 33 22 3b 20 6d 61 3d  32 35 39 32 30 30 30 0d  |43\"; ma=2592000.|\n00000030  0a 43 61 63 68 65 2d 43  6f 6e 74 72 6f 6c 3a 20  |.Cache-Control: |\n00000040  70 75 62 6c 69 63 2c 20  6d 61 78 2d 61 67 65 3d  |public, max-age=|\n00000050  33 31 35 33 36 30 30 30  2c 20 69 6d 6d 75 74 61  |31536000, immuta|\n00000060  62 6c 65 0d 0a 43 6f 6e  74 65 6e 74 2d 54 79 70  |ble..Content-Typ|\n00000070  65 3a 20 61 70 70 6c 69  63 61 74 69 6f 6e 2f 6a  |e: application/j|\n00000080  73 2d 73 6f 75 72 63 65  6d 61 70 2b 6a 73 6f 6e  |s-sourcemap+json|\n00000090  0d 0a 44 61 74 65 3a 20  4d 6f 6e 2c 20 32 30 20  |..Date: Mon, 20 |\n000000a0  4d 61 79 20 32 30 32 34  20 31 33 3a 34 36 3a 31  |May 2024 13:46:1|\n000000b0  31 20 47 4d 54 0d 0a 45  74 61 67 3a 20 22 37 32  |1 GMT..Etag: \"72|\n000000c0  35 33 37 66 32 38 36 65  66 64 37 37 63 38 37 39  |537f286efd77c879|\n000000d0  34 38 32 33 32 61 33 36  31 35 39 66 38 61 31 66  |48232a36159f8a1f|\n000000e0  64 63 39 62 39 62 34 65  62 38 34 61 62 62 66 66  |dc9b9b4eb84abbff|\n000000f0  38 30 63 37 62 32 63 34  37 37 36 63 33 35 22 0d  |80c7b2c4776c35\".|\n00000100  0a 53 65 72 76 65 72 3a  20 43 61 64 64 79 0d 0a  |.Server: Caddy..|\n00000110  53 65 72 76 65 72 2d 54  69 6d 69 6e 67 3a 20 0d  |Server-Timing: .|\n00000120  0a 58 2d 52 65 71 75 65  73 74 2d 49 64 3a 20 62  |.X-Request-Id: b|\n00000130  61 62 37 32 36 33 63 2d  32 34 61 37 2d 34 30 62  |ab7263c-24a7-40b|\n00000140  65 2d 62 31 31 35 2d 37  38 65 31 35 61 34 65 34  |e-b115-78e15a4e4|\n00000150  30 62 30 0d 0a 58 2d 52  75 6e 74 69 6d 65 3a 20  |0b0..X-Runtime: |\n00000160  30 2e 30 33 37 33 39 35  0d 0a 58 2d 53 6f 75 69  |0.037395..X-Soui|\n00000170  6e 2d 53 74 6f 72 65 64  2d 4c 65 6e 67 74 68 3a  |n-Stored-Length:|\n00000180  20 32 33 36 32 37 34 36  0d 0a 58 2d 53 6f 75 69  | 2362746..X-Soui|\n00000190  6e 2d 53 74 6f 72 65 64  2d 54 74 6c 3a 20 38 37  |n-Stored-Ttl: 87|\n000001a0  36 30 68 30 6d 30 73 0d  0a 0d 0a 7b 0a 20 20 22  |60h0m0s....{.  \"|\n000001b0  76 65 72 73 69 6f 6e 22  3a 20 33 2c 0a 20 20 22  |version\": 3,.  \"|\n000001c0  73 6f 75 72 63 65 73 22  3a 20 5b 22 2e 2e 2f 2e  |sources\": [\"../.|\n000001d0  2e 2f 2e 2e 2f 6e 6f 64  65 5f 6d 6f 64 75 6c 65  |./../node_module|\n000001e0  73 2f 40 72 61 69 6c 73  2f 75 6a 73 2f 6c 69 62  |s/@rails/ujs/lib|\n000001f0  2f 61 73 73 65 74 73 2f  63 6f 6d 70 69 6c 65 64  |/assets/compiled|\n00000200  2f 72 61 69 6c 73 2d 75  6a 73 2e 6a 73 22 2c 20  |/rails-ujs.js\", |\n00000210  22 2e 2e 2f 2e 2e 2f 2e  2e 2f 6e 6f 64 65 5f 6d  |\"../../../node_m|\n00000220  6f 64 75 6c 65 73 2f 40  72 61 69 6c 73 2f 61 63  |odules/@rails/ac|\n00000230  74 69 6f 6e 63 61 62 6c  65 2f 73 72 63 2f 61 64  |tioncable/src/ad|\n00000240  61 70 74 65 72 73 2e 6a  73 22 2c 20 22 2e 2e 2f  |apters.js\", \"../|\n00000250  2e 2e 2f 2e 2e 2f 6e 6f  64 65 5f 6d 6f 64 75 6c  |../../node_modul|\n00000260  65 73 2f 40 72 61 69 6c  73 2f 61 63 74 69 6f 6e  |es/@rails/action|\n00000270  63 61 62 6c 65 2f 73 72  63 2f 6c 6f 67 67 65 72  |cable/src/logger|\n00000280  2e 6a 73 22 2c 20 22 2e  2e 2f 2e 2e 2f 2e 2e 2f  |.js\", \"../../../|\n00000290  6e 6f 64 65 5f 6d 6f 64  75 6c 65 73 2f 40 72 61  |node_modules/@ra|\n000002a0  69 6c 73 2f 61 63 74 69  6f 6e 63 61 62 6c 65 2f  |ils/actioncable/|\n000002b0  73 72 63 2f 63 6f 6e 6e  65 63 74 69 6f 6e 5f 6d  |src/connection_m|\n000002c0  6f 6e 69 74 6f 72 2e 6a  73 22 2c 20 22 2e 2e 2f  |onitor.js\", \"../|\n000002d0  2e 2e 2f 2e 2e 2f 6e 6f  64 65 5f 6d 6f 64 75 6c  |../../node_modul|\n000002e0  65 73 2f 40 72 61 69 6c  73 2f 61 63 74 69 6f 6e  |es/@rails/action|\n000002f0  63 61 62 6c 65 2f 73 72  63 2f 69 6e 74 65 72 6e  |cable/src/intern|\n00000300  61 6c 2e 6a 73 22 2c 20  22 2e 2e 2f 2e 2e 2f 2e  |al.js\", \"../../.|\n00000310  2e 2f 6e 6f 64 65 5f 6d  6f 64 75 6c 65 73 2f 40  |./node_modules/@|\n00000320  72 61 69 6c 73 2f 61 63  74 69 6f 6e 63 61 62 6c  |rails/actioncabl|\n00000330  65 2f 73 72 63 2f 63 6f  6e 6e 65 63 74 69 6f 6e  |e/src/connection|\n00000340  2e 6a 73 22 2c 20 22 2e  2e 2f 2e 2e 2f 2e 2e 2f  |.js\", \"../../../|\n00000350  6e 6f 64 65 5f 6d 6f 64  75 6c 65 73 2f 40 72 61  |node_modules/@ra|\n00000360  69 6c 73 2f 61 63 74 69  6f 6e 63 61 62 6c 65 2f  |ils/actioncable/|\n00000370  73 72 63 2f 73 75 62 73  63 72 69 70 74 69 6f 6e  |src/subscription|\n00000380  2e 6a 73 22 2c 20 22 2e  2e 2f 2e 2e 2f 2e 2e 2f  |.js\", \"../../../|\n00000390  6e 6f 64 65 5f 6d 6f 64  75 6c 65 73 2f 40 72 61  |node_modules/@ra|\n000003a0  69 6c 73 2f 61 63 74 69  6f 6e 63 61 62 6c 65 2f  |ils/actioncable/|\n000003b0  73 72 63 2f 73 75 62 73  63 72 69 70 74 69 6f 6e  |src/subscription|\n000003c0  5f 67 75 61 72 61 6e 74  6f 72 2e 6a 73 22 2c 20  |_guarantor.js\", |\n000003d0  22 2e 2e 2f 2e 2e 2f 2e  2e 2f 6e 6f 64 65 5f 6d  |\"../../../node_m|\n000003e0  6f 64 75 6c 65 73 2f 40  72 61 69 6c 73 2f 61 63  |odules/@rails/ac|\n000003f0  74 69 6f 6e 63 61 62 6c  65 2f 73 72 63 2f 73 75  |tioncable/src/su|\n"}
{"level":"error","ts":"2024-05-20T13:46:11.341Z","logger":"http.handlers.cache","msg":"Impossible to set value into Badger, Value with size 2363173 exceeded 1048576 limit. Value:\n00000000  48 54 54 50 2f 30 2e 30  20 32 30 30 20 4f 4b 0d  |HTTP/0.0 200 OK.|\n00000010  0a 41 6c 74 2d 53 76 63  3a 20 68 33 3d 22 3a 34  |.Alt-Svc: h3=\":4|\n00000020  34 33 22 3b 20 6d 61 3d  32 35 39 32 30 30 30 0d  |43\"; ma=2592000.|\n00000030  0a 43 61 63 68 65 2d 43  6f 6e 74 72 6f 6c 3a 20  |.Cache-Control: |\n00000040  70 75 62 6c 69 63 2c 20  6d 61 78 2d 61 67 65 3d  |public, max-age=|\n00000050  33 31 35 33 36 30 30 30  2c 20 69 6d 6d 75 74 61  |31536000, immuta|\n00000060  62 6c 65 0d 0a 43 6f 6e  74 65 6e 74 2d 54 79 70  |ble..Content-Typ|\n00000070  65 3a 20 61 70 70 6c 69  63 61 74 69 6f 6e 2f 6a  |e: application/j|\n00000080  73 2d 73 6f 75 72 63 65  6d 61 70 2b 6a 73 6f 6e  |s-sourcemap+json|\n00000090  0d 0a 44 61 74 65 3a 20  4d 6f 6e 2c 20 32 30 20  |..Date: Mon, 20 |\n000000a0  4d 61 79 20 32 30 32 34  20 31 33 3a 34 36 3a 31  |May 2024 13:46:1|\n000000b0  31 20 47 4d 54 0d 0a 45  74 61 67 3a 20 22 37 32  |1 GMT..Etag: \"72|\n000000c0  35 33 37 66 32 38 36 65  66 64 37 37 63 38 37 39  |537f286efd77c879|\n000000d0  34 38 32 33 32 61 33 36  31 35 39 66 38 61 31 66  |48232a36159f8a1f|\n000000e0  64 63 39 62 39 62 34 65  62 38 34 61 62 62 66 66  |dc9b9b4eb84abbff|\n000000f0  38 30 63 37 62 32 63 34  37 37 36 63 33 35 22 0d  |80c7b2c4776c35\".|\n00000100  0a 53 65 72 76 65 72 3a  20 43 61 64 64 79 0d 0a  |.Server: Caddy..|\n00000110  53 65 72 76 65 72 2d 54  69 6d 69 6e 67 3a 20 0d  |Server-Timing: .|\n00000120  0a 58 2d 52 65 71 75 65  73 74 2d 49 64 3a 20 62  |.X-Request-Id: b|\n00000130  61 62 37 32 36 33 63 2d  32 34 61 37 2d 34 30 62  |ab7263c-24a7-40b|\n00000140  65 2d 62 31 31 35 2d 37  38 65 31 35 61 34 65 34  |e-b115-78e15a4e4|\n00000150  30 62 30 0d 0a 58 2d 52  75 6e 74 69 6d 65 3a 20  |0b0..X-Runtime: |\n00000160  30 2e 30 33 37 33 39 35  0d 0a 58 2d 53 6f 75 69  |0.037395..X-Soui|\n00000170  6e 2d 53 74 6f 72 65 64  2d 4c 65 6e 67 74 68 3a  |n-Stored-Length:|\n00000180  20 32 33 36 32 37 34 36  0d 0a 58 2d 53 6f 75 69  | 2362746..X-Soui|\n00000190  6e 2d 53 74 6f 72 65 64  2d 54 74 6c 3a 20 38 37  |n-Stored-Ttl: 87|\n000001a0  36 30 68 30 6d 30 73 0d  0a 0d 0a 7b 0a 20 20 22  |60h0m0s....{.  \"|\n000001b0  76 65 72 73 69 6f 6e 22  3a 20 33 2c 0a 20 20 22  |version\": 3,.  \"|\n000001c0  73 6f 75 72 63 65 73 22  3a 20 5b 22 2e 2e 2f 2e  |sources\": [\"../.|\n000001d0  2e 2f 2e 2e 2f 6e 6f 64  65 5f 6d 6f 64 75 6c 65  |./../node_module|\n000001e0  73 2f 40 72 61 69 6c 73  2f 75 6a 73 2f 6c 69 62  |s/@rails/ujs/lib|\n000001f0  2f 61 73 73 65 74 73 2f  63 6f 6d 70 69 6c 65 64  |/assets/compiled|\n00000200  2f 72 61 69 6c 73 2d 75  6a 73 2e 6a 73 22 2c 20  |/rails-ujs.js\", |\n00000210  22 2e 2e 2f 2e 2e 2f 2e  2e 2f 6e 6f 64 65 5f 6d  |\"../../../node_m|\n00000220  6f 64 75 6c 65 73 2f 40  72 61 69 6c 73 2f 61 63  |odules/@rails/ac|\n00000230  74 69 6f 6e 63 61 62 6c  65 2f 73 72 63 2f 61 64  |tioncable/src/ad|\n00000240  61 70 74 65 72 73 2e 6a  73 22 2c 20 22 2e 2e 2f  |apters.js\", \"../|\n00000250  2e 2e 2f 2e 2e 2f 6e 6f  64 65 5f 6d 6f 64 75 6c  |../../node_modul|\n00000260  65 73 2f 40 72 61 69 6c  73 2f 61 63 74 69 6f 6e  |es/@rails/action|\n00000270  63 61 62 6c 65 2f 73 72  63 2f 6c 6f 67 67 65 72  |cable/src/logger|\n00000280  2e 6a 73 22 2c 20 22 2e  2e 2f 2e 2e 2f 2e 2e 2f  |.js\", \"../../../|\n00000290  6e 6f 64 65 5f 6d 6f 64  75 6c 65 73 2f 40 72 61  |node_modules/@ra|\n000002a0  69 6c 73 2f 61 63 74 69  6f 6e 63 61 62 6c 65 2f  |ils/actioncable/|\n000002b0  73 72 63 2f 63 6f 6e 6e  65 63 74 69 6f 6e 5f 6d  |src/connection_m|\n000002c0  6f 6e 69 74 6f 72 2e 6a  73 22 2c 20 22 2e 2e 2f  |onitor.js\", \"../|\n000002d0  2e 2e 2f 2e 2e 2f 6e 6f  64 65 5f 6d 6f 64 75 6c  |../../node_modul|\n000002e0  65 73 2f 40 72 61 69 6c  73 2f 61 63 74 69 6f 6e  |es/@rails/action|\n000002f0  63 61 62 6c 65 2f 73 72  63 2f 69 6e 74 65 72 6e  |cable/src/intern|\n00000300  61 6c 2e 6a 73 22 2c 20  22 2e 2e 2f 2e 2e 2f 2e  |al.js\", \"../../.|\n00000310  2e 2f 6e 6f 64 65 5f 6d  6f 64 75 6c 65 73 2f 40  |./node_modules/@|\n00000320  72 61 69 6c 73 2f 61 63  74 69 6f 6e 63 61 62 6c  |rails/actioncabl|\n00000330  65 2f 73 72 63 2f 63 6f  6e 6e 65 63 74 69 6f 6e  |e/src/connection|\n00000340  2e 6a 73 22 2c 20 22 2e  2e 2f 2e 2e 2f 2e 2e 2f  |.js\", \"../../../|\n00000350  6e 6f 64 65 5f 6d 6f 64  75 6c 65 73 2f 40 72 61  |node_modules/@ra|\n00000360  69 6c 73 2f 61 63 74 69  6f 6e 63 61 62 6c 65 2f  |ils/actioncable/|\n00000370  73 72 63 2f 73 75 62 73  63 72 69 70 74 69 6f 6e  |src/subscription|\n00000380  2e 6a 73 22 2c 20 22 2e  2e 2f 2e 2e 2f 2e 2e 2f  |.js\", \"../../../|\n00000390  6e 6f 64 65 5f 6d 6f 64  75 6c 65 73 2f 40 72 61  |node_modules/@ra|\n000003a0  69 6c 73 2f 61 63 74 69  6f 6e 63 61 62 6c 65 2f  |ils/actioncable/|\n000003b0  73 72 63 2f 73 75 62 73  63 72 69 70 74 69 6f 6e  |src/subscription|\n000003c0  5f 67 75 61 72 61 6e 74  6f 72 2e 6a 73 22 2c 20  |_guarantor.js\", |\n000003d0  22 2e 2e 2f 2e 2e 2f 2e  2e 2f 6e 6f 64 65 5f 6d  |\"../../../node_m|\n000003e0  6f 64 75 6c 65 73 2f 40  72 61 69 6c 73 2f 61 63  |odules/@rails/ac|\n000003f0  74 69 6f 6e 63 61 62 6c  65 2f 73 72 63 2f 73 75  |tioncable/src/su|\n"}

Example access log showing cache-handler failing to insert into Badger:

{"level":"info","ts":"2024-05-20T13:46:08.418Z","logger":"http.log.access","msg":"handled request","request":{"remote_ip":"172.17.0.1","remote_port":"64822","client_ip":"172.17.0.1","proto":"HTTP/2.0","method":"GET","host":"localhost","uri":"/assets/application.js-72537f286efd77c87948232a36159f8a1fdc9b9b4eb84abbff80c7b2c4776c35.map","headers":{"Accept-Encoding":["gzip, deflate, br, zstd"],"Accept-Language":["en-US,en;q=0.9,hr;q=0.8,sr;q=0.7,bs;q=0.6"],"Cookie":[],"Priority":["u=4, i"],"Sec-Fetch-Site":["same-origin"],"Sec-Fetch-Mode":["no-cors"],"Sec-Fetch-Dest":["empty"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"localhost"}},"bytes_read":0,"user_id":"","duration":0.925976959,"size":2362746,"status":200,"resp_headers":{"Content-Type":["application/js-sourcemap+json"],"Cache-Control":["public, max-age=31536000, immutable"],"Etag":["\"72537f286efd77c87948232a36159f8a1fdc9b9b4eb84abbff80c7b2c4776c35\""],"Content-Length":["2362746"],"Server":["Caddy"],"Alt-Svc":["h3=\":443\"; ma=2592000"],"X-Request-Id":["1856bec2-2d8c-410d-b3d2-929a8e4618aa"],"X-Runtime":["0.070594"],"Server-Timing":[""],"Cache-Status":["my_cache; fwd=uri-miss; detail=BADGER-INSERTION-ERROR; key=GET-https-localhost-/assets/application.js-72537f286efd77c87948232a36159f8a1fdc9b9b4eb84abbff80c7b2c4776c35.map"]}}

3. Caddy version:

2.7.6

4. How I installed and ran Caddy:

I use this Dockerfile

FROM caddy:2.7.6-builder AS builder

RUN xcaddy build --with github.com/caddyserver/cache-handler

FROM caddy:2.7.6-alpine

# Install Redis
RUN apk update && apk add redis

COPY --from=builder /usr/bin/caddy /usr/bin/caddy

COPY config/caddy-dev.json /etc/caddy/caddy-dev.json

# Start Redis in the background and then start Caddy
CMD redis-server --daemonize yes && caddy run --config /etc/caddy/caddy-dev.json

a. System environment:

Docker

b. Command:

Build

docker build -t tls_reverse_proxy_dev -f Dockerfile.dev .

Run

docker run -d --name tls_reverse_proxy_dev_container  -p 80:80 -p 443:443  tls_reverse_proxy_dev

d. My complete Caddy config:

{
  "apps": {
    "cache": {
      "log_level": "DEBUG",
      "headers": [
        "Content-Type",
        "Authorization"
      ],
      "ttl": "5m",
      "cache_name": "my_cache",
      "redis": {
        "url": "127.0.0.1:6379"
      }
    },
    "http": {
      "servers": {
        "my_server": {
          "listen": [
            ":443"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "localhost"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "cache",
                  "ttl": "5m"
                },
                {
                  "handler": "reverse_proxy",
                  "upstreams": [
                    {
                      "dial": "host.docker.internal:3000"
                    }
                  ],
                  "transport": {
                    "protocol": "http"
                  }
                }
              ],
              "terminal": true
            }
          ],
          "logs": {}
        }
      }
    }
  },
  "admin": {
    "identity": {
      "issuers": [
        {
          "module": "acme",
          "email": "drago@test.com"
        }
      ]
    }
  },
  "logging": {
    "logs": {
      "default": {
        "exclude": [
          "http.log.access"
        ],
        "writer": {
          "output": "file",
          "filename": "/var/log/caddy-dev/caddy.log",
          "roll": true,
          "roll_size_mb": 64,
          "roll_keep": 20
        },
        "encoder": {
          "format": "json",
          "time_format": "iso8601"
        }
      },
      "log0": {
        "writer": {
          "output": "file",
          "filename": "/var/log/caddy-dev/access.log",
          "roll": true,
          "roll_size_mb": 64,
          "roll_keep": 20
        },
        "encoder": {
          "format": "json",
          "time_format": "iso8601"
        },
        "include": [
          "http.log.access"
        ]
      }
    }
  },
  "storage": {
    "module": "file_system",
    "root": "~/caddy-dev/data"
  }
}

Additional info

When I connect to the Docker container, I can see both Caddy and Redis are running correctly:

> docker exec -it tls_reverse_proxy_dev_container sh

> redis-cli ping
PONG

> ps aux
PID   USER     TIME  COMMAND
    1 root      0:04 caddy run --config /etc/caddy/caddy-dev.json
    8 root      0:00 redis-server *:6379
   25 root      0:00 sh
   33 root      0:00 ps aux

I looked at caddyserver/cache-handler GitHub issues but couldn’t find anyone having the same problem.

I’d appreciate any help.

1 Like

Hey @Drago thank you for using the cache-handler module.
The JSON configuration is a little bit tricky. You have to specify the distributed directive (that’s not required in the caddy file because when we parse it we can set the distributed directive to true but during unmarshal we don’t detect that).

Here is the config that is working

        "cache": {
            "log_level": "DEBUG",
            "headers": [
                "Content-Type",
                "Authorization"
            ],
            "ttl": "5m",
            "cache_name": "my_cache",
            "distributed": true,
            "redis": {
                "url": "127.0.0.1:6379"
            }
        }
2 Likes

Oh this is awesome. Thank you so much :pray:

I was reading the cache-handler code today and from this one part I understood the distributed flag is being set because I saw a comparison like (pseudocode):

if Redis.url != null || Redis.configuration != null || ...
  config.distributed = true
end

Thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.