# Global Data, where can it go?

**URL:** <https://www.ruby-forum.com/t/global-data-where-can-it-go/97094>\
**Category:** Rails\
**Created:** [January 25, 2006, 9:17am UTC](https://www.ruby-forum.com/t/global-data-where-can-it-go/97094 "2006-01-25T09:17:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [January 25, 2006, 9:17am UTC](https://www.ruby-forum.com/t/global-data-where-can-it-go/97094/1 "2006-01-25T09:17:43Z")

</div>

I’m developing a stateful Ruby application that needs to keep data in  
one location where all people accessing the site can modify its  
contents. This data will be read-from/written to often, so I would  
prefer for rails to keep it in memory, rather than a database.

My frist try involved global variables. After discovering that they  
were being “re-initialized” with each new user, I put something like  
this in the application controller:

if not $global  
$global = []  
end

But, then I had the problem with only certain pages being able to access  
them. With the above code in my controller, I could not access it from  
the model, only the view.

Are there good ways to keep dynamic data in a central, location  
available site-wide?

Thanks in advance for the help.

-Dustin

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [January 25, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/global-data-where-can-it-go/97094/2 "2006-01-25T17:57:29Z")

</div>

i just drop global variables (use them very sparingly) in my  
applications.rb controller like

$Admins = [‘user1’,‘user2’]

sort of thing. $Admins is now available to any controller or view

adam

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [January 25, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/global-data-where-can-it-go/97094/3 "2006-01-25T18:12:42Z")

</div>

On Jan 25, 2006, at 12:17 AM, Dustin wrote:

> $global = []
> 
> Thanks in advance for the help.
> 
> -Dustin

Dustin-

```
This looks like a job for distributed ruby or DRb. You can use a drb

```

server that runs on a high port number and published a hash. Then in  
your rails app you can access the hash just like a normal object and  
store and lookup whatever you want in it. This way you can have one  
process that handles your state data that is outside the rails app  
and is persistent between requests.

```
Here is an example:

```

drb\_server.rb:

require ‘drb/drb’

DRb.start\_service(‘druby://127.0.0.1:13500’, Hash.new)  
puts DRb.uri  
DRb.thread.join

```
Now to use this lets just look at an irb example for the client. You

```

can put this code somewhere in your rails app to initialize the Drb  
objects and get the hash. Here’s a simple example:

ezra:~ ez$ irb  
irb(main):001:0\> require ‘drb/drb’  
=\> true

irb(main):002:0\> DRb.start\_service  
=\> #\<DRb::DRbServer:0x206e64 @front=nil, @config=  
{:tcp\_acl=\>nil, :idconv=\>#\<DRb::DRbIdConv:  
0x20e344\>, :argc\_limit=\>256, :verbose=\>false, :load\_limit=\>26214400},  
@grp=#ThreadGroup:0x206d24, @thread=#\<Thread:0x206978 sleep\>,  
@protocol=#\<DRb::DRbTCPSocket:0xd17a8 @acl=nil, @config=  
{:tcp\_acl=\>nil, :idconv=\>#\<DRb::DRbIdConv:  
0x20e344\>, :argc\_limit=\>256, :verbose=\>false, :load\_limit=\>26214400},  
@msg=#\<DRb::DRbMessage:0xd0e84 @argc\_limit=256,  
@load\_limit=26214400\>, @socket=#TCPServer:0xd4570, @uri=“druby://  
ezra.local:61721”\>, @idconv=#DRb::DRbIdConv:0x20e344, @uri=“druby://  
ezra.local:61721”\>

irb(main):003:0\> test = DRbObject.new(nil, ‘druby://127.0.0.1:3500’)  
=\> #\<DRb::DRbObject:0x509f64 @ref=nil, @uri=“druby://127.0.0.1:3500”\>

irb(main):004:0\> test  
=\> #\<DRb::DRbObject:0x509f64 @ref=nil, @uri=“druby://127.0.0.1:3500”\>

irb(main):005:0\> test = {:foo =\>‘bart bart’, :bar =\> ‘niknik’}  
=\> {:foo=\>“bart bart”, :bar=\>“niknik”}

irb(main):006:0\> test[:foo]  
=\> “bart bart”

irb(main):009:0\> test.merge!({:mydata =\> {:title =\> ‘testing  
drb’, :description =\> ‘a distributed hash object to use in my rails  
apps!’}})  
=\> {:foo=\>“bart bart”, :bar=\>“niknik”, :mydata=\>{:title=\>“testing  
drb”, :description=\>“a distributed hash object to use in my rails  
apps!”}}

irb(main):010:0\> test  
=\> {:foo=\>“bart bart”, :bar=\>“niknik”, :mydata=\>{:title=\>“testing  
drb”, :description=\>“a distributed hash object to use in my rails  
apps!”}}

irb(main):011:0\> test[:mydata][:title]  
=\> “testing drb”

Hope that helps you. Stay tuned as I will soon be releasing a plugin  
the publishes remote stand alone ActiveRecord models. These can be  
used for long running processes that take too long to be in the  
request response cycle.

Cheers-  
-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [January 25, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/global-data-where-can-it-go/97094/4 "2006-01-25T18:38:18Z")

</div>

You might want to check out a shared cache solution, like memcached.

[http://www.ruby-forum.com/topic/51948#24014](http://www.ruby-forum.com/topic/51948#24014)  
[http://wiki.rubyonrails.org/rails/pages/MemCached](http://wiki.rubyonrails.org/rails/pages/MemCached)  
[http://wiki.rubyonrails.org/rails/pages/Action+Cache+Update+Plugin](http://wiki.rubyonrails.org/rails/pages/Action+Cache+Update+Plugin)

Dustin wrote:

> I’m developing a stateful Ruby application that needs to keep data in  
> one location where all people accessing the site can modify its  
> contents. This data will be read-from/written to often, so I would  
> prefer for rails to keep it in memory, rather than a database.
> 
> My frist try involved global variables. After discovering that they  
> were being “re-initialized” with each new user, I put something like  
> this in the application controller:
> 
> if not $global  
> $global = []  
> end
> 
> But, then I had the problem with only certain pages being able to access  
> them. With the above code in my controller, I could not access it from  
> the model, only the view.
> 
> Are there good ways to keep dynamic data in a central, location  
> available site-wide?

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [January 25, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/global-data-where-can-it-go/97094/5 "2006-01-25T18:15:45Z")

</div>

> This looks like a job for distributed ruby or DRb. You can use a drb  
> server that runs on a high port number and published a hash. Then in  
> your rails app you can access the hash just like a normal object and  
> store and lookup whatever you want in it. This way you can have one  
> process that handles your state data that is outside the rails app  
> and is persistent between requests.

This is truly interesting. Is any performance hit to be expected?

–

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [January 25, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/global-data-where-can-it-go/97094/6 "2006-01-25T18:39:58Z")

</div>

On Jan 25, 2006, at 9:14 AM, Juan Lupión wrote:

> > This looks like a job for distributed ruby or DRb. You can use a drb  
> > server that runs on a high port number and published a hash. Then in  
> > your rails app you can access the hash just like a normal object and  
> > store and lookup whatever you want in it. This way you can have one  
> > process that handles your state data that is outside the rails app  
> > and is persistent between requests.
> 
> This is truly interesting. Is any performance hit to be expected?

Juan-

```
I wouldn't expect there to be a noticeable performance hit. Rails

```

can use a DRb store for the sessions and it is one of the best  
performing session containers, faster then ActiveRecord session  
store. DRb is a very nice feature of ruby. I highly recommend playing  
with it a bit and seeing what you can do with it.

```
I will probably be releasing my DrbRailsRunner plugin this weekend

```

after some more testing. This will allow you to publish your models  
with a drb server. this can be used to share models between multiple  
rails apps or to just offload a certain model instance with a  
persistent state to drb thereby removing it from the rails request/  
response cycle.

```
This will be awesome for kicking off long running tasks to the drb

```

server. So you could have a page make an ajax request for some action  
that takes a while to run, then this action will hand over the job to  
the drb server. The drb server will start the job and publish a  
percent\_complete variable. In your view that kicked off the long  
running action you have a periodically call remote ajax request that  
checks the percent\_complete every few seconds and updates a progress  
bar. Then when the percent\_complete is 100% done you can ask Drb for  
the results of the long running taks and display them or redirect  
somewhere to display them or whatever. Cool thing is, you can have  
any ruby code you want in the drb server. So it could be generating  
huge PDF’s or running complex queries to get reports or using open-  
uri to fetch the content of multiple other web pages and merging them  
together. The possibilities are endless.

Cheers-  
-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [June 2, 2006, 12:50am UTC](https://www.ruby-forum.com/t/global-data-where-can-it-go/97094/7 "2006-06-02T00:50:44Z")

</div>

On Jun 1, 2006, at 2:31 PM, Andy T. wrote:

> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Hey Andy-

```
Yeah I did release it a week ago. It's called BackgrounDRb. It does

```

a lot more then what I was thinking back in January. You can get info  
about it here:

[http://brainspl.at/articles/2006/05/25/backgroundrb-new-release](http://brainspl.at/articles/2006/05/25/backgroundrb-new-release)  
[http://brainspl.at/articles/2006/05/30/backgroundrb-gets-a-mailing-list](http://brainspl.at/articles/2006/05/30/backgroundrb-gets-a-mailing-list)  
Mailing List: [http://rubyforge.org/mailman/listinfo/backgroundrb-devel](http://rubyforge.org/mailman/listinfo/backgroundrb-devel)  
rubyforge project: [http://rubyforge.org/projects/backgroundrb/](http://rubyforge.org/projects/backgroundrb/)  
README: [http://backgroundrb.rubyforge.org](http://backgroundrb.rubyforge.org)

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [June 1, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/global-data-where-can-it-go/97094/8 "2006-06-01T23:31:23Z")

</div>

Ezra Z. wrote:

> On Jan 25, 2006, at 9:14 AM, Juan Lupiï¿½n wrote:  
> I will probably be releasing my DrbRailsRunner plugin this weekend  
> after some more testing. This will allow you to publish your models  
> with a drb server. this can be used to share models between multiple

Ezra, did you ever release this?
