SefaUn
SefaUn

Reputation: 1220

Varnish not Caching Requests

I'am developing video-stream api. I need to cache my videos buffer. So I decided to use Varnish to cache my video datas. My backend coded with node.js express. Varnish running for proxy. But responses not caching in Varnish.

My docker-compose.yml

version: '3.9'

services:

  varnish:
    image: varnish:stable
    container_name: varnish
    restart: always
    tmpfs:
      - /var/lib/varnish:exec
    ports:
      - 8080:80
    environment:
      - VARNISH_SIZE=2G
    command: -p default_keep=300
    volumes:
      - ./DevOps/varnish/default.vcl:/etc/varnish/default.vcl
    depends_on:
      - backend

  backend:
    container_name: backend
    build:
      context: ./DevOps/backend
      dockerfile: Dockerfile
    command: bash -c "npm i && rm -rf build && npm run build && nodemon ./build/index.js"
    restart: always
    ports:
      - 5000:5000
    volumes:
      - ./:/home/backend

my default.vcl file

vcl 4.1;

#Node.js - on port 5000
backend default {
  .host = "backend";
  .port = "5000";
  .connect_timeout = 10s;
  .first_byte_timeout = 2s;
  .between_bytes_timeout = 60s;
  .max_connections = 800;
}

sub vcl_deliver {
  if (obj.hits > 0) {
    set resp.http.X-Cache = "HIT";
    set resp.http.X-Cache-Hits = obj.hits;
  } else {
    set resp.http.X-Cache = "MISS";
    set resp.http.X-Cache-Hits = obj.hits;
  }

  return (deliver);
}

sub vcl_recv {
  return(pass);
}

My postman request is http://localhost:8080/video-stream. Request response is below

HTTP1.1 206 Partial Content
X-Powered-By: Express
Accept-Ranges: bytes
Content-Length: 15000000
Content-Type: video/mp4
ETag: W/"e4e1c0-ad5L4Yk20ccS2jYEtId+g4yxJm4"
Date: Wed, 13 Jul 2022 14:50:13 GMT
X-Varnish: 32774
Age: 0
Via: 1.1 varnish (Varnish/6.0)
X-Cache: MISS
X-Cache-Hits: 0
Content-Range: bytes 0-14999999/15000000
Connection: keep-alive

Where is my mistake ???

Upvotes: 0

Views: 900

Answers (1)

Thijs Feryn
Thijs Feryn

Reputation: 4808

You're bypassing the cache in vcl_recv by adding a return(pass) statement.

Remove that statement to revert back to the built-in VCL and your video content will probably be cached.

However, the built-in VCL has some rules to determine whether or not response should be stored in the cache and whether or not a request should be served from the cache.

To learn more about standard behavior and the built-in VCL, please have a look at https://www.varnish-software.com/developers/tutorials/varnish-builtin-vcl/

Upvotes: 1

Related Questions