# Still, big session problem

**URL:** <https://www.ruby-forum.com/t/still-big-session-problem/92131>\
**Category:** Deployment\
**Created:** [April 16, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131 "2007-04-16T15:22:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcontonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jcontonio](https://www.ruby-forum.com/u/jcontonio)\
**Post date:** [April 16, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131/1 "2007-04-16T15:22:44Z")

</div>

I am still having a rather bad sesssion problem on my production  
server.

/tmp is getting loaded with ruby\_sess files and eventually (probably  
takes 2 weeks) crashes out my application. Can someone point me to an  
article that better explains how to deal with file based sessions? Or  
should I just be running a cron job?

---

<div class="post-metadata">

**Author:** ![jcontonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jcontonio](https://www.ruby-forum.com/u/jcontonio)\
**Post date:** [April 16, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131/2 "2007-04-16T18:45:12Z")

</div>

I would highly recommend switching to ActiveRecordStore for keeping  
track of your sessions. You’ll still have to periodically delete  
sessions, but it’s easy to do, and much more efficient (and scalable)  
than file-based sessions.

```
http://wiki.rubyonrails.com/rails/pages/HowtoChangeSessionStore
```

---

<div class="post-metadata">

**Author:** ![jcontonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jcontonio](https://www.ruby-forum.com/u/jcontonio)\
**Post date:** [April 16, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131/3 "2007-04-16T19:48:49Z")

</div>

Depending on the database, ActiveRecordStore is a bad idea. Especially  
when using a database that does not support row level locking (like  
MyISAM tables in MySQL-databases), constant concurrent reads and writes  
are a problem. In my opinion, the place for these large, small and  
potentially unstructures datasets is eighter the file system or memory.  
(thus, memcached or pstore)

If you do not have any problems with session performance, it might be  
wise to just set up a hourly cronjob that sweeps all files older than 15  
minutes (or whatever your session length is set to) inside your  
temp-directory.

---

<div class="post-metadata">

**Author:** ![jcontonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jcontonio](https://www.ruby-forum.com/u/jcontonio)\
**Post date:** [April 16, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131/4 "2007-04-16T20:00:19Z")

</div>

Mike,

Just switched over. Thank you for that suggestion, it was so easy!

I should clear the table data every now and then right?

---

<div class="post-metadata">

**Author:** ![jcontonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jcontonio](https://www.ruby-forum.com/u/jcontonio)\
**Post date:** [April 16, 2007, 8:03pm UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131/5 "2007-04-16T20:03:19Z")

</div>

Hopefully this sparks a debate then because having run into my file  
based session problem, I’d like to know what are the cons of  
ActiveRecordStore. All I know is, 50,000 ruby\_sess files taking down  
my app aren’t good.

On Apr 16, 1:48 pm, Florian G. \<ruby-forum-incom…@andreas-

---

<div class="post-metadata">

**Author:** ![jcontonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jcontonio](https://www.ruby-forum.com/u/jcontonio)\
**Post date:** [April 16, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131/6 "2007-04-16T20:24:14Z")

</div>

jcontonio wrote:

> Hopefully this sparks a debate then because having run into my file  
> based session problem, I’d like to know what are the cons of  
> ActiveRecordStore. All I know is, 50,000 ruby\_sess files taking down  
> my app aren’t good.
> 
> On Apr 16, 1:48 pm, Florian G. \<ruby-forum-incom…@andreas-

Thats a problem that could also be solved by dividing sessions in  
multiple folders (take numbers and search your folders). The main  
Problem of pstore is that it spams one folder - a scenario that files  
systems are not build for. They may allow it, but they can’t handle it  
very well. A more intelligent storage could cope with that. BTW, thats a  
similar idea as in partitioning tables for better write performance on  
tables with table locking. If your system can’t handle your set,  
partition it. (or clean up regularily, as i suggested above)

As for cons against DB based store: as i mentioned before, it’s  
performance is not database-agnostic, while pstore and memcached  
performance pretty much behaves the same on most system. It suffers from  
many concurrent hits on your database while filesystems are better  
designed to handle such tasks. Another thought is that volatile data  
like the session can be kept memcached, as in case of something really  
nasty happening, your sessions will likely be invalid anyway.

On the other hand: these are thoughts are for final optimization. As you  
needed a quick fix, ActiveRecordStore is perhaps not that bad ;).

---

<div class="post-metadata">

**Author:** ![jcontonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jcontonio](https://www.ruby-forum.com/u/jcontonio)\
**Post date:** [April 18, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131/7 "2007-04-18T16:13:21Z")

</div>

Hi,

The Reiser 3 file system was built to handle millions of files in a  
directory effortlessly. And, unlike many other file systems, it is  
space efficient with tiny files like this. If you’re using Linux, then  
this is a good solution for your session storage. (though perhaps more  
difficult to retroactively fit :).

Ext3 now has directory hashing too, but I don’t think it’s enabled by  
default, and I’ve not tested it’s performance.

Looking at the FileStore code though (from actionpark 1.13.3, it cannot  
be trusted to atomically update fragments in a multi-process setup  
(though it seems to be thread safe). This means you could conceivably  
read a fragment as another process is writing to it, and get a broken  
session object or corrupted cache fragment.

If you’re using an OS with an atomic rename system call (like Linux, and  
probably any UNIX) then you could tweak the FileStore code to avoid the  
race. If not, and you still want to use FileStore, then I’d recommend  
using one FileStore object over drb.

If you store sessions in memcached, bear in mind that when its ram pool  
fills it will automatically expire entries that have been used the least  
recently.

This might be more detail than is necessary for the original poster, but  
maybe it’s useful to someone.

John.

On Mon, 2007-04-16 at 20:24 +0200, Florian G. wrote:

> Thats a problem that could also be solved by dividing sessions in  
> multiple folders (take numbers and search your folders). The main  
> Problem of pstore is that it spams one folder - a scenario that files  
> systems are not build for.

> As for cons against DB based store: as i mentioned before, it’s  
> performance is not database-agnostic, while pstore and memcached

## – [http://johnleach.co.uk](http://johnleach.co.uk)

[http://johnleach.co.uk](http://johnleach.co.uk)

---

<div class="post-metadata">

**Author:** ![jcontonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jcontonio](https://www.ruby-forum.com/u/jcontonio)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131/8 "2007-09-25T23:05:25Z")

</div>

On 4/16/07, Rob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just run a cron job.
> 
> Here’s one way:  
> [http://www.realityforge.org/articles/2006/03/01/removing-stale-rails-sessions](http://www.realityforge.org/articles/2006/03/01/removing-stale-rails-sessions)
> 
> - rob

Actually that was for AR sessions - here’s a method for file sessions:

> **[Homepage](https://www.missiondata.com/)**
>
> Mission Data is a full-service digital development firm. We build smart digital products that transform the way companies do business.

- Rob

---

<div class="post-metadata">

**Author:** ![jcontonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jcontonio](https://www.ruby-forum.com/u/jcontonio)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131/9 "2007-09-25T23:03:02Z")

</div>

On 4/16/07, jcontonio [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rob,
> 
> Thanks for the help. Any idea how this works on a FreeBSD system? The  
> find command is kinda different here.
> 
> A for the link on that page to the Ruby way of doing things, it’s  
> rather confusing where to put that.

Sorry, I don’t know with FreeBSD.

- Rob

---

<div class="post-metadata">

**Author:** ![jcontonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jcontonio](https://www.ruby-forum.com/u/jcontonio)\
**Post date:** [October 2, 2007, 11:31am UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131/10 "2007-10-02T11:31:02Z")

</div>

Rails 2.0 features a hashed cookie-based session store by default, which  
is the best performer and is highly recommended as long as you keep your  
session size below 4 KB or so.

–  
Roderick van Domburg  
[http://www.nedforce.com](http://www.nedforce.com)

---

<div class="post-metadata">

**Author:** ![jcontonio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jcontonio](https://www.ruby-forum.com/u/jcontonio)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/still-big-session-problem/92131/11 "2007-09-25T23:05:39Z")

</div>

Rob,

Thanks for the help. Any idea how this works on a FreeBSD system? The  
find command is kinda different here.

A for the link on that page to the Ruby way of doing things, it’s  
rather confusing where to put that.
