Memcached vs Redis
During a recent software purchase evaluation, one software mentioned using Memcached and Redis caches. In this article, I’ll examine the similarities and differences between these two popular caches for easy comprehension and memorization.
1. Memcached with Redis
In general, we would consider using caching to improve performance when processing large amounts of data.
Memcached is a distributed memory caching system designed to be easy to use and ideally suited for caching or session storage. Redis is an in-memory data structure store that provides a rich set of features. It is useful as a cache, database, message broker, and queue.
2. Install
2.1. Install Memcached
Download the latest version of the Memcached package and run make to install it:
$wget http://memcached.org/latest $tar ZXVF - memcached - 1.6.3. Tar. Gz $CD memcached - 1.6.3 $. / configure && make && make test && sudo make installCopy the code
2.2. Install Redis
Similarly, install the latest version of Redis Server:
$wget http://download.redis.io/releases/redis-5.0.8.tar.gz $tar XZF redis - 5.0.8. Tar. Gz $CD redis - 5.0.8 $makeCopy the code
3. Seeking common
3.1. Submillisecond delay
Both Memcached and Redis provide sub-millisecond response times by keeping data in memory.
3.2. Data partitioning
Similarly, both in-memory databases allow distribution of data across multiple nodes.
3.3. Programming language support
Again, both support all the major programming languages, including Java, Python, JavaScript, C, and Ruby.
In addition, both databases in memory have several Java clients. For example, Memcached has Xmemcached and memcached-java-client, and Redis has Jedis, Lettuce, and Redisson.
3.4. Cache cleanup
Memcached allows flushing the cache using the flush_all command. Similarly, Redis allows us to delete everything from the cache by using the commands: FLUSHDB and FLUSHALL.
3.5. Scalability
Both caching solutions provide high scalability for processing big data when the demand grows exponentially.
4. The ground while putting aside differences
4.1. Command line mode
Memcached can Telnet to the server and execute commands:
$Telnet 192.168.8.123 6605 Trying 192.168.8.123 Connected to 192.168.8.123 $stats STAT pid 14868 STAT uptime 175931 STAT time 1220540125 // ...Copy the code
Unlike Memcached, Redis has a dedicated command line interface: Redis CLI, which allows you to execute commands:
$ redis-cli COMMAND
1) 1) "save"
2) (integer) 1
3) 1) "admin"
2) "noscript"
// ...
2) 1) "multi"
2) (integer) 1
3) 1) "noscript"
2) "fast"
// ...
3) 1) "geodist"
2) (integer) -4
3) 1) "readonly"
// ...
// ...Copy the code
Here, we execute the COMMAND to list all the commands provided by Redis.
4.2. Disk I/O dump
Memcached Use third-party tools such as libmemcached Tools or Memcached DD to process disk dumps.
However, Redis provides a highly configurable default mechanism for disk dumps, such as RDB (Redis database files) or AOF (attach files only). This is useful for archiving and recovery.
Using the Redis CLI, we can execute the synchronous SAVE command to get a snapshot of the data in memory:
$ redis-cli SAVE OKCopy the code
Here, the command stores the snapshot in the dump. RDB binary and returns a status determination when done.
However, execution of the asynchronous BGSAVE starts the background process of producing the snapshot:
$ redis-cli BGSAVE
OKCopy the code
In addition, we can check the Unix time of the last successful database snapshot using the LASTSAVE command.
$ redis-cli LASTSAVE
(integer) 1410853592Copy the code
4.3. Data structure
Memcached stores key-value pairs as strings, limiting the size of each value to 1MB. However, Redis also supports other data structures, like Lists, sets, and hashes, which can store up to 512MB in size.
4.4. Copy
Memcached supports replication using third parties such as RepcAched.
Unlike Memcached, Redis gives us the ability to multiply the cluster by replicating the primary storage for better scalability and high availability.
First, we can use REPLICAOF to create a copy of the main Redis server. Next, we execute the PSYNC command on the replicated copy to start the replication from the host.
4.5. The transaction
Memcached does not support transactions, although its operations are atomic.
Redis provides transaction support for executing commands out of the box.
We can start a transaction using the MULTI command. EXEC is then used to execute subsequent commands. Finally, Redis provides the WATCH command to conditionally execute transactions.
4.6. Publish and subscribe messages
Memcached does not provide a publish/subscribe mechanism out of the box.
Redis, on the other hand, uses the PUB/SUB message queue to provide the ability to subscribe to messages.
This is useful when designing applications that require real-time communication, such as chat rooms, social media feeds, and intra-server communication.
Redis comes with specialized commands such as [PUBLISH], [SUBSCRIBE] and [UNSUBSCRIBE] for publishing messages to channels and subscribing and unsubscribing clients to specified channels, respectively.
4.7. Geospatial support
Geospatial support is very useful for implementing location-based functionality of applications. Unlike Memcached, Redis comes with special [commands for managing real-time geospatial data]
For example, the GEODIST command calculates the distance between two geospatial entries. Similarly, the GEORADIUS command returns all entries within the supplied radius.
Alternatively, we can use Spring Data Redis to enable Redis geospatial support in Java applications.
4.8. The architecture
Redis uses a single core and performs better than Memcached at storing small data sets.
Memcached implements a multithreaded architecture by using multiple kernels. Therefore, Memcached performs better than Redis for storing larger data sets.
Another benefit of Memcached’s multithreaded architecture is its high scalability by leveraging multiple computing resources.
Redis can scale horizontally through clusters and is relatively complex to set up and operate. Of course, we can use Java applications to enable the Redis cluster through Jedis or Lettuce.
4.9. The LUA Scripting
Unlike Memcached, you can run [LUA scripts] on Redis. It provides commands like [EVAL*] and *[SCRIPT LOAD], which are useful for executing LUA scripts.
For example, we can execute the EVAL command to evaluate the script:
$ redis-cli eval "return redis.call('set',KEYS[1],'welldone')" 1 website
OKCopy the code
Here, we can use eval to set the value of key=website to welldone.
4.10. Memory utilization
Memcached has a higher memory utilization than Redis when comparing string data structures.
However, when Redis uses the hash structure, it provides higher memory utilization than Memcached.
5. Write at the end
In this article, we explored Memcached and Redis. First, we examine the similarities between the two in-memory databases. We then looked at the differences in the capabilities provided by the two caching solutions.
There are many memory caching solutions available today. Therefore, we should consider the characteristics of the cache engine and match them to our actual use cases.
We can safely conclude that Memcached is a reliable choice for solving simple caching problems. In general, however, Redis outperforms Memcached by providing richer functionality and a wide variety of features that are better for solving complex scenarios. Ding ding ding! Concern about the public number: the big guy outside the pot, join the pot outer ring, irregular welfare output, hi welcome you to join oh private blog address: www.developlee.top