1

Understanding The CAP Theorem

 1 week ago
source link: https://blog.bitsrc.io/cap-theorem-a19aabd089c2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
0*BdVBPO1Fgv_1iQMn.png

Introduction

Developers face a lot of trouble when it comes to considering consistency, availability, and portion tolerance while designing a distributed system.

Combined, these three aspects have built a theorem called the CAP Theorem. This concept plays a significant role in designing and managing microservices architectures. So, let’s dive straight into the CAP theorem and how it can be used while designing microservices architecture.

What is the CAP Theorem?

The CAP theorem, which is also known as Brewer’s theorem, was introduced by computer scientist Eric Brewer in 2000. It states that in a distributed system, it is impossible to achieve the three aspects that are consistency, availability, and portion tolerance simultaneously. A distributed system is a network that stores data on more than one node at the same time.

First, let’s explore these three aspects of the CAP theorem.

0*hXcRVp26I0YF8FrM.png

Consistency

By having consistency, all the nodes will have the same copies of a replicated data item which is visible for various transactions. In the distributed system, every node in the cluster returns the same, most recent and successful write. Furthermore, every client will have the same view of the data.

There are various types of consistency models. In the CAP theorem, Consistency means sequential consistency, a very strong form of consistency.

Availability

Availability means that each read or write request for a data item will get a response. It can either be a successful response or will receive a message that the operation cannot be completed. Every working node returns a valid response for all the read and write requests in a reasonable amount of time.

Partition tolerance

A partition is a communications break within a distributed system. It can be a lost or temporarily delayed connection between two nodes. Partition tolerance means that the cluster must continue to work despite any number of communication breakdowns between nodes in the system. Distributed systems that guarantee partition tolerance can gracefully recover from partitions once the partition heals.

0*YTkr-FL-Hg0at0Nv.png

Based on the above diagram, database systems can be divided into these three categories.

1. CP Category

This category provides Consistency and Partition Tolerance. Even though, there is a risk of data being unavailable. There are some well known databases that support CP:

  1. MongoDB
  2. Redis
  3. HBase

2. CA Category

This category provides Consistency and Availability. There are some well known databases that support CA:

  1. RDBMS (SQL Databases)

3. AP Category

This category provides Availability and Partition Tolerance. There are some databases that support AP, some industry known, but some not:

  1. DynamoDB
  2. Cassandra
  3. CouchDB

Great, now you have a basic understanding of the CAP theorem and its three aspects. So, let’s dig into its implications for microservices.

CAP Theorem and Microservices

Unlike the monolith architecture, microservices architecture consists of multiple smaller, independent services, each handling a specific business function.

When applying the CAP theorem to microservices architecture, the trade-offs are evident. A trade-off means that achieving one outcome involves giving up something else.

Developers must consider the specific requirements of their projects. They might prioritize consistency for certain critical data, availability for user responsiveness, or partition tolerance for robustness in the face of network issues. Since microservices architecture follows a distributed nature, where each service operates independently and communicates over a network, it is crucial to consider the CAP theorem.

Practical Considerations of the CAP Theorem on Microservices

Below are some of the practical considerations, while applying the CAP theorem for microservices.

1. Consistency vs. Availability

Microservices tackle different business logic with complex data. If one microservice requires strong consistency, sacrificing availability may be unavoidable.

For example, in a financial system, it is crucial to handle day-to-day transactions, and the balance needs to be updated regularly. In such cases, a microservice might choose consistency over availability, ensuring that all nodes see the same data, even if it means some nodes may not be available for requests.

On the other hand, certain microservices may prioritize availability over consistency. For example, social media platforms require partition tolerance over consistency. It is okay for different users to see slightly different versions of the timeline for a short period. As a result, the system will remain available even in the face of network partitions or failures.

2. Eventual Consistency

Eventual Consistency means over time, the system converges towards one consistent state. However, during the transient period, users accessing different data centers may observe different versions of the data.

0*68UcDBRmHf8GeB0H.png

Many microservices architectures use eventual consistency to maintain availability and partition tolerance. Furthermore, eventual consistency allows microservices to continue functioning during network partitions or other failures.

Implementing eventual consistency in microservices is very critical. Proper data synchronization mechanisms and conflict resolution strategies need to be implemented if we are using eventual consistency. Event sourcing and distributed databases that support eventual consistency become valuable tools in managing data across microservices.

To know more about Eventual Consistency, check out this article.

3. Partition Tolerance in Microservices

Microservices must be designed to handle network partitions. For example, in a cloud-based environment, network partitions can be a common occurrence. When a network partition occurs, microservices need to decide whether to prioritize consistency or availability.

Partition tolerance can be improved by using techniques like sharding, which distributes data among several databases or services. Microservices designs can sustain a level of availability in partitioned environments by guaranteeing that a single database or service failure does not lead to a whole system outage.

4. Polyglot Persistence in Microservices

When designing microservices, we can use different databases for different microservices. It is known as polyglot persistence. It allows microservices to use the most appropriate data storage technology based on their need.

Consider the below example. It explains an e-commerce platform that has multiple microservices. A relational database has been used for Managing Inventory, Financial, and Payment related data. To store user details and product catalog data document db has been used. To store social network data, graph-based databases have been used.

However, it is also difficult to maintain consistency while ensuring partition tolerance across various data stores. In this case, we must consider the trade-offs associated with polyglot persistence and choose the right balance between consistency and availability based on the requirements of each microservice.

To know more about Polyglot Persistence, check out this article.

Real-World Use Cases

1. Netflix

0*HPjnftW72GkZWHRI.jpg

Netflix follows the microservices architecture. It prioritizes availability over consistency. Netflix’s recommendation system can be considered as one example. It should be available all the time. Even if it fails at some point, it should allow us to show the results correctly because there will be many people using this service at the same time. Therefore, consistency is not that required, because the user can be shown a recommendation that is not constant, rather than not showing anything at all and losing clients.

2. Uber

0*bCSoK0UZxJ-bJsuS.png

Uber’s microservices architecture prioritizes partition tolerance. As a result, it ensures that the platform is operational and responsive even in the face of network disruptions. This emphasis on partition tolerance contributes to a robust and reliable system.

Best Practices and Recommendations

1. Designing Microservices with CAP in Mind

It is essential to design every microservice with CAP in mind. We need to understand the trade-offs required for each microservice and design accordingly. Also, it is recommended to document and communicate the chosen trade-offs to maintain consistency across the development team.

2. Monitoring

We need to implement monitoring tools to track the performance of every microservice. Based on the performance and the needs of the system, trade-offs can be changed.

3. Evolution of Microservices Architecture Over Time

Microservices architecture can be changed over time. As the system evolves, so do the trade-offs. Therefore, we need to regularly revisit the design choices, considering changes in requirements, technology, and the overall ecosystem.

Wrapping Up

Understanding the CAP theorem and its implications for microservices is essential for architects and developers designing distributed systems.

The trade-offs between Consistency, Availability, and Partition Tolerance will directly impact the performance, resilience, and responsiveness of microservices architectures.

Therefore, it is crucial to select the appropriate aspect/s of the CAP theorem based on the requirement of the microservice.

By adopting best practices, considering practical challenges, and staying ahead of new technologies, organizations can navigate the complexities introduced by the CAP theorem and build scalable and adaptive microservices architectures.

I hope you found this article helpful.

Thank you for reading.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK