Draft
Refactor document updates to Redis CAS + async Mongo write-back with retry#220
Conversation
4 tasks
Copilot
AI
changed the title
[WIP] Refactor document update for caching system
Refactor document updates to Redis CAS + async Mongo write-back with retry
May 28, 2026
3 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors DocumentBase.Update to prefer an atomic cache-side compare-and-swap (CAS) path when supported (intended for Redis), and moves the database persistence step into an asynchronous write-back with bounded retries to reduce synchronous contention.
Changes:
- Added a new cache contract (
diface.IAtomicVersionCache) for version-checked atomic updates. - Updated
DocumentBase.Updateto use cache CAS + background persistence, including snapshot cloning for async write-back. - Implemented Redis Lua-based CAS and added a focused unit test covering CAS retry + async write-back retry/success.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| orm/nosql/document.go | Cache-first CAS update flow plus async Mongo write-back and JSON-based snapshot cloning. |
| orm/nosql/document_update_test.go | New unit test for CAS conflict retry and async write-back retry/success. |
| orm/nosql/diface/icache.go | Introduces IAtomicVersionCache interface for atomic versioned cache updates. |
| orm/nosql/cache/redis_cache.go | Adds Redis Lua CAS implementation plus version-key management helpers. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+192
to
+204
| if _, err := atomicCache.CompareAndSwapCache( | ||
| d.ctx, | ||
| d.Key, | ||
| d.version, | ||
| cacheSnapshot, | ||
| DefaultCacheTTL, | ||
| ); err != nil { | ||
| return err | ||
| } | ||
| prevVersion := d.version | ||
| d.version = nextVersion | ||
| go d.asyncWriteBack(snapshot, prevVersion) | ||
| return nil |
Comment on lines
+21
to
+23
| // IAtomicVersionCache provides atomic version-checked cache updates. | ||
| type IAtomicVersionCache interface { | ||
| CompareAndSwapCache( |
Comment on lines
+105
to
+117
| res := c.Eval( | ||
| ctx, | ||
| compareAndSwapLua, | ||
| []string{key.String(), versionKey(key)}, | ||
| expectedVersion, | ||
| nextVersion, | ||
| data, | ||
| expire.Milliseconds(), | ||
| ) | ||
| if err := res.Err(); err != nil { | ||
| return noptions.NoVersion, err | ||
| } | ||
| return nextVersion, nil |
Comment on lines
+201
to
+203
| prevVersion := d.version | ||
| d.version = nextVersion | ||
| go d.asyncWriteBack(snapshot, prevVersion) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR introduces a high-concurrency document update path that prioritizes cache-side atomicity and eventual consistency. Updates now use Redis CAS semantics with retry/backoff and persist to Mongo asynchronously to reduce synchronous contention while preventing stale overwrites.
Atomic cache update (Redis + Lua)
IAtomicVersionCache) for cache implementations.RedisCache.CompareAndSwapCache(...)::versionkey.Optimistic concurrency control (version/CAS)
DocumentBase.Update(...)now uses cache CAS when supported.Async Mongo write-back (eventual consistency)
asyncWriteBack).Focused behavior coverage