Sobes.tech
Middle+

What is sequential consistency?

sobes.tech AI

Answer from AI

Sequential Consistency is a consistency model in distributed systems or multithreaded programs that guarantees that the result of any execution is the same as if all operations of all processors (threads) were executed in some sequential order, and the operations of each individual processor (thread) appear in this sequence in the order in which they were issued in the program.

In other words:

  1. Preservation of order of operations within a thread: Operations initiated by one thread are visible to other threads in the same order in which they were performed by that thread.
  2. Single global order: There exists a single, global, linear order of execution of all operations of all threads, which is respected by all threads.

This model is one of the strongest and most intuitive, but its implementation can be costly in terms of performance due to the need for synchronization and preventing reordering of operations by the compiler and hardware.

Example:

// Thread 1
x = 1;
y = 2;

// Thread 2
// Read operations for x and y

With sequential consistency, if Thread 1 first writes 1 to x, and then 2 to y, then Thread 2 will never "see" y as 2 before it sees x as 1. Possible sequential executions:

  • x = 1; y = 2; read(x); read(y); (read(x) = 1, read(y) = 2)
  • x = 1; read(x); y = 2; read(y); (read(x) = 1, read(y) = 2)
  • read(x); x = 1; read(y); y = 2; (read(x) might be 0, read(y) might be 0, then read(x) = 1, read(y) = 2)

It is impossible for Thread 2 to see y = 2 while x is still 0 if Thread 1 first writes x = 1, then y = 2.