Demystifying MongoDB Consistency

MongoDB Consistency Scenarios

Continuing from the previous Post

Data consistency is a big deal, so its worth taking a look at MongoDB handles consistency. In a single database scenario, if we have 2 observers A & B, and there is a document with a value X=’123′. At some point A decides to update the document with say  ‘789’ , during that update B’s observation could either be 123 or 789 if it waited for the  lock to be released.

When you scale out things become a little more involved, assume again 2 observers A & B with a primary database and a secondary database. Mongo only accepts writes into the primary database at a time. So lets say A updates the document and sets the value to 789, replication should also occur to take that value and populate it in the secondary database. Eventually the value will be replicated in the primary as well as the secondary database. But lets take a step backward, A may have updated the document, but B may not see that change yet because replication has not happened yet to the secondary database. This is what is called as Eventual Consistency. Eventually the document will make it over to the secondary database.

Demystifying MongoDB Consistency


Table Variables

Microsoft introduced table variables with SQL Server 2000 as an alternative to using temporary tables. In many cases a table variable can outperform a solution using a temporary table, although we will need to review the strengths and weaknesses of each in this article.Table variables store a set of records, so naturally the declaration syntax looks very similar to a CREATE TABLE statement, as you can see in the following example:

Table Variables

c# Interview Questions

What’s the difference between the Debug class and Trace class?
Documentation looks the same.  Use Debug class for debug builds, use Trace class for both debug and release builds.

Can you declare an override method to be static if the original method is not static?
No.  The signature of the virtual method must remain the same.  (Note: Only the keyword virtual is changed to keyword override)

What is the difference between a Struct and a Class?
Structs are value-type variables and are thus saved on the stack, additional overhead but faster retrieval.  Another difference is that structs cannot inherit.

What’s the difference between an interface and abstract class?
In an interface class, all methods are abstract – there is no implementation.  In an abstract class some methods can be concrete.  In an interface class, no accessibility modifiers are allowed.  An abstract class may have accessibility modifiers.

When do you absolutely have to declare a class as abstract?
1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.
2.  When at least one of the methods in the class is abstract.

What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array.  The CopyTo() method copies the elements into another existing array.  Both perform a shallow copy.  A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array.  A deep copy (which neither of these methods performs) would create a new instance of each element’s object, resulting in a different, yet identacle object.

Note: MemberwiseClone() also creates a new copy but its a shallow copy. To perform a deep copy class should be marked as serializable and the object to be copied should be serialized and deserialized.

Explain ACID rule of thumb for transactions.
A transaction must be:
1. Atomic – it is one unit of work and does not dependent on previous and following transactions.
2. Consistent – data is either committed or roll back, no “in-between” case where something has been updated and something hasn’t.
3. Isolated – no transaction sees the intermediate results of the current transaction).
4. Durable – the values persist if the data had been committed even if the system crashes right after.

When should you call the garbage collector in .NET?
As a good rule, you should not call the garbage collector.  However, you could call the garbage collector when you are done using a large object (or set of objects) to force the garbage collector to dispose of those very large objects from memory.  However, this is usually not a good practice.

Note:  GC can be implemented using Finalize or extend the idisposable interface  and implement the dispose() function . Finalize method is nothing but a destructor such as ~myClass(){}. GC.Collect()  can be used to force Garbage Collection.

How do you convert a value-type to a reference-type?
Use Boxing.

Boxing vs Unboxing

Boxing is the process of converting a value type to the type object.When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit.

int i = 123;
// The following line boxes i. 
object o = i;

o = 123;
i = (int)o;  // unboxing

In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed.

Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface. An unboxing operation consists of:

Checking the object instance to make sure that it is a boxed value of the given value type.

Copying the value from the instance into the value-type variable.

The following statements demonstrate both boxing and unboxing operations:

int i = 123;      // a value type 
object o = i;     // boxing 
int j = (int)o;   // unboxing

For all your application development needs, visit for a fiscally conscious proposal that meets your needs ( So I can keep this blog going as well!!!!)

Alternatively click through the link   if you found this article interesting. (This will help the companies Search engine rankings)