Recently I am preparing for an interview, and I will summarize what I have seen.

I. Differences between Redis and Memcache

  • 1. Data types

Memcache supports simple data types and requires the client to handle complex objects by itself

  • 2. Persistence

Redis supports persistent storage of data on the ground Memcache does not support persistent storage of data

  • 3. Distributed storage

Redis supports master-slave replication mode Memcache can be distributed using consistent hashes

  • 4. Different value sizes

Memcache is an in-memory cache with a key length of less than 250 characters and a single item store of less than 1M, which is not suitable for virtual machines

  • 5. Different data consistency

Redis uses a single-threaded model, which ensures that the data is submitted sequentially. Memcache requires the use of CAS to ensure data consistency. CAS (Check and Set) is a mechanism to ensure concurrency consistency and falls under the category of “optimistic locking”. The principle is simple: take the version number, take the action, compare the version number, and if they are consistent, take the action, and if they are inconsistent, abandon any action

  • 6. CPU utilization

The Redis single-threaded model can only use one CPU and can start multiple Redis processes

Second, the difference between Redis and MongoDB

  • 1. Data storage

MongoDB is a database based on distributed file storage. Written in C++. MongoDB is a product between a relational database and a non-relational database. The non-relational database has the most abundant functions and is the most similar to a relational database. It supports a very loose data structure, a JSON-like BSON format, so it can store more complex data types. The biggest feature of Mongo is that it supports a very powerful query language, its syntax is a little similar to the object-oriented query language, can almost achieve most of the functions similar to the relational database single table query, but also supports the establishment of indexes on data.

The storage format of Redis is key-value mode. In addition to the basic string type, Redis also implements hash, list, set, and zset data types.

Comments:

BSON is a Binary storage format. It adopts the name and pair representation method similar to the structure of C language, and supports the embedded document object and array object. It has the characteristics of lightness, traversal and high efficiency. Can effectively describe both unstructured and structured data.

In contrast to JSON, BSON focuses on improving storage and scanning efficiency. Large elements in a BSON document are prefixed with length fields for easy scanning. In some cases, BSON uses more space than JSON because of the length prefix and explicit array indexes.

  • 2. Data persistence

Redis itself supports two types of persistence, RDB(snapshot) and AOF append. RDB persistence is to write a snapshot of a data set in memory to disk within a specified time interval. The actual operation process is to fork a child process, first write the data set to a temporary file, after successful write, and then replace the previous file, using binary compressed storage. AOF persistence records every write and delete operation processed by the server in the form of log. Query operation will not be recorded, but recorded in text. You can open the file to see the detailed operation record.

At startup, MongoDB initializes a thread that loops continuously (unless the application crashes) to get the persistent data from the defer queue and write it to the disk’s journal and mongofile. Of course, since it is not written to disk when the user adds the Record, according to the MongoDB developers, it does not incur a performance penalty, as a review of the code shows that when a CUD operation is performed, the Record type is put into the DEFER queue for a GROUPCOMMIT commit write. When MongoDB writes, it creates a journal, which records the disk address and bytes changed by the write operation. Thus, in the event of a sudden server outage, logs can be replayed at startup to re-perform operations that were not flushed to disk before the outage. The data file is swiped every 60 seconds by default. The data files are stored in the /data/db/journal directory. By default, MongoDB writes these operations to the log every 100 milliseconds or after a number of megabytes of data have been written. That is, MongoDB does not write to the log every time it writes, but instead commits changes in batches. Therefore, by default, MongoDB will not lose data for more than 100 milliseconds.

  • 3. Consistency

Redis supports things, which is weak and only ensures that the operations in things are executed in order. MongoDB does not support things, it is up to the client itself to guarantee

  • 4. Memory and storage

MongoDB: suitable for large data storage, dependent on virtual memory management of the system, using mirror file storage; High memory occupation, it is recommended to independently deploy in 64-bit system (32-bit has maximum 2.5G file limit, 64-bit has no change limit)

After Redis2.0, the virtual memory feature is added to break through the physical memory limit; Data can be time-limited, similar to Memcache

  • 5. Availability

MongoDB: Supported master-slave,replicaset (internal using Paxos election algorithm, automatic failure recovery),auto sharding mechanism, shielding the failover and sharding mechanism on the client side

Redis: Depends on the client for distributed read and write; In master-slave replication, every time the slave node reconnects to the master node, it relies on the entire snapshot. There is no incremental replication. Automated sharding is not supported and relies on a program to set up a consistent hash mechanism

  • 6. Application scenarios

Redis: It is suitable for systems that have high requirements on reading and writing efficiency, complex data processing business and high requirements on security (such as sina weibo counting and Weibo publishing systems, which have high requirements on data security and reading and writing). MongoDB: It mainly solves the problem of access efficiency of massive data.

Reference:

  • 1. Comparison and application scenarios between MongoDB and Redis
  • 2. Differences between Redis and MongoDB
  • 3. Introduction to BSON and the difference between BSON and JSON
  • 4. Redis – Basic data types and internal storage structure
  • 5. Several ways of Redis persistence
  • 6. Comparison of application indexes between MongoDB and Redis
  • 7. Differences between MongoDB and MySQL