Redis (Remote Dictionary Server) is a database that makes full use of memory.

Compared with traditional hard disk databases, the performance of redis is much higher.

In order to ensure efficiency, data is cached in memory, and redis will periodically write updated data to disk or write modification operations to additional record files, and on this basis, master-slave (master-slave) is implemented Synchronization, even if downtime, data can be recovered. Therefore, redis achieves both high performance and secure persistent storage.

Redis is a key-value storage system that supports many types of value storage, including: string (string), list (linked list), set (collection), sorted set, and hash (hash type) [details](uniCloud/redis ?id=data-type)

Use Tencent Cloud node12 and redis, be sure to read this document carefully: keepRunningAfterReturn

# Comparison between Redis and MongoDB

  • Read and write speed: MongoDB data is stored on disk, and the read and write syntax is complex and slow. Redis reads and writes in memory and only accesses data based on the key, which is much faster.
  • Concurrency: uniCloud's mongoDB has limited concurrency. Redis has almost no limit, more depends on the concurrency limit of the cloud function.
  • Query capability: MongoDB supports all query syntaxes, various where and joint tables. Redis can only operate data based on key and limited syntax.
  • Billing: MongoDB charges based on the number of reads and writes (currently Alibaba Cloud is free). Redis is not free, but charges based on storage capacity. Generally, the data that needs to exist in redis is commonly used data, not too much, and the cost performance is very high.

# Common application scenarios of redis

  • Frequently read and infrequently changed library search If your application has 50 million pieces of news data, such data does not change frequently. The page uses fixed query conditions to obtain and display these data; every time each user opens the page, the query statement is executed through the database to obtain data. Obviously, this kind of efficiency is very low and wastes resources. A more efficient way is to cache it in redis, and get the value from the cache before each fetch. If the data cannot be fetched, then request the database. And add the data to the cache, the next user can read directly from the cache, so that the request can be responded quickly.
  • High concurrency, short-term high-frequency access data For example, hotspot data, spikes and other high-concurrency scenarios. Direct use of mongoDB will encounter performance bottlenecks. All requests directly access the database, and the database will experience connection exceptions. At this time, we should also use cache as middleware, and redis does a buffer operation, so that requests can access redis first instead of directly accessing the database.
  • ranking question Common ranking questions, such as hottest topics, game rankings, and more. Using mongoDB database queries consumes a lot of resources, which can be easily realized through Redis, and can be obtained directly by using ZRank.
  • Delete expired data Redis is not a persistent database in the true sense. You can add a valid time to the data. When the valid time exceeds, Redis will automatically delete the corresponding data.

Others include: counter, message queue push, friend follow, number of fans, etc. I won’t list them all here

# Notice

  • Although redis supports persistent storage, it is asynchronous. In extreme cases (such as: power failure), there is a possibility of data loss; if it is changed to synchronization, the performance will be lost. Therefore, mongoDB is still required for strong consistent data requirements.
  • One redis for each service space, which can only be accessed by cloud functions in this service space.

Although Redis has obvious advantages, we still cannot completely replace mongoDB with Redis. Recommendation: use mongoDB+redis in combination. All data has a copy in MongoDB, and redis is used for caching into redis.

HBuilderX 3.5.2+, the new JQL Cache Redis, can conveniently cache the data in MongoDB to Redis. See details