Interviewer: How does Zookeeper solve the problem of inconsistency between read-write and double-write concurrency, and the implementation principle of shared locks?

hello! Hello everyone, I'm Xiaoqi, an unreliable programmer.
Xiaoqi intends to share some techniques in a relaxed and humorous way. If you think you have learned something through Xiaoqi's article, then give Xiaoqi a like.
The article is continuously updated, you can search for [Xiaoqi JAVA Interview] on WeChat to read it as soon as possible, and reply to [Information] and there are more benefits I have prepared for you!

I. Introduction

Today, during the Qingming holiday, when the cherry blossoms in Beijing Yuyuantan Park were in full bloom, my girlfriend insisted on taking me to see the cherry blossoms.

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Coming here is like coming to the ocean of flowers, which makes the artistic bacteria all over me start restless, and I can't help but sing a poem "The cherry blossoms fall to the end, and the dragon marks the five streams. You until Ye Langxi", ah, beautiful cherry blossoms.

At this time, the park ranger next to me looked at me with confusion and asked me, lad, did you memorize the poem wrong? Shouldn't it be Yang Hualuoziquyi?

I can only smile awkwardly and say that today, I will change the words myself. . .

insert image description here

Just when I was showing my full body of artistic bacteria, suddenly the phone rang, I saw it was a Beijing number, the phone number ended in 6543, and I connected the call

me: hello

Opponent: Hello, is that Xiao Qi?

Me: Exactly here, may I ask who is yours?

The other party: I am from XX company. I saw your resume on XX recruitment website. Are you still looking for a job?

Me: Yes, I'm still looking.

The other party: Is it convenient for you to interview now?

At this time, I looked at my family leader, and my family leader said with disgust, let you accompany me out to the park, it's not windy or rainy, let's face it.

2. Interview

Me: Hello interviewer, I am now convenient for the interview.

Interviewer: Okay, I see your resume says you are proficient in zookeeper, right?

Me: I don't dare to do it. It's all my colleagues who love me. I'm really ashamed (in fact, my heart has swelled up).

insert image description here

3. Inconsistency between reading and writing and double writing

Interviewer: Okay, what about the inconsistency between reading and writing and double writing under concurrent conditions?

Me: The problem of read-write inconsistency is that in the concurrent situation, multiple threads read and write the same data at the same time, resulting in inconsistent read-write.

insert image description here

For example, here my thread A query cache is empty, then I need to query the database, the query database name is Zhang San, and then I need to change the cache name to Zhang San.

But at this time, after thread A queries the database and before updating the cache, thread B modifies the data name of the database to Li Si, but the cache name is still Zhang San, so there is an inconsistency between the database and the cache.

Interviewer: Well, what about the inconsistency between the two writings?

insert image description here

Double write inconsistency means that both threads are writing operations, and the result still causes the problem of inconsistency between the database and the cache.

For example, thread B modifies the data after thread A writes the data and before updating the cache, but it is thread A that finally modifies the cache, so the database and cache are inconsistent at this time.

Interviewer: Well, how to solve it?

Me: use shared lock to solve.

Fourth, the realization principle of Zookeeper shared lock

Interviewer: Well, how does zookeeper implement shared locks, and what is its implementation principle?

Me: This is a bit complicated to talk about, or I will go to the park with my girlfriend first, and let's talk when we go back in the evening.

Interviewer: No, you have aroused my interest now, let's finish talking at once, let's talk

Me: Well, the implementation principle of zookeeper shared lock is that if the adjacent threads are all read requests, then they share the same lock.

insert image description here

Here we can see that threads 1, 2, and 3 are the operations of three consecutive read requests, so they share the same lock A, that is, the three threads can read data concurrently.

Then we see that thread 4 is a write request, so he can only perform his own write operation after all the previous threads are executed.

Here thread 5 is also a write request, so he can only execute his own write operation after all the threads in front of him have finished executing.

Then threads 6, 7, and 8 are all read threads, so they acquire the same lock. After thread 5 finishes executing, threads 6, 7, and 8 can perform read operations together.

We said that the monitoring here is to solve the flock problem.

Interviewer: This guy is amazing. I have nothing to ask here. Do you have any questions to ask me?

me: uh. . . Our company has a lot of overtime.

Interviewer: How do you see it? You've never been to the company, how do you know?

Me: Because it's a holiday and you're still making an interview appointment, you can imagine the company's overtime culture is quite strong.

Interviewer: Well, our company does not advocate overtime work, but employees love to work overtime.

me: uh. . . Then I'll think about it for two days, I feel like I might be a little out of place. . .

Interviewer: Then if you don't come, can you help recommend some other students?

Me: Then you should learn the principle of zookeeper first, otherwise, if I recommend some loyal readers of the Xiaoqi interesting programming series, you will definitely be overwhelmed.

Interviewer: Then I'll take a look at Xiaoqi's interesting programming series, and then I'll see the tricks

me: uh. . . In that case, both of them are in a state of perfection, and it is a bit embarrassing that neither of them can abuse the other. . .

V. Summary

The zookeeper here has not been sorted out, and the article will continue to be updated later. It is recommended to collect it.

The commands involved in the article must be knocked several times like me. Only in the process of knocking can you find out whether you really master the commands.

If you think my article is not bad, please like it. In addition, you can search [Xiaoqi JAVA Interview] on WeChat to read more good articles and get the information I have prepared for you.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44096133/article/details/123963417