The Programmer's Guide
  • About
  • Algorithm
    • Big O Notation
      • Tree
      • Problems
    • Basic Notes
    • Data Structure Implementation
      • Custom LinkedList
      • Custom Stack
      • Custom Queue
      • Custom Tree
        • Binary Tree Implementation
        • Binary Search Tree Implementation
        • Min Heap Implementation
        • Max Heap Implementation
        • Trie Implementation
      • Custom Graph
        • Adjacency List
        • Adjacency Matrix
        • Edge List
        • Bidirectional Search
    • Mathematical Algorithms
      • Problems - Set 1
      • Problems - Set 2
    • Bit Manipulation
      • Representation
      • Truth Tables
      • Number System
        • Java Program
      • Problems - Set 1
    • Searching
    • Sorting
    • Array Algorithms
    • String Algorithms
    • Tree
      • Tree Traversal Techniques
      • Tree Implementation
      • Applications of Trees
      • Problems - Set 1
    • Graph
      • Graph Traversal Techniques
      • Shortest Path Algorithms
      • Minimum Spanning Tree (MST) Algorithms
    • Dynamic Programming
      • Problems - Set 1
    • Recursion
    • Parallel Programming
    • Miscellaneous
      • Problems - Set 1
  • API
    • API Basics
      • What is an API?
      • Types of API
        • Comparison - TBU
      • Synchronous vs Asynchronous API
    • API Architecture
      • Synchronous & Asynchronous Communication
    • API Specification
  • Cloud Computing
    • Cloud Fundamentals
      • Cloud Terminology
      • Core Terminology
      • Cloud Models
      • Cloud Service Models
      • Benefits, Challenges and Risk of Cloud Computing
      • Cloud Ecosystem
  • Database
    • DBMS
      • Types of DBMS
        • Relational DBMS (RDBMS)
        • NoSQL DBMS
        • Object-Oriented DBMS (OODBMS)
        • Columnar DBMS
        • In-Memory DBMS
        • Distributed DBMS
        • Cloud-Based DBMS
        • Hierarchical DBMS
      • DBMS Architecture
      • DBMS Structure
    • SQL Databases
      • Terminology
      • RDBMS Concepts
        • Entity Relationship Diagram (ERD)
          • ERD Examples
        • Normalization
        • Denormalization
        • ACID & BASE Properties
          • ACID Properties
          • BASE Properties
        • Locking and Unlocking
      • SQL Fundamentals
        • SQL Commands
          • DDL (Data Definition Language)
          • DML (Data Manipulation Language)
          • DCL (Data Control Language)
          • TCL (Transaction Control Language)
          • DQL (Data Query Language)
        • SQL Operators
          • INTERSECT
          • EXCEPT
          • MINUS
          • IN and NOT IN
          • EXISTS and NOT EXISTS
        • SQL Clauses
          • Joins
          • OVER
          • WITH
          • CONNECT BY
          • MODEL
          • FETCH FIRST
          • KEEP
          • OFFSET with FETCH
        • SQL Functions
          • Oracle Specific
        • SQL Data Types
          • Numeric Types
          • Character Types
          • Date & Time Types
          • Large Object Types
        • Others
          • Indexing
      • Vendor Specific Concepts
        • Oracle Specific
          • Data Types
          • Character Set
          • Rownum, Rowid, Urowid
          • Order of Execution of the query
          • Keys
          • Tablespace
          • Partition
      • Best Practice
      • Resources & References
        • O’Reilly SQL Cookbook (2nd Edition)
          • 1. Retrieving Records
          • 2. Sorting Query Results
          • 3. Working with Multiple Tables
          • 4. Inserting, Updating, and Deleting
          • 5. Metadata Queries
          • 6. Working with Strings
          • 7. Working with Numbers
          • 8. Date Arithmetic
          • 9. Date Manipulation
          • 10. Working with Ranges
          • 11. Advanced Searching
          • 12. Reporting and Reshaping
          • 13. Hierarchical Queries
          • 14. Odds 'n' Ends
    • SQL vs NoSQL
    • Best Practices
  • Git
    • Commands
      • Setup and Configuration Commands
      • Getting and Creating Projects
      • Tracking Changes
      • Branching and Merging
      • Sharing and Updating Projects
      • Inspection and Comparison
      • Debugging
      • Patching
      • Stashing and Cleaning
      • Advanced Manipulations
    • Workflows
      • Branching Strategies
        • Git Flow
        • Trunk-Based Development
        • GitHub Flow
        • Comparison
      • Merge Strategies
        • Merge
        • Rebase
        • Squash
        • Fast-forward vs No-fast-forward
        • MR vs PR
      • Conflict Resolution
        • Handling Merge Conflicts
        • Merge Conflicts
        • Rebase Conflicts
        • Divergent Branches After git pull
        • Force Push
      • Patch & Recovery
        • Cherry-pick strategies
        • Revert vs Reset
        • Recover from a bad rebase
      • Rebasing Practices
        • Merge vs Rebase
        • Rebase develop branch on main branch
      • Repository Management
        • Working Directory
        • Mirror a repository
        • Convert a local folder to a Git repo
        • Backup and restore a Git repository
  • Java
    • Java Installation
    • Java Distributions
    • Java Platform Editions
      • Java SE
      • Java EE
      • Jakarta EE
      • Java ME
      • JavaFX
    • Java Overview
      • OOP Principles
        • Encapsulation
        • Inheritance
        • Polymorphism
        • Abstraction
          • Abstract Class & Method
          • Interface
            • Functional Interfaces
            • Marker Interfaces
          • Abstract Class vs Interface
      • OOP Basics
        • What is a Class?
          • Types of Classes
        • What is an Object?
          • Equals and HashCode
            • FAQ
          • Shallow Copy and Deep Copy
          • Ways to Create Object
          • Serialization & Deserialization
        • Methods & Fields
          • Method Overriding & Overloading
          • Method Signature & Header
          • Variables
        • Constructors
        • Access Modifiers
      • Parallelism & Concurrency
        • Ways to Identify Thread Concurrency or Parallelism
        • Thread Basics
          • Thread vs Process
          • Creating Threads
          • Thread Context Switching
          • Thread Lifecycle & States
          • Runnable & Callable
          • Types of Threads
          • Thread Priority
        • Thread Management & Synchronisation
          • Thread Resource Sharing
          • Thread Synchronization
            • Why is Synchronization Needed?
            • Synchronized Blocks & Methods
          • Thread Lock
            • Types of Locks
            • Intrinsic Lock (Monitor Lock)
            • Reentrant Lock
          • Semaphore
          • Thread Starvation
          • Thread Contention
          • Thread Deadlock
          • Best Practices for Avoiding Thread Issues
      • Keywords
        • this
        • super
        • Access Modifiers
      • Data Types
        • Default Values
        • Primitive Types
          • byte
          • short
          • int
          • long
          • float
          • double
          • char
          • boolean
        • Non-Primitive (Reference) Types
          • String
            • StringBuilder
            • StringBuffer
              • Problems
            • Multiline String
            • Comparison - String, StringBuilder & StringBuffer
          • Array
          • Collections
            • List
              • Array vs List
              • ArrayList
              • Vector
                • Stack
                  • Problems
              • LinkedList
            • Queue
              • PriorityQueue
              • Deque (Double-Ended Queue)
                • ArrayDeque
                • ConcurrentLinkedDeque - TBU
                • LinkedBlockingDeque - TBU
            • Map
              • HashMap
              • Hashtable
              • LinkedHashMap
              • ConcurrentHashMap
              • TreeMap
              • EnumMap
              • WeakHashMap
            • Set
              • HashSet
              • LinkedHashSet
              • TreeSet
              • EnumSet
              • ConcurrentSkipListSet
              • CopyOnWriteArraySet
        • Specialized Classes
          • BigInteger
          • BigDecimal
            • Examples
          • BitSet
          • Date and Time
            • Examples
          • Optional
          • Math
          • UUID
          • Scanner
          • Formatter
            • Examples
          • Properties
          • Regex (Pattern and Matcher)
            • Examples
          • Atomic Classes
          • Random
          • Format
            • NumberFormat
            • DateFormat
            • DecimalFormat
        • Others
          • Object
          • Enum
            • Pre-Defined Enum
            • Custom Enum
            • EnumSet and EnumMap
          • Record
          • Optional
          • System
          • Runtime
          • ProcessBuilder
          • Class
          • Void
          • Throwable
            • Error
            • Exception
              • Custom Exception Handling
              • Best Practice
            • Error vs Exception
            • StackTraceElement
    • Java Features by Version
      • How New Java Features are Released ?
      • Java Versions
        • Java 8
        • Java 9
        • Scoped Values
        • Unnamed Variables & Patterns
      • FAQ
    • Concepts
      • Set 1
        • Streams
          • flatmap
          • Collectors Utility Class
          • Problems
        • Functional Interfaces
          • Standard Built-In Interfaces
          • Custom Interfaces
        • Annotation
          • Custom Annotation
          • Meta Annotation
        • Generics
          • Covariance and Invariance
        • Asynchronous Computation
          • Future
          • CompletableFuture
          • Future v/s CompletableFuture
          • ExecutorService
            • Thread Pool
            • Types of Work Queues
            • Rejection Policies
            • ExecutorService Implementations
            • ExecutorService Usage
          • Locks, Atomic Variables, CountDownLatch, CyclicBarrier - TBU
          • Parallel Streams, Fork/Join Framework,Stream API with Parallelism - TBU
      • Set 2
        • Standards
          • ISO Standards
          • JSR
            • JSR 303, 349, 380 (Bean Validation)
        • Operator Precedence
      • Set 3
        • Date Time Formatter
        • Validation
      • Set 4
        • Input from User
        • Comparison & Ordering
          • Object Equality Check
          • Comparable and Comparator
            • Comparator Interface
          • Sorting of Objects
          • Insertion Ordering
    • Packages
      • Core Packages
        • java.lang
          • java.lang.System
          • java.lang.Thread
      • Jakarta Packages
        • jakarta.validation
        • javax.validation
      • Third-party Packages
    • Code Troubleshoot
      • Thread Dump
      • Heap Dump
    • Code Quality & Analysis
      • ArchUnit
      • Terminologies
        • Cyclic dependencies
    • Code Style
      • Naming Convention
      • Package Structure
      • Formatting
      • Comments and Documentation
      • Imports
      • Exception Handling
      • Class Structure
      • Method Guidelines
      • Page 1
      • Code Smells to Avoid
      • Lambdas and Streams Style
      • Tools
    • Tools
      • IntelliJ IDEA
        • Shortcuts for MAC
      • Apache JMeter
        • Examples
      • Thread Dump Capture
        • jstack
        • VisualVM - TBU
        • jcmd - TBU
        • JConsole - TBU
        • YourKit Java Profiler - TBU
        • Eclipse MAT - TBU
        • IntelliJ IDEA Profiler - TBU
        • AppDynamics - TBU
        • Dynatrace - TBU
        • Thread Dump Analyzers - TBU
      • Heap Dump Capture
        • jmap
        • VisualVM - TBU
        • jcmd - TBU
        • Eclipse MAT (Memory Analyzer Tool) - TBU
        • IntelliJ IDEA Profiler - TBU
        • YourKit Java Profiler - TBU
        • AppDynamics - TBU
        • Dynatrace - TBU
        • Kill -3 Command - TBU
        • jhat (Java Heap Analysis Tool) - TBU
        • JVM Options - TBU
      • Wireshark
        • Search Filters
    • Best Practices
      • Artifact and BOM Versioning
  • Maven
    • Installation
    • Local Repository & Configuration
    • Command-line Options
    • Build & Lifecycle
    • Dependency Management
      • Dependency
        • Transitive Dependency
        • Optional Dependency
      • Dependency Scope
        • Maven Lifecycle and Dependency Scope
      • Dependency Exclusions & Overrides
      • Bill of Materials (BOM)
      • Dependency Conflict Resolution
      • Dependency Tree & Analysis
      • Dependency Versioning Strategies
    • Plugins
      • Build Lifecycle Management
      • Dependency Management
      • Code Quality and Analysis
      • Documentation Generation
      • Code Generation
      • Packaging and Deployment
      • Reporting
      • Integration and Testing
      • Customization and Enhancement
        • build-helper-maven-plugin
        • properties-maven-plugin
        • ant-run plugin
        • exec-maven-plugin
        • gmavenplus-plugin
      • Performance Optimization
    • FAQs
      • Fixing Maven SSL Issues: Unable to Find Valid Certification Path
  • Spring
    • Spring Basics
      • What is Spring?
      • Why Use Spring
      • Spring Ecosystem
      • Versioning
      • Setting Up a Spring Project
    • Core Concepts
      • Spring Core
        • Dependency Injection (DI)
        • Stereotype Annotation
      • Spring Beans
        • Bean Lifecycle
        • Bean Scope
          • Singleton Bean
        • Lazy & Eager Initialization
          • Use Case of Lazy Initialization
        • BeanFactory
        • ApplicationContext
      • Spring Annotations
        • Spring Boot Specific
        • Controller Layer (Web & REST Controllers)
    • Spring Features
      • Auto Configuration
        • Spring Boot 2: spring.factories
        • Spring Boot 3: spring.factories
      • Spring Caching
        • In-Memory Caching
      • Spring AOP
        • Before Advice
        • After Returning Advice
        • After Throwing Advice
        • After (finally) Advice
        • Around Advice
      • Spring File Handling
      • Reactive Programming
        • Reactive System
        • Reactive Stream Specification
        • Project Reactor
          • Mono & Flux
      • Asynchronous Computation
        • @Async annotation
      • Spring Security
        • Authentication
          • Core Components
            • Security Filter Chain
              • HttpSecurity
              • Example
            • AuthenticationManager
            • AuthenticationProvider
            • UserDetailsService
              • UserDetails
              • PasswordEncoder
            • SecurityContext
            • SecurityContextHolder
            • GrantedAuthority
            • Security Configuration (Spring Security DSL)
          • Authentication Models
            • One-Way Authentication
            • Mutual Authentication
          • Authentication Mechanism
            • Basic Authentication
            • Form-Based Authentication
            • Token-Based Authentication (JWT)
            • OAuth2 Authentication
            • Multi-Factor Authentication (MFA)
            • SAML Authentication
            • X.509 Certificate Authentication
            • API Key Authentication
            • Remember-Me Authentication
            • Custom Authentication
          • Logout Handling
        • Authorization
        • Security Filters and Interceptors
        • CSRF
          • Real-World CSRF Attacks & Prevention
        • CORS
        • Session Management and Security
        • Best Practices
      • Spring Persistence
        • JDBC
          • JDBC Components
          • JDBC Template
          • Transaction Management
          • Best Practices in JDBC Usage
          • Datasource
            • Connection Pooling
              • HikariCP
            • Caching
        • JPA (Java Persistence API)
          • JPA Fundamentals
          • ORM Mapping Annotations
            • 1. Entity and Table Mappings
            • 2. Field/Column Mappings
            • 3. Relationship Mappings
            • 4. Inheritance Mappings
            • 5. Additional Configuration Annotations
          • Querying Data
            • JPQL
            • Criteria API
            • JPA Specification
              • Example - Employee Portal
            • Native SQL Queries
            • Named Queries
            • Query Return Types
            • Pagination & Sorting
              • Example - Employee Portal
            • Projection
          • Fetch Strategies in JPA
        • JPA Implementation
          • Hibernate
            • Properties
            • Example
        • Spring Data JPA
          • Repository Abstractions
          • Entity-to-Table Mapping
          • Derived Query Methods
        • Cross-Cutting Concerns
          • Transactions
          • Caching
          • Concurrency
        • Examples
          • Employee Portal
            • API
    • Distributed Systems & Communication
      • Distributed Scheduling
      • Inter-Service Communication
        • 1. RestTemplate
        • 2. WebClient
        • 3. OpenFeign
        • Retry Mechanism
          • @Retryable annotation
            • Example
    • Security & Data Protection
      • Encoding | Decoding
        • Types
          • Base Encoding
            • Base16 - TBD
              • Encoding and Decoding in Java - TBD
            • Base32
              • Encoding and Decoding in Java
            • Base64 -TBD
              • Encoding and Decoding in Java - TBD
          • Text Encoding - TBD
            • Extended ASCII
              • Encoding and Decoding in Java - TBD
                • ISO-8859-1
                • Windows-1252 - TBD
                • IBM Code Pages - TBD
            • ASCII
              • Encoding and Decoding in Java
        • Java Guidelines
          • Text Encoding Decoding Examples
          • Base Encoding Decoding Examples
          • Best Practices and Concepts
          • Libraries
      • Cryptography
        • Terminology
        • Java Cryptography Architecture (JCA)
        • Key Management
          • Key Generation
            • Tools and Libraries
              • OpenSSL
              • Java Keytool
                • Concept
                • Use Cases
            • Key & Certificate File Formats
          • Key Distribution
          • Key Storage
          • Key Rotation
          • Key Revocation
        • Encryption & Decryption
          • Symmetric Encryption
            • Algorithm
            • Modes of Operation
            • Examples
          • Asymmetric Encryption
            • Algorithm
            • Mode of Operation
            • Examples
    • Utilities & Libraries
      • Apache Libraries
        • Apache Camel
          • Camel Architecture
            • Camel Context
            • Camel Endpoints
            • Camel Components
            • Camel Exchange & MEP
          • Spring Dependency
          • Different Components
            • Camel SFTP
        • Apache Commons Lang
      • MapStruct Mapper
      • Utilities by Spring framework
        • FileCopyUtils
    • General Concepts
      • Spring Boot Artifact Packaging
      • Classpath and Resource Loading
      • Configuration - Mapping Properties to Java Class
      • Validations in Spring Framework
        • Jakarta Validation
          • Jakarta Bean Validation Annotations
    • Practical Guidelines
      • Spring Configuration
      • Spring Code Design
  • Software Testing
    • Software Testing Methodologies
      • Functional Testing
      • Non Functional Testing
    • Software Testing Life Cycle (STLC)
    • Integration Test
      • Dynamic Property Registration
    • Java Test Framework
      • JUnit
        • JUnit 4
          • Examples
        • JUnit 5
          • Examples
        • JUnit 4 vs JUnit 5
  • System Design
    • Foundations
      • Programming Paradigms
      • Object-Oriented Design
        • SOLID Principles
        • GRASP Principles
        • Composition
        • Aggregation
        • Association
      • Design Pattern
        • Creational Pattern
        • Structural Pattern
        • Behavioral Pattern
        • Examples
          • Data Collector
          • Payment Processor
        • Design Enhancements
          • Fluent API Design
            • Examples
    • Architectural Building Blocks
      • CAP Theorem
      • Load Balancer
        • Load Balancer Architecture
        • Load Balancing in Java Microservices
          • Client-Side Load Balancing Example
          • Server-Side Load Balancing Example
        • Load Balancer Monitoring Tool
      • Scaling
        • Vertical Scaling (Scaling Up)
        • Horizontal Scaling (Scaling Out)
        • Auto-Scaling
        • Database Scaling via Sharding
      • Caching
        • Pod-Level vs Distributed Caching
      • Networking Metrics
        • Types of Delay
        • Scenario
      • System Characteristics
      • Workload Types
      • Resilience & Failure Handling
    • Performance
      • Why Is My API Sometimes Slow ?
    • Security
      • Security by Design
      • Zero Trust Security Model
      • Zero Trust Architecture
      • Principles
        • CIA
        • Least Privilege Principle
        • Defense in Depth
      • Security Threats & Mitigations
        • OWASP
          • Top 10 Security Threats
          • Application Security Verification Standard
          • Software Assurance Maturity Model
          • Dependency Check
          • CSRFGuard
          • Cheat Sheets
          • Security Testing Guide
          • Threat Dragon
        • Threat Modeling
      • Compliance & Regulation
        • PCI DSS
    • Deployment Patterns
    • Diagrams
      • UML Diagrams
        • PlantUML
          • Class Diagram
          • Object Diagram
          • Sequence Diagram
          • Use Case Diagram
          • Activity Diagram
          • State Diagram
          • Architecture Diagram
          • Component Diagram
          • Timing Diagram
          • ER Diagram (Entity-Relationship)
          • Network Diagram
    • Common Terminologies
    • Problems
      • Reference Materials
      • Cache Design
  • Interview Guide
    • Non-Technical
      • Behavioural or Introductory Guide
      • Project Specific
    • Technical
      • Java Interview Companion
        • Java Key Concepts
          • Set 1
          • Set 2
        • Java Code Snippets
        • Java Practice Programs
          • Set 3 - Strings
          • Set 4 - Search
          • Set 5 - Streams and Collection
      • SQL Interview Companion
        • SQL Practice Problems
          • Set 1
      • Spring Interview Companion
        • Spring Key Concepts
          • Set 1 - General
          • Set 2 - Core Spring
        • Spring Code Snippets
          • JPA
      • Application Server
      • Maven
      • Containerized Application
      • Microservices
    • General
      • Applicant Tracking System (ATS)
      • Flowchart - How to Solve Coding Problem?
Powered by GitBook
On this page
  • About
  • CAP Theorem
  • 1. Consistency (C)
  • 2. Availability (A)
  • 3. Partition Tolerance (P)
  • The Trade-Off (Consistency, Availability, Partition Tolerance)
  • 1. CP Systems (Consistency + Partition Tolerance)
  • 2. AP Systems (Availability + Partition Tolerance)
  • 3. CA Systems (Consistency + Availability)
  • Real-World Example of Trade-Offs
  • 1. Cassandra (AP System):
  • 2. HBase (CP System):
  • 3. Single-node PostgreSQL (CA System):
  • Why we can't have all 3 (C, A, P) at once ?
  • 1. Network Partitions are inevitable
  • 2. Partition Tolerance (P) is mandatory
  • 3. The Core CAP Trade-off
  • Scenario: Distributed Database with Partition
  • 4. Visualizing the Impossibility
  • 5. Summary of the Conflict

Was this helpful?

  1. System Design
  2. Architectural Building Blocks

CAP Theorem

PreviousArchitectural Building BlocksNextLoad Balancer

Last updated 8 months ago

Was this helpful?

About

CAP Theorem, also known as Brewer’s Theorem, is a concept in distributed systems, introduced by computer scientist Eric Brewer in 2000. It states that in any distributed data store, it is impossible to simultaneously achieve all three of the following guarantees:

  1. Consistency (C): Every read receives the most recent write or an error. In other words, all nodes in the system return the same data at any given time.

  2. Availability (A): Every request (read or write) receives a response (success or failure) without guarantee that it contains the most recent write.

  3. Partition Tolerance (P): The system continues to operate despite network partitions or communication breakdowns between parts of the system.

CAP Theorem

1. Consistency (C)

  • Consistency means that after an update is made, all nodes (or replicas) in the distributed system see the same data at the same time. A consistent system appears as if it is running on a single node.

  • If we read data from any node in the system, the data should be identical.

Real-world impact: If a system is consistent, even during a failure, all nodes reflect the same data. However, it can lead to increased latency or reduced availability as nodes need to synchronize.

Example: Traditional relational databases (RDBMS) often guarantee consistency, where once a transaction is committed, all reads will reflect the latest write.

2. Availability (A)

  • Availability means the system guarantees that every request (read or write) will receive a response, even if some of the nodes are down.

  • There’s no guarantee that this response contains the latest data (since the system might return a previous version of the data if some nodes haven’t synchronized).

Real-world impact: High availability ensures that users are not left waiting indefinitely for a response, but it might serve stale data to some users during partition scenarios.

Example: Systems like DNS or NoSQL databases like Cassandra prioritize availability, ensuring responses to requests even during a failure.

3. Partition Tolerance (P)

  • Partition tolerance means the system continues to function even when there is a communication breakdown (partition) between nodes.

  • In distributed systems, partitions are inevitable due to network failures, and the system must tolerate such partitions while maintaining either consistency or availability.

Real-world impact: Partition tolerance is usually a requirement in distributed systems, as network failures or latency issues can lead to nodes being unable to communicate with each other.

Example: NoSQL databases like MongoDB and Cassandra are designed with partition tolerance in mind, ensuring that even if nodes are temporarily disconnected, the system continues to operate.

The Trade-Off (Consistency, Availability, Partition Tolerance)

The CAP Theorem essentially says that a distributed system can only guarantee two out of the three properties simultaneously. This creates the following trade-offs -

1. CP Systems (Consistency + Partition Tolerance)

In a CP system, we prioritize Consistency and Partition Tolerance. This means that:

  • Consistency: All nodes in the system will always return the most up-to-date, consistent data. No matter which node we query, we get the most recent version of the data.

  • Partition Tolerance: The system will continue to function even if a network partition (a break in communication between nodes) occurs. It ensures that despite the partition, the system does not end up in a split-brain scenario where different nodes have conflicting data.

What is sacrificed?

Availability: During a partition, some parts of the system might become unavailable to maintain consistency. If two nodes can’t communicate, the system may choose to block reads or writes to prevent inconsistencies. This results in increased latency or downtime for parts of the system.

Example Scenarios:

  • Banking systems: In financial transactions, consistency is critical. We cannot afford to show inconsistent account balances or allow double spending, so systems will prioritize consistency and block requests during partitions.

  • HBase and Zookeeper are examples of CP systems, where they block some operations in favor of ensuring that data is always consistent across nodes, but this comes at the cost of availability during partitions.

CP Trade-offs in Practice:

  • When we need strong guarantees of correctness, CP systems are ideal.

  • However, expect that when partitions happen (e.g., network outages or node failures), some parts of the system may become unavailable until the partition heals.

2. AP Systems (Availability + Partition Tolerance)

In an AP system, we prioritize Availability and Partition Tolerance. This means that:

  • Availability: The system guarantees that every request (whether a read or write) will receive a response, even if some of the nodes are down or disconnected. The system remains available to users, even if some parts of the system experience network failures.

  • Partition Tolerance: Like in CP systems, AP systems ensure that the system remains operational despite network partitions. Even if nodes cannot communicate due to a partition, the system continues to respond to queries, allowing operations to continue on isolated partitions.

What is sacrificed?

Consistency: Since nodes may not be able to communicate, the system sacrifices consistency. Different nodes might have slightly different views of the data, leading to eventual consistency. This means that after a partition heals, the system will reconcile data between nodes, and eventually, all nodes will agree on the same state.

Example Scenarios:

  • Social media platforms: If you post a photo on a social media site, you expect that the site will remain available, even if some data centers are temporarily out of sync. AP systems prioritize availability, allowing users to continue interacting with the platform, while the system resolves inconsistencies later.

  • Amazon DynamoDB, Cassandra, and Riak are examples of AP systems. These databases prioritize high availability and partition tolerance at the expense of strong consistency, offering eventual consistency instead.

AP Trade-offs in Practice:

  • If availability is critical to your business (e.g., e-commerce, social media, real-time applications), AP systems are preferred.

  • The downside is that during partitions, clients might read stale or inconsistent data until the partition is resolved and data converges.

3. CA Systems (Consistency + Availability)

In a CA system, we prioritize Consistency and Availability. This means that:

  • Consistency: The system guarantees that all nodes return the most up-to-date data. Every read reflects the most recent write, so all clients see a consistent view of the data.

  • Availability: The system guarantees that every request gets a response, even if nodes are down. There is no scenario where the system becomes unresponsive.

What is sacrificed?

Partition Tolerance: CA systems do not tolerate network partitions. If there is a network partition (a break in communication between nodes), the system cannot guarantee both consistency and availability. It must either stop responding or sacrifice consistency, and most CA systems choose to stop responding, leading to system downtime during a partition.

Example Scenarios:

  • Single-node relational databases (RDBMS): Traditional databases like MySQL and PostgreSQL that run on a single node are typically CA. They are highly available, and since there is no partition in a single-node setup, consistency is guaranteed.

  • Enterprise applications: Systems that require both high availability and consistency within a single, non-distributed node may prefer a CA approach, but this doesn’t work well in distributed environments.

CA Trade-offs in Practice:

  • CA systems are suited for non-distributed, single-node architectures where partitioning is not a concern (since there’s only one node).

  • In practice, most modern systems are distributed, making CA systems impractical for larger, scalable applications. CA systems break down in the presence of network partitions because we must give up either availability or consistency in such cases.

Real-World Example of Trade-Offs

1. Cassandra (AP System):

  • Cassandra is designed for high availability and partition tolerance.

  • If a network partition occurs, Cassandra will still allow reads and writes to continue, even if different nodes hold slightly different data. After the partition is resolved, Cassandra uses eventual consistency to reconcile the differences between nodes.

  • It sacrifices strong consistency during the partition.

2. HBase (CP System):

  • HBase guarantees strong consistency and partition tolerance.

  • If a network partition happens, HBase may block reads and writes to ensure that all data is consistent across nodes. This means some operations may become unavailable until the partition is healed.

  • It sacrifices availability to maintain consistency.

3. Single-node PostgreSQL (CA System):

  • A single-node relational database like PostgreSQL guarantees consistency and availability as long as no network partition happens (since it’s a single node).

  • However, if we distribute PostgreSQL across multiple nodes, we would need to choose between consistency and availability during a network partition, often sacrificing partition tolerance.

Why we can't have all 3 (C, A, P) at once ?

We can't have Consistency (C), Availability (A), and Partition Tolerance (P) all at once in a distributed system due to the inherent limitations in handling network partitions.

1. Network Partitions are inevitable

  • In any distributed system, network partitions can happen. A partition occurs when some nodes in the system are unable to communicate with others due to network issues (like a node going down, network latency, or a network link failure). This is a fundamental challenge because, once a partition occurs, some parts of the system cannot communicate with others.

2. Partition Tolerance (P) is mandatory

  • Partition tolerance is required and a must to have in modern distributed systems because network partitions can occur unexpectedly. The system must continue operating (serving requests, processing data, etc.) even when some nodes are unreachable. Without partition tolerance, the system would simply go down every time a network partition occurs, which is unacceptable for large-scale, resilient applications.

    Since partitions are unavoidable, a distributed system must either:

    • Handle them and tolerate the partition (allowing nodes to operate independently for some time),

    • Or stop all operations during a partition, which breaks availability and limits the utility of the system.

3. The Core CAP Trade-off

Given that partitions will happen and that partition tolerance is essential, a distributed system must choose between Consistency and Availability during a partition:

If the system chooses Consistency (C):

  • Consistency means all nodes must return the latest, correct data. If a network partition happens and some nodes can’t communicate with others, maintaining consistency would require stopping operations in some parts of the system to ensure that no inconsistent data is read or written.

  • Trade-off: This means the system sacrifices Availability (A). Some users won’t be able to get a response until the partition is resolved and the system can guarantee that the data is consistent across all nodes.

If the system chooses Availability (A):

  • Availability means that every request gets a response, regardless of network partitions. During a partition, nodes that cannot communicate must continue operating independently and provide answers to user requests.

  • Trade-off: This sacrifices Consistency (C). Since nodes are unable to communicate, they might return stale or inconsistent data. The system is available but may temporarily serve different versions of data from different parts of the system.

Scenario: Distributed Database with Partition

Let’s break down a real-world scenario to understand why it's impossible to achieve all three at the same time:

Imagine we have a distributed database that spans multiple data centers. Suddenly, there’s a network partition that isolates some of the data centers from each other, meaning nodes in one region can’t talk to nodes in another.

Option 1: Prioritize Consistency and Partition Tolerance (CP)

  • In this case, to maintain consistency, the system must ensure that all nodes agree on the current state of the data before any new reads or writes are processed.

  • But since some nodes are isolated due to the partition, they cannot confirm the state of the data with the other nodes. To prevent inconsistencies, the system will block reads and writes until the partition is resolved and the nodes can communicate again.

  • Outcome: The system remains consistent, but during the partition, it sacrifices availability. Some parts of the system become unavailable until the partition is healed.

Option 2: Prioritize Availability and Partition Tolerance (AP)

  • In this case, the system continues to allow reads and writes during the partition, ensuring that all nodes remain available to handle user requests.

  • However, since the nodes cannot communicate with each other, they may start working with slightly different versions of the data, leading to temporary inconsistencies.

  • Outcome: The system remains available, but the data might be inconsistent across different nodes during the partition.

Option 3: Prioritize Consistency and Availability (CA)

  • In this case, we want to maintain both consistency and availability. The system must return the correct, consistent data for every request, and it must stay available to all users.

  • But when a partition happens, maintaining both becomes impossible. Since nodes in different parts of the system can’t communicate, we either have to:

    • Allow operations to continue independently (which breaks consistency), or

    • Block operations (which breaks availability).

    We can’t do both at the same time because the partition prevents nodes from ensuring consistency while staying available.

  • Outcome: Partition tolerance is sacrificed. The system cannot tolerate network partitions and either stops responding or provides inconsistent data.

4. Visualizing the Impossibility

  • Without a network partition, we could theoretically have C, A, and P together because all nodes are able to communicate freely.

  • With a network partition, nodes can’t exchange information, so we’re forced to choose:

    • Do we want all nodes to return the same, consistent data (Consistency)? Then some nodes may need to go offline until the partition heals.

    • Or do we want the system to remain fully operational and available (Availability)? Then some nodes might return outdated or inconsistent data because they can’t communicate with the rest of the system.

5. Summary of the Conflict

  1. Partition tolerance (P) is a fundamental requirement in any distributed system because partitions are unavoidable.

  2. When a partition happens, the system can either:

    • Remain available (A) and serve all requests, but risk returning inconsistent data.

    • Ensure consistency (C) by forcing some nodes to block operations, sacrificing availability.

Since partitions will happen, we must choose between Consistency or Availability. We cannot have both while also ensuring that the system tolerates network partitions.

This is why the CAP Theorem states that Consistency, Availability, and Partition Tolerance cannot all be guaranteed simultaneously in a distributed system. We must choose two.