Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the getlasterror function of MongoDB by Redis

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly shows you "Redis how to achieve the getlasterror function of MongoDB", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Redis how to achieve the getlasterror function of MongoDB" this article.

MongoDB does not wait for the data to be written before returning by default. You can ensure that your write is successful by using the getLastError command.

To put it bluntly, the write of MongoDB is asynchronous, and the return to the client is successful, but the write is not necessarily successful, and when multiple data nodes replication, there is no guarantee that the data will be synchronized to other nodes. GetLastError is created for this. You specify a condition, and then the function blocks until the server informs you that the condition is met. (of course, to prevent death, etc., this function also has a timeout parameter), example:

Db.runCommand ({getlasterror:1,w:2})

The above command indicates that the write operation is written to two nodes and then returns success.

This function makes the guarantee of data security more flexible.

Recently, some friends have implemented this feature on Redis, not by modifying the Redis source code, but by using the existing features to spell it out. The specific process is as follows:

How to realize the getlasterror function of MongoDB by Redis

1. ClientSUBSCRIBEstoKEYonslave *

2.ClientwritesKEY (e.g.setfoobar) tomaster

3.ClientpublishesKEYtomaster

Due to the single-threaded write operation of Redis, the following two operations are performed sequentially

4.MasterwillreplicateKEYtoslave

5Masterwillpropogate*PUBLISHKEY*toslave

Finally, after Master synchronizes the data, the client receives a message from SUBSCRIBE:

6.GetsKEYeventonSUBSCRIBE!

The above is all the content of the article "how to achieve the getlasterror function of MongoDB by Redis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report