Erased memories and cracked Varnish

×

Error message

  • Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in _menu_load_objects() (line 569 of /home/flinkco/public_html/includes/menu.inc).
  • Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /home/flinkco/public_html/includes/common.inc).

Websites have no memories. As fancy techies like to say: "HTTP is a stateless protocol". Every click is like no click ever happened before. So recalling on a subsequent page, selections and stuff you submitted many clicks ago, is not part of the protocol. Going beyond it, making the visitor experience more convenient, requires website developers to open their boxes of tricks.

One such trick PHP developers like is the $_SESSION variable. [Note: the dollar-sign is a techy thing and does not imply there's any money in it -- on the contrary]. Dumping "stuff" in the $_SESSION for later recollection during the visitor's browsing experience is a natural thing to do for a programmer. It works for authenticated and anonymous users. It doesn't require a database, at least not explicitly. It's dead-easy. Happy days.

Mostly.

Say, you log out. You are still you. You're at that same keyboard using the same browser on the same website. But as far as Drupal is concerned you've been erased. You are anonymous. You are uid ZERO. You still have a session, or rather you have a new $_SESSION. But it's blank. You and that Drupal website have just met for the first time... again.

But that's not the only drawback of $_SESSION. If your module, your site, is deployed in a configuration that comprises a caching engine, like Varnish, then that seemingly innocuous use of $_SESSION may have just cracked the spine of your visitors' accelerated browsing experience. Why? Because the PHP/Varnish combo sees $_SESSION as an indication that there's something UNIQUE about the page. And unique pages are no good for caching. So even if the page in question is as static as a lamp post, the presence of $_SESSION causes Varnish to balk at pretty much everything on your site and you'll lose all the benefits it can provide.

So what to do?

There are at least three simple solutions and luckily they all come conveniently packaged in one light-weight download: Session Cache API. Going into details would be beyond the scope of this article, but if you're interested, the README and the project page explain all options nicely.

As you log off, just remember: Session Cache API. You may erase the rest....
;-)

References:
o Drupal performance tip #6
o http://stackoverflow.com/questions/5467028/varnish-cache-not-caching-php...

File under: 

Drupal 8: contrib roads, take me home

Read More »

Comments

Good job. Nice article.

Keep up the good work Rik, it's much appreciated