ElastiCache : Compare Memcached & Redis
Overview
Amazon ElastiCache offers fully managed Redis and Memcached. Seamlessly deploy, run, and scale popular open source compatible in-memory data stores. Build data-intensive apps or improve the performance of your existing apps by retrieving data from high throughput and low latency in-memory data stores. Amazon ElastiCache is a popular choice for Gaming, Ad-Tech, Financial Services, Healthcare, and IoT apps.
Scenario
In this article, we’re going to introduce Memcached and Redis, compare the difference between them, and talk about their use case. We’ll test both Memcached and Redis with the command line, and try them with simple .csv data.
Introduce Memcached
Memcached is an easy-to-use, high-performance, in-memory data store. It offers a mature, scalable, open-source solution for delivering sub-millisecond response times making it useful as a cache or session store. Memcached is a popular choice for powering real-time applications in Web, Mobile Apps, Gaming, Ad-Tech, and E-Commerce. Memcached store data in-memory, if there’s downtime, it will lose all data inside.
Benifit
- Low Latency
- Easy-to-use
- Scalability
- Support multiple programming languages
- Multithreaded architecture
Use case
-
Caching
-
Session store
Start Memcached with telnet
To set up AWS Memcached service, please check Getting Started with Amazon ElastiCache for Memcached.
Store command format
'command name' 'key' 'flags' 'exptime' 'bytes' 'data block'
Description :
command name : set/add/replace
key: the key store data
flags: 32-bit unsigned integer that the server store with the data (provided by the user), and return along with the data when the item is retrieved
expertise: expiration time in seconds, 0 means no delay
bytes: number of bytes in the data block
data block: value
Examples
[ec2-user@ip-10-0-0-173 redis-stable]$ telnet "Your-AWS-Memcached-endpoint" 11211
Trying 10.0.0.174...
Connected to "Your-AWS-Memcached-endpoint".
Escape character is '^]'.
set a 0 0 5 // set key a as "Hello"
Hello
STORED
get a // get key a
VALUE a 0 5
Hello
END
delete a // delete key a
DELETED
get a
END
set b 0 0 2 // set key b as "Hi"
Hi
STORED
replace b 0 0 7 // replace key b with "Goodbye"
Goodbye
STORED
get b // get key b, check if b has been replaced
VALUE b 0 7
Goodbye
END
append b 0 0 2 // add "Hi" behind key b
Hi
STORED
prepend b 0 0 5 // add "Hello" before key b
Hello
STORED
get b // get key b, check what value is in b now
VALUE b 0 14
HelloGoodbyeHi
END
add c 0 0 1 // add key c with value 1
1
STORED
incr c 10 // increase key c by given number 10
11
decr c 5 // decrease key c by given number 5
6
flush_all // clear everything
OK
quit // leave
Other Commands :
stats: Return general-purpose statistics like uptime, version, memory occupation, …
stats slabs: Return slabs statistics, will display slabs statistics (size, memory usage, commands count, …) organized by slabs ID
version: print Memcached version
For more information, please check Memcached telnet command summary.
Since Memcached does not have a client, we just can test some simple commands with it. In this article, we won’t try it in any project, but we will test Redis with .csv file below.
Introduce Redis
Redis is a fast, open-source, in-memory key-value data store for use as a database, cache, message broker, and queue. It delivers sub-millisecond response times enabling millions of requests per second for real-time applications in Gaming, Ad-Tech, Financial Services, Healthcare, and IoT. Redis is a popular choice for caching, session management, real-time analytics, geospatial, chat/messaging, media streaming, and gaming leaderboards. Redis can make a backup, you can load your data again after downtime.
Benifit
- Low Latency
- Easy-to-use
- Scalability
- Support multiple programming languages
- Backups
- Replication
- Pub/Sub
- Lua scripting
- Geospatial support
Use case :
-
Cacheing
-
Chat, messaging and queues
-
Game leaderboard
-
Session store
-
Rich media streaming
-
Geospatial
-
Machine Learning
-
Real-time analytics
Start Redis with Redis-cli
To set up AWS Redis service, please check Getting Started with Amazon ElastiCache for Redis
[ec2-user@ip-10-0-0-173 redis-stable]$ src/redis-cli -c -h "Your-Redis-Endpoint" -p 6379
Your-Redis-Endpoint:6379> ping // Ping if server is healthy
PONG
Your-Redis-Endpoint:6379> set a "Hello" // set key a as hello
OK
Your-Redis-Endpoint:6379> get a // get key a
"Hello"
Your-Redis-Endpoint:6379> DEL a // delete key a
(integer) 1
Your-Redis-Endpoint:6379> get a // check key a if it exist
(nil)
Your-Redis-Endpoint:6379> set b "Goodbye" EX 5 // set key b with 5 seconds expiration time
OK
Your-Redis-Endpoint:6379> get b // get key b
"Goodbye"
Your-Redis-Endpoint:6379> get b // get key b again
"Goodbye"
Your-Redis-Endpoint:6379> get b // get key b after 5 sseconds
(nil)
Your-Redis-Endpoint:6379> HMSET C name "Redis" description "Testing" birthday "Today" // set hash table c
OK
Your-Redis-Endpoint:6379> HGETALL C // get hash table c
1) "name"
2) "Redis"
3) "description"
4) "Testing"
5) "birthday"
6) "Today"
Your-Redis-Endpoint:6379> flushall // clear all keys in redis-cli
OK
Your-Redis-Endpoint:6379> quit // quit Redis client
There are lots of commands we can use in Redis. Confirm your needs and check Redis commands for more information.
Import .csv data into Redis-cli
Use nano testfile.csv
to create testfile.csv.
id, first name, age, gender, nickname, salary
1, John Smith, 40, Male, John, 10000
2, Marco Polo, 43, Male, Marco, 10000
40, John Cena, 40, Male, John, 10002
1999999, Tom Cruse, 50, Male, Tom, 10001
Try to import testfile.csv into Redis-cli.
$ awk -F, 'NR > 1{ print "SET", "\"employee_"$1"\"", "\""$0"\"" }' testfile.csv | redis-cli -c -h "Your-Redis-Endpoint" -p 6379
About awk command:
-F : Input Field Separator Variable
NR : Number of Records Variable
$1 : displays the first field of the current record
$0 : displays detail of all record.
See what is inside Redis-cli.
Your-Redis-Endpoint:6379> keys *
1) "employee_40"
2) "employee_1999999"
3) "employee_1"
4) "employee_2"
Get employee_40 and see what’s inside.
Your-Redis-Endpoint:6379> get employee_40
"40, John Cena, 40, Male, John, 10002"
Now you can see your file imports into Redis-cli but without the attributes. The only value will be stored in Redis-cli.
Similarities
Sub-millisecond latency Both Redis and Memcached support sub-millisecond response times. By storing data in-memory they can read data more quickly than disk-based databases.
Developer ease of use Both Redis and Memcached are syntactically easy to use and require a minimal amount of code to integrate into your application.
Data partitioning Both Redis and Memcached allow you to distribute your data among multiple nodes. This allows you to scale out to better handle more data when demand grows.
Support for a broad set of programming languages Both Redis and Memcached have many open-source clients available for developers. Supported languages include Java, Python, PHP, C, C++, C#, JavaScript, Node.js, Ruby, Go and many others.
Data store Both Memcached and Redis are in-memory data store. They can keep data in RAM, to provide high performance caching.
How to choose between Memcached & Redis
Select Memcached if you have these requirements:
- Simple model.
- Run large nodes with multiple cores or threads.
- Scale-out/in,
- Adding and removing nodes as demand on your system increases and decreases.
- Partition your data across multiple shards.
- Cache objects.
Select Redis if you have these requirements:
- Complex data types, ex: strings, hashes, lists, and sets.
- Sort or rank in-memory data-sets.
- Persistence of your key store.
- Replicate your data to one or more read replicas for read-intensive applications.
- Automatic failover.
- Pub/Sub.
- Backup and restore capabilities.
Example: Cache in Games
-
Datastore: Both Memcached and Redis store data in-memory. When it comes to reading data, we can get it from memory. And so does write data, we’ll write data into memory first then write to the database.
-
Data Persistence: ElastiCache stores the snapshots, allowing users to subsequently use them to restore Redis clusters. A snapshot is a copy of your entire Redis cluster at a specific moment. And the snapshots are stored in S3. which is durable storage, meaning that even a power failure won’t erase your data. However, there’s no snapshot for Memcached. So if the cache has crashed in processing, we’ll lose all data in Memcached.
Original Redis can store data in the disk to prevent failure, but Amazon thought Redis as a bridge between application and database. They give up the old version of storing the backup in the disk, and change it with a snapshot.
-
Data read and write: The method we process data looks like Read → Modify → Write, it is fine in a single thread. When it comes to multiple threads, we need to think about data synchronize. If we use Memcached in the game, we’ll need to get the whole data list first. However, if there’s another data in the list been modify while the first data in progress, the system will confuse about which data it needs to store. We need extra programs to deal with it, like Optimistic Concurrency Control. If we use Redis in the game, we just need to write the data we want into the cache, we don’t even need to get whole data in the list. Like MULTI or WATCH. It will be used in coins, health or mana change in games.
Redis can also create read-replica. When you run a Cache Node with a Read Replica, the “primary” serves both writes and reads. The Read Replica acts as a “standby” which is “promoted” in failover scenarios. After failover, the standby becomes the primary and accepts your cache operations. Read Replicas also make it easy to elastically scale out beyond the capacity constraints of a single Cache Node for read-heavy cache workloads. You can use it in monsters’ information or anything that can be read but can’t be changed.
Conclusion
Here’s an easy table for Memcached and Redis:
Memcached | Redis | |
---|---|---|
Primary database model | Key-value store | Key-value store |
Secondary database models |
Document store Graph DBMS Search engine Time Series DBMS |
|
Data type | Simple | Complex |
Partitioning methods | none | Sharding |
Replication methods | none |
Master-slave replication Multi-master replication |
Sub-millisecond latency | Yes | Yes |
Developer ease of use | Yes | Yes |
Advanced data structures | No | Yes |
Multithreaded architecture | Yes | No |
Snapshots | No | Yes |
Nowadays, we can select Memcached or Redis in different use. Memcached is designed for simplicity while Redis offers a rich set of features that make it effective for a wide range of use cases. They both offer a strong type of cache using between application and database. Check what you need for the cache service, then choose the most suitable one for your program.
Appendix
-
Amazon ElastiCache : https://aws.amazon.com/elasticache/
-
Memcached: https://aws.amazon.com/Memcached/
-
Redis : https://aws.amazon.com/redis/
-
Choosing between Redis and Memcached: https://aws.amazon.com/tw/elasticache/redis-vs-memcached/
-
Comparing Memcached and Redis: https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/SelectEngine.html
-
System Properties Comparison Memcached vs. Redis : https://db-engines.com/en/system/Memcached%3BRedis
-
Memcached vs Redis, Which One to Pick? : https://www.linkedin.com/pulse/Memcached-vs-redis-which-one-pick-ranjeet-vimal
Tag:Cache, ElastiCache, Memcache, Redis