003 Abstractions
003 Abstractions
• What is abstraction?
• Database abstraction
• Abstractions in distributed systems
What is abstraction?#
Abstraction is the art of obfuscating details that we don’t need. It allows us to
concentrate on the big picture. Looking at the big picture is vital because it hides
the inner complexities, thus giving us a broader understanding of our set goals
and staying focused on them. The following illustration is an example of
abstraction.
Abstraction of a bird
With the abstraction shown above, we can talk about birds in general without
being bogged down by the details.
The developers use a lot of libraries to develop the big systems. If they start
building the libraries, they won’t finish their work. Libraries give us an easy
interface to use functions and hide the inside detail of how they are
implemented. A good abstraction allows us to reuse it in multiple projects with
similar needs.
Database abstraction
Transactions is a database abstraction that hides many problematic outcomes
when concurrent users are reading, writing, or mutating the data and gives a
simple interface of commit, in case of success, or abort, in case of failure. Either
way, the data moves from one consistent state to a new consistent state. The
transaction enables end users to not be bogged down by the subtle corner-cases
of concurrent data mutation, but rather concentrate on their business logic.
• What is an RPC?
• How does RPC work?
• Summary
What is an RPC?
RPC is an interprocess communication protocol that’s widely used in distributed
systems. In the OSI model of network communication, RPC spans the transport
and application layers.
When the procedure execution finishes, the results are returned to the calling
environment where execution restarts as a regular procedure call.
To see how it works, let’s take an example of a client-server program. There are
five main components involved in the RPC program, as shown in the following
illustration:
Client Server
RPC runtime
The client, the client stub, and one instance of RPC runtime are running on the
client machine. The server, the server stub, and one instance of RPC runtime are
running on the server machine.
5. The server stub unpacks the message, takes the parameters out of it, and
calls the desired server routine, using a local procedure call, to do the
required execution.
Client Server
Execute
Return Call Call Return
10 1 5 6
9 2 4 7
RPC RPC
runtime runtime
Wait
Receive Send Receive Send
3
Call packet
Result packet
8
Summary
The RPC method is similar to calling a local procedure, except that the called
procedure is usually executed in a different process and on a different
computer.
• What is consistency?
• Eventual consistency
• Causal consistency
• Sequential consistency
• Strict consistency aka linearizability
• Summary
What is consistency?
In distributed systems, consistency may mean many things. One is that each
replica node has the same view of data at a given point in time. The other is that
each read request gets the value of the recent write. These are not the only
definitions of consistency, since there are many forms of consistency. Normally,
consistency models provide us with abstractions to reason about the
correctness of a distributed system doing concurrent data reads, writes, and
mutations.
Strongest consistency
Weakest consistency
There are consistency models that lie between these two ends, some of which are
shown in the following illustration:
Strict consistency/
linearizability
Sequential
consistency
Causal
consistency
Eventual
consistency
Weakest Strongest
consistency consistency
model model
Database rules are at the heart of ACID consistency. If a schema specifies that a
value must be unique, a consistent system will ensure that the value is unique
throughout all actions. If a foreign key indicates that deleting one row will also
delete associated rows, a consistent system ensures that the state can’t contain
related rows once the base row has been destroyed.
Eventual consistency
Eventual consistency is the weakest consistency model. The applications that
don’t have strict ordering requirements and don’t require reads to return the
latest write choose this model. Eventual consistency ensures that all the replicas
will eventually return the same value to the read request, but the returned value
isn’t meant to be the latest value. However, the value will finally reach its latest
state.
x=2
Eventual consistent
distributed system
1 of 6
x=2
T1: write(x) = 10
Eventual consistent
Alice distributed system
2 of 6
x = 2 10
T1: write(x) = 10
Eventual consistent
Alice distributed system
x = 2 10
4 of 6
Alice and Bob read from a different replica where values haven't yet updated, so they get stale value.
x = 2 10
The system returns the same value to both read requests, but the value is old
5 of 6
x = 2 10
At time T3, system reaches the stable state and returns the latest value to both read
requests
6 of 6
Example
The domain name system is a highly available system that enables name
lookups to a hundred million devices across the Internet. It uses an eventual
consistency model and doesn’t necessarily reflect the latest values.
Causal consistency
Causal consistency works by categorizing operations into dependent and
independent operations. Dependent operations are also called causally-related
operations. Causal consistency preserves the order of the causally-related
operations.
Time
P1 write(x)a
x=a
b= x+5
y=b
P2 read(x)a write(y)b
Causally related
operations
Causal consistency
This model doesn’t ensure ordering for the operations that are not causally
related. These operations can be seen in different possible orders.
Causal consistency is weaker overall, but stronger than the eventual consistency
model. It’s used to prevent non-intuitive behaviors.
Example
The causal consistency model is used in a commenting system. For example, for
the replies to a comment on a Facebook post, we want to display comments after
the comment it replies to. This is because there is a cause-and-effect relationship
between a comment and its replies.
Note: There are many consistency models other than the four discussed
in this lesson, and there is still room for new consistency models.
Researchers have developed new consistency models. For example,
Wyatt Lloyd, et al., proposed the causal+consistency model to speed up
some specific types of transactions.
Sequential consistency
Sequential consistency is stronger than the causal consistency model. It
preserves the ordering specified by each client’s program. However, sequential
consistency doesn’t ensure that the writes are visible instantaneously or in the
same order as they occurred according to some global clock.
Example
There are three users of a system consisting of three nodes (replicas), and each node
initially has a value of x that equals 2
1 of 9
x=2 x=2 x=2
T1: write(x): 10
2 of 9
x = 10 x=2 x=2
T1: write(x): 10
ack
Node A performs the write operation on x and returns an acknowledgment back to Alice
3 of 9
x = 10 x=2 x=2
T1: write(x): 10
ack
Node A forwards the write operation to the other nodes in the system
4 of 9
x = 10 x = 10 x=2
T1: write(x): 10
ack
5 of 9
x = 10 x = 10 x=2
T1: write(x): 10
ack
T2: read(x)
6 of 9
x = 10 x = 10 x=2
T1: write(x): 10
ack
T2: read(x)
10
7 of 9
x = 10 x = 10 x=2
T1: write(x): 10
ack
T2: read(x)
10
T3: read(x)
8 of 9
x = 10 x = 10 x=2
T3: read(x)
2
Node C has not yet received the write operation, so it returns the old value of x = 2 to Bob
9 of 9
Example
Summary
Linearizable services appear to carry out transactions/operations in sequential,
real-time order. They make it easier to create suitable applications on top of
them by limiting the number of values that services can return to application
processes.
Linearizable services have worse performance rates than services with weaker
consistency in exchange for their strong assurances. Think about a read in a key-
value store that returns the value written by a concurrent write. The read
imposes no limits on future reads if the key-value store is weakly consistent.
• Fail-stop
• Crash
• Omission failures
• Temporal failures
• Byzantine failures
Failures are obvious in the world of distributed systems and can appear in
various ways. They might come and go, or persist for a long period.
Byzantine
Temporal
Omission
Crash
Fail-stop
Easy to Difficult to
deal with deal with
This is a spectrum of failure models. The difficulty level when dealing with a failure
increases as we move to the right
Fail-stop
In this type of failure, a node in the distributed system halts permanently.
However, the other nodes can still detect that node by communicating with it.
Crash
In this type of failure, a node in the distributed system halts silently, and the
other nodes can’t detect that the node has stopped working.
Omission failures
In omission failures, the node fails to send or receive messages. There are two
types of omission failures. If the node fails to respond to the incoming request,
it’s said to be a send omission failure. If the node fails to receive the request
and thus can’t acknowledge it, it’s said to be a receive omission failure.
Temporal failures
In temporal failures, the node generates correct results, but is too late to be
useful. This failure could be due to bad algorithms, a bad design strategy, or a
loss of synchronization between the processor clocks.
Byzantine failures
The Spectrum of Failure Models
In Byzantine failures, the node exhibits random behavior like transmitting
arbitrary messages at arbitrary times, producing wrong results, or stopping
midway. This mostly happens due to an attack by a malicious entity or a
software bug. A byzantine failure is the most challenging type of failure to deal
with.