Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of basic knowledge of Redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you the basic knowledge of Redis example analysis, I believe most people do not know how, so share this article for your reference, I hope you read this article after a lot of gains, let us go to understand it together!

What is Redis?

Redis is an open source (BSD license), in-memory data structure storage system that can be used as a database, cache, and messaging middleware.

It supports multiple types of data structures such as strings, hashes, lists, sets, ordered sets and range queries, bitmaps, hyperloglogs, and geospatial index radius queries.

Redis has built-in replication, LUA scripts, LRU-driven events, transactions and different levels of disk persistence, and provides high availability through Redis Sentinel and Auto-Partitioning (Cluster).

The above introduction from redis.cn, simply put, Redis is a high-performance NoSQL database that supports multiple data structures and can be persisted.

Redis Storage Implementation

Redis is a NoSQL database with key/value storage type with performance up to 100,000 qps. The approximate storage implementation system is as follows:

A RedisServer contains N redisDb, redisDb has dict pointer and expiration time pointer, the core is dictEntry pointer list pointer, each specific dictEntry list node stores any type of key and value, whether key or value can be redisObject. It can be argued that the nature of pointer lists plays an important role in redis performance.

The relationship between data structure and storage implementation in Redis is as follows:

INT compresses storage String, constant numeric objects are shared. SDS stores strings, variable-length character arrays, and shared common strings. A double-ended list LINKEDLIST is used to store lists and support bidirectional traversal. HT is hash table, stores set and hash, scales according to fill rate, supports event triggering. INTSET compressed storage set, coded int16_t/ int32_t/ int64_t. SKIPLIST stores ordered sets, joint dict handles zsets. ZIPLIST stores hash, list, and zset through double-ended pointer compression.

The network model in Redis storage implementation supports Epoll/Select/Kqueue, etc., and the event model is mainly TimeEvent/FileEvent. Since FileEvent Processor is single-threaded, redis is a single-threaded model. Redis single-thread model is pure memory operation, the core is based on non-blocking IO multiplexing mechanism, single-thread instead avoids the frequent context switch problem of multi-thread, so the overall storage implementation performance is very high.

Common command sets for Redis

Redis is simple and easy to use. For ease of memory, the command set is classified as follows:

Access to Redis is rich in client types, covering almost all major programming languages:

Cluster deployment of Redis

Redis has been stable in production since version 2.4, Lua scripts and Watch dog were introduced in version 2.6, master-slave synchronization was optimized in version 2.8, and support for Sentinel and HyperLog began. Clustering in Redis 2.x is mainly achieved through master-slave synchronization, and data is asynchronous replication and incremental synchronization.

Finite state machines play an important role in master-slave synchronization and data persistence. Data persistence is achieved through snapshots, and the communication protocol is RESP, a binary security protocol that is easy to implement and understand. AOF eventually realizes data persistence by writing to disk via fsync.

Starting with 3.0, Redis supports clusters, the Gossip distributed protocol used between nodes within clusters. The majority rule is used to judge whether the node is down or not, and Gossip protocol transmits the information of the decision to elect a new replacement, and Gossip protocol transmits the election result.

Redis clusters enable automatic failover, replica migration, and online resharding. Fragmentation changes and data migration are also agreed upon through the Gossip protocol.

However, Redis cluster also has shortcomings, for example, with the increase of nodes, failover will increase significantly, Gossip takes a period of time to propagate information, and the whole room switches slowly. The migration of synchronous blocking by key has great influence on reading and writing, and it is very difficult to debug Gossip protocol, which will bring a lot of inconvenience to troubleshooting.

Follow redis official website to see many new changes since redis 4.0.

Common applications of Redis

Redis is widely used, with outstanding performance in key/value NoSQL, and even similar to document-based NoSQL-MongoDB in some aspects. A comparison of Redis, Memcache and MongoDB is shown below:

Obtaining user related data according to user ID is a common scenario in Internet applications. This kind of data has the characteristics of large amount of reading and writing, but the single data is not long, and some fields are frequently updated. It is generally realized by using key/value database such as redis. Redis does a great job of incrementing and decrementing numbers in memory. Sets and Sorted Sets also make it easy to perform these operations, and Redis provides both of these data structures, making it ideal for leaderboard scenarios.

For distributed lock services, Redis can provide high-performance distributed lock services, such as instant kill scenarios for e-commerce services, global self-increasing IDs, and so on.

The above is all the content of this article "Example Analysis of Redis Basics", thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report