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

Cache result of LocalSite::read to avoid unnecessary db calls #4585

Merged
merged 7 commits into from
Apr 3, 2024

Conversation

Nutomic
Copy link
Member

@Nutomic Nutomic commented Apr 2, 2024

This function is called in a lot of different places, for example whenever a vote is made or a post is created, whether locally or over federation. This value changes very rarely, so we can avoid unnecessary db calls by caching it.

Comment on lines 22 to 27
static CACHE: Lazy<Cache<(), LocalSite>> = Lazy::new(|| {
Cache::builder()
.max_capacity(1)
.time_to_live(Duration::from_secs(1))
.build()
});
Copy link
Member

Choose a reason for hiding this comment

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

Should the static be defined in the function? I thought stuff like this was usually handled at the module level.

Copy link
Member Author

Choose a reason for hiding this comment

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

Its only used in this one function, so no need to make it available anywhere else.

Copy link
Contributor

Choose a reason for hiding this comment

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

why not extend the time to live, and invalidate cache in the 'update' method? 1 sec is really short

Copy link
Member Author

Choose a reason for hiding this comment

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

Because some Lemmy instances may run more than one backend for horizontal scaling. So the value can be updated through another backend and we wont know about it unless we read again from the db.

Copy link
Member

@dessalines dessalines Apr 2, 2024

Choose a reason for hiding this comment

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

I grepped the other time_to_live , and like the others it'd be good to extract this into a const somewhere. Ideally they should all live in utils or someplace common.

Copy link
Collaborator

Choose a reason for hiding this comment

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

improving expensive db queries is needed. removing 0.1ms db queries will not make a dent unless called thousands of times per second

Copy link
Member

Choose a reason for hiding this comment

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

Does the time to live make it so the cache is invalidated every second?

Copy link
Collaborator

@phiresky phiresky Apr 2, 2024

Choose a reason for hiding this comment

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

yes, as is this will make the number of queries for local site go to around 1 per second for an active instance. there's similar caching elsewhere. the number could potentially be higher but since this is a cheap query this is probably fine

Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised that 1 second is enough to improve performance. This must be called a lot of times per second.

Copy link
Member Author

Choose a reason for hiding this comment

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

Lemmy.ml receives roughly 230 votes per minute, so it will be called at least that many times. Actually more for post creations and other actions. Still it probably wont make a noticable difference unless you aggressively cache api responses.

@Nutomic
Copy link
Member Author

Nutomic commented Apr 3, 2024

Changed it to use the same const for all cache durations (with shorter duration in debug mode).

@dessalines dessalines merged commit 0876846 into main Apr 3, 2024
2 checks passed
@SleeplessOne1917 SleeplessOne1917 deleted the cache-local-site-read branch April 3, 2024 23:13
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.

6 participants