openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Conditionally fetch blog RSS feed - performance improvement

Open tfmorris opened this issue 6 years ago • 10 comments

Currently we unconditionally fetch and XML parse the RSS feed. We do cache the result, but only for 5 minutes and the feed changes on the scale of months, not minutes. We should use one of HTTP's conditional GET mechanisms to only fetch the payload when it's changed.

Describe the problem that you'd like solved

The payload includes

<channel>
    <lastBuildDate>Wed, 27 Nov 2019 19:06:51 +0000</lastBuildDate>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>

which could be used to tailor the polling period, but by the time we've done all the XML parsing, we've done the bulk of the work, so we should instead conditionally fetch the URL and only do the parsing if we get new data.

Proposal & Constraints

The response headers include Last-Modified and ETag:

Last-Modified: Wed, 27 Nov 2019 19:06:51 GMT
ETag: "e520140570cbdfad48c7432feffb9d91-gzip"

and the Last-Modified time matches the payload build time, so we can use request headers of either If-Modified-Since or If-None-Match to conditionally GET the RSS feed, shortcutting any additional work if no payload is returned.

Additional context

Where we cache the blog posts: https://github.com/internetarchive/openlibrary/blob/077bcfe3d557a2adfa3aeafb0aa86b45be0ec86c/openlibrary/plugins/upstream/utils.py#L1429-L1431

Stakeholders

tfmorris avatar Jan 08 '20 00:01 tfmorris

Hello,

I'm new to this codebase and interested in tackling this. Could I be assigned to this issue? Any additional guidance would be appreciated.

Thanks!

BenjaminVC avatar Jul 27 '23 07:07 BenjaminVC

@mekarpeles Can I please take on this issue?

XiyueJasmineZhang avatar Nov 28 '23 16:11 XiyueJasmineZhang

@tfmorris @mekarpeles @xayhewalo Can you please assign me this issue and guide me further if I am struck at any problem?

tusharv01 avatar Dec 20 '23 07:12 tusharv01

@tfmorris How would you feel about increasing the cache time from 5 minutes to 1 day?

I agree it is wasteful to parse all the xml so often and I think it is be a fair tradeoff to increase the polling time.

The alternative of getting/storing/sending the last modified time and checking the headers seems like a decent amount of work for relatively little benefit.

RayBB avatar Mar 21 '24 18:03 RayBB

Increasing the cache timeout sounds fine, but you might want to check with @mekarpeles concerning how much staleness he's willing to live with. Even increasing it to the 1 hr RSS polling interval would be a 12x reduction in work though.

To be honest, I don't remember how I envisioned state management for this working, but you wouldn't need to look at response headers because you'd get an explicit 304 Not Modified error response. Having said that, the way things are set up now, you'd have already incurred a cache miss, so you'd no longer have the information that you need to avoid redoing the work.

tfmorris avatar Mar 21 '24 18:03 tfmorris

Hi hi! I took the liberty of opening up a PR that addresses this. Comments and concerns definitely appreciated!

https://github.com/internetarchive/openlibrary/pull/10015

astatide avatar Nov 09 '24 01:11 astatide

Presumably now we're only calling this when we recache the home page so it's probably not a huge cost.

mekarpeles avatar Nov 11 '24 09:11 mekarpeles

Ah, okay. Do you think it's still worth pursuing the PR, then?

Also, curious if there's any other good first issue stuff, in that case... (most everything seems to be assigned and/or have an attached in-progress PR)

astatide avatar Nov 11 '24 21:11 astatide

Ah, okay. Do you think it's still worth pursuing the PR, then?

I think that increasing the time that blog posts are cached to a day should solve this issue to everybody's satisfaction (anybody can correct me if I'm wrong).

Staff will be able to evict this cache entry if we want the latest changes to be displayed immediately.

jimchamp avatar Nov 13 '24 22:11 jimchamp

Hi @mekarpeles @jimchamp I have gone through the issue, reproduced and also looked at the closed PR which was abandoned. I'd like to fix this by increasing the cache timeout to a day, as suggested in the comments. I've tested this locally and have the fix ready. Will open a PR shortly.

Saad259 avatar Feb 22 '26 03:02 Saad259