DOCUMENT
STORE
GRAPH
DBMS
SEARCH
ENGINE
TIME SERIES
DBMS
MEMCACHED IS A PLAIN KEY VALUE STORE WHICH ONLY SUPPORTS
BINARY STRINGS AS VALUE.
Redis also includes Lua scripts debugger in v.3.2 which makes writing complex scripts
easier and helps in boosting performance.
You can also vertically scale Redis when you need more memory or compute with-
out downtime if you have a HA setup or are using Redis Cluster.
Memcached supports both the TCP and UDP communication protocols.
Data is sent to the Memcached server in two forms:
Memcached doesn’t provide a mechanism to distribute data across nodes (shard-
ing). So, horizontal scalability is as simple as adding more nodes - the problem of
partitioning your data into different shards will have to be done on the applica-
tion/client level. There are some open source tools that can help you with this.
Memcached does not support any server-side scripting.
VS
Redis stands for REmote DIctionary Server, created in 2009 by
Salvatore Sanlippo. Memcached, on the other hand, was created
in 2003 by Brad Fitzpatrick.
Both Redis and Memcached are
NoSQL in-memory
data structures
Used to speed up
applications
Support sub-
millisecond latency
Open
source
Written
in C
To start off, Redis is much more comprehensively documented than Memcached.
This makes it easier to learn, administer and use.
Redis is primarily a key value store. While keys are binary strings, the
advantage with Redis is that the value is not limited to only binary strings. They
can be a variety of data structures which enable storing of complex objects
and provide a rich set of operations over them. Redis also
provides for extensibility via Redis modules that provide for additional data
structures and features not available within the core feature set, such as:
COMPARING REDIS VS. MEMCACHED
DOCUMENTATION
DATABASE MODEL
As mentioned above, Redis offers multiple data structure types allowing it to
be extremely flexible to use, including Strings, Hashes, Lists, Sets, Sorted Sets,
Bitmaps, Bitfields, HyperLogLog, Geospatial indexes, and Streams.
Memcached only supports plain binary strings which are great for read-only data. If you
don't need all the bells and whistles of Redis, Memcached is a simpler database to use.
“I’ m a Plan Text String!”
001110101011001110010101010
{23334}{112345569}{766538}{665455}
{ A: “foo”,B: “bar”, C: “baz”
[ A>B>C>D>E]
{A,B,C,D,E,}
{A:0.1,B:0.3,C:100,D:1337}
{A:(51.5,0.12),b:(32.1,34.7)}
00110101 11001110 10101010
{id1=time1.seq1(A:”xyz”,B:”cdf”),id2=time2.seq2(D:”abc”)}
DATA STRUCTURES
KEY
Strings
Bitmaps
Bit field
Hashes
Lists
Sets
Sorted Sets
Geospatial
Indexes
Hyperloglogs
Streams
Image source: https://redislabs.com/redis-enterprise/data-structures/
The greater popularity of a database results in a larger community of users,
more discussions and user-generated tutorials, and more help and support
through third-party tools such as DBaaS platforms and analysis tools to help
optimize your deployments.
Redis is the 8th most popular database in the world as of March 2021,
according to DB-Engines because of its simplicity, rich data structures, and
great documentation. Memcached is ranked as the 28th most popular data-
base. When Redis and Memcached are ranked with respect to key-value da-
tabase models, Redis stands in 1st and Memcached comes 4th. Though, if
you are looking only for an open-source key-value database, or one that
can be deployed on-premise, Memcached comes in 2nd as both Amazon
DynamoDB and Microsoft Azure Cosmos DB are both commercial databases
that can only be deployed in the cloud.
Source: https://db-engines.com/en/ranking/key-value+store
DATABASE RANK & POPULARITY
RANK
Mar
2021
1.
2.
3.
4.
5.
1.
2.
3.
4.
6.
1.
2.
3.
4.
5.
Redis
Amazon DynamoDB
Microsoft Azure Cosmos DB
Memcached
Hazelcast
154.15
68.89
32.97
25.97
8.77
+1.58
-0.25
+0.75
-0.26
+0.67
+6.57
+6.38
+0.78
+0.60
+0.02
Key-value,Multi-model
Multi-model
Multi-model
Key-value
Key-value,Multi-model
Feb
2021
Mar
2020
Mar
2021
Feb
2021
Mar
2020
DBMS Database Model
SCORE
Redis and Memcached both follow client-server architecture. Clients populate
the data in the server in the form of key-value.
Redis is single threaded where on the other hand Memcached has a multithreaded
architecture. Memcached scales better on a system with more cores that can handle
more operations if compute capacity is scaled. However, more than one Redis
instance can be initiated on the same system to utilize additional cores.
ARCHITECTURE
Redis, being a multi-model database, can be used with any type of data model.
In Redis, it is easy to write code as it simplifies complex tasks and has advanced
data structures not limited to simple string values. For instance, if your applica-
tion stores data in sets and you want to keep track of sets in a list you can do
this easily in Redis. A similar task on Memcached is not possible. But there are
other ways to do the same tasks that will require more lines of code.
Memcached, on the other hand, only stores plain string values. So, the
application is left to deal with the data structure complexity.
EASE OF USE
Redis supports partitioning of data across multiple node instances. Current users
of Redis leverage different techniques like range partitioning, hash partitioning,
and consistent hashing for data partitioning. In Redis, data partitioning can be
implemented in three different ways:
Memcached also supports data partitioning across multiple nodes, and consistent
hashing is a recommended approach to ensure the traffic load is evenly
distributed.
Client-side partitioning
Proxy-assisted partitioning (example: twemproxy)
Server-side partitioning with query routing within the cluster nodes
DATA PARTITIONING
Redis supports almost all of the most used programming languages, from
high-level to low-level languages. Memcached supports fewer languages
compared to Redis, but still supports all the popular ones:
SUPPORTED LANGUAGES
MEMCACHED REDIS
.Net
C
C++
ColdFusion
Erlang
Java
Lisp
Lua
OCaml
Perl
PHP
Python
Ruby
C
C#
C++
Clojure
Crystal
D
Dart
Elixir
Erlang
Fancy
Go
Haskell
Haxe
Java
JavaScript
(Node.js)
Lisp
Lua
MatLab
Objective-C
OCaml
Pascal
Perl
PHP
Prolog
Pure Data
Python
R
Rebol
Ruby
Rust
Scala
Scheme
Smalltalk
Swift
Tcl
Visual Basic
VS
Redis “transactions” are executed with the three below guarantees:
TRANSACTIONS
Transactions are serialized and executed sequentially
Either all of the commands, or none, are processed (atomic transactions)
Optimistic locking offers an extra guarantee using check-and-set
Redis makes sure that all or only one command from one client machine is executed
at once. All the commands in the transactions are executed when the “EXEC” com-
mand is called to ensure the atomicity.
Memcached, on the other hand, does not provide transaction management.
Redis offers a simple leader-follower (master-slave) replication that creates
exact copies of the master instances, with these features:
You can also leverage the high availability features, Redis Sentinels or Redis
Cluster, for advanced failover protection.
Native Memcached does not support replication, but you can use Repcached,
a free open source patch, to achieve high availability with multi master and
asynchronous replication, and support for all Memcached commands.
DATA PARTITIONING
Master send data commands to the slave as long as they’re connected.
If the connection breaks, the slave will follow partial resynchronization only
copying the data that was missed during disconnection.
If partial resynchronization isn’t possible, it will try full resynchronization.
Snapshots are read-only views of your database as it was at a certain point in
time. Redis supports snapshots, and by default, saves snapshots of the dataset
on disk in a binary file called dump.rdb. You can manually call a snapshot,
customize the frequency, or change the threshold for running the operation.
RDB stands for “Redis Database Backup”. It is a compact, point-in-time snapshot of
the database at a specific time. It takes up less space, maximizes Redis performance,
and is good for disaster recovery.
AOF stands for “Append Only File”. AOF keeps track of all commands executed, and
in a disastrous situation, it re-execute the commands to get the data back. This
method takes more space, and is not a very durable method of snapshotting.
Memcached on the other hand does not support on disk persistence.
SNAPSHOTS/PERSISTENCE
Here are the two persistence options Redis supports:
RDB persistence
AOF persistence
EVAL
Sentinels
Redis Server
EVALSHA
Lua is the embedded scripting language for Redis, available as of v.2.6, which lets
you perform operations inside Redis to simplify code and increase performance.
The two main functions used to evaluate scripts using the Lua interpreter are:
SERVER-SIDE SCRIPTS
When Lua script is being executed all other requests are blocked as shown below:
REDIS
TM
LUA SCRIPTS DIAGRAM
1
2
C1
C2
C3
C1
C2
C3
C1
C2
C3
S1
S2
S3
3
C1
C2
C3
S1
S2
S3
BLOCKED
Clients
Client C1 sends a Lua Script request
Script run in progress,serverblocks all
other requests.
Script ends (OK or EPR)
Everyone is unblocked, so everyone
see either no changes from the script or
all changes from the script.
2021 COMPARISON
{C}
</>
x
/
{ }
There are two techniques to horizontally scale your Redis database:
SCALABILITY
Adding shards in Redis Clusters
Adding nodes to a Redis HA (master/replica) setup
Text lines:
Send commands and receive responses from the server.
Unstructured data: Receive or send value information for a given
key, and data is returned in the same format provided.
Memcached uses LRU algorithm to evict data when space is required. It first searches for the
already expired data to delete if expired data is not available the LRU algorithm is used.
The “publish” is used to push data to the clients.
Memcached does not support publish and subscribe messaging.
NOEVICTION
In “noeviction” an error is returned when
memory reaches it bound.
ALLKEYS-LRU
Lru stands for “least recent used”. This
policy removes the least recently used
data.
ALLKEYS-LFU
Lfu stands for “least frequently
used”. This policy removes the
least frequently used data.
VOLATILE-LRU
Volatile data is with expiration
data set. This policy removes
the least recently used volatile
data.
VOLATILE-LFU
Volatile data is with expiration
data set. This policy removes
the least frequently used volatile
data.
ALLKEYS-RANDOM
This policy removes the data
randomly.
VOLATILE-RANDOM
This policy removes the volatile
data randomly
VOLATILE-TTL
“Ttl” stands for time to live. This policy
removes the data that have the shortest
time to live.
Redis uses TCP as a network protocol and does not support UDP.
COMMUNICATION PROTOCOL
Redis supports different types of eviction policies. Let's take a look at some.
SUPPORTED CACHE EVICTION POLICIES
1
2
3
4
5
6
7
8
The client uses:
The server uses:
Subscribe and unsubscribe are used to get messages from a specific channel.
Redis supports Pub/Sub messaging (publish and subscribe). There are three
commands that are used for this purpose.
PUBLISH & SUBSCRIBE MESSAGING
Subscribe
Unsubscribe
Publish
Redis supports Kafka-like streams with v.5.0 or higher using a new data structure
“Redis Streams”. It has the concept of consumer groups, like Apache Kafka, that
lets client applications consume messages in a distributed fashion, making it easy
to scale and create highly available systems.
Memcached does not offer native support for Streams, but there are open source
library tools like Kafcache for stream processing at low latency.
STREAMS SUPPORT
Redis has a data structure called Geospatial indexes that stores the longitude and
latitude data of a location. You can perform different operations on the geospatial
data, like calculating the distance between two points or finding nearby places.
Memcached does not have any special data structures to handle geospatial data.
GEOSPATIAL SUPPORT
A performance comparison between in-memory key-value data stores is more of
an intellectual exercise than of any practical importance - unless you are deploy-
ing systems at such scale that this becomes interesting as a cost-saving measure.
This is because such stores are IO bound and usually the network latency might
play a bigger role in application perceived latency than the database latency.
A more practical performance aspect is storage efficiency - how much data can be
packed in the same amount of memory. Even here, the internal data structures used
by Redis vary based on the data size. So any discussion on performance between
these databases should be taken with a pinch of salt. In a comparison from a 2016
research paper, they measured the databases performance in terms of:
While writing data, as you can see, in the table below Memcached shows exceptional speed
even after the number of records moves up to million.
PERFORMANCE
DATABASES VERSIONS USED
IN THE PAPER:
The time taken to complete operations.
How efficiently they use memory during operations.
THE CALCULATED TIME TO WRITE KEY-VALUE PAIRS (MS)
MEMORY USAGES OF IN-MEMORY DATABASES FOR WRITE OPERATION (MB)
MEMORY USAGES OF IN-MEMORY DATABASES FOR DELETE OPERATION (MB)
WRITE OPERATION
Reading data stays almost consistent in Redis even for a million records but in Memcached
as the number of records go up the time also increases a little bit.
READ OPERATION
While discussing memory usage, Redis is always the best as you can see in the results
As you can see Redis is better than Memcached.
Managed services for Memcached are far less available, but is still supported through
Amazon Elasticache.
MEMORY USE
The greater popularity and community for Redis has also driven the need for managed ser-
vices, hosting and support. Popular managed database providers for Redis™* include
ScaleGrid, Redis Labs, AWS Elasticache, Azure Cache and DigitalOcean. This page provides
a great comparison of the top providers for Redis™. Redis also has extensive in-ternal
reporting tools like cache hits, memory usage, ops, and even a slow query logged.
MANAGED SERVICES/SUPPORT
Redis has native TLS support starting Redis 6.0. Earlier versions of Redis recommended the
use of stunnel for providing TLS support.
Memcached 1.5.13 and above versions support authentication and encryption via TLS. This
feature is still in experimental stages.
TRANSPORT LAYER SECURITY (TLS) SUPPORT
Upto Redis 5.x, Redis only supported a simple password based authentication. This password
was stored in plaintext on the server. Redis in version 6.0 onwards supports a fully featured
ACL.
Memcached version 1.4.3 and greater has SASL support. Previously Memcached had no au-
thentication layer.
AUTHENTICATION
Databases versions used in the paper:
Redis
Memcached
Database
Redis
Memcached
1,000
34
23
10,000
214
100
100,000
1,666
276
1,000,000
14.638
2,813
3.0.7
1.4.14
Database Version
Number of records
Database
Redis
Memcached
1,000
2.5
5.3
10,000
3.8
27.2
100,000
4.3
211
1,000,000
62.7
264.9
Number of records
Database
Redis
Memcached
1,000
0
2.2
10,000
0
2.1
100,000
0
2.2
1,000,000
0
2.2
Number of records
THE ELAPSED TIME TO READ VALUE CORRESPONDING
TO A GIVEN KEY PER DATABASE (MS)
Database
Redis
Memcached
1,000
8
9
10,000
6
14
100,000
8
14
1,000,000
8
30
Number of records
*Redis is a trademark of Redis Labs Ltd. Any rights therein are reserved to Redis Labs Ltd. Any use by ScaleGrid is for
referential purposes only and does not indicate any sponsorship, endorsement or affiliation between Redis and ScaleGrid.
评论0
最新资源