Detailed Senior JAVA face questions (three) - Redis (redis cluster, virtual slot, consistency hash algorithm, master election, out of policy, String data structure)

Detailed next Redis

Hope this one can get the rest of the stuff, if all else fails, then we have at this and next we get the demand for development as this is the last edition of the demand, after this day I have a evolutionary version, another day I've got this Ultimate Edition, another day Ultimate Edition predictable evolution. Digimon a whole evolutionary process, under Tucao she was more busy recently not ready ahead of time to write something today. Mainly pesticides S19 of the season came on the points go, you the boss sorry.

Interviewer: Redis cluster model which ah small leek?

1, master-slave mode (master / slave)
Master-slave mode Introduction
  • A Master can have multiple Slave
  • Support replication master-slave, the host will automatically synchronize data to the slave, the separation can be read (default master node may read and write)
Master and slave modes advantages
  • Support separate read and write, master reduced pressure
  • master node after hanging up, does not affect read slave node (also effective to do data backup, to prevent the special circumstances result in data loss can not recover master)
  • Slave Slaves can also accept other connections and synchronization request, which can effectively synchronize the Master pressure offloading
Master and slave modes disadvantages
  • Hung up the master does not support a write operation and slave will not re-elect a new master (a simple main light on this killing from the modes)
  • master down, down the front part of the data has failed to synchronize to a slave, also introduce data inconsistencies after switching IP.
  • When using sentinel mode, clients do not connect directly to Redis, but the connection sentinel ip and port
  • Expansion is difficult, it is the case when cool a large amount of data stored in the server can not fit
2, Sentinel mode (Sentinel)
Sentinel Model Description
  • Monitoring master (master) and the slave is functioning properly (from the server).
  • Slave automatically (from a server) to Master (master) when Master (primary server) fails.
Sentry Mode Advantages
  • Sentinel model is based on a master-slave mode, the master from all the advantages of having a Sentinel model.
  • Can automatically switch from the main, the system is more robust, more available.
Sentinel mode shortcomings
  • Expansion is difficult, it is the case when cool a large amount of data stored in the server can not fit
redis cluster cluster model (this is a fine interviewer will ask)
redis cluster Profile
  • After 3.0 redis redis cluster support (Redis cluster) can be done on multiple machines, a plurality of master deployment (instances), stores the data portion of each master , and each Node can be mounted a plurality of master slave node (from Example ), then hang up if mater, redis cluster this mechanism, it will automatically be a slave to switch to Master .

Interviewer: that's good, then hang up if mater, redis that is how to elect a new master from multiple slave?

  1. Raft of new primary election process based on election protocol to achieve when they found themselves in the master node has been offline state from the node from the node will broadcast a message to all the requirements to receive this message, and have the right to vote of the master node from the node to the vote
  2. If a master node has the right to vote, and vote for the master node has not been from the other node, the master node will be asked to vote returned a message from the node, indicating that the master node support from node becomes the new primary node
  3. If the primary cluster with N nodes has the right to vote, then when collected from a cluster node greater than or equal to N / 2 + when one votes, the slave node becomes the new master node
  4. If not, then enter a new cluster configuration from the era able to gather enough support votes in a configuration era, and the primary election again, until the election of a new master node has been
  5. It will be revoked after the new master node appear to have been off the assembly line all slots assigned to the master node, and all of these slots assigned to himself (and the lions, the new Lion King will occupy the old wives)
  6. The new master node cluster PONG broadcast messages to inform other node has become the new master node (the new Lion to leave their scent told neighbors in the territory here is mine)
  7. The new master node receives the request and begins processing tank associated

Interviewer: This is an election to say well, since before you talk to the storage portion of each master data, redis how the data into different nodes?
Virtual partition groove way clever use of the Hash space, good dispersion of hash function used to map all of the data to a fixed range of the set of integers, the integer defines a groove (slot). This range is generally much larger than the number of nodes, such as Redis Cluster groove ranges from 0 to 16383 (2 ^ 14). Slot is the cluster of data management and migration of basic units. The main purpose of using a large range of data slots are split in order to facilitate expansion and clusters. Each slot will be responsible for a certain number of nodes,
such as increasing the node needs to allocate part of the tank to get on the new node from an existing node. If you want to remove a node needs to node slots moved to another node, then the node will not have any slots can be removed from the cluster.
Interviewer: small leek can ah, you know why to use virtual slot? Consistency hash algorithm to find out about it
in fact this question is to ask why redis not consistent hash algorithm to do it directly.
Consistent hashing is arranged on the storage node Hash end to end rings, each key will be found after computing Hash clockwise store storage node, such as node is not found then the next clockwise with it to find a node . When a node joins or exit, affects only the nodes on the ring clockwise Hash subsequent adjacent node.
But generally less nodes in practice, once the delete or add a node data mapping the impact of the hash ring very large, and can not guarantee load balancing. Unless doubling or halving the node.
Interviewer: So if I now want to add a node, redis how to ensure that the migration process in the slot normally write and modify
with a case to answer it easier to understand:
suppose migrate from a node A to node B slot
This tank 1 in the state transition is labeled node A will Migrating (migration)
Node B is Importing state (input port)
mapping relationship between this time slot will not be modified, the data migration if the query tank 1 occurs,
the client a node will go up query data, if the corresponding data is still on the machine, then the direct return, if the data is not on the node is returned ASK a steering.
If the client receives ASK turn, then sends the request to the target command adjusted steering the specified node node B. ASKING first send a command, then send real command request.
Groove does not update the client after completion of the request record to the node mapping: tank 1 should still be mapped to the node A, instead of node B.
Once the node A for the complete migration of slot 1, node A again in receipt of a request for a command tank 1 will be returned to the client MOVED turn, will command slot 1 on request long turned to node B.

Here is already almost, but did not ask you to not answer the interviewer how easily stopped, do not tie the battlefield, you must dig your limits unless you say the interviewer has begun stupefied. So we continue to battle 300 rounds

The interviewer (Do not hold back praise this chives): Look before you talk to through the main redis from, then you know how redis ensure consistent master data from it
first master to read and write commands can be executed only from the library a read command (default configuration can be modified as if written support, but does not make sense because it will be covered by master),
synchronous two cases

  • Synchronization: when one sync command is sent from the primary server when the server starts, the primary server receives the sync command to save a snapshot in the background begins (operation performed rdb), and the command received during storage cached from the server when the initial synchronization data request will not be processed blocking client (client) is, master unaffected.
  • Command transmission: the database is modified in a state of the primary server, leading to the master, the master server will write their own command executed from the server to the implementation of the database server's status from inconsistent,
  • Heartbeat
    in the propagation phase command, from the server the default frequency of once per second to the main server sends a command
    to detect the primary connection is normal from the server
    detecting loss of the command, the master checks to see if the server receives from the server and the offset from the server after the command and the main server, and if not will transmit buffer backlog from behind offset command to the server from the server.

Interviewer: Redis data out of what kinds of strategies?

  • noeviction : When not delete a policy, to achieve the maximum memory limit, if you need more memory, direct returns an error message.
  • the LRU-AllKeys : All key common; delete key priority least recently used (less recently used, LRU) is.
  • LRU-volatile : limited set of section expire; delete key priority least recently used (less recently used, LRU) a.
  • Random-AllKeys : All key common; delete some random key.
  • Random-volatile : limited set of section expire; delete some random key.
  • TTL-volatile : limited set of section expire; preferentially remove the remaining time (time to live, TTL) short key.

What kinds that have expired key to delete the policy?

  • Regularly delete :
    at the same time set the expiration time of the key, create a timer, so key timer expiration time comes, perform the removal of the key immediately;
    the timing for the memory deletion is friendly, does not require memory operation, but by using a timer, you can guarantee will expire as soon as the delete key, but it is not friendly for CPU, if the key expired more, then, since the timer will be more, delete this operation will take up to CPU resources;

  • Inert Delete :
    faire key expires regardless, but each time obtained from the key space is key, key expiration time of inspection are made, if expired, it can be deleted;
    inert operation for the CPU is friendly, expired keys only when the judge read the program has not expired only removed, but this will only delete an expired key, but for the memory is unfriendly, if multiple keys have expired, and these keys and just have not been accessed, this part of the memory of this would have been released;

  • Deleted regularly :
    Every so often, the program checks the database once, delete the expired keys;
    periodically delete a compromise two options above, from time to time to remove outdated keys, and long time limit by deleting operation performed to reduce the impact and frequency of the deletion of the CPU time, in addition, also effective in reducing the waste of memory; but the difficulty of this strategy is a long interval, this needs to be set according to their own business situation;

redis how large the data extracted Key
EDIS-CLI of "bigkeys"
calculated: string is calculated using STRLEN method, list is calculated llen

That talk in which the most commonly used Redis underlying data structure of the String
Redis in order to facilitate the expansion, performance and safety, their definition of a structure for storing strings. I.e. SDS (simple dynamic string),
which in addition to saving string buf, also holds free (buf represents the remaining space) and len (length of the current substring).
This design has several advantages:

  1. Get string length complexity is O (1)
  2. Add the remaining space is determined by the length of the string and to decide whether to modify the expansion, will not cause buffer overflows
  3. Each time expansion is twice the length of the expansion has been to reduce the number of memory reallocation brought modify the string
  4. Len binary security because it is by the end of the string to determine whether the C language rather than by '\ 0' standard for judging the end of the string

redis first so be it, take a break in preparation for a module if interested can leave a message and see which module is more to see, I want to be the first to write it.

Published 19 original articles · won praise 45 · views 110 000 +

Guess you like

Origin blog.csdn.net/zhibo_lv/article/details/105239297
Recommended