Next.js Discord

Discord Forum

Next Image overflow my VPS storage in one day

Unanswered
California pilchard posted this in #help-forum
Open in Discord
California pilchardOP
Okay I host my NextJS (v15) app on a hetzner with Coolify. But my VPS storage is completely overflow with cached image with next/image. How do we prevent caching when there is no more space because when hitting VPS storage limit, my docker containers just crash and everything stop working. How are we supposed to manage optimized image in NextJS ? Thx a lot !

3 Replies

Bengal
Great question I guess. I think there is no native way as far as is know. But maybe mount a local file path or block storage to the folder where the images are located. Keep that big enough and have a simple cron job deleting older images again.

We also started to use Cloudflare in front and use Cloudflare images. That way we get way faster transcoding and have images being served from the CDN
@Bengal Great question I guess. I think there is no native way as far as is know. But maybe mount a local file path or block storage to the folder where the images are located. Keep that big enough and have a simple cron job deleting older images again. We also started to use Cloudflare in front and use Cloudflare images. That way we get way faster transcoding and have images being served from the CDN
California pilchardOP
I wanna prevent using external paid service. But I have the same problem with fetch-cache for some of my server side queries. Maybe using something like Redis instead of internal nextjs cache can be a solution ? I believe Redis allows limiting the cache size and automatically removes old keys when there’s no more space, no ?
Bengal
Comparing Redis as cache handler instead of local cache:
you have bit more complexity setting it up, as you need redis hosted on your VPS (or somewhere else). Directly on the VPS with docker is basically very simple.
Redis needs to be configured to have keyspace notifications set to Ex and key eviction to volatile-lfu and max memory set. In this way, the storage will never run full and old keys with a revalidate time set will be deleted automatically. Redis does also remove keys when the storage runs full, deleting first the items that are least used.
The local cache is not cleaning up. So your storage can fill up quickly depending on your setup. A temp fix could be to have it being cleaned up using cron, but yeah, can be a bit difficult to respect the actual revalidate times.