Wednesday, March 13, 2013

Why we need Memcached? MongoDB is there

After long pause I have came up with a question “Why we need Memcached?”. Before going into actual discussion, I want share some basics about Memcahed server and Why we need Memcached server
 
Traditional web application uses MySQL as their back-end and serve the information to client. This client serve will be more complicated on increasing count and their request frequency. Later these requests become few thousands and millions per second. This will leads to web application to react very slow and even dead response to some request. To avoid, Danga Interactive initially designed a system which is keeping data in primary storage instead of secondary. See wiki http://en.wikipedia.org/wiki/Memcached.
 
How memcached works?
Memcahed is purposely designed to boost up web application performance. This performance improvement achieved by reducing database operations like read,write. In more details, we can say, this is reduce hard-drive works and time. So Memcahed system intended to store same frequent database result in memory(RAM). In end, if the client request the same response, the system fetch information from this temp RAM memory, instead of go to database and lookup the respective information.
 
In advance, Memcached is prepared to store more all the information which are need more time to generate. These information may be any thing database query result or any data generated from complex calculation. Those data stored in Memcached system as KEY-VALUE pair. KEY is one which is generated as unique hash and VALUE is the actual information which will be used later. Since these are kept in RAM, the system performance in consistently increase. And One important thing is, this RAM consuming may leads to memory leak. To avoid Memcached have certain process like limit maximum memory consume; overwrite existing information. Memcache is also refresh RAM for further process by deleting least recently used information from its memory.


MongoDB is there.
MongoDB is a kind of database which is working on the basis of No-SQL structure also referred as Non-Relational Database. Non-Relation exactly means that there is no relation between the data. Simply we can say that we don't have JOINs in this database. MongoDB is hold data as BSON(Binary JSON). That is, JSON is converted into binary and stored in database. As we know JSON is combination of KEY-VALUE set. Unlike Memcached, Data are kept in persistent storage and hierarchy of KEY-VALUE combination is happen any depth. And more powerful user can query the database at any level of depth.
On comparing speed of MongoDB with Memcached server, MongoDB simply produce amazing result. I have researched following articles, 

1. http://blog.adlegant.com/performance-benchmark-mysql-vs-mongodb-vs-memcached/

2. http://permalink.gmane.org/gmane.comp.db.mongodb.user/1591

From these two article the only bad thing I read about Mongodb is, MongoDB gives postive response on writing data before actually the data set into storage. But no problem, MongoDB provide option to customize this. Since I am talking about alternate for Memcached, fastest reading is more important rather than writing. I have found some technical stack comparison between these two in http://vschart.com/compare/memcached/vs/mongodb. And some more properties of MongoDB like TTL(Time to live) and more space for KEY as well as VALUE compare to Memcached are more impressive. Finally, I feel, there is nothing wrong in using MongoDB an alternative for Memcached server.
 
And my last word, this is my personal opinion only. On setting up Mongodb as an alternate of Memcache, please do research against your requirement and then continue.