What is your motivation for removing these rows at all? Generally, large
database tables that are properly indexed shouldn’t pose much of a
The reasoning behind this is that I want old sessions that are
inactive for 10 minutes to be terminated automagically. It’s a
“security” concern (notice the quotes - intentional ). This is a
workplace-only application for our employees to use. Theoretically,
there’s no reason that a user’s session, that has been idle for 10+
minutes, should have access without logging in again. This will be
seen by customers in our retail stores, and I don’t necessarily want
all this data just “available” in the event one of our sales
associates walks away from a computer, and a customer starts poking
around for no good reason. Obviously that’s not “great” security, but
it’s “Good Enough™”.
Company-wide we only have roughly ~350 employees, so the size of the
DB has never been a major concern for me. I was more curious about
the implications of, as Hassan pointed out, loading the Rails
environment every 10 minutes. I was actually thinking more along the
lines of memory leaks (even a very small one could snowball very fast
on a 10-minute cron job), but Hassan’s point is more…to the point.
This is something I’ve been looking into (but not implemented) and
the problem with this approach seems to be the startup overhead of
loading your Rails environment over and over.
So you might want to look at the various daemon options, i.e. just
run something continuously to periodically expunge old sessions.
Thanks. This is exactly the kind of stuff I was worried about, and I
appreciate you bringing up a point I was probably just too tired to
see Do you have any thoughts/ideas on a daemonized version that I
could run as a rake task? I’ve never written a real daemon before -
in any language - so I’m not sure even where to start. Are there any
gems that would be useful in the process?
Well, what you are both looking at doing is:
Session.delete_all, :conditions => [‘updated_at < ?’, 10.minutes.ago]
That translates into a pretty quick database query, depending on the
number of sessions you anticipate having. WDYT?
Yep, that’s essentially what my rake task does right now. It’s just
that the cost of loading the Rails environment once every 10 minutes
(as a cron job would do) would be a bit too costly, as Hassan points
out. A daemonized version of that is probably the way to go.
Thank you all for your help