Which NoSQL and why NoSQL?
There are number of NoSQL option to make web application scalable and optimize performance. Having studied a number of them, I short listed redis and memcache. Both of them are key-Value prototype of NoSQL classification. However, I have choosen Redis due to following two reasons.
- MemcachedDB only can store values as string whereas Redis supports multiple data structures such as strings, hashes, lists, sets and sorted sets.
- Values up to 512MB in size (memcached limited to 1MB per key)
Now we come how to integrate a NoSQL with your web application. Generally web application communicates with a relational Database like MySQL, MSSQL or Oracle to store and retrieve information. When you have a large and popular web application, dealing with huge amount of data the Relational Database becomes quickly a performance bottleneck. In order to improve the performance of the web application, we then need in-memory data storage or cache. There NoSQL comes in play. Wikipedia defines as “A NoSQL or Not Only SQL database provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases.” Its hard to find a large organization who haven’t started to use No SQL option. There are 4 types of NoSQL prototypes: Column (ie Cassendra), Document (ie MongoDB), Key-Value (ie Redis, MemcachedDB) and Graph (ie OreintDB). Generally, this is how the Redis will be added to new or existing web application. We will be using redis as primary data cache backed up by MySQL. All data will be stored in MySQL Database and immediately cached using key and value in Redis. When we need to retrieve information, we will retrieve it from Redis using desired key. Hence, it reduces a huge amount of load from MySQL. Redis is extremely fast, serving 110,000-200,000 set and 80,000 – 200,00 get per second depending on low to high server configuration. Hence, information will be retrieved in no time.
Installation and Configuration of Redis
Installation of Redis is pretty straight forward. Details can be found from http://redis.io/download. Still I’m providing the command in a CentOS environment.
wget http://download.redis.io/releases/redis-2.8.9.tar.gz tar xzf redis-2.8.9.tar.gz cd redis-2.8.9 make
Now the server has started , we will set a key with value and later retrieve it.
src/redis-cli redis> set foo bar OK redis> get foo "bar"
Integrating with PHP
When integration with any language, redis has a good list of client for almost all languages. When it comes to PHP, it has 5 php client listed in their website (http://redis.io/clients). Studying all of the PHP client, I opted for predis as it in active development and probably most mature.
In my staging environment, I have XAMPP installed and I use GIT as source code repository. If you are using GIT as your source code repository you can follow the below procedures:
cd /opt/lampp/htdocs mkdir redis_php cd redis_php/ git clone git://github.com/nrk/predis.git emacs test.php