WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Software Engineering Β· Basic Β· question 19 of 100

Can you explain the difference between "strong" and "weak" consistency models in distributed systems?

πŸ“• Buy this interview preparation book: 100 Software Engineering questions & answers β€” PDF + EPUB for $5

In distributed systems, strong and weak consistency models refer to the degree to which different nodes in the system agree on the state of the data.

A strong consistency model ensures that all nodes see the same version of the data at any given time, providing a linearizable view of the system. In other words, if a write operation completes, all subsequent read operations will return the value written. This consistency model provides the illusion of a single, monolithic system and makes it easier to reason about the behavior of the distributed application. However, achieving strong consistency introduces a lot of overhead in terms of coordination, resulting in higher latency and reduced availability.

A weak consistency model, on the other hand, allows for more flexibility in the synchronization between different nodes. In a weak consistency model, the system only provides a best-effort guarantee of consistency, allowing different nodes to see different versions of the data at different times. For example, in a system that replicates data between nodes, a write operation may complete at one node and not yet be propagated to another node, resulting in inconsistent views of the data for a short period of time. Weak consistency is often used in systems where high availability and low latency are more important than strict consistency, such as in distributed caching systems, where a small amount of stale data is tolerable as long as it is updated eventually.

To illustrate these concepts, let’s consider an example of a distributed key-value store. In a strong consistency model, if a client writes a value to a key and then reads that key immediately after, it will always retrieve the value that was just written. In contrast, in a weak consistency model, if the client writes a value to a key and then immediately reads that key from a different node, it may retrieve the previous value stored at the key instead of the value that was just written.

Here is an example of how strong consistency can be implemented using the Raft consensus algorithm:

# Raft algorithm for consensus

class RaftNode:
  def __init__(self):
    # Initialize node state
    self.currentTerm = 0
    self.votedFor = None
    self.log = []
    self.commitIndex = 0
    self.lastApplied = 0
    
    # Start election timer
    self.startElectionTimer()
    
  def requestVote(self, candidateId, lastLogIndex, lastLogTerm):
    # Handle vote request from candidate
    if self.currentTerm > candidateTerm:
      return False
    
    # Check candidate's log for consistency
    if (lastLogTerm > self.log[-1].term) or \
        (lastLogTerm == self.log[-1].term and lastLogIndex >= len(self.log)):
      # Grant vote if candidate log is at least as up-to-date as the voter's log
      self.votedFor = candidateId
      return True
    
    return False
    
  def appendEntries(self, prevLogIndex, prevLogTerm, entries, leaderCommit):
    # Handle append entries request from leader
    if self.currentTerm > leaderTerm:
      return False
    
    # Check log for consistency
    if (prevLogIndex == 0 or prevLogTerm == self.log[prevLogIndex].term) and \
        self.log[prevLogIndex+1:] == entries:
      # Append entries to log
      self.log += entries
      
      # Update commit index
      self.commitIndex = min(leaderCommit, len(self.log)-1)
      
      return True
    
    return False

In this example, when a client writes a value to a key, the write request is sent to the Raft leader. The leader appends the write to its log and sends append entries requests to all followers. The write is considered committed once a majority of nodes acknowledge receipt of the append entries request, and the leader updates its commit index accordingly. When a client reads a key, it sends a read request to the Raft leader, which uses its commit index to provide a strong, linearizable view of the system.

In contrast, here is an example of how weak consistency can be implemented using the Amazon Dynamo key-value store:

# Amazon Dynamo algorithm for key-value store

class DynamoNode:
  def __init__(self, partition):
    # Initialize node state
    self.partition = partition
    self.KVS = {}
    
  def read(self, key):
    # Handle read request by returning the locally stored value, if available
    if key in self.KVS:
      return self.KVS[key]
    
    # Look up key in other nodes to provide eventual consistency
    value = self.partition.lookup(key)
    if value is not None:
      return value
    
    # Return default value if key is not found
    return ""
    
  def write(self, key, value):
    # Handle write request by storing the value locally and propagating
    # the update to other nodes
    self.KVS[key] = value
    self.partition.propagate(key, value)

In this example, when a client writes a value to a key, the write request is stored locally at the node responsible for that key’s partition. The node then propagates the update to other nodes responsible for the same partition, but does not wait for acknowledgement before returning to the client. When a client reads a key, the node looks up the key locally and in other nodes, but may return a stale value if the value has not yet been propagated to all nodes. In this way, the system provides eventual consistency, allowing different nodes to have different views of the data at different times.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Software Engineering interview β€” then scores it.
πŸ“ž Practice Software Engineering β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Software Engineering questions & answers β€” PDF + EPUB for $5

All 100 Software Engineering questions Β· All topics