# Rails vs. J2EE: Sharing state in memory?

**URL:** <https://www.ruby-forum.com/t/rails-vs-j2ee-sharing-state-in-memory/50349>\
**Category:** Rails\
**Created:** [December 15, 2005, 3:57pm UTC](https://www.ruby-forum.com/t/rails-vs-j2ee-sharing-state-in-memory/50349 "2005-12-15T15:57:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mariano\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@Mariano\_K](https://www.ruby-forum.com/u/Mariano_K)\
**Post date:** [December 15, 2005, 3:57pm UTC](https://www.ruby-forum.com/t/rails-vs-j2ee-sharing-state-in-memory/50349/1 "2005-12-15T15:57:07Z")

</div>

Hi,

I am from a Java background and pretty new to Ruby and Rails.

What I am wondering is how I would shared state accross requests and  
users  
without involving IO, i.e. use memory.

My current understanding is that for each request a new process ist  
spawn  
and therefore it gets its own memory. So no sharing can take place  
between  
requests?

Do I understand this right? For those who know Java, let me rephrase  
that  
to prevent missunderstandings?

Is there any equivalent to using the Application Context in J2EE, a  
Singleton or static class members to store objects?

Cheers,  
Mariano

---

<div class="post-metadata">

**Author:** ![Mariano\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@Mariano\_K](https://www.ruby-forum.com/u/Mariano_K)\
**Post date:** [December 15, 2005, 4:36pm UTC](https://www.ruby-forum.com/t/rails-vs-j2ee-sharing-state-in-memory/50349/2 "2005-12-15T16:36:20Z")

</div>

On 12/15/05, Mariano K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Do I understand this right? For those who know Java, let me rephrase that  
> to prevent missunderstandings?
> 
> Is there any equivalent to using the Application Context in J2EE, a  
> Singleton or static class members to store objects?

There’s no direct equivalent to that in the CGI model. This isn’t  
really a Ruby or Rails thing so much as a ‘Container’ vs. ‘CGI’  
difference.  
That being said, you can use DRb (Distributed Ruby) as a ‘workalike’  
to the application scope you’re used to in Java. DRb does involve  
inter-process communication, but it doesn’t require any ‘IO’ if it is  
running on the same system as your application.

Objects in application scope can make it hard to scale your app. The  
Rails opinion on this is that some fast IO (session stores cached by  
your OS or database engine) is a fair trade for making scaling  
‘boring’. Still, sometimes they are a good answer to a problem, and  
DRb can help you out.

–Wilson.

---

<div class="post-metadata">

**Author:** ![Mariano\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@Mariano\_K](https://www.ruby-forum.com/u/Mariano_K)\
**Post date:** [December 15, 2005, 6:12pm UTC](https://www.ruby-forum.com/t/rails-vs-j2ee-sharing-state-in-memory/50349/3 "2005-12-15T18:12:50Z")

</div>

On Thu, 15 Dec 2005, Mariano K. wrote:

> Hi,
> 
> I am from a Java background and pretty new to Ruby and Rails.
> 
> What I am wondering is how I would shared state accross requests and users  
> without involving IO, i.e. use memory.

there are several ways, not all related to rails:

```
 - use the database. a good db running on a box with battery backed 

```

ram is  
not goint to hit disk that often. even when it does, caching is  
the job  
of the database.

```
 - use shared memory. using systemvipc in ruby is trivial.

 - use memory. if you have fastcgi configured for session affinity 

```

you can  
ensure that all request travel to the same fastcgi process which,  
just in  
case you didn’t know, are persistent. therefore a simple  
variable can  
persist data between requests.

```
 - use drb. this elimiates the need for session affinity and makes 

```

your data  
available even after the web servers has stopped and restarted.  
if drb  
uses a unix domain socket the data exchange is very fast, adding  
only a  
small memcopy in kernel space.

> My current understanding is that for each request a new process ist spawn  
> and therefore it gets its own memory. So no sharing can take place between  
> requests?

nope. fastcgi is persistent - [http://fastcgi.com](http://fastcgi.com).

> Do I understand this right? For those who know Java, let me rephrase that  
> to prevent missunderstandings?
> 
> Is there any equivalent to using the Application Context in J2EE, a  
> Singleton or static class members to store objects?

the simple answer is that, under fastcgi, ALL classes have this  
ability.

kind regards.

-a

---

<div class="post-metadata">

**Author:** ![Mariano\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@Mariano\_K](https://www.ruby-forum.com/u/Mariano_K)\
**Post date:** [December 15, 2005, 6:18pm UTC](https://www.ruby-forum.com/t/rails-vs-j2ee-sharing-state-in-memory/50349/4 "2005-12-15T18:18:56Z")

</div>

let me add, that for example mysql supports memory-based tables that  
are completely stored in ram.

it might sound strange that there is no such thing as application state  
aka ServletContext. However, it’s part of the paradigm commonly  
referred to as ‘share nothing’

i found this as an intro:  
[http://www.zefhemel.com/archives/2004/09/01/the-share-nothing-architecture](http://www.zefhemel.com/archives/2004/09/01/the-share-nothing-architecture)

this strategy is really great as it increases the changes of running  
your application in cluster with no sweat at all. it will scale very  
well.

if database and webserver are on the same machine the performance loss  
for using mem-tables is not dramatic.

> > I am from a Java background and pretty new to Ruby and Rails.  
> > the job  
> > persist data between requests.  
> > spawn  
> > Is there any equivalent to using the Application Context in J2EE, a  
> > Singleton or static class members to store objects?
> 
> the simple answer is that, under fastcgi, ALL classes have this  
> ability.
> 
> kind regards.
> 
> ## -a

===============================================================================

> | ara [dot] t [dot] howard [at] noaa [dot] gov  
> | all happiness comes from the desire for others to be happy. all misery  
> | comes from the desire for oneself to be happy.  
> | – bodhicaryavatara

===============================================================================

---

<div class="post-metadata">

**Author:** ![Mariano\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@Mariano\_K](https://www.ruby-forum.com/u/Mariano_K)\
**Post date:** [December 15, 2005, 6:30pm UTC](https://www.ruby-forum.com/t/rails-vs-j2ee-sharing-state-in-memory/50349/5 "2005-12-15T18:30:59Z")

</div>

this might also be interesting for you:

> **[memcached - a distributed memory object caching system](http://memcached.org/)**
>
> memcached

> **[dev(E)iate](https://deveiate.org/)**
>
> The personal website of Michael Granger.

kind of similar to memory based tables, just noticeably faster…

> aka ServletContext. However, it’s part of the paradigm commonly  
> for using mem-tables is not dramatic.
> 
> > > 
> > 
> > ```
> > not goint to hit disk that often. even when it does, caching is
> > 
> > ```
> 
> variable
> 
> > ```
> > small memcopy in kernel space.
> > 
> > ```
> > 
> > ## that -a

===============================================================================

> > | ara [dot] t [dot] howard [at] noaa [dot] gov  
> > | all happiness comes from the desire for others to be happy. all  
> > misery  
> > | comes from the desire for oneself to be happy.  
> > | – bodhicaryavatara

===============================================================================

---

<div class="post-metadata">

**Author:** ![Mariano\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@Mariano\_K](https://www.ruby-forum.com/u/Mariano_K)\
**Post date:** [December 15, 2005, 7:16pm UTC](https://www.ruby-forum.com/t/rails-vs-j2ee-sharing-state-in-memory/50349/6 "2005-12-15T19:16:15Z")

</div>

On 15-dec-2005, at 15:56, Mariano K. wrote:

> Do I understand this right? For those who know Java, let me  
> rephrase that to prevent missunderstandings?
> 
> Is there any equivalent to using the Application Context in J2EE,  
> a Singleton or static class members to store objects?

Sort of (if I correctly decipher what you want).

You can stuff things in a class variable. It’s nasty, it’s dirty and  
it might not work in devlepment environment and you will need to  
assure session affinity. And the class vars will be for everyone, not  
divided by session. And you should be very careful with it because  
you might start leaking memory 🙂 but this is much more immediate  
than other options, which are:

a) Drb (this is what I’d look into)  
b) memcache  
c) using another frontend framework (Wee or Borges) which is more  
“stateful” for parts of the app.
