4cat icon indicating copy to clipboard operation
4cat copied to clipboard

Use memcache

Open stijn-uva opened this issue 2 years ago • 0 comments

The configuration manager currently queries the postgres database every time a config value is read. This is not a huge problem, since Postgres is fast and does some caching by itself. But it does mean a lot of database round-trips that are not necessary, because the value has not changed in the meantime and the query's result will not have changed either.

This PR optionally uses memcached to cache these values so the database is only queried when necessary. If memcached is not available, the database is queried each time, same as before.

The library we use, pymemcache, is not thread-safe, which matters in two places:

  • Workers run in their own threads so need their own memcache client.
  • Flask may run requests in their own thread so they need their own client as well.

This is taken care of by ConfigWrapper. If in workers you always use self.config, and in Flask you always use g.config, which you should as of #455, this should not be an issue. The configuration reader will now raise a RuntimeException when trying to use memcache in a non-threadsafe context.

@dale-wahl I had to remove some tests because worker initialisation has changed a little bit. You might want to take a look to see if the tests can be put back somehow - I'm not familiar enough with pytest yet to know this. Also, I haven't looked at the Docker side of things yet, but it might be relatively straightforward to add this to Docker as well - just need to have a memcached daemon that both the frontend and backend can talk to running somewhere.

stijn-uva avatar Oct 23 '23 15:10 stijn-uva