1. Overview
1.概述
In this article, we’ll examine the similarities and differences of two popular in-memory databases, Memcached and Redis.
在本文中,我们将研究两个流行的内存数据库Memcached和Redis的相似之处和区别。
2. Memcached and Redis
2.Memcached和Redis
Often, we think about caching to improve performance while processing a large amount of data.
通常,我们会考虑缓存,以提高处理大量数据时的性能。。
Memcached is a distributed memory caching system designed for ease of use and simplicity and is well-suited as a cache or a session store.
Memcached是一个分布式内存缓存系统,其设计目的是为了便于使用和简化,非常适合作为缓存或会话存储。
Redis is an in-memory data structure store that offers a rich set of features. It is useful as a cache, database, message broker, and queue.
Redis是一个内存数据结构存储,提供了丰富的功能。它可以作为缓存、数据库、消息代理和队列使用。
3. Installation
3.安装
3.1. Installing Memcached
3.1.安装Memcached
We can install the latest Memcached server by downloading the package and running make:
我们可以通过下载软件包并运行make来安装最新的Memcached服务器>。
$ wget http://memcached.org/latest
$ tar -zxvf memcached-1.6.3.tar.gz
$ cd memcached-1.6.3
$ ./configure && make && make test && sudo make install
3.2. Installing Redis
3.2.安装Redis
Similarly, we can install the latest Redis server:
同样地,我们可以安装最新的Redis服务器。
$ 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
$ make
4. Similarities
4.相似性
4.1. Sub-Millisecond Latency
4.1.亚毫秒级的延迟
Both Memcached and Redis offers sub-millisecond response times by keeping data in memory.
Memcached和Redis都通过将数据保存在内存中而提供亚毫秒级的响应时间。
4.2. Data Partitioning
4.2.数据分区
Similarly, both in-memory databases allow distributing data across multiple nodes.
同样地,这两种内存数据库都允许在多个节点上分布数据。
4.3. Programming Languages Support
4.3.支持的编程语言
Likewise, both support all major programming languages including Java, Python, JavaScript, C, and Ruby.
同样地,两者都支持所有主要的编程语言,包括Java、Python、JavaScript、C和Ruby。
Additionally, there are a few Java clients available for both in-memory databases. For instance, Xmemcached and Memcached-java-client are available for Memcached, while Jedis, Lettuce, and Redisson are available for Redis.
此外,还有一些Java客户端可用于两种内存数据库。例如,Xmemcached和Memcached-java-client可用于Memcached,而Jedis、Lettuce和Redisson可用于 Redis。
4.4. Cache Clearing
4.4.缓存清除
Memcached allows clearing the cache using the flush_all command. Similarly, Redis allows us to delete everything from a cache by using commands like FLUSHDB and FLUSHALL.
Memcached允许使用flush_all命令来清除缓存。同样,Redis允许我们通过使用FLUSHDB和FLUSHALL等命令,从缓存中删除一切。
4.5. Scaling
4.5.缩放
Both caching solutions offer high scalability to handle large data when demand grows exponentially.
这两种缓存解决方案都提供了高可扩展性,以便在需求成倍增长时处理大量数据。
5. Differences
5.差异
5.1. Command-Line
5.1.命令行
Memcached allows us to run commands by connecting to the server using telnet:
Memcached允许我们通过使用telnet:连接到服务器来运行命令。
$ telnet 10.2.3.4 5678
Trying 10.2.3.4...
Connected to 10.2.3.4.
$ stats
STAT pid 14868
STAT uptime 175931
STAT time 1220540125
// ...
In contrast to Memcached, Redis comes with a dedicated command-line interface, redis-cli, allowing us to execute commands:
与Memcached相比,Redis带有一个专门的命令行界面,redis-cli,允许我们执行命令。
$ 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"
// ...
// ...
Here, we’ve executed COMMAND to list all the commands provided by Redis.
在这里,我们执行了COMMAND以列出Redis提供的所有命令。
5.2. Disk I/O Dumping
5.2.磁盘I/O转储
Memcached handles disk dumping only with third-party tools like libmemcached-tools or forks like memcached-dd.
Memcached仅通过第三方工具,如libmemcached-tools或memcached-dd等分叉处理磁盘转储。
However, Redis provides highly configurable default mechanisms like RDB (Redis database file) or AOF (Append-only files) for disk dumping. This can be useful for archival and recovery.
然而,Redis为磁盘转储提供了高度可配置的默认机制,如RDB(Redis数据库文件)或AOF(Append-only文件)。这对归档和恢复很有用。
Using redis-cli, we can execute the synchronous SAVE command to take a snapshot of the in-memory data:
使用redis-cli,我们可以执行同步的SAVE命令来获取内存中数据的快照。
$ redis-cli SAVE
OK
Here, the command stores the snapshot in a dump.rdb binary file and returns the status OK when complete.
这里,命令将快照存储在一个dump.rdb二进制文件中,完成后返回状态OK。
However, the execution of the asynchronous BGSAVE starts the background process of taking a snapshot:
然而,执行异步的BGSAVE开始了拍摄快照的后台过程。
$ redis-cli BGSAVE
OK
Additionally, we can use the LASTSAVE command to check the Unix time of the last successful DB snapshot.
此外,我们可以使用LASTSAVE命令来检查最后一次成功的DB快照的Unix时间。
$ redis-cli LASTSAVE
(integer) 1410853592
5.3. Data Structures
5.3.数据结构
Memcached stores key-value pairs as a String and has a 1MB size limit per value. However, Redis also supports other data structures like list, set, and hash, and can store values of up to 512MB in size.
Memcached将键值对存储为一个字符串,每个值的大小限制为1MB。然而,Redis也支持其他数据结构,如list、set和hash,并且可以存储最大512MB的值。
5.4. Replication
5.4.复制
Memcached supports replication with third-party forks like repcached.
Memcached支持与第三方分叉的复制,如repcached。
Unlike Memcached, Redis provides us functionality to multiply clusters by replicating the primary storage for better scalability and high availability.
与Memcached不同,Redis为我们提供了通过复制主存储来增加集群的功能,以获得更好的扩展性和高可用性。
First, we can use the REPLICAOF command to create a replica of the Redis master server. Next, we execute the PSYNC command on the replica to initiate the replication from the master.
首先,我们可以使用REPLICAOF命令来创建一个Redis主服务器的副本。接下来,我们在副本上执行PSYNC命令,以启动从主服务器的复制工作。
5.5. Transactions
5.5.事务
Memcached doesn’t support transactions, although its operations are atomic.
Memcached不支持事务,尽管它的操作是原子性的。
Redis provides out-of-the-box support for transactions to execute commands.
Redis为transactions提供了开箱即用的支持,以执行命令。
We can start the transaction using the MULTI command. Then, we can use the EXEC command for the execution of the following subsequent commands. Finally, Redis provides the WATCH command for the conditional execution of the transaction.
我们可以使用MULTI命令启动事务。然后,我们可以使用EXEC命令来执行以下后续命令。最后,Redis为事务的有条件执行提供了WATCH命令。
5.6. Publish and Subscribe Messaging
5.6.发布和订阅消息
Memcached doesn’t support publish/subscribe messaging out-of-the-box.
Memcached不支持开箱即用的发布/订阅消息。
Redis, on the other hand, provides functionality to publish and subscribe to messages using pub/sub message queues.
另一方面,Redis提供了发布和使用pub/sub 消息队列订阅消息的功能。
This can be useful when designing applications that require real-time communication like chat rooms, social media feeds, and server intercommunication.
在设计需要实时通信的应用程序时,如聊天室、社交媒体馈送和服务器互通,这可能很有用。
Redis comes with dedicated commands like PUBLISH, SUBSCRIBE, and UNSUBSCRIBE to publish a message to the channel, subscribe, and unsubscribe the client to the specified channels, respectively.
Redis带有专门的命令,如PUBLISH,SUBSCRIBE,以及UNSUBSCRIBE,分别向通道发布消息,订阅,和取消客户端对指定通道的订阅。
5.7. Geospatial Support
5.7.地理空间支持
Geospatial support is useful for implementing location-based features for our applications. Unlike Memcached, Redis comes with special commands to manage real-time geospatial data.
地理空间支持对于为我们的应用程序实现基于位置的功能非常有用。与Memcached不同,Redis带有特殊的命令来管理实时地理空间数据。
For instance, the GEODIST command calculates the distance between two geospatial entries. Likewise, the GEORADIUS command returns all the entries within the radius provided.
例如,GEODIST命令计算了两个地理空间条目之间的距离。同样,GEORADIUS命令返回所提供的半径内的所有条目。
Additionally, we can use Spring Data Redis to enable Redis geospatial support in a Java application.
此外,我们可以使用Spring Data Redis来在Java应用程序中启用Redis地理空间支持。
5.8. Architecture
5.8.建筑
Redis uses a single core and shows better performance than Memcached in storing small datasets when measured in terms of cores.
Redis使用单核,在存储小数据集时,以核数计算,显示出比Memcached更好的性能。
Memcached implements a multi-threaded architecture by utilizing multiple cores. Therefore, for storing larger datasets, Memcached can perform better than Redis.
Memcached通过利用多个核心实现了多线程架构。因此,对于存储较大的数据集,Memcached可以比Redis表现更好。
Another benefit of Memcached’s multi-threaded architecture is its high scalability, achieved by utilizing multiple computational resources.
Memcached的多线程架构的另一个好处是它的高可扩展性,通过利用多种计算资源实现。
Redis can scale horizontally via clustering, which is comparatively more complex to set up and operate. Also, we can use Jedis or Lettuce to enable a Redis cluster using a Java application.
Redis可以通过集群进行横向扩展,相对而言,集群的设置和操作更为复杂。另外,我们可以使用Jedis或Lettuce来使用Java应用程序启用Redis集群。
5.9. LUA Scripting
5.9.LUA脚本
In contrast to Memcached, we can execute LUA scripts against Redis. It provides commands like EVAL and SCRIPT LOAD, useful for the execution of the LUA scripts.
与Memcached相比,我们可以针对Redis执行LUA脚本。它提供了诸如EVAL和SCRIPT LOAD等命令,对于执行LUA脚本非常有用。
For instance, we can execute the EVAL command to evaluate the script:
例如,我们可以执行EVAL命令来评估该脚本。
$ redis-cli eval "return redis.call('set',KEYS[1],'baeldung')" 1 website
OK
Here, we’ve set the key website to the value baeldung by evaluating a script.
在这里,我们通过评估一个脚本,将键website设置为baeldung值。
5.10. Memory Usage
5.10.内存使用情况
Memcached has a higher memory utilization rate than Redis when comparing the String data structure.
在比较String数据结构时,Memcached的内存利用率比Redis高。
In spite of that, when Redis uses the hash structure, it provides a higher memory utilization rate than Memcached.
尽管如此,当Redis使用哈希结构时,它提供的内存利用率比Memcached高。
6. Conclusion
6.结语
In this article, we explored Memcached and Redis. First, we looked at the similarities of both in-memory databases. Then, we looked at the differences in the features provided by both caching solutions.
在这篇文章中,我们探讨了Memcached和Redis。首先,我们看了这两个内存数据库的相似之处。然后,我们看了这两个缓存解决方案所提供的功能的差异。
There are many in-memory caching solutions available. Therefore, we should consider the features of a caching engine and match them against our use cases.
目前有许多内存缓存解决方案。因此,我们应该考虑缓存引擎的功能,并将其与我们的用例相匹配。
We can certainly conclude that Memcached is a solid choice for solving simple caching problems. However, generally speaking, Redis outperforms Memcached by offering richer functionality and various features that are promising for complex use-cases.
我们当然可以得出结论,Memcached是解决简单缓存问题的一个可靠选择。然而,一般来说,Redis通过提供更丰富的功能和各种特性而胜过Memcached,这对复杂的用例是有希望的。