# Heavy Database Polling, Optimization question, (for CTI App)

**URL:** <https://www.ruby-forum.com/t/heavy-database-polling-optimization-question-for-cti-app/81987>\
**Category:** Rails\
**Created:** [January 16, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/heavy-database-polling-optimization-question-for-cti-app/81987 "2007-01-16T18:40:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [January 16, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/heavy-database-polling-optimization-question-for-cti-app/81987/1 "2007-01-16T18:40:02Z")

</div>

Hi, I’ve done some work intergrating call center software with ruby on  
rails.

Specifically I’ve built a login page for our call center reps, about  
every 3 seconds it performs an AJAX query to see if there’s any new  
inbound calls.

If there it is, it pop up’s a custom page.

The application runs for 100 users on mongrel rails, (single box) quite  
well.  
It’s got enough spare capacity to run perhaps 3-500 users at the same  
time.

However I want to be able to scale this to around 2-5 thousand reps. (or  
more)

Clearly I’ll need to move to a clustered solution.

Here’s an optimization I’d like to make.  
Rather then polling the database for each individual user every 3  
seconds.

I’d like to have rails it’s self poll more often, but keep an internal  
queue of what needs to be sent out.  
When the AJAX connection hits Rails, it checks an internal object,  
rather then the database for new actitvity.

Or better yet, when there is new activity it somehow pushes it out the  
User, (Avoid the users polling the database).

I suppose I’m looking for some sort of global session variable, that can  
be split across a cluster.  
I’m not really sure how to approach this.

Could anyone offer some hints on how to do this?

Worst case, I can always beef up the database cluster to an extreme,  
that’d work, but it just doesn’t seem like the best solution.

- Thanks!

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [January 16, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/heavy-database-polling-optimization-question-for-cti-app/81987/2 "2007-01-16T20:29:29Z")

</div>

The industry standard way would be “middleware”.

This would be another application, could be rails or not, that your  
clients poll. This “middleware” application then manages, requests to  
your database. This would act as a queue that your clients can poll,  
send requests to, and retrieve requests.

That’s a fairly standard application architecture. A search on  
“middleware architecture” would give some results.

From a rails perspective, I think your rails options would be to run a  
daemon process that answered the client requests and kept an internal  
queue in memory of requests and responses.

You introduce another layer of complexity but scaling is easier.

I remember DHH talking about one of 37 signals products & how they  
scaled the application to meet the demands of thousands of clients  
polling every few seconds.

If I remember rightly, they decided that this was something that rails  
was not suitable for and re-wrote this layer in C.

rgds,

- matt.

David H. wrote:

> Hi, I’ve done some work intergrating call center software with ruby on  
> rails.
> 
> Specifically I’ve built a login page for our call center reps, about  
> every 3 seconds it performs an AJAX query to see if there’s any new  
> inbound calls.
> 
> If there it is, it pop up’s a custom page.
> 
> The application runs for 100 users on mongrel rails, (single box) quite  
> well.  
> It’s got enough spare capacity to run perhaps 3-500 users at the same  
> time.
> 
> However I want to be able to scale this to around 2-5 thousand reps. (or  
> more)
> 
> Clearly I’ll need to move to a clustered solution.
> 
> Here’s an optimization I’d like to make.  
> Rather then polling the database for each individual user every 3  
> seconds.
> 
> I’d like to have rails it’s self poll more often, but keep an internal  
> queue of what needs to be sent out.  
> When the AJAX connection hits Rails, it checks an internal object,  
> rather then the database for new actitvity.
> 
> Or better yet, when there is new activity it somehow pushes it out the  
> User, (Avoid the users polling the database).
> 
> I suppose I’m looking for some sort of global session variable, that can  
> be split across a cluster.  
> I’m not really sure how to approach this.
> 
> Could anyone offer some hints on how to do this?
> 
> Worst case, I can always beef up the database cluster to an extreme,  
> that’d work, but it just doesn’t seem like the best solution.
> 
> - Thanks!

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [January 16, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/heavy-database-polling-optimization-question-for-cti-app/81987/3 "2007-01-16T22:20:38Z")

</div>

David H. wrote:

> Or better yet, when there is new activity it somehow pushes it out the  
> User, (Avoid the users polling the database).

This would be the most scalable. I suggest you look into the various  
Comet implementations to find a solution that will meet your needs.

Eric

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [January 16, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/heavy-database-polling-optimization-question-for-cti-app/81987/4 "2007-01-16T21:51:32Z")

</div>

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

> The application runs for 100 users on mongrel rails, (single box) quite  
> well.  
> It’s got enough spare capacity to run perhaps 3-500 users at the same  
> time.
> 
> However I want to be able to scale this to around 2-5 thousand reps. (or  
> more)
> 
> Clearly I’ll need to move to a clustered solution.

I’m no expert in this stuff, but my next thought would be using  
Memcache. Of course, do any reading you can on the various ajax chat  
services out there. The folks at [lingr.com](http://lingr.com) posted a nice article on  
how Lingr works: [http://blog.lingr.com/2006/08/lingr\_under\_the.html](http://blog.lingr.com/2006/08/lingr_under_the.html)  
(keywords: jetty, comet).

Campfire still uses http polling, but don’t knock it! I can go  
through wifi resets or trips to the coffee shop, and as soon as my  
wifi connects, it continues right where it left off. Not sure this is  
very important for a call center though…

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [January 17, 2007, 1:14am UTC](https://www.ruby-forum.com/t/heavy-database-polling-optimization-question-for-cti-app/81987/5 "2007-01-17T01:14:23Z")

</div>

David H. wrote:

> Wow, a lot of great advise, and places to start looking, thanks all!

Hi there,

Here is some more info on memcached. Memcache is a scalable memory-based  
cache that can be used in a cluster setup. Memcache can really take the  
load off or your database.

> **[Memcached](https://en.wikipedia.org/wiki/Memcached)**
>
> Memcached (pronounced variously mem-cash-dee or mem-cashed) is a general-purpose distributed memory-caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Memcached is free and open-source software, licensed under the Revised BSD license. Memcached runs on Unix-like operating systems (Linux and macOS) and on Microsoft Windows. It depends on t Me...

I would also check out backgroundrb. backgroundrb could run a job every  
second to query the db and put the result in memcached.

Sincerely,  
Jason E.

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [January 16, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/heavy-database-polling-optimization-question-for-cti-app/81987/6 "2007-01-16T22:28:18Z")

</div>

Wow, a lot of great advise, and places to start looking, thanks all!
