Table of Contents
Intro
While working on a tech blog post and tech event search engine, I noticed that fetching RSS or Atom feeds is starting to take a longer time, so I thought about a way of speeding up that operation since there are plans to scale collection at some point.
This post will describe a way of optimizing the process of fetching blog post feeds.
Overview
This query will sort requests by expected duration (response time). The output will be an ordering of the requests to make. First, all of those that we have timing data for will be sorted by duration. Then we'll have the ones we don't have timing data for, and those can be run in any order.
We split the requests in batches. Inside each batch, all the urls are fetched in parallel. So an estimate for the running time of a batch is the maximum response time in that batch. The batches themselves are run sequentially.
The aim of this is to not let urls with slow response times slow down the entire batch.
This is a small example with batch size 3.
.response { background-color: orange; height: 0.2em !important; display: inline-block; padding-bottom: 3px; padding-top: 10px; margin-top: 3px; float: left; clear: left; } .batch { background: #336699; display: inline-block; padding-left: 4px; padding-bottom: 4px; padding-top: 4px; padding-right: 4px; }unordered [3,10,4] ; [13,9,2] ; [11,20,8] total time: 10 + 13 + 20 = 43
A timeline visualization of these response times
ordered [2,3,4] ; [8,9,10] ; [11,13,20] total time: 4 + 10 + 20 = 34
Another timeline visualization, but this time the requests are ordered by the expected response time and afterwards, the batches are built.
So the difference between these two timelines can be seen above, we save 9 seconds due to reordering.
After implementing this new logic, the running time for fetching 1597 urls was decreased from 24 min 25 seconds to 20 min 17 seconds.
Implementation
In the example below, we use wCTE to gradually refine our results. First we get the urls which are passed as a parameter when the query is executed. These are the urls which we plan to make requests to.
Then we compute average response time based on previous measurements
since the collector measures and stores such information on every run
in the entry_timings
table.
After we're done with this, we'll use a JOIN
to find out which feed
urls we already know the response times to, and we sort them in
ascending order by those times.
If we find feeds that we have no timing information for, we will allow those to be run at the end of the collection process.
WITH planned_urls AS ( -- all requests we plan on makingSELECTunnest(%(urls)s) AS url ), timing_urls AS ( -- urls that we have timing data for.-- get duration for each url in average millisecondsSELECT feed_url, (AVG(EXTRACT(EPOCH FROM a.end_time - a.start_time)*1000))::integerAS duration FROM entry_timing a WHERE a.end_time ISNOTNULLAND a.start_time ISNOTNULLGROUPBY a.feed_url ), known_urls AS ( -- requests where we have timing data.-- we sort these in ascending order of their expected-- duration.SELECT b.feed_url, b.duration FROM planned_urls a JOIN timing_urls b ON a.url = b.feed_url ORDERBY b.duration ), unknown_urls AS ( -- requests where we don't have timing data.-- we can't say anything about these so the order-- won't matter here.SELECT a.url, 9999999 as duration FROM planned_urls a LEFTJOIN timing_urls b ON a.url = b.feed_url WHERE b.feed_url ISNULL ), all_reordered AS ( SELECT * FROM known_urls UNIONALLSELECT * FROM unknown_urls ) SELECT *, rank() over(ORDERBY duration ASC) AS ordinal FROM all_reordered;
Conclusion
We've seen a way of improving run time of HTTP requests run in batches by reordering and building the batches using stored timing information from previous runs.