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

feat: alternative sharded redis cache #197

Closed
wants to merge 1 commit into from

Conversation

wschurman
Copy link
Member

@wschurman wschurman commented Oct 5, 2022

Why

This is the alternative to #196 mentioned in #196 (comment).

How

Instead of doing the sharding logic in the entity cache adapter level, do it within the redis interface. This requires some bookkeeping and tricks within the multi-redis logic, but isn't horrendous.

The main thing to review is the ShardedRedis class in the test. This is what would need to be duplicated to an application that uses it (though with a real consistent hash).

Test Plan

Run test (same test as other PR to prove it works).

@wschurman wschurman requested review from ide and quinlanj October 5, 2022 17:58
@codecov
Copy link

codecov bot commented Oct 5, 2022

Codecov Report

Merging #197 (6f53cf0) into main (dbf1e03) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #197   +/-   ##
=======================================
  Coverage   96.17%   96.17%           
=======================================
  Files          81       81           
  Lines        2065     2065           
  Branches      269      245   -24     
=======================================
  Hits         1986     1986           
- Misses         74       79    +5     
+ Partials        5        0    -5     
Flag Coverage Δ
integration 96.17% <100.00%> (ø)
unittest 96.17% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...tity-cache-adapter-redis/src/GenericRedisCacher.ts 100.00% <ø> (ø)
...ntity-cache-adapter-redis/src/RedisCacheAdapter.ts 100.00% <100.00%> (ø)
packages/entity/src/EntityLoader.ts 89.36% <0.00%> (ø)
...ty-example/src/adapters/InMemoryDatabaseAdapter.ts 45.83% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@quinlanj quinlanj left a comment

Choose a reason for hiding this comment

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

This code here looks fine to me. I'm assuming the plan is to make a custom class that implements IRedis to do your sharding.

export interface IRedis {
mget(...args: [...keys: string[]]): Promise<(string | null)[]>;
multi(): IRedisTransaction;
del(...args: [...keys: string[]]): Promise<any>;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
del(...args: [...keys: string[]]): Promise<any>;
del(...args: [...keys: string[]]): Promise<number>;

Integer reply: The number of keys that were removed.

private getRedisClientsForKeys(
keys: readonly string[]
): Map<number, { keys: string[]; redisClient: Redis }> {
const shardGroupsForKeys = new Map(keys.map((k) => [k, getShardForKey(k)]));
Copy link
Member

Choose a reason for hiding this comment

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

Instead of shard groups, aren't these just shards? (Map<Key, Shard>)

return redisClientsForKeys;
}

private getRedisInstanceForShardGroup(shardGroup: number): Redis {
Copy link
Member

Choose a reason for hiding this comment

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

The other places in the code refer to "Redis clients":

Suggested change
private getRedisInstanceForShardGroup(shardGroup: number): Redis {
private getRedisClientForShardGroup(shardGroup: number): Redis {

return ret;
}

async del(...args: string[]): Promise<void> {
Copy link
Member

Choose a reason for hiding this comment

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

In a non-test implementation, this should return a number (the sum of the individual del results)

@wschurman
Copy link
Member Author

This was a proof of concept. No longer needed for now.

@wschurman wschurman closed this Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants