-
Notifications
You must be signed in to change notification settings - Fork 951
Add support for asynchronous release to replicaKeysWithExpire on writable replica #2849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for asynchronous release to replicaKeysWithExpire on writable replica #2849
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## unstable #2849 +/- ##
============================================
+ Coverage 72.26% 72.43% +0.17%
============================================
Files 128 128
Lines 70370 70428 +58
============================================
+ Hits 50851 51017 +166
+ Misses 19519 19411 -108
🚀 New features to boost your workflow:
|
Signed-off-by: Scut-Corgis <[email protected]>
a428436 to
7fa9355
Compare
|
We generally discourage the use of writable replicas. What user scenarios would you use it in? The async way code is pretty simple, so i guess there is no harm to use it. @zuiderkwast WDYT? |
In our daily operations, we often need to do master-slave switches. Our goal is to avoid noticeable impact on the business—like key loss or reduced availability (e.g., write failures). |
Co-authored-by: Binbin <[email protected]> Signed-off-by: jiegang0219 <[email protected]>
Yeah, this method is described here: https://valkey.io/topics/admin/#upgrading-or-restarting-a-valkey-instance-without-downtime. Is this where you found the recommendation? I think there is no need to use writeable replicas. We should instead recommend the FAILOVER command, which does a coordinated failover between the primary and the replica. Is there any benefit in using writeable replicas during this switch-over or is the documentation simply outdated? |
The method in the linked document aligns with what I described — we use writeable replicas for primary-replica switching. The FAILOVER command has two modes: either it makes the primary lose some written data, or it disables writes for a period to align their offsets before switching. Both modes are actually business-perceptible for us. @zuiderkwast |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The FAILOVER command has two modes: either it makes the primary lose some written data, or it disables writes for a period to align their offsets before switching. Both modes are actually business-perceptible for us.
Interesting. So if you (and other users) really need writable replicas, then we can't deprecate them. The implementation is very simple so I want to accept it. @enjoy-binbin Do you agree?
The reason we are skeptical to writable replicas in general is that it can cause data inconsistency. If some data written directly to the replica (such as SET k v) and some data is replicated from the primary (such as HSET k f v), the replication can fail if the key is of a different type than what the replication expected (for example repliction of HSET would fail if it's not a hash). It is a theoretical problem. In practice, it's easy to avoid it, but it's good to be aware of it.
Problem
When executing
FLUSHALL ASYNCon a writable replica that hasa large number of expired keys directly written to it, the main thread
gets blocked for an extended period while synchronously releasing the
replicaKeysWithExpiredictionary.Root Cause
FLUSHALL ASYNCis designed for asynchronous lazy freeing of core datastructures, but the release of
replicaKeysWithExpire(a dictionary trackingexpired keys on replicas) still happens synchronously in the main thread.
This synchronous operation becomes a bottleneck when dealing with massive
key volumes, as it cannot be offloaded to the lazyfree background thread.
This PR addresses the issue by moving the release of
replicaKeysWithExpireto the lazyfree background thread, aligning it with the asynchronous design
of
FLUSHALL ASYNCand eliminating main thread blocking.User scenarios
In some operations, people often need to do primary-replica switches.
One goal is to avoid noticeable impact on the business—like key loss
or reduced availability (e.g., write failures).
Here is the process: First, temporarily switch traffic to writable replicas.
Then we wait for the primary pending replication data to be fully synced
(so primry and replicas are in sync), before finishing the switch. We don't
usually need to do the flush in this case, but it's an optimization that can
be done.