Read Consistency with Database Replicas

At Shopify, we’ve long used database replication for redundancy and failure recovery, but only fairly recently started to explore the potential of replicas as an alternative read-only data source for applications.

Thomas Saunders
8 min readadvanced
--
View Original

Overview

The article discusses the challenges and solutions related to read consistency when using database replicas at Shopify. It highlights the issues of replication lag and presents the implemented solution of monotonic read consistency to ensure coherent data retrieval while maintaining performance.

What You'll Learn

1

How to implement monotonic read consistency in database applications

2

Why replication lag can lead to stale data in read replicas

3

When to use causal consistency versus monotonic read consistency

Prerequisites & Requirements

  • Understanding of database replication concepts
  • Familiarity with ProxySQL(optional)

Key Questions Answered

What is replication lag and how does it affect read consistency?
Replication lag refers to the delay between the primary database and its replicas, causing applications to read potentially stale data. This can lead to inconsistencies when related data is fetched from different replicas with varying lag times, impacting user experience and data integrity.
How does Shopify ensure read consistency with database replicas?
Shopify implements monotonic read consistency by routing successive reads to the same server, ensuring that they reflect a consistent state of data. This approach allows applications to avoid reading stale data while still benefiting from the performance of read replicas.
What are the advantages and disadvantages of tight consistency?
Tight consistency guarantees that all replicas are up to date with the primary server before operations are allowed, ensuring the freshest data. However, it is costly and can negate the performance benefits of using replicas, leading to delayed reads.
What issues did Shopify encounter when implementing consistent reads?
Shopify faced challenges such as server unavailability affecting read consistency and ProxySQL's additional load balancing causing intermittent inconsistencies. These issues were addressed by modifying server selection algorithms and disabling unnecessary rebalancing during consistent-read requests.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Backend
Proxysql
Used as a proxy layer for MySQL database servers to manage read requests and ensure consistency.
Database
Mysql
The primary database system used for storing application data.

Key Actionable Insights

1
Implement monotonic read consistency to enhance user experience in read-heavy applications.
By ensuring that all successive reads are routed to the same server, you can avoid the pitfalls of stale data and improve the reliability of your application's data retrieval.
2
Consider the trade-offs between tight consistency and performance when designing your database architecture.
While tight consistency offers the freshest data, it can significantly impact performance. Assess your application's needs to determine the best consistency model.
3
Utilize unique identifiers for consistent read requests to maintain coherence across database queries.
By passing a unique identifier in query comments, you can leverage a pseudorandom selection algorithm to ensure that related requests are consistently routed to the same server.

Common Pitfalls

1
Failing to account for server unavailability can lead to data inconsistencies during consistent read requests.
This occurs when the selected server becomes unavailable between requests. To mitigate this, always index into the entire list of configured servers before disqualifying the selected one.
2
Relying on ProxySQL's load balancing can introduce unexpected read inconsistencies.
Disabling additional load balancing for consistent-read requests helps maintain the integrity of the data being read, preventing forced rerouting to underweighted servers.

Related Concepts

Database Replication
Read Consistency Models
Causal Consistency
Monotonic Reads