Made of Everything You're Not

Personal blog of PHP programmer Eric Lamb.
  • Blog
  • Portfolio
« Setting Up A Linux Web Server
E3 2009 »

Sometimes, Poor Design Works

As programmers, we can get obsessed with the small things. I personally have no problem spending hours trying to optimize a section of code, not for performance or to improve user experience or anything like that, but because there's just something funny about the algorithm.

HTTP Cookie

The professional in us tries, sometimes futily, to keep this behavior in check. Sometimes though you just have to do "just" one more thing to really make it perfect.  This, more often than not, balloons up into a weeklong chore to, in the end, dig yourself out of the hole you made with zero progress on the initial problem.

This is the way of the code monkey.

Which is why it's a little surprising to see how HTTP cookies are implemented. According to the official RFC (HTTP State Management Mechanism):

The user agent makes a series of requests on the origin server, after each of which it receives a new cookie. All the cookies have the same Path attribute and (default) domain. Because the request URLs all have /acme as a prefix, and that matches the Path attribute, each request contains all the cookies received so far.

Think about that for a second. On every request the cookies are sent to the server. ON EVERY REQUEST.

Try this; install a FireFox plugin called Live HTTP Headers (if you don't already have it installed). Start the plugin and refresh this page.

Live HTTP Headers

You should notice that every request for ANYTHING (images, js, css) sends the cookies up to the server.

I don't know about you, but I only have one or maybe two, points in an application that can evaluate cookies so to send on every request is a little... wasteful.

I don't know for sure if there's more work on the server side to deal with the cookies (though I would imagine the HTTP server has to do something to make the cookies available to a scripting language) but what I focus on is the bandwidth.

Now, I know it's the 21st century and bandwidth is now fast and cheap. Yay us.

But consider the state of the Internet and bandwidth in 1997 when the specification was first drafted. Most people were using 28.8 and 33.6 baud modems to browse the Internet and 56k was still a year away.

According to the spec:

Practical user agent implementations have limits on the number and size of cookies that they can store. In general, user agents' cookie support should have no fixed limits. They should strive to store as many frequently-used cookies as possible. Furthermore, general-use user agents should provide each of the following minimum capabilities individually, although not necessarily simultaneously:

* at least 300 cookies

* at least 4096 bytes per cookie (as measured by the size of the characters that comprise the cookie non-terminal in the syntax description of the Set-Cookie header)

* at least 20 cookies per unique host or domain name

User agents created for specific purposes or for limited-capacity
devices should provide at least 20 cookies of 4096 bytes, to ensure
that the user can interact with a session-based origin server.

So, unless I'm crazy, the above makes it acceptable for a site to use 20 cookies, each with a maximum size of 4096 bytes (4Kb). This equals out to a possible 81920 (82Kb) bytes of cookie data being sent on EVERY REQUEST.

This basically means that an image that weighed in at a cool 2Kb comes out to need 82Kb of bandwidth to transfer. Doing the math on a full site with say 20 images, 1 HTML file, 1 CSS file and 3 JS files and it really starts to add up. So at a time when bandwidth was scarce cookie usage was a good way to screw up the user experience if you didn't pay attention.

Granted, you'd have to be an idiot to write a program that used 20 cookies with each containing 4kb of data, but we're programmers. Most of us are stupid; some are really stupid.

It's easy to point at cookies and laugh.

The thing we need to keep in mind, though, is that this isn't a that big a problem anymore. Sure, once, it may have been an possible issue. Technology almost solved it though. Faster bandwidth, with faster computers made will eventually make any issue just disappear.

Which is kind of the point; something I try to keep in mind.

Related Posts

The Lesson of Adobe Reader
Portability Is A Good Goal
How to Exploit an Online Poll

Tags: cookies program design

This entry was written by Eric Lamb and posted on June 04th, 2009 at 6:00 am and is filed under Brain Dump, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response below.

3 Comments

  1. eldris says:
    June 04, 2009 at 10:40 am

    Wow, that’s pretty insane. I guess it’s too late to change that architecture now though. It would make far more sense if the server requested cookies and only then are cookies sent, and only the relevant ones.

    I actually don’t think I’ll get over the stupidity of this :D

    In regards to bandwidth, download speeds may be much higher now, but upload is relatively slow. I think mine’s about 350kb/s, so that’s only about 44KB per second I can send these cookies to the server x_x

    Reply | Quote
  2. Eric Lamb says:
    June 05, 2009 at 03:14 pm

    Thanks for pointing out the bandwidth discrepancy Eldris; I have a pretty fast connection so I sometimes forget not everyone lives at my speeds smile

    Reply | Quote
  3. eldris says:
    June 06, 2009 at 05:06 am

    Well, my download is at about 6mb/s. Uploads, in the UK at least, are generally much slower than download speeds. Very annoying when I’m trying to upload a new wordpress build to my site. Still, for the most part I don’t think it’s noticable. I think a lot of people don’t even realise there is a difference, if their connection has that.

    Reply | Quote

Leave a Reply

Click here to cancel reply.

  • Subscribe: Entries | Comments
  • About Me

    Email Email
    Twitter Twitter
    310.739.3322
  • Categories

    • Brain Dump
    • Business
    • Code
    • IT
    • Programming
    • Rant
    • Servers
  • Archives

    • February 2012
    • October 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
  • Advertisement

Copyright © 2008 - 2013 Eric Lamb - All rights reserved