Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Put a cache on /state_ids #7931

Merged
merged 1 commit into from
Jul 23, 2020
Merged

Put a cache on /state_ids #7931

merged 1 commit into from
Jul 23, 2020

Conversation

richvdh
Copy link
Member

@richvdh richvdh commented Jul 21, 2020

If we send out an event which refers to prev_events which other servers in
the federation are missing, then (after a round or two of backfill attempts),
they will end up asking us for /state_ids at a particular point in the DAG.

As per #7893, this is quite
expensive, and we tend to see lots of very similar requests around the same
time.

We can therefore handle this much more efficiently by using a cache, which (a)
ensures that if we see the same request from multiple servers (or even the same
server, multiple times), then they share the result, and (b) any other servers
that miss the initial excitement can also benefit from the work.

[It's interesting to note that /state has a cache for exactly this
reason. /state is now essentially unused and replaced with /state_ids, but
evidently when we replaced it we forgot to add a cache to the new endpoint.]

If we send out an event which refers to `prev_events` which other servers in
the federation are missing, then (after a round or two of backfill attempts),
they will end up asking us for `/state_ids` at a particular point in the DAG.

As per #7893, this is quite
expensive, and we tend to see lots of very similar requests around the same
time.

We can therefore handle this much more efficiently by using a cache, which (a)
ensures that if we see the same request from multiple servers (or even the same
server, multiple times), then they share the result, and (b) any other servers
that miss the initial excitement can also benefit from the work.

[It's interesting to note that `/state` has a cache for exactly this
reason. `/state` is now essentially unused and replaced with `/state_ids`, but
evidently when we replaced it we forgot to add a cache to the new endpoint.]
@richvdh richvdh requested a review from a team July 21, 2020 23:57
Comment on lines +382 to +384
resp = await self._state_ids_resp_cache.wrap(
(room_id, event_id), self._on_state_ids_request_compute, room_id, event_id,
)
Copy link
Member

Choose a reason for hiding this comment

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

The call above to self._state_resp_cache gets wrapped by dict(), which we don't do here. It doesn't seem necessary, but curious about the difference.

Copy link
Member Author

Choose a reason for hiding this comment

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

the dict there was added in https://github.com/matrix-org/synapse/pull/6176/files. It looks like it's so that we can add a room_version property after it comes out of the cache, without accidentally modifying the cached response.

It is unclear to me why that implementation was chosen instead of adding the room_id in _on_context_state_request_compute.

In any case, I think it's unnecessary in the new code.

@richvdh richvdh merged commit 7078866 into develop Jul 23, 2020
@richvdh richvdh deleted the rav/cache_state_ids branch July 23, 2020 17:38
babolivier pushed a commit that referenced this pull request Sep 1, 2021
* commit 'f88c48f3b':
  1.18.0rc1
  Fix error reporting when using `opentracing.trace` (#7961)
  Fix typing replication not being handled on master (#7959)
  Remove hacky error handling for inlineDeferreds. (#7950)
  Convert tests/rest/admin/test_room.py to unix file endings (#7953)
  Support oEmbed for media previews. (#7920)
  Convert state resolution to async/await (#7942)
  Fix up types and comments that refer to Deferreds. (#7945)
  Do not convert async functions to Deferreds in the interactive_auth_handler (#7944)
  Convert more of the media code to async/await (#7873)
  Return an empty body for OPTIONS requests. (#7886)
  Downgrade warning on client disconnect to INFO (#7928)
  Convert presence handler helpers to async/await. (#7939)
  Update the auth providers to be async. (#7935)
  Put a cache on `/state_ids` (#7931)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants