Once you encounter a lot of data and high concurrency in your work, you need to use NoSQL to help. NoSQL mainly uses the following three types: Redis, Mongodb, and Memcached. The following table introduces their differences and usage scenarios.
Comparison item | Redis | Mongodb | Memcached |
---|---|---|---|
data storage | The storage format of Redis is the same as Memcache, and it is also Key-Value mode, but Redis has another implementation. In addition to the basic string type, redis also implements hash, list, set, and zset data types. | The storage format of MongoDB is a document type, which is a type of json format, so that there is an opportunity to index certain fields. You can simply imitate a relational database. And MongoDB does have the concepts of db and table. | The storage format of Memcache is Key-Value mode, but unfortunately, the format of Value can only be a string, which also limits the application scope of Memcache |
safety verification | Redis has permission verification, but it is global | Mongo's permission verification is similar to RBAC, which creates different accounts for different libraries and assigns account permissions | Memcache does not have its own authorization verification, it can only be restricted through firewalls and other means |
Data validity period | Redis can add expiration time to key | MongoDB itself can actually be regarded as a data warehouse, there is no such thing as the past time | Memcache can also set the expiration time, here it is recommended to use a timestamp to set |
Data persistence | Redis itself supports two persistence, snapshot and AOF append method | MongoDB first persists journal and then persists data | Memcached, there is no persistence function, data will be lost if power is off |
special function | Executable Lua script | Fragmentation, index (using inverted index), executable js script, single document transaction (2PC) | Slab Allocation mechanism, apply for a large block of memory, divide it into chunks of various sizes, and form Slab Classes of the same size |
scenes to be used | It is suitable for systems with high requirements for read and write efficiency, complex data processing services and high security requirements | Uncertain data model; high QPS requirements; need to be able to scale quickly; weak transactions | Reduce database load and improve performance in a dynamic system; as a cache, it is suitable for more reads and less writes and large data volumes |