What is causal consistency in mongodb?

Hi @Caio_Morceli and welcome in the MongoDB Community :muscle: !

Let’s try to explain the 2 concepts with a few lines:

Eventual Consistency means that the data you are reading might not be consistent right now but it will be eventually. You get this if you read from secondaries using any of the readPreference that can read from a secondary. This means you chose to race with the replication and you don’t have the guarantee to read your own writes as you chose to write on the Primary and read from a Secondary without a guarantee.

Causal Consistency basically prevents that from happening. If within a causal consistent session you write something, then read it 2 lines later, you now have a guarantee that you will read this write operation no matter what, even if you are racing against the replication. Of course it’s a trade off, this means you will have to hang a little to get what you want.

Few years ago when 3.6 was released with this new feature, I published this demo (which is a bit old but…)

The idea was to demonstrate the concept. To guarantee that my secondary was slower than my script, I sent an internal command to pause the replication process. This is just to get a “consistent” result every time I run this script and not only when I get lucky with the race.

Cheers,
Maxime.

3 Likes