In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Introduction
Recently, I participated in Mr. Jiang's training on Redis, which deepened my understanding of Redis database.
● directory ●
One redis applicable scenario
Two redis advantages
Three Redis data types
Four redis persistence
Five redis architecture
● content ●
One redis applicable scenario
Redis is most suitable for all data in-momory scenarios. Although Redis also provides persistence, it is actually more of a disk-backed function, which is quite different from the traditional persistence. It seems that Redis is more like an enhanced version of Memcached.
1 session caching (Session Cache)
One of the most common scenarios where Redis is used is session caching (session cache). The advantage of caching sessions with Redis over other stores, such as Memcached, is that Redis provides persistence. When maintaining a cache that is not strictly consistent, most people will be unhappy if all the user's shopping cart information is lost.
2 queue
One of the advantages of Reids in the field of memory storage engines is that it provides list and set operations, which makes Redis a good message queuing platform to use. The operation used by Redis as a queue is similar to the push/pop operation of list by a native program language such as Python.
3 full page cache
Large Internet companies use Redis as a cache to store data, increasing the speed of their pages. Even if the Redis instance is restarted, users will not see a drop in page loading speed because of disk persistence.
4 rankings / counters
Redis does a great job of incrementing or decrementing numbers in memory. Set and Sorted Set also make it very easy for us to perform these operations.
Two Redis advantages
1 extremely high performance
Redis can read at a speed of 110000 times per second and write at a speed of 81000 times per second.
2 rich data types
Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.
3 atom
All operations of Redis are atomic, meaning either successful execution or failure to execute at all. A single operation is atomic. Multiple operations also support transactions, that is, atomicity, wrapped by MULTI and EXEC instructions.
4 rich features
Redis also supports publish/subscribe, notification, key expiration, and so on.
MySQL+Memcached evolves MySQL+Redis
MySQL is suitable for massive data storage. Many companies have used this architecture to load hot data into cache through Memcached to accelerate access. However, with the continuous increase in the amount of business data and the continuous growth of visits, many problems have been encountered:
MySQL needs to disassemble libraries and tables constantly, and Memcached also needs to expand with it. Expansion and maintenance work take up a lot of development time.
Data consistency between Memcached and MySQL databases.
Memcached data hit rate is low or downmachine, a large number of access directly penetrated to the DB,MySQL can not support.
Cache synchronization across computer rooms
If you simply compare the difference between Redis and Memcached, most will get the following point of view:
Redis not only supports simple KBE data, but also provides storage of data structures such as list,set,zset,hash.
Redis supports data backup, that is, data backup in master-slave mode.
Redis supports data persistence, which can keep the data in memory on disk, and can be loaded and used again when rebooting.
Three Redis data types
Redis supports five data types:
1 string (string)
2 hash (hash)
3 list (list)
4 set (collection)
5 zset (sorted set: ordered set)
3.1 string
String is the most basic type of redis, which can be understood as exactly the same type as Memcached, with a key corresponding to a value.
The string type is binary safe. The string of redis can contain any data. Such as jpg images or serialized objects.
String type is the most basic data type of Redis, and the value of string type can store 512MB at most.
Redis 127.0.0.1 keyman 6379 > SET name "keyman"
Redis 127.0.0.1 6379 > GET name
Application scenario
String is the most commonly used data type, and ordinary key/value storage can be classified into this category. That is, it can fully realize the functions of the current Memcached, and it is more efficient. You can also enjoy the regular persistence of Redis, operation log and Replication and other functions. In addition to providing the same get, set, incr, decr and other operations as Memcached
Redis also provides the following operations:
Get string length
To the string append content
Set and get a segment of a string
Set and get a bit of a string (bit)
Set the contents of a series of strings in batch
Implementation: String is stored in redis as a string by default, which is referenced by redisObject. When it encounters operations such as incr,decr, it will be converted to numerical calculation. In this case, the encoding field of redisObject is int.
3.2 hash
Common command
Hget,hset,hgetall et al.
Application scenario
In Memcached, some structured information is often packaged into HashMap, which is stored as a string value, user's nickname, age, gender, score and so on after serialization on the client. When you need to modify one of these items, you usually need to take out and deserialize all the values, modify the value of a certain item, and then serialize and store it back. This not only increases the overhead, but also does not apply to situations where concurrent operations are possible (for example, two concurrent operations need to modify the integral). The Hash structure of Redis allows you to modify only one attribute value as if you were Update an attribute in the database.
3.3 list
Common command
Lpush 、 rpush 、 lpop 、 rpop 、 lrange
Application scenario
There are many application scenarios of Redis list, and it is also one of the most important data structures of Redis. For example, twitter's watch list, fan list and so on can be implemented by Redis's list structure.
Lists is a linked list, and anyone with a little knowledge of data structures should be able to understand its structure. Using the Lists structure, you can easily achieve functions such as ranking the latest messages. Another application of Lists is message queuing
You can take advantage of the PUSH operation of Lists to store the task in Lists, and then the worker thread uses the POP operation to fetch the task for execution.
Redis also provides api for manipulating a certain segment of Lists. You can directly query and delete the elements of a certain segment of Lists.
3.4 set
Common command
Sadd, spop, smembers, sunion, etc.
Application scenarios:
Redis set external function is similar to list is a list function, the special feature is that set can automatically arrange weight, when you need to store a list of data, but do not want to duplicate data, set is a good choice, and set provides an important interface to determine whether a member is in a set collection, which list can not provide.
The concept of a Sets set is a combination of unrepeated values. Using the Sets data structure provided by Redis, you can store some collective data. For example, in Weibo application, you can store all the followers of a user in a collection and all their fans in a collection. Redis also provides intersection, union, difference and other operations for collections, which can easily implement functions such as common concern, common preferences, second-degree friends, and so on. For all the above collection operations, you can also use different commands to choose whether to return the results to the client or save to a new collection.
3.5 sort set
Common commands:
Zadd,zrange,zrem,zcard et al.
Working with scen
The usage scenario of Redis sorted set is similar to that of set, except that set is not automatically ordered, while sorted set can sort members by providing an additional parameter of score, and it is inserted in order, that is, automatic sorting. When you need an ordered and non-repeating list of collections, you can choose the sorted set data structure. For example, twitter's public timeline can be stored as a score with the publication time, so that the acquisition is automatically sorted according to time.
In addition, Sorted Sets can be used to make weighted queues, such as the score of ordinary messages is 1 and the score of important messages is 2, and then the worker thread can choose to get work tasks in reverse order of score. Give priority to important tasks
Four redis persistence
Redis is a memory database, in order to ensure that the data will not be lost after a power outage, the data in memory needs to be persisted to the hard disk.
Redis provides two ways to persist
RDB (Redis DataBase)
AOF (Append Only File)
4.1 RDB
At different points in time, the data stored by redis is snapped and stored on media such as disks, and snapshots can be replicated to other servers to create server replicas with the same data. If the system fails, the data since the last snapshot is lost. If the amount of data is large, the snapshot will be saved for a long time.
4.2 AOF
To achieve persistence from another perspective, all write instructions executed by redis are recorded, and data recovery can be achieved by repeating these write instructions from front to back the next time redis is restarted. Add a write command to the end of the AOF file (append only file).
Five redis architecture
5.1 stand-alone
5.2 Master and slave
5.3 Sentinel
5.4 Cluster
5.3 Sentinel
The emergence of the Sentinel mainly solves the problem that human intervention is needed when master-slave replication fails.
Main functions of Sentinel
Cluster monitoring: responsible for monitoring whether Redis master and slave processes are working properly
Message notification: if a Redis instance fails, the sentry is responsible for sending a message to the administrator as an alarm notification.
Failover: if master node is down, it will be automatically transferred to slave node.
Configuration Center: if a failover occurs, notify the client client of the new master address
5.4 Cluster
Even with Sentinel, each instance of redis is fully stored, and the content of each redis is complete data, which is a waste of memory and has a bucket effect.
In order to maximize the use of memory, you can use clustering, that is, distributed storage.
That is, each redis stores different content, with a total of 16384 slot.
Each redis gets some slot,hash_slot = crc16 (key) mod 16384 to find the corresponding slot.
The cluster needs at least 3 masters and 3 slaves, and each instance uses a different configuration file. The master and slave do not need to be configured, and the cluster will choose its own.
High availability: automatic failover after the host is down, so that the front-end service has no impact on the user.
Read-write separation: diverts the read pressure of the host to the slave.
Load balancing can be implemented on client components, sharing different proportions of read request pressure according to the operation of different servers.
Welcome to follow my Wechat official account "IT Little Chen" and learn and grow together!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.