Eliminating Duplicate Requests
If you’re building a backend service and can avoid the problem of serving duplicate requests, that’s a good thing. A duplicate is defined in this case as: if two requests R1 and R2 both have a response r_1, they are duplicates. Eliminating duplicates avoids doing unnecessary additional work.
At Tumblr we use finagle for building our backend stack. One of the benefits of using finagle is that services are composable. Here’s an example of a web server that supports Authorization. See how on line 75 you just compose these things together? This leads to very clean separation of concerns in your code.
This week I noticed that 5-10% of requests for a particular service were duplicates. I created a DuplicateRequestFilter that simply caches the most recent 500 requests/responses and if the request has recently been seen, we serve up the old response. Because this caches a Future, and not the actual result, it means that in flight requests are also handled appropriately. That is, we start serving a cached response as soon as the first request comes in, not after the first response goes out. Because this is implemented in terms of a finagle service filter, it required no changes to the actual backend service. Dropping this file in, and adding the class to the service composition, were the only required steps.
Pretty neat.