-
Notifications
You must be signed in to change notification settings - Fork 5
fix lack of idempotency in SpiceDB writes #148
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #148 +/- ##
==========================================
+ Coverage 65.67% 65.77% +0.09%
==========================================
Files 25 25
Lines 3292 3392 +100
==========================================
+ Hits 2162 2231 +69
- Misses 890 913 +23
- Partials 240 248 +8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
6316aa6 to
68b7862
Compare
68b7862 to
af0f2f6
Compare
vroldanbet
commented
Jul 29, 2025
ecordell
reviewed
Jul 30, 2025
I started all of this because of flakes that were trivially to reproduce, and the underlying issue was WriteToSpiceDB was not idempotent. It was used by both the optimistic and pesimistic locking workflows, and the lack of idempotency meant the workflows couldn't recover by themselves from transient errors. When the tests failed for the wrong reasons, they flaked. We introduced `failpoints` as a mechanism to simulate panics similar to hardware or network failures. However, some recent changes meant the failpoints were no longer unique and were causing failures _in the wrong places_. - when the pessimistic wrote an exclusive lock, retrying it due to failure wouldn't work, because the precondition prevented it (_make sure no one has a lock on this resource_!) - when the optimistic wrote with CREATE semantics, a subsequent retry after a successful write due to failure response failure would fail because the tuples already exist. This commit proposes making WriteToSpiceDB truly idempotent by introducing idempotency keys in the SpiceDB schema. All writes will include a relationships that identify the workflow and the hash of the payload as the idempotency key. The flow is as follows: - perform write - if failure happens, check if idempotency key was written in previous request - if exists, assume operation was successful - if it does not, bubble up the error The cost of the extra ReadRelationships is only paid in the even of a retry due to a failure. The tests were written with the assumption that the system would bubble up errors after recovery. This goes against the expectations of a durable workflow engine, which embraces idempotency and is expected to retry on errors, rather than have the client retry, unless those are unrecoverable. This was all by design: the workflow wouldn't be responsible to retry things, but rather execute compensatory operations after an operation failed. This meant the client had to retry those errors, and it turns out troubleshooting what happened on a transient error is not that trivial for folks building on top of the spicedb-kubeapi-proxy.
af0f2f6 to
4b28081
Compare
adds relationship expiration to idempotency keys, so they don't end up accumulating indefinitely in the database
ecordell
approved these changes
Jul 31, 2025
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Built on top of #146, this PR only adds the last commit
I started all of this because of flakes that were trivially reproducible in #146, and the discovery that the underlying
WriteToSpiceDBworkflow activity was non-idempotent.It was used by both the optimistic and pessimistic locking workflows, and the lack of idempotency meant the workflows couldn't recover by themselves from transient errors. When the tests failed for the wrong reasons, they flaked.
We introduced
failpointsas a mechanism to simulate panics similar to hardware or network failures. However, some recent changes meant the failpoints were no longer unique and were causing failures in the wrong places.CREATEsemantics, a subsequent retry after a successful write due to a failure response would fail because the tuples already exist.This commit proposes making
WriteToSpiceDBtruly idempotent by introducing idempotency keys in the SpiceDB schema. All writes will include a relationships that identify the workflow and the hash of the payload as the idempotency key.The flow is as follows:
The cost of the extra ReadRelationships is only paid in the event of a retry due to a failure.
The tests were written with the assumption that the system would bubble up errors after recovery. This goes against the expectations of a durable workflow engine, which embraces idempotency and is expected to retry on errors, rather than have the client retry, unless those are unrecoverable.
This was all by design: the workflow wouldn't be responsible for retrying things, but rather execute compensatory operations after an operation failed. This meant the client had to retry those errors, and it turns out that troubleshooting what happened on a transient error is not that trivial for folks building on top of the spicedb-kubeapi-proxy.