Difference between Redis and Memcached
Redis is an open source, in-memory data store, used as a database, cache and message broker.
Redis 5.0.3 is the latest stable version. Official Download Link
The in-memory database is a database where it keeps the dataset in RAM. Means that for every interaction with the database, you will only access the Main memory; No DISK operations involved during this interaction. Hence the operation will be faster as it directly access main memory instead of any disk operation.
Redis stands for REmote DIctionary Server.
Following are the features that redis offers to be used as a "real" data store instead of just a cache.
- Powerful data types and powerful commands:
- Persistence to disk, by default.
- Transactions with optimistic locking (WATCH/MULTI/EXEC)
- Pub/sub. Extremely fast.
- Values up to 512MB in size (memcached limited to 1MB per key)
- Lua scripting (as of V2.6)
- Built in clustering (as of V3.0)
- Extremely fast at everything
It is free, open source, high-performance, distributed memory object caching system
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Its simple design promotes quick deployment, ease of development and Its API is available for most popular languages.
|Process||Redis is single threaded||Memcached is multi-threaded|
|Data Persistence||Yes: Redis persist the in-memory data to a file system on regular intervals. During the failure of Redis node, the data can be restored from Redis Data file.||No: If you are using Memcached then data may lost with server restart and rebuilding cache is a costly process.|
|Replication||Yes: Redis supports master-slave replication.||No: Memcached does not supports replication.|
|Data Structures||Redis has got stronger data structures. it supports Strings, Lists, Sets, Hashes, Bitmaps etc.||it supports strings and object. In Memcached, you have to serialize the objects or arrays in order to save them and to read them back you have to un-serialize them.|
|LRU||No: LRU is not supported by Redis. When memory is full, it will look at three random keys and deletes the one which is closest to expiry.||Yes: Memcached supports LRU. When Memory is full, the one you have not used recently (LRU- least recently used) will get deleted.|
|CAS||No: Redis does not support CAS ( Check and Set). It is useful for maintaining cache consistency.||Yes: Memcached supports CAS (Check and Set)|
|Data Length||Redis had a maximum of 2GB key length||Memcached had a maximum of 250 bytes length|
|Pub/Sub Model||Yes - Redis supports Pub/Sub. Redis creates Publish and Subscribe channels where the Redis clients can subscribe to any channel to consume the data and any subscribed client to a channel can publish the data||No - Memcached doesn't supports Pub/Sub|
|Lua||Redis supports Lua scripting||Memcached doesn't supports Lua scripting|
|Transactions||Redis supports Transactions||Memcached doesn't supports Transactions|
|Geospatial support||Redis have Geospatial support||Memcached doesn't have Geospatial support|
You may also like - Database Interview Questions
Following are some useful Redis commands:
- redis-server: runs the Redis Server itself.
- redis-sentinel: runs Redis Sentinel, a tool for monitoring and failover.
- redis-cli: runs a command line interface utility to interact with Redis.
- redis-cli --help: to check all the commands that can be used with redis-cli.
- redis-benchmark: checks Redis performance.
We can use the redis-cli to send a command to it directly. To test that Redis is working properly. If everything is working well, we should get PONG as a reply in the shell.
redis-cli ping //Output: PONG
You may also like - React Js Interview Questions
Written in: ANSI C