Support > About cybersecurity > Why not use Redis as the main database
Why not use Redis as the main database
Time : 2025-03-06 15:44:27
Edit : Jtti

Redis is a persistent in-memory database on disk, and the data model is key-value capable of supporting strings, lists, sets, ordered sets, hashes, streams, and so on. As an in-memory database, Redis stores all data in memory instead of on the hard disk. Why not use Redis as the primary database and just for caching?

Cause one: Redis has a fast read and write speed but is only suitable for storing temporary data rather than persistent data. If Redis is used as the primary database, the data stored in the database cannot be persisted, and the data will be permanently lost when errors occur on the server.

Cause two: Redis is a single thread can only support processing one request at a time, if Redis used as the main database can not handle concurrent requests, will lead to performance problems and response time delay. MySQL, on the other hand, is a relational database that can handle multiple requests and is not prone to performance bottlenecks.

Reason 3: Redis does not support SQL queries, so you can not use SQL language, and MySQL database provides more powerful SQL query functions to achieve more complex queries. MySQL data is stored on disk, and MySQL as the main database storage cost is lower than Redis, but the performance is worse.

Considering the actual situation, setting Redis as the main database will face high costs, and the storage capacity will be limited. Because Redis stores data in memory by default, the price of memory is much higher than disk, and the cost of Redis storage as a primary database is high. The storage capacity is limited by the memory size and is not suitable for storing massive data.

Redis supports simple key-value operations, and lacks the ability of complex queries (such as JOIN, aggregation, etc.), while the main database needs to support complex business logic. Redis high performance is suitable for caching and can speed up data access. The cost advantage of using MySQL in practice is even better. Due to performance and cost, many database service providers use a combination of memory and disk solutions.

The advantage of a single database is that the transactional implementation is simpler and is guaranteed by the database itself. The use of Redis is more for performance and concurrency considerations, if it is encountered needs to be executed for a long time and the results do not often change the SQL is more suitable for running the results of the cache, and then to request the buffer read, which can improve speed. In the case of large concurrency, all requests are directly accessed to the database, and the database connection will be prone to exceptions, and Redis can make a buffer operation, so that the request first accesses Redis instead of directly accessing the database.

Question: What are the storage methods to share?

A: B-tree can support random read and search by range system, search time complexity is logd(n) (d is the output of each node). Mysql's InnoDB engine and OS's file system use B+ trees.

Hash storage CRUD is very fast, but does not support sequential scanning. bitcask is a storage system based on the hash table structure. He appends the write operation (including the deletion of the identity) to the end of the file. And regularly merge new and old files & records.

Log Structured Merge Tree (LSM tree) is an improvement on B+ numbers. The idea is to store incremental write operations in the memory and flush them to the disk when they exceed the threshold, thus reducing random write operations. Read operations combine disk data with write operations in memory. Implementation with Memtable/SSTable, implementation details will not be delved into here. It is suitable for service scenarios where write operations are frequent. The data storage mode of column clusters in BigTable/HBase/Cassandra is LSM tree.

Question: Why is Redis single-threaded so fast?

Answer: There are three main reasons, pure memory operation; Single-threaded operation (avoiding frequent context switches); A non-blocking I/O multiplexing mechanism is adopted.

Relevant contents

Setting up a contact form in WordPress without coding How do I remove the.NET runtime and SDK Singapore VPS is the platform of choice for AI training and big data analytics What is the server's network transmission medium? How to deploy Django through Nginx on Ubuntu system DNS contamination and DNS hijacking: A guide to differentiation and protection How long is the service life of the UPS uninterruptible power supply dedicated to the server room? What are the things that enterprises should pay attention to when choosing SAAS software? How to install WordPress plugin on Linux server? Detailed tutorial on installing Git on Windows
Go back

24/7/365 support.We work when you work

Support