2MySQL & memcacheIt’s common to use MySQL as the backend for storing and retrieving what are essentially key/value pairs.In some scenario, we needs to maintain a bit of state, session data, counters, small lists, and so on.When MySQL isn’t able to keep up with the volume, we often turn to memcached* as a write-thru cache. But there’s a bit of a mis-match at work here.Memcached doesn’t have much in the way of a query language or server-side operations it can perform on the data.MySQL lies at the other end of the spectrum. It has a rich query language and support for all sorts of server-side processing on the dataSo when we combine the two, our app has to know it’s talking to both and deal with coordinating data changes between the cache and the back-end server.*memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
3redisA fast, efficient key/value store that provides a reasonable set of server-size operations aimed at making the most common operations trivial.
5getting into .,redis-0.900]$ ./redis-server >& /tmp/redis.log & 29719redis-0.900]$ telnet localhost 6379TryingConnected to demo.connectivedi.com ( ).Escape character is '^]'.SET mykey 20ConnectivityDirector+OKGET mykey$20GET nonkey$-1EXISTS nonkey:0EXISTS mykey:1QUITConnection closed by foreign host.redis-0.900]$
6datatypes and operations Strings SET, GET, EXISTS etcLists RPUSH, LPUSHSets SADD, SREMOthersKEYS: list all keys matching a patternRANDOMKEYRENAMEEXPIRE: set expiration time (in seconds) of a keyTTL: get the time-to-live of a key
7why redis?readily available client APIs available for a number of languages, including:Ruby Python Perl PHPErlang TCL Lua JavaPerformancethe overriding focus for redis is performance80, ,000 (or more) operations per second on modest modern hardware is likely enough to make a dramatic difference to most applications.much of this performance comes from it’s minimal feature set.carefully chosen features that can be supported by very fast algorithms and, in come cases, even lock-free atomic operations.
8why redis? Durability and Availability Unlike memcached, redis can save its state to disk so that you can shut down a server without losing all the data.SAVE: save data to disk (synchronously)BGSAVE: save data to disk (asynchronously)LASTSAVE: get timestamp of last saved dataFor performance reasons, redis doesn’t log every change to disk as it happens (then it’d be a lot more like a database).Instead, it can be configured to save a copy of the database to disk after a certain amount of time has passed or a certain threshold of changes have occurred in the data.The method of writing to disk requires no locking and has no consistency problems.The server simply forks a child which inherits a copy of the data it can write to disk. That process of writing to disk should take somewhere from a few seconds to maybe a minute or two. Uses Copy-on-write optimization technique.redis also has built-in support for master/slave replication, so it’s possible to scale in read-heavy environmentssimplicity + functionality+ performance = redis
9what next? Central lib: PHP lib : http://code.google.com/p/redis/