Brightbox
  • Home
  • Pricing & Sign up
  • Why Brightbox?
  • Products & Services
  • FAQs
  • About
  • Blog
  • Wiki
  • Forums
  • Contact
Blog RSS feed
POSTED BY

Caius Durling

caius@brightbox.co.uk

twitter_banner

Recent Posts

  • Ruby Enterprise 1.8.7-2010.02 Packages for Ubuntu Hardy & Lucid
  • Ubuntu 10.04 LTS "Lucid" now available
  • New: Automatically add SSH keys to new Brightboxes
  • Ruby Enterprise 1.8.7-2010.01 Packages for Ubuntu Hardy & Lucid
  • Ubuntu Lucid beta box offer

Clearing out Rails Sessions 19 Nov 09

As I’m sure you’re aware, Rails can be told to use ActiveRecord (and hence the MySQL database) to store session data. (New Rails apps use the cookie store by default—See the rails sessions guide for more info on both.)

However, this session data is never deleted, which means your session table continues to grow and grow forevermore. Your old sessions are left stored in the database, and although the table is indexed to help with finding sessions, it will eventually fill the disk up.

At this point you might be thinking the solution is obvious, just empty the sessions table after so long and let it fill up again. Rails even provides a rake task that does this for us, rake db:sessions:clear. The problem with taking this approach is that any active sessions get lost as well, which could be people with items in their baskets, currently logged in users, etc.

There is another solution, which is to only delete sessions that we consider to no longer be active. The updated_at column in the sessions table has an index on it, and thus looks designed for this type of query to be run. On the Brightbox control panel, we’ve decided this is session data that hasn’t been updated for over 24 hours. (A side effect of this is customers that haven’t visited the control panel in the last 24 hours are logged out. We’ve decided this is ok, but YMMV of course.)

We use the following rake task that clears out sessions 24 hours or older, which is run via cron at 3am every morning. You can change the threshold by editing "1 DAY" in the query, see the MySQL DATE_ADD() docs for valid values.

desc "Clear expired sessions"
task :clear_expired_sessions => :environment do
    sql = 'DELETE FROM sessions WHERE updated_at < DATE_SUB(NOW(), INTERVAL 1 DAY);'
    ActiveRecord::Base.connection.execute(sql)
end

Posted 19 November 2009 by Caius Durling


Recent blog posts

  • Ruby Enterprise 1.8.7-2010.02 Packages for Ubuntu Hardy & Lucid
    about 1 month ago
  • Ubuntu 10.04 LTS “Lucid” now available
    about 1 month ago
  • New: Automatically add SSH keys to new Brightboxes
    about 1 month ago
  • Ruby Enterprise 1.8.7-2010.01 Packages for Ubuntu Hardy & Lucid
    2 months ago
  • Ubuntu Lucid beta box offer
    3 months ago
  • Load Balancing with Stomp and ActiveMessaging
    3 months ago

Join our email list

Flickr (more...)

RSS feeds

Blog feed

Flickr feed

Recent Wiki updates

System Status feed




Brightbox Partners and Vendors

Wiki | Forums | Terms & Conditions | Privacy | Site Map

Copyright © 2010 Brightbox Systems Ltd. All rights reserved