5.2. Disk I/O Dumping

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.

Using redis-cli, we can execute the synchronous SAVE command to take a snapshot of the in-memory data:

$ redis-cli SAVE
OK

Here, the command stores the snapshot in a dump.rdb binary file and returns the status OK when complete.

However, the execution of the asynchronous BGSAVE starts the background process of taking a snapshot:

$ redis-cli BGSAVE
OK

Additionally, we can use the LASTSAVE command to check the Unix time of the last successful DB snapshot.

$ redis-cli LASTSAVE
(integer) 1410853592

5.3. Data Structures

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.

5.4. Replication

Unlike Memcached, Redis provides us functionality to multiply clusters by replicating the primary storage for better scalability and high availability.

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.

5.5. Transactions

Memcached doesn't support transactions, although its operations are atomic.

Redis provides out-of-the-box support for transactions to execute commands.

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.

Here, we've set the key website to the value baeldung by evaluating a script.

5.10. Memory Usage

Memcached has a higher memory utilization rate than Redis when comparing the String data structure.

In spite of that, when Redis uses the hash structure, it provides a higher memory utilization rate than Memcached.

6. Conclusion

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.

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.

Persistence bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2: