SlideShare a Scribd company logo
APPLYING CAP TO BUILD DISTRIBUTED
ROBUST PHOENIX API APP
PHOENIX API APP BACKED BY
COCKROACHDB
2
Who Are we
yeongsheng@odd-e.com
AGENDA
‣ YOUR WEAKEST LINK
‣ #NEWSQL + D.A.S.H
‣ COCKROACHDB
‣ WHEN RUBBER HITS THE ROAD
‣ SHOW ME THE CODE
‣ Q&A
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
YOUR WEAKEST LINK
▸ Traditional RDBMS
▸ NoSQL
▸ NewSQL
▸ YugaqByteDB
▸ TiDB
▸ CockroachDB
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
D.A.S.H
▸ Beyond 12-Factor-App
▸ 4 properties of CloudNative Ready DB
DISPOSABILITY
API SYMMETRY
SHARED NOTHING
HORIZONTAL SCALING
D :
A :
S :
H :
WHY?
COCKROACHDB
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
ENABLES GLOBAL, SCALABLE, RESILIENT SERVICES
▸ Make life easier for humans. Low-touch and
highly automated distributed DB for
operators yet simple to reason about for
developers.
▸ Industry-leading consistency, even on
massively scaled deployments. Enabling
distributed transactions, yet removing the
pain of eventual consistency issues.
▸ Always-on database that accepts reads and
writes on all nodes without generating
conflicts.
▸ Flexible deployment in any environment,
without tying you to any platform or vendor.
▸ Support familiar tools for working with
relational data (i.e., SQL).
WHAT GIVES?
CONSISTENCY
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
PERILS OF INCONSISTENCIES
▸ Emin Gün Sirer, an Associate Professor at Cornell University, wrote a
blog post blaming eventually consistent data stores for the lost
bitcoins. He mentions MongoDB, Cassandra and Riak among the
NoSQL solutions that are vulnerable to banking thefts because:
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
CONSENSUS REPLICATION
▸ Raft
▸ Commit when quorum
has written data (nodes
> = 3)
▸ 1 consensus group per
data range
ution
replication protocol (Raft)
transactions
t, repair, rebalance
Gossip /
Raft
SQL API
Distributed, transactional KV
Node 1
Node 2
Node 3
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
CONSISTENCY<-CONSENSUS<-MULTI-ACTIVE AVAILABILITY
▸ “CONSISTENCY” - ACID semantics & CAP theorem. Data should be
anomaly-free.
▸ “CLUSTER” - Single logical DB, multiple nodes, joined together to
form a uniform consistent cluster.
▸ “RANGE” - All data (tables, indices, etc) as a giant sorted map of KV
pairs; 64 MB in size per range; Auto split and balanced across nodes.
▸ “LEASEHOLDER” - Ranges replicated across nodes & 1 replica of
range holds “range lease”; coordinates all reads/writes to that range.
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
CONSISTENCY<-CONSENSUS<-MULTI-ACTIVE AVAILABILITY
▸ “RAFTLEADER” - 1 replica for each range is “leader” for writes; Leader
coordinates all writes to raft group (followers); Quorum consensus for
writes. Leaseholder usually also Raftleader. When a write doesn't achieve
consensus, forward progress halts to maintain consistency within the cluster.
Ensures ACID semantics for multi-tables data operations are consistent.
▸ “REPLICATION” - Synchronous distribution of copies of data that are
ensured to be consistent.
▸ “MULTI-ACTIVE AVAILABILITY” - Each node in cluster can handle reads/
writes for a subset of stored data (range and quorum consensus concepts).
Symmetrical -> works nicely with load balancers over SQL API (PostgreSQL
wire format).
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
@cockroachdb
Put “cherry” Leader
apricot
banana
blueberry
cherry
grape
Follower
apricot
banana
blueberry
grape
Follower
apricot
banana
blueberry
grape
Consensus Replication
Put “cherry” Leader
apricot
banana
blueberry
cherry
grape
Follower
apricot
banana
blueberry
grape
Follower
apricot
banana
blueberry
grape
Put “cherry”
Consensus Replication
RAFT
CONSENSUS
PROTOCOL
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
@cockroach
Write committed when
2 out of 3 nodes have written data
Follower
apricot
banana
blueberry
cherry
grape
Follower
apricot
banana
blueberry
grape
Consensus Replication
Put “cherry” Leader
apricot
banana
blueberry
cherry
grape
Put “cherry”
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
@cockroachdb
Follower
apricot
banana
blueberry
cherry
grape
Follower
apricot
banana
blueberry
grape
Consensus Replication
Put “cherry” Leader
apricot
banana
blueberry
cherry
grape
Put “cherry”
AckAck
Follower
apricot
banana
blueberry
cherry
grape
Follower
apricot
banana
blueberry
grape
Consensus Replication
Put “cherry” Leader
apricot
banana
blueberry
cherry
grape
Put “cherry”
cherry
Ack
CAN IT BE
TRUSTED?
WHEN RUBBER HITS
THE ROAD
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
COCKROACHDB PASSES JENSEN TESTING
▸ CockroachDB Beta passes Jepsen
testing
▸ Jepsen tests lessons 2+ years on...
▸ Register, Bank, Monotonic & Sets
▸ G2, Sequential, Comments & Nemesis
modes (clock skews, network failures)
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
DEMO
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
▸ https://github.com/yeongsheng-tan/lunchbox_api.git
▸ Branches
▸ master
▸ cockroachdb_postgrex_cdb
▸ cockroachdb_postgrex_cdb_with_api_jwt_auth
▸ cockroachdb_postgrex_cdb_api_jwt_auth_e2e_tests
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
WHAT IS NEEDED?
▸ Production code -> hex
package: postgrex_cdb (yet to
support correlated sub-
queries)
▸ Test code -> hex package:
ecto_replay_sandbox
▸ No prod code changes!!!
▸ Test code changes.
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
CREDITS
▸ Nate Stewart
▸ Ben Darnell
▸ Aphyr Jepsen
▸ Diana Hsieh
▸ Emin Gün Sirer

More Related Content

Similar to Applying CAP theorem to build distributed robust Phoenix API app

Data Pipeline with Docker on AWS
Data Pipeline with Docker on AWSData Pipeline with Docker on AWS
Data Pipeline with Docker on AWS
Adeesh Fulay
 
Apache kafka-a distributed streaming platform
Apache kafka-a distributed streaming platformApache kafka-a distributed streaming platform
Apache kafka-a distributed streaming platform
confluent
 
Apache Kafka - A Distributed Streaming Platform
Apache Kafka - A Distributed Streaming PlatformApache Kafka - A Distributed Streaming Platform
Apache Kafka - A Distributed Streaming Platform
Paolo Castagna
 
Event Streaming Architectures with Confluent and ScyllaDB
Event Streaming Architectures with Confluent and ScyllaDBEvent Streaming Architectures with Confluent and ScyllaDB
Event Streaming Architectures with Confluent and ScyllaDB
ScyllaDB
 
Using Kafka to integrate DWH and Cloud Based big data systems
Using Kafka to integrate DWH and Cloud Based big data systemsUsing Kafka to integrate DWH and Cloud Based big data systems
Using Kafka to integrate DWH and Cloud Based big data systems
confluent
 
Plandas-CacheCloud
Plandas-CacheCloudPlandas-CacheCloud
Plandas-CacheCloud
Gyuman Cho
 
Pets vs. Cattle: The Elastic Cloud Story
Pets vs. Cattle: The Elastic Cloud StoryPets vs. Cattle: The Elastic Cloud Story
Pets vs. Cattle: The Elastic Cloud Story
Randy Bias
 
DevOps in Age of Kubernetes
DevOps in Age of KubernetesDevOps in Age of Kubernetes
DevOps in Age of Kubernetes
Mesosphere Inc.
 
Apache Kafka vs. Traditional Middleware (Kai Waehner, Confluent) Frankfurt 20...
Apache Kafka vs. Traditional Middleware (Kai Waehner, Confluent) Frankfurt 20...Apache Kafka vs. Traditional Middleware (Kai Waehner, Confluent) Frankfurt 20...
Apache Kafka vs. Traditional Middleware (Kai Waehner, Confluent) Frankfurt 20...
confluent
 
Apache Kafka vs. Integration Middleware (MQ, ETL, ESB) - Friends, Enemies or ...
Apache Kafka vs. Integration Middleware (MQ, ETL, ESB) - Friends, Enemies or ...Apache Kafka vs. Integration Middleware (MQ, ETL, ESB) - Friends, Enemies or ...
Apache Kafka vs. Integration Middleware (MQ, ETL, ESB) - Friends, Enemies or ...
confluent
 
Build agile and elastic data pipeline
Build agile and elastic data pipelineBuild agile and elastic data pipeline
Build agile and elastic data pipeline
Deba Chatterjee
 
Open stack in sina
Open stack in sinaOpen stack in sina
Open stack in sina
Hui Cheng
 
DevOps vs. Site Reliability Engineering (SRE) in Age of Kubernetes
DevOps vs. Site Reliability Engineering (SRE) in Age of KubernetesDevOps vs. Site Reliability Engineering (SRE) in Age of Kubernetes
DevOps vs. Site Reliability Engineering (SRE) in Age of Kubernetes
DevOps.com
 
Mario Cartia - SMACK is the new LAMP! - Codemotion Milan 2017
Mario Cartia - SMACK is the new LAMP! - Codemotion Milan 2017Mario Cartia - SMACK is the new LAMP! - Codemotion Milan 2017
Mario Cartia - SMACK is the new LAMP! - Codemotion Milan 2017
Codemotion
 
Container Orchestration Wars (2017 Edition)
Container Orchestration Wars (2017 Edition)Container Orchestration Wars (2017 Edition)
Container Orchestration Wars (2017 Edition)
Karl Isenberg
 
CockroachDB
CockroachDBCockroachDB
CockroachDB
andrei moga
 
Learnings From Shipping 1000+ Streaming Data Pipelines To Production with Hak...
Learnings From Shipping 1000+ Streaming Data Pipelines To Production with Hak...Learnings From Shipping 1000+ Streaming Data Pipelines To Production with Hak...
Learnings From Shipping 1000+ Streaming Data Pipelines To Production with Hak...
HostedbyConfluent
 
Technical overview of Azure Cosmos DB
Technical overview of Azure Cosmos DBTechnical overview of Azure Cosmos DB
Technical overview of Azure Cosmos DB
Microsoft Tech Community
 
App modernization on AWS with Apache Kafka and Confluent Cloud
App modernization on AWS with Apache Kafka and Confluent CloudApp modernization on AWS with Apache Kafka and Confluent Cloud
App modernization on AWS with Apache Kafka and Confluent Cloud
Kai Wähner
 
Scaling big with Apache Kafka
Scaling big with Apache KafkaScaling big with Apache Kafka
Scaling big with Apache Kafka
Nikolay Stoitsev
 

Similar to Applying CAP theorem to build distributed robust Phoenix API app (20)

Data Pipeline with Docker on AWS
Data Pipeline with Docker on AWSData Pipeline with Docker on AWS
Data Pipeline with Docker on AWS
 
Apache kafka-a distributed streaming platform
Apache kafka-a distributed streaming platformApache kafka-a distributed streaming platform
Apache kafka-a distributed streaming platform
 
Apache Kafka - A Distributed Streaming Platform
Apache Kafka - A Distributed Streaming PlatformApache Kafka - A Distributed Streaming Platform
Apache Kafka - A Distributed Streaming Platform
 
Event Streaming Architectures with Confluent and ScyllaDB
Event Streaming Architectures with Confluent and ScyllaDBEvent Streaming Architectures with Confluent and ScyllaDB
Event Streaming Architectures with Confluent and ScyllaDB
 
Using Kafka to integrate DWH and Cloud Based big data systems
Using Kafka to integrate DWH and Cloud Based big data systemsUsing Kafka to integrate DWH and Cloud Based big data systems
Using Kafka to integrate DWH and Cloud Based big data systems
 
Plandas-CacheCloud
Plandas-CacheCloudPlandas-CacheCloud
Plandas-CacheCloud
 
Pets vs. Cattle: The Elastic Cloud Story
Pets vs. Cattle: The Elastic Cloud StoryPets vs. Cattle: The Elastic Cloud Story
Pets vs. Cattle: The Elastic Cloud Story
 
DevOps in Age of Kubernetes
DevOps in Age of KubernetesDevOps in Age of Kubernetes
DevOps in Age of Kubernetes
 
Apache Kafka vs. Traditional Middleware (Kai Waehner, Confluent) Frankfurt 20...
Apache Kafka vs. Traditional Middleware (Kai Waehner, Confluent) Frankfurt 20...Apache Kafka vs. Traditional Middleware (Kai Waehner, Confluent) Frankfurt 20...
Apache Kafka vs. Traditional Middleware (Kai Waehner, Confluent) Frankfurt 20...
 
Apache Kafka vs. Integration Middleware (MQ, ETL, ESB) - Friends, Enemies or ...
Apache Kafka vs. Integration Middleware (MQ, ETL, ESB) - Friends, Enemies or ...Apache Kafka vs. Integration Middleware (MQ, ETL, ESB) - Friends, Enemies or ...
Apache Kafka vs. Integration Middleware (MQ, ETL, ESB) - Friends, Enemies or ...
 
Build agile and elastic data pipeline
Build agile and elastic data pipelineBuild agile and elastic data pipeline
Build agile and elastic data pipeline
 
Open stack in sina
Open stack in sinaOpen stack in sina
Open stack in sina
 
DevOps vs. Site Reliability Engineering (SRE) in Age of Kubernetes
DevOps vs. Site Reliability Engineering (SRE) in Age of KubernetesDevOps vs. Site Reliability Engineering (SRE) in Age of Kubernetes
DevOps vs. Site Reliability Engineering (SRE) in Age of Kubernetes
 
Mario Cartia - SMACK is the new LAMP! - Codemotion Milan 2017
Mario Cartia - SMACK is the new LAMP! - Codemotion Milan 2017Mario Cartia - SMACK is the new LAMP! - Codemotion Milan 2017
Mario Cartia - SMACK is the new LAMP! - Codemotion Milan 2017
 
Container Orchestration Wars (2017 Edition)
Container Orchestration Wars (2017 Edition)Container Orchestration Wars (2017 Edition)
Container Orchestration Wars (2017 Edition)
 
CockroachDB
CockroachDBCockroachDB
CockroachDB
 
Learnings From Shipping 1000+ Streaming Data Pipelines To Production with Hak...
Learnings From Shipping 1000+ Streaming Data Pipelines To Production with Hak...Learnings From Shipping 1000+ Streaming Data Pipelines To Production with Hak...
Learnings From Shipping 1000+ Streaming Data Pipelines To Production with Hak...
 
Technical overview of Azure Cosmos DB
Technical overview of Azure Cosmos DBTechnical overview of Azure Cosmos DB
Technical overview of Azure Cosmos DB
 
App modernization on AWS with Apache Kafka and Confluent Cloud
App modernization on AWS with Apache Kafka and Confluent CloudApp modernization on AWS with Apache Kafka and Confluent Cloud
App modernization on AWS with Apache Kafka and Confluent Cloud
 
Scaling big with Apache Kafka
Scaling big with Apache KafkaScaling big with Apache Kafka
Scaling big with Apache Kafka
 

Recently uploaded

What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
Gerardo Pardo-Castellote
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 

Recently uploaded (20)

What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 

Applying CAP theorem to build distributed robust Phoenix API app

  • 1. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP PHOENIX API APP BACKED BY COCKROACHDB
  • 3. AGENDA ‣ YOUR WEAKEST LINK ‣ #NEWSQL + D.A.S.H ‣ COCKROACHDB ‣ WHEN RUBBER HITS THE ROAD ‣ SHOW ME THE CODE ‣ Q&A
  • 4. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP YOUR WEAKEST LINK ▸ Traditional RDBMS ▸ NoSQL ▸ NewSQL ▸ YugaqByteDB ▸ TiDB ▸ CockroachDB
  • 5. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP D.A.S.H ▸ Beyond 12-Factor-App ▸ 4 properties of CloudNative Ready DB DISPOSABILITY API SYMMETRY SHARED NOTHING HORIZONTAL SCALING D : A : S : H :
  • 7. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
  • 8. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP ENABLES GLOBAL, SCALABLE, RESILIENT SERVICES ▸ Make life easier for humans. Low-touch and highly automated distributed DB for operators yet simple to reason about for developers. ▸ Industry-leading consistency, even on massively scaled deployments. Enabling distributed transactions, yet removing the pain of eventual consistency issues. ▸ Always-on database that accepts reads and writes on all nodes without generating conflicts. ▸ Flexible deployment in any environment, without tying you to any platform or vendor. ▸ Support familiar tools for working with relational data (i.e., SQL).
  • 10. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP PERILS OF INCONSISTENCIES ▸ Emin Gün Sirer, an Associate Professor at Cornell University, wrote a blog post blaming eventually consistent data stores for the lost bitcoins. He mentions MongoDB, Cassandra and Riak among the NoSQL solutions that are vulnerable to banking thefts because:
  • 11. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP CONSENSUS REPLICATION ▸ Raft ▸ Commit when quorum has written data (nodes > = 3) ▸ 1 consensus group per data range ution replication protocol (Raft) transactions t, repair, rebalance Gossip / Raft SQL API Distributed, transactional KV Node 1 Node 2 Node 3
  • 12. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP CONSISTENCY<-CONSENSUS<-MULTI-ACTIVE AVAILABILITY ▸ “CONSISTENCY” - ACID semantics & CAP theorem. Data should be anomaly-free. ▸ “CLUSTER” - Single logical DB, multiple nodes, joined together to form a uniform consistent cluster. ▸ “RANGE” - All data (tables, indices, etc) as a giant sorted map of KV pairs; 64 MB in size per range; Auto split and balanced across nodes. ▸ “LEASEHOLDER” - Ranges replicated across nodes & 1 replica of range holds “range lease”; coordinates all reads/writes to that range.
  • 13. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP CONSISTENCY<-CONSENSUS<-MULTI-ACTIVE AVAILABILITY ▸ “RAFTLEADER” - 1 replica for each range is “leader” for writes; Leader coordinates all writes to raft group (followers); Quorum consensus for writes. Leaseholder usually also Raftleader. When a write doesn't achieve consensus, forward progress halts to maintain consistency within the cluster. Ensures ACID semantics for multi-tables data operations are consistent. ▸ “REPLICATION” - Synchronous distribution of copies of data that are ensured to be consistent. ▸ “MULTI-ACTIVE AVAILABILITY” - Each node in cluster can handle reads/ writes for a subset of stored data (range and quorum consensus concepts). Symmetrical -> works nicely with load balancers over SQL API (PostgreSQL wire format).
  • 14. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP @cockroachdb Put “cherry” Leader apricot banana blueberry cherry grape Follower apricot banana blueberry grape Follower apricot banana blueberry grape Consensus Replication Put “cherry” Leader apricot banana blueberry cherry grape Follower apricot banana blueberry grape Follower apricot banana blueberry grape Put “cherry” Consensus Replication RAFT CONSENSUS PROTOCOL
  • 15. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP @cockroach Write committed when 2 out of 3 nodes have written data Follower apricot banana blueberry cherry grape Follower apricot banana blueberry grape Consensus Replication Put “cherry” Leader apricot banana blueberry cherry grape Put “cherry”
  • 16. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP @cockroachdb Follower apricot banana blueberry cherry grape Follower apricot banana blueberry grape Consensus Replication Put “cherry” Leader apricot banana blueberry cherry grape Put “cherry” AckAck Follower apricot banana blueberry cherry grape Follower apricot banana blueberry grape Consensus Replication Put “cherry” Leader apricot banana blueberry cherry grape Put “cherry” cherry Ack
  • 17. CAN IT BE TRUSTED? WHEN RUBBER HITS THE ROAD
  • 18. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP COCKROACHDB PASSES JENSEN TESTING ▸ CockroachDB Beta passes Jepsen testing ▸ Jepsen tests lessons 2+ years on... ▸ Register, Bank, Monotonic & Sets ▸ G2, Sequential, Comments & Nemesis modes (clock skews, network failures)
  • 19. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
  • 20. DEMO
  • 21. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP ▸ https://github.com/yeongsheng-tan/lunchbox_api.git ▸ Branches ▸ master ▸ cockroachdb_postgrex_cdb ▸ cockroachdb_postgrex_cdb_with_api_jwt_auth ▸ cockroachdb_postgrex_cdb_api_jwt_auth_e2e_tests
  • 22. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP WHAT IS NEEDED? ▸ Production code -> hex package: postgrex_cdb (yet to support correlated sub- queries) ▸ Test code -> hex package: ecto_replay_sandbox ▸ No prod code changes!!! ▸ Test code changes.
  • 23. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP
  • 24. APPLYING CAP TO BUILD DISTRIBUTED ROBUST PHOENIX API APP CREDITS ▸ Nate Stewart ▸ Ben Darnell ▸ Aphyr Jepsen ▸ Diana Hsieh ▸ Emin Gün Sirer