Load balancing my ass

I was planning to write about two different topics this night. Instead I passed the whole time to tweak/reprogram the throttle.module of Drupal (/me flexes his non-existent PHP muscles). If you visited the site in these hours you probably saw all sort of crazy stuff, from php errors, to silly quotes, red warnings and so on. It was fun. After the suspension of my account at Canaca I passed a good amount of time to reconsider what I can optimize on the site. So I went again to parse and check the php.ini setting, I removed some stuff from .htaccess and, finally, I enabled the throttle.module in Drupal that remained disabled till today.

By default this module doesn’t do much, despite I dreamt from the descriptions and help files that it could do some magic. Instead after delving some more on its use I discovered that it was worth nothing if not well configured. Concretely it tracks the hits on the site within a minute. Checking this number you can set five different load levels. For example if you set it at “12” it will progress through the levels from twelve to twelve. If the site serves 11 or less pages in a minute you are flagged at level 0, if the page served in a minute go from 12 to 23 you rise at level 1 and so on till you reach the last “emergency” level at 5 which equals to 60 or more pages served in a minute.

But what happens when the site moves between these levels? Nothing by default. This is why I felt kind of deluded when I discovered that it didn’t manage the load concretely by distributing or slowing down the SQL-queries or enable on its own some sort of magic cache. Instead it does nothing at all, it just writes a message on the logs simply stating that you moved from a level to another. So I researched more the function and I discovered that it basically works like a switch. On and off (with the next version of Drupal the levels will go and the module will really have just two states). What matters is the first and the last level only, all the levels in between are just there for the administrator of the site to have an idea of how the load is progressing. When the fifith level is reached the site actually does something… if you configure it. You can flag for the throttle each of the blocks on the sidebar and each single module that builds the Drupal core. The throttle module checks what is flagged and once the emergency level on the site is reached it simply disables it.

If you carefully study what you need and what you don’t you can already improve the performance dramatically during a sudden heavy load. For example right now my site is set to remove everything but the nodes. If the site is swamped it will disable on its own the search module, the comments, the categories, the users management and everything you see on the sidebar aside the throttle module itself. What is left is just the main page with the articles (“nodes” in drupal).

This is already something really sweet but then I started to play with the potential of the idea to make it more effective. The first idea was to actually show to everyone the throttle block on the sidebar (hidden by default). This because if my site starts to disable its own services I want the user to know. So I added custom messages for each single level, so that who is browsing the site can control its current load and will be warned in the case the site is running in “safe mode” with everything disabled. The next step was to optimize the code of the block, since it already contains on its own an SQL query that is repeated for each page, so an uneeded overhead (the “detection accuracy” is how often the site checks the accesses to decide if it’s time to jump on a different throttle level).

The last idea was the most effective. I decided to link some events to the messages that the module writes on the logs. These messages are executed only when the site moves from one level to another, so I thought that I could use each step, between each level to do something dynamically. The code I added did this trick nicely. Now when you move up one level, the main page will show “x-5” articles. By default my site shows 25 articles on the mian page. As the load and the throttle level go up the site will display less and less articles on a single page, reducing directly the amount of queries on the database. When the fifth level is reached and the site goes in “emergency mode” not only it will disabled the modules and the blocks on the sidebar, but it will also reduce the articles shown on the main page to five.

And this isn’t all. I’ve also set another switch changing when the site hits level five or goes back at 0. When it hits level five the throttle module will enable automatically the use of the “cache”. This is a setting I keep currently disabled. In general a php site builds the pages dynamically for each request. So for each page it displays it performs many SQL queries all at once. The cache bypasses this process, if it is enabled the pages are stored already “built” in the database itself, so requiring just one single query per request. With my setting the cache will be enabled when the site reaches the emergency level and will be disabled only when it will go back down at level 0. This is a major improvement in particular when a specific node of the site is being accessed, since the engine will keep delivering the same pre-built page without recreating it with each access.

In a summary:
– The site will constantly monitor its load and show it by moving between six throttle levels (from zero to five).
– For each following throttle level the posts on the mainpage will decrease, till a minimum of five per page.
– When the highest load level is reached all the blocks on the sidebar aside the throttle will be temporarily disabled.
– Most of the functions of the site will be turned temporarily off (comments, categories, news aggregator, menu, files handling, the possibility to log-in and the search module).
– The caching of the pages will be enabled.
– When the site will step down to the fourth level all the functions will be restored and when it will reach again the level zero the cache will be also disabled.
– Right now the site will go in “emergency mode” when it will reach 60 or more hits in a minute.

I hope this will be enough. Maybe I can consider this as another form of “design”.

Posted in: Uncategorized | Tagged:

Leave a Reply