Skip to content
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

refactor(sdk): StreamRegistry cache invalidation #2885

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

teogeb
Copy link
Contributor

@teogeb teogeb commented Nov 18, 2024

Refactor StreamRegistry caching:

  • added private populateMetadataCache() method to populate the cache
  • added invalidateCache() helper function to simplify cache invalidation
  • renamed cache fields to be nouns instead of method verbs

Small functionality changes

  • The metadata is in cache after setStreamMetadata() and createStream() calls (we can easily populate the cache as the metadata is a method parameter)
  • When cached functions are called with allowCached == false, the value is stored to cache. Pre-existing value is invalidated (i.e. possible concurrent calls will also get fresh data).

@github-actions github-actions bot added the sdk label Nov 18, 2024
@teogeb teogeb changed the title refactor(sdk): StreamRegistry#populateMetadataCache() refactor(sdk): StreamRegistry cache invalidation Nov 18, 2024
packages/sdk/src/contracts/StreamRegistry.ts Outdated Show resolved Hide resolved
Comment on lines +521 to +524
if (!allowCached) {
invalidateCache(this.metadataCache, streamId)
}
return this.metadataCache.get(streamId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way this used to work is that when setting useCache = false it would just do a direct call and not use cache. Now it seems to be clearing cache. Is there a problematic scenario where if the underlying call rejects (e.g. JSON RPC is down) we clear a cache but can't provide a replace value?

Comment applies to later usages in this PR as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was described in the "small functionality changes" section. Calling the function with allowCached = false indicates that the caller needs the value to be up-to-date. Before this change one component could have seen that stream metadata is foo, and another that it is bar. The current application wide consistency is most likely better.

I.e. it follows the principle that it is better to fail a task than to allow data corruption to happen.

@@ -18,14 +24,11 @@ export class CachingMap<K extends MapKey, V, P extends any[]> {

private readonly cachedFn: (...args: P) => Promise<V>
private readonly cache: LRU<K, { data: V, maxAge: number }>
private readonly opts: Options<P, K>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nitpick) A Readonly<> here could provide more clarity to the reader that the options are not meant to change during the life of an instance of CachingMap.

Suggested change
private readonly opts: Options<P, K>
private readonly opts: Readonly<Options<P, K>>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't used this kind of deep annotation in the sdk code base before.

Many of our objects are immutable, but adding many Readonly<> annotations to all of those would just decrease readability. Using readonly in the field type is not the same, but it communicates the intention quite well.

(Also the name and the type of the field should communicate clearly that we are using the Configuration Option Pattern which implies immutability by default.)

packages/sdk/src/contracts/StreamRegistry.ts Outdated Show resolved Hide resolved
packages/sdk/src/contracts/StreamRegistry.ts Outdated Show resolved Hide resolved
@teogeb teogeb requested a review from harbu November 18, 2024 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants