Drupal - What is the proper way to set up Drupal 7 on load balanced servers so that CSS and JS files are in sync?

Drupal's core aggregation does not work on a cluster because it creates the aggregates during the page rendering. Once the page is stored into the cache another server of the pool will read from the cache so it will not render the page again meaning that the CSS or JS aggregation will not be done locally.

To solve this problem, I created a NFS share between the servers just for the "css" and "js" folders. It was running ok for a while but under heavy load Drupal was sometimes generating empty files (2 servers creating the file at the same time). So even keeping in sync the servers is clearly not a solution.

But just as Beebee said, you can use the Advanced CSS/JS Aggregation module. This one is clever enough to be able to rebuild an aggregate just from the URL, because it stores in the database a mapping of the files that are part of the aggregate.

Advagg is also able to handle groups of aggregated files. This can reduce your bandwidth usage if your visitors navigate across different pages because the common files can be merged into a separate aggregate and the delta files merged into another aggregate.


Use csync2 / lsyncd to keep uploaded files (or all files) in sync.

An example configuration for Drupal can be found at http://www.severalnines.com/blog/scaling-drupal-multiple-servers-galera-cluster-mysql

Also, ensure your load balancer is configured for sticky sessions. In this way, if a user uploads a file or causes Drupal to generate new css/js files, those files will be served from the same server. Otherwise the files may be uploaded/generated to one server on a POST, but then another server may serve the response page before it gets the files.

We have used the above and had excellent production stability across three web-servers. We have about 100k+ nodes, ~40 content types, ~60 content providers, ~80k hits per day.

Originally I created a module which stalled Drupal page loads until file synchronizations completed, but we found it wasn't necessary.

Tags:

7