It ’s enough to ask Zookeeper to read this article during the interview

1. What is Zookeeper?

ZK is an open source distributed coordination tool based on the Java language, so you must install JDK before using ZK, which can be used for distributed locks, registration centers and event notifications. It is similar to the tree-like directory structure of the file system, and the defined node path is unique.

2. What are the application scenarios of Zookeeper?

1. Distributed lock (redis or zk)
2. Registration center (Dubbo + zk)
3. Distributed configuration center disconfig
4. Distributed messaging middleware
5. Publish and subscribe (event notification)
6. Master election

3. How does Zookeeper implement distributed locks?

Zk implements the concept of distributed locks: nodes remain unique, event notifications, and temporary nodes (life cycle and session session association).
Because the Zookeeper node path remains unique, it does not allow duplication and has temporary node characteristics. The current node will automatically disappear after the connection is closed, thus achieving Distributed lock.
Implementation principle:
1. Multiple requests create the same node (lockPath) at the same time, as long as anyone can create a successful one who can obtain the lock;
2. If the node is created, suddenly the node has been created by other requests, then wait directly;
3 .As long as the node can be created successfully, it starts to enter the normal business logic operation, and the other does not acquire the lock to wait;
4. After the normal business logic process is executed, call zk to close the connection to release the lock, notify other nodes, and thus other requests Start to enter the resource to acquire the lock.

4. What is the difference between Zookeeper and Eureka?

Zookeeper Eureka
Can it be a registration center can can
CAP theory CP, to ensure data consistency AP to ensure service availability
Design thinking Centralized thought Decentralized thinking
How to ensure data consistency Zab Atomic Broadcast Protocol The Peer to Peer mode is used for data replication. In order to prevent mutual circular replication, the method of passing parameters in the url is used to distinguish between ordinary requests and replication requests. Those who accept replication requests can no longer send replication requests to each other, which solves the problem of circular replication. Use the version number to prevent the old data from being copied to the new data, and use the heartbeat mechanism to repair the data
How to determine if a node is down Heartbeat? Heartbeats will be removed if they are not active, and it may be determined to be down for network reasons. Therefore, with the self-protection mechanism, a large number of heartbeats lost in a short period of time will open the self-protection mechanism and will not be removed temporarily. After the network failure recovers, the self-protection mechanism will be exited. The client also has a cache registration list, in case all Eureka cannot be accessed due to network problems, as long as there is a node in eureka, the entire service registration center can be used.

Fifth, the difference between Zookeeper and Redis implementing distributed locks?

Zookeeper Redis
The idea of ​​implementing distributed locks 1. Node unique 2. Event notification 3. Temporary node 1. Key unique 2. Event notification 3. Temporary key
Acquire lock Create the same temporary node at the same time, whoever creates it first will acquire the lock Use the setnx command to create the same key, create a successful acquisition lock, return 0 if the creation fails, and 1 if successful
Release lock Deleting a node is equivalent to releasing the lock. Node events notify other threads to start competing for locks Deleting the key is equivalent to releasing the lock. The event notifies other threads to start competing for the lock
Timeout lock The temporary node time ends (Session), indicating that the lock has timed out and the transaction is rolled back The key time is over, indicating that the lock has timed out and the transaction is rolled back

6. The underlying election principle of Zookeeper? How to maintain data consistency?

The zxid is the leader, and the zxid is the same as the myid. Myid is set by itself. It is
based on the Zab atomic broadcast protocol to maintain data consistency. It is similar to the 2PC two-phase submission protocol
. When a transaction request is issued, our leader node will create a global zxid (transaction id, the larger the zxid, the newer the data)
2. The leader node will bring zxid to each follower in the first stage of the notification stage The node sends a confirmation synchronization notification (zxid + 1 at this time)
3. As long as more than half of the follower nodes confirm the synchronization ack, the leader will send commit transaction data to all followers at this time;
zxid is modified after the first phase of the notification is successful ,

7. What is CAP theory? Why can't CAP take care of all three?

C: Strong data consistency, ensure that the data can not be wrong, the service is not available when data is synchronized, pay attention to data consistency
A: Weak consistency of data, data can be wrong, allow dirty reads, service is available when data is synchronized, data will eventually be consistent , Pay attention to service availability
P: network partition fault tolerance, allowing network failures, will not judge a node down due to network failures

CAP takes care of the three, that is, the service is available and the data is consistent. For example: when the master node synchronizes the data to the slave node, the data of the slave node is not the latest. If you send a request to the slave node, the returned data must be old data. If you want to ensure that it is new data, you can only synchronize The stage service is not available.

8. Why must the Zookeeper cluster nodes be odd? Why is the number of clusters greater than three?

Reasons for odd number
1. Because the remaining number of nodes must satisfy more than half of the mechanism, the remaining nodes> total nodes / 2, three clusters and four clusters can only be down one, it is not as good as three
2, even number is easy to cause the same number of votes

The reason why the number of clusters is greater than three: if
two of them are clustered, the failure of one will not satisfy more than half of the mechanism, it is better not to cluster

9. What are the three types of nodes in Zookeeper? What is the role?

1. Leader type leader type is responsible for writing requests, and is synchronized with each node;
2. Follower type follower is responsible for reading requests and voting resolution
3. Observer type observer and Follower most of the characteristics are the same, the only difference is that they can not participate Election and voting
Note: When the zk cluster is expanded in the later period, it is recommended not to use the follow node type, which will cause the election time to become longer.
Why use ObServer type?
Mainly does not affect the efficiency of the original election time, the purpose is to improve the efficiency of the client query; the
write operation is forwarded to the leader to write, the read operation follower or observer read

10. What is the difference between Zab atomic broadcast protocol, Paxos protocol and 2PC / 3PC?

eleven,

12. Talk about the split brain problem in Zookeeper? How to solve?

In the case of a cluster, generally only one master node will be elected, and other nodes will comfort you from the ground, then if the network jitters or some nodes cannot communicate, it will cause some nodes to re-elect elections, which will exist Multiple master nodes.

Solve the split brain problem through more than half of the mechanism. The number of remaining nodes is greater than half, not equal to half, either there is no leader, or there is only one leader

thirteen,

Fourteen, ACL four types of permissions

1. world: default, accessible all over the world
2. auth: authenticated users can access
3. digest: username and password access, commonly used
4. ip: specified ip can access

Fifteen, there are four types of nodes:

1. Temporary node-the session closes and disappears automatically (zk.close ())
2. Persistent node-permanently persistent to the hard disk
3. Temporary ordered node, self-increment
4, persistent ordered node

Sixteen, distributed lock implementation method: concept: to solve multiple jvms, only one jvm can execute in the end

1. Implement distributed lock based on database 2. Implement distributed lock
based on Redis 3. Implement distributed lock
based on Zk 4. Implement distributed lock
based on redisson framework

17.

18. What is the difference between Eureka and Nacos?

Nineteen, the zab atomic broadcast protocol has two modes

1. Recovery mode: When the node is started for the first time, the leader is elected, and the slave node enters recovery mode to synchronize the data of the master node.
2. Broadcast mode: solve the data synchronization problem of each node

20. 2PC two-phase submission protocol to solve the problem of distributed data synchronization?

The 2PC two-phase commit protocol is used in distributed transaction scenarios to solve the data consistency among multiple distributed systems, such as the database XA mechanism.
Multiple servers write at the same time, all errors are rolled back

. It is a global transaction id
transaction request: only in the write operation will the transaction
Zk bottom layer be used to solve the distributed transaction problem.

21. Under what circumstances will zxid be inconsistent?

One suddenly went down during synchronization

twenty two,

twenty-three,

twenty four,

twenty five,

Twenty-six,

Twenty-seven,

Twenty-eight,

Twenty-nine,

thirty,

The root node is also allowed
to create multiple parent nodes without the node name. You must first create a parent node and then create a child node, otherwise an error will be reported.

Single jvm multi-threads generate order numbers consistent issue using synchronized or lock lock
multiple jvm generate order number at the same time if duplicate

The synchronized or lock lock can only be valid in the local jvm

The ZK transaction maintains data consistency based on the 2PC two-phase submission protocol. When the master node processes the transaction request, if the master node does not have an exception, the master node will first notify the slave node to synchronize the data, or will the master node submit the transaction Anymore? Is the increase in zxid before or after notifying the slave node to synchronize?

Published 53 original articles · Likes2 · Visits 1868

Guess you like

Origin blog.csdn.net/qq_42972645/article/details/104626705