Report this

What is the reason for this report?

Varnish cache setup in a multiserver env

Posted on October 16, 2021
George Tudor

By George Tudor

Web Developer

Hello. I’m currently running 3 droplets with a Load Balancer ahead of them, a separate Redis droplet, a Mysql Cluster, an Elasticsearch droplet and a Spaces CDN. I’m runing Laravel btw.

I haven’t tried Varnish Cache before and I’m completely new to it, so should I add it in front of the LB or should I add it on each of those 3 droplets. By my logic, it should be in front of the LB.

I’d appreciate any hint. Thanks



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

Adding Varnish Cache to your Laravel infrastructure can help improve performance and reduce load on your web servers by caching static content and HTTP responses. The placement of Varnish Cache depends on your specific use case and what you want to cache.

Where to place Varnish Cache in your setup:

Here are some considerations for where to place Varnish Cache in your setup:

In front of a load balancer: This is a common setup that allows Varnish to cache responses from your web servers and reduce the load on your backend infrastructure. When a request comes in, Varnish can serve cached content if available. If the content is not in the cache or is expired, Varnish forwards the request to the load balancer and then to your web servers. This can help offload your web servers and reduce response times.

On each web server: This approach is more suited for situations where you want to cache content specific to each server or if you have particular requirements on each server’s caching logic. Each server would have its own cache, and Varnish can help optimize the content for that specific server.

Considerations for placing Varnish Cache:

When deciding where to place Varnish Cache, consider the following:

  • Caching requirements: What types of content do you want to cache? How long do you want to cache it for?
  • Application behavior: How dynamic is your Laravel application? Does it generate a lot of unique content for each user or request?
  • Server resources: How much memory and CPU resources do you have available for Varnish Cache?

Configuring Varnish Cache for Laravel applications:

Varnish Cache may require configuration to handle the dynamic nature of your Laravel application, such as handling cookies and cache purging when content changes.

Here are some tips for configuring Varnish Cache for Laravel applications:

  • Use the Varnish Cache Varnish Configuration Language (VCL) to define caching rules for your Laravel application.
  • Use the Spatie Laravel Varnish package to simplify the configuration process.
  • Use the Laravel cache facade to purge cached content when it becomes outdated.

Best practices for using Varnish Cache:

  • Don’t cache private or sensitive data: Varnish Cache is a public cache, so don’t cache private or sensitive data, such as user sessions.
  • Purge or expire cached content when it becomes outdated: Make sure you have mechanisms in place to purge or expire cached content when it becomes outdated. This is especially important for dynamic content, such as user-generated content or product pages that are frequently updated.
  • Monitor Varnish Cache performance: Monitor Varnish Cache performance to ensure that it is working as expected and not causing any performance problems for your Laravel application.

Conclusion

Ultimately, the best place to place Varnish Cache in your Laravel infrastructure depends on your specific use case and how you want to optimize your infrastructure for caching and load balancing. You might start by deploying Varnish Cache in front of a load balancer and monitoring its performance, making adjustments as needed based on real-world results and application behavior.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.