

Building a Real-time SMS Voting App Part 1: Node.js and CouchDB - crabasa
http://www.twilio.com/blog/2012/09/building-a-real-time-sms-voting-app-part-1-node-js-couchdb.html

======
jjacobson
Carter, you are a bad ass for putting your code up on Hacker News.

~~~
crabasa
I'm not sure I'm a badass, but I think it's hard for many developers to
subject their code to public scrutiny, especially when it's with a language
that's outside of their wheelhouse.

I wrote this app to teach myself Node.js, CouchDB, Socket.io, HighCharts.js
and bunch of other things that I really wanted to learn. I hope it's useful
for people new to these things, and definitely appreciate all the feedback I'm
getting from the community.

~~~
viraj_shah
I'm anxiously waiting for the remaining parts. :)

------
praveenhm
Good article on building the node.js stack.

------
benmmurphy
does the vote counter get incremented atomically?

~~~
crabasa
Good question. Incoming votes get added to an in-memory array, which is then
periodically flushed to the DB. During the flush, incoming votes are queued-up
and aren't processed until the flush to the DB is complete.

~~~
shreyansj
Does that mean that when you implement visualization of voting data (in Part
2), there will be a small delay between the votes that have been cast and the
votes that are being visualized? I have no experience with socket.io, so I am
just asking out of curiosity.

~~~
crabasa
<<I really wish I could edit my comments>>

Scratch my comment above, the caching and flushing is introduced in a future
blog post. In this iteration of the code base, an attempt is made to save a
vote to the DB. If it fails, it fails, there is no attempt to retry.

[https://github.com/crabasa/votr-
part1/blob/master/events.js#...](https://github.com/crabasa/votr-
part1/blob/master/events.js#L53)

A comment was made about whether the vote persistence would be considered
ACID. Most likely not! Each vote that comes in fetches a copy of the event
document. If votes came in fast enough, this would create a problem.

However, rather than corrupting the data, I think the votes would simply fails
since CouchDB uses a revision property to prevent this:

[http://wiki.apache.org/couchdb/Document_revisions?action=sho...](http://wiki.apache.org/couchdb/Document_revisions?action=show&redirect=DocumentRevisions)

------
juanbyrge
Wow you are a brilliant hacker. How did you get so goods?

