One, storage
Memcached basically only supports simple key-value storage methods.
In addition to Key-value, REDIS supports data structures such as list,set,sorted Set,hash.
Backup of Redis support data, i.e. data backup of Master-slave mode;
Redis Support data Persistence (snapshot, AOF), you can keep the in-memory data on the disk, restart the time can be loaded again for use;
Redis can achieve the master-slave replication, to achieve fault recovery;
Redis's sharding technology makes it easy to distribute data across multiple Redis instances.
Second, data consistency
Memcached's CAS command guarantees data consistency in concurrent scenarios.
Redis transaction support is weaker and can only guarantee that each operation in a transaction executes continuously.
Third, memory management
Memcached how the pre-allocated memory pool is used. Using slab and different sizes of chunk to manage memory, item selects the appropriate chunk storage based on size, the way in which memory pools can eliminate the overhead of requesting/freeing memory, and can reduce the amount of memory fragmentation, but this approach also creates a degree of wasted space, and when memory is still large , the new data may also be removed.
Redis stores data in a way that uses the field to request memory. and rarely use free-list and other ways to optimize memory allocation, there will be a certain degree of memory fragmentation, Redis and according to the storage command parameters, the data with the expiration time is stored separately, and call them temporary data, non-temporary data is never rejected, even if the physical memory is not enough, The result is that swap does not eliminate any non-temporal data (but attempts to cull some of the temporary data), which makes Redis more suitable as a storage rather than a cache.
Four, network IO model
Memcached is a multi-threaded, non-blocking IO multiplexing network model that is divided into the main thread of the listener and the worker sub-thread. The listener thread listens to the network connection, accepts the request, passes the connection descriptor pipe to the worker thread, reads and writes IO, and the network layer uses the Libevent encapsulated event Library. Multithreading model can play a multi-core role, but the introduction of the cache coherency and lock problems, such as memcached most commonly used stats command, the actual memcached all operations to the global variable lock, count, etc., resulting in performance loss.
Redis is a single-threaded IO multiplexing model that encapsulates a simple Aeevent event processing framework that implements Epoll, Kqueue, and select. For only IO operations, single-threaded can maximize the speed advantage, but Redis also provides some simple computing functions, such as sorting, aggregation and so on, for these operations, the single-threaded model will seriously affect the overall throughput, CPU calculation process, the entire IO schedule is blocked.
Five, performance
The performance of both is very high. Since Redis uses only single cores, and memcached can use multicore, on average, Redis has a higher performance than memcached for storing small data on a per-core scale. In more than 100k of data, memcached performance is higher than Redis, although Redis has recently been optimized for the performance of storing big data, but it is slightly inferior to memcached. No matter which one you use, the number of requests processed per second will not be a bottleneck.
See MORE:
Develop a small program example tutorial
HTTP protocol Collation
PHP Security Web Attack
Second-kill system design optimization
MySQL optimization
Common IO models under Linux
Memcached and Redis Comparison