more knowledge points, I put together a mind map, focus on the background to reply to Redis, sent to youWhat is Redis persistence? What kinds of persistence do redis have? What are the pros and cons?
Redis is a very fire non-relational database, how much fire? As long as an internet company will use it. Redis related questions can be said to be the interview must ask, below I from the personal experience as an interviewer, summed up a few must master the knowledge points.
Description: Redis is an open source non-relational database that is written in ANSI C, adheres to the BSD protocol, supports networks, can be persisted in memory, key-value databases, and provides APIs in multiple languages.
Traditional databases follow ACID rules. The acronym for NOSQL (not just SQL) is a general designation of a database management system that differs from a traditional relational database, and generally follows the CAP theorem for distributed distribution.
Github Source: Github.com/antirez/redis
Redis Official website: redis.io/
Persistence is the memory data written to disk, to prevent service downtime memory loss.
Redis provides two ways to persist: RDB (default) and AOF
RDB is a redis database abbreviation
function Core function rdbsave (generate RDB file) and rdbload (load memory from file) two functions
AOF is the append-only file abbreviation
The Flushappendonlyfile function is called whenever a server (timed) task or function is executed, and the function performs the following two jobs
AoF Write Save:
Write: Writes the cache in Aof_buf to the AoF file, depending on the condition
Save: Depending on the condition, call the Fsync or Fdatasync function to save the AOF file to disk.
The content is a command text store in the Redis Communication Protocol (RESP) format.
(You can see a lot of interviews are actually serial guns, the interviewer is actually waiting for you to answer this point, if you answered the comments on you added another point)
RESP is a communication protocol used before the Redis client and server;
Features of RESP:
Simple, fast parsing and readability.
1, memory capacity is limited 2, processing capacity is limited 3, can not be highly available.
The Redis Replication (replication) feature allows the user to create as many replicas of the server as a Redis server, where the server being replicated is the primary server (master), and the server replica created by replication is the slave server (slave). As long as the network connection between the master and slave servers is normal, the master and slave servers will have the same data, the primary server will always be on their own data updates synchronized to the slave server, thus ensuring that the master-slave server data is the same.
Redis Sentinel is a distributed system that monitors Redis master-slave servers and automatically fails over when the primary server is offline. Three of these features:
Monitoring (Monitoring): Sentinel will constantly check whether your primary server and slave server are functioning properly.
Reminder (Notification): When a problem occurs with a Redis server being monitored, Sentinel can send notifications to administrators or other applications through the API.
Automatic failover (Automatic failover): Sentinel starts an automatic failover operation when a primary server is not working properly.
Cons: Master-slave mode, switching takes time to lose data
does not solve the pressure of master writing
Cluster (proxy type):
Twemproxy is a Twitter open source, a redis and memcache fast/lightweight proxy server; Twemproxy is a fast single-threaded agent that supports Memcached ASCII protocol and Redis protocol.
After Redis 3.0, the Redis-cluster cluster is supported, the Redis-cluster is in a non-central structure, each node holds the data and the entire cluster state, and each node is connected to all other nodes.
These two questions are too long online to find a good two unlocked articles
79121213What type of data does Redis support?
You can refer to my previous article.
If someone asks you what the CAP theory is, send him the article.Redis common commands?
First take SETNX to grab the lock, after the grab, then use expire to lock plus an expiration time to prevent the lock forgot to release.
What happens if the process unexpectedly crash or restarts maintenance before expire is executed after setnx?
The set directive has very complex parameters, which should be used to synthesize setnx and expire as an instruction!
Generally use the list structure as a queue, Rpush production messages, Lpop consumer messages. When Lpop has no message, try again with a proper sleep.
In the case of the consumer offline, the production of the message will be lost, the use of professional Message Queuing such as RABBITMQ.
Is it possible to produce a single consumption multiple times?
With the Pub/sub theme subscriber pattern, you can implement 1:N Message Queuing.
The general cache system is to follow the key to cache the query, if there is no corresponding value, it should go to the backend system lookup (such as DB). Some malicious requests will deliberately query the nonexistent key, the volume of the request is very large, it will cause great pressure on the back-end system. This is called cache penetration.
How to avoid it?
1: The case of NULL query results is also cached, the cache time is set a little bit shorter, or the key corresponding to the data insert after the cache cleanup.
2: Filter The key that does not exist. All possible keys can be placed in a large bitmap, which is filtered by the bitmap when queried.
When a cache server restarts or a large number of caches fail at a certain time period, this can put a lot of pressure on the backend system when it fails. Cause the system to crash.
How to avoid it?
1: After the cache fails, the number of threads that read the database write cache is controlled by a lock or queue. For example, a key allows only one thread to query the data and write the cache, and other threads wait.
2: Do two-level cache, A1 for the original cache, A2 for the copy cache, A1 failure, can access A2,A1 cache expiration time set to short-term, A2 set to long-term
3: Different key, set different expiration time, so that the time of cache failure is as uniform as possible.
Well, I wish you all the best interview! Take out a mobile phone to sweep attention ~ irregular delivery of books