Recent work has introduced the learned Bloom filter . As formalized in , the learned Bloom filter, like a standard Bloom filter, provides a compressed representation of a set of keys that allows membership queries. Given a key , a learned Bloom filter will always return yes if is in , and will generally return no if is not in , but may return false positives. What makes a learned Bloom filter interesting is that it uses a function that can be obtained by “learning” the set to help determine the appropriate answer. Specifically, we recall the following definition from :
A learned Bloom filter on a set of positive keys and negative keys is a function and threshold , where is the universe possible query keys, and an associated standard Bloom filter , referred to as a backup filter. The backup filter is set to hold the set of keys . For a query , the learned Bloom filter returns that if , or if and the backup filter returns that . The learned Bloom filter returns otherwise.
In less mathematical terms, a learned Bloom filter consists of pre-filter before a Bloom filter, where the backup Bloom filter now acts to prevent false negatives. The pre-filter suggested in 
comes from a neural network and estimates the probability an element is in the set, allowing the use of a backup Bloom filter that can be substantially smaller than a standard Bloom filter for the set of keys. If the function has a sufficiently small representation, then the learned Bloom filter can be correspondingly smaller than a standard Bloom filter for the same set.
Given this formalization of the learned Bloom filter, and the additional analysis from  for determining the false positive rate of a learned Bloom filter, it seems natural to ask whether this structure can be improved. Here we show, perhaps surprisingly, that a better structure is to use a Bloom filter before using the function , in order to remove most queries for keys not in . We emphasize that this initial Bloom filter does not declare that an input is in , but passes on all matching elements the learned function , and it returns when the Bloom filter shows the element is not in . Then, as before, we use the function to attempt to remove false positives from the initial Bloom filter, and then use the backup filter to allow back in keys from that were false negatives for . Because we have two layers of Bloom filters surrounding the learned function , we refer to this as a sandwiched learned Bloom filter. The sandwiched learned Bloom filter is represented pictorially in Figure 1.
In hindsight, our result that sandwiching improves performance makes sense. The purpose of the backup Bloom filter is to remove the false negatives arising from the learned function. If we can arrange to remove more false positives up front, then the backup Bloom filter can be quite porous, allowing most everything that reaches it through, and therefore can be quite small. Indeed, our analysis shows that the backup filter can be remarkably small, so that as the budget of bits available for the Bloom filters increases, any additional bits should go to the initial Bloom filter. We present our analysis below.
Ii Analyzing Sandwiched Learned Bloom Filters
We model the sandwiched learned Bloom filter as follows. The middle of the learned Bloom filter we treat as an oracle for the keys , where . For keys not in there is an associated false positive probability , and there are false negatives for keys in . (The value is like a false negative probability, but given this fraction is determined and known according to the oracle outcomes.) This oracle can represent the function associated with Definition 1 for learned Bloom filters, but might also represent other sorts of filter structures as well. Also, as described in , we note that in the context of a learned Bloom filter, the false positive rate is necessarily tied to the query stream, and so in general may be an empirically determined quantity; see  for further details and discussion on this point. Here we show how to optimize over a single oracle, although in practice we may possibly choose from oracles with different values and , in which case we can optimize for each set of value and choose the best suited to the application.
We assume a total budget of bits to be divided between an initial Bloom filter of bits and a backup Bloom filter of bits. To model the false positive rate of a Bloom filter that uses bits per stored key, we assume the false positive rate falls as . This is the case for a standard Bloom filter (where when using the optimal number of hash functions, as described in the survey ), as well as for a static Bloom filter built using a perfect hash function (where , again described in ). The analysis can be modified to handle other functions for false positives in terms of in a straightforward manner. It is important to note that if , the backup Bloom filter only needs to hold keys, and hence we take the number of bits per stored key to be . Note that if we find the best value of is , then no initial Bloom filter is needed, but otherwise, an initial Bloom filter is helpful.
The false positive rate of a sandwiched learned Bloom filter is then
To see this, note that for , first has to pass through the initial Bloom filter, which occurs with probability . Then either causes a false positive from the learned function with probability , or with remaining probability it yields a false positive on the backup Bloom filter, with probability .
As and are all constants for the purpose of this analysis, we may optimize for in the equivalent expression
The derivative with respect to is
This equals 0 when
This yields that the false positive rate is minimized when
This result may be somewhat surprising, as here we see that is a constant, independent of . That is, the number of bits used for the backup filter is not a constant fraction of the total budgeted number of bits , but a fixed number of bits; if the number of budgeted bits increases, one should simply increase the size of the initial Bloom filter as long as the backup filter is appropriately sized.
In hindsight, returning to the expression for the false positive rate
we can see the intuition for why this would be the case. If we think of sequentially distributing the bits among the two Bloom filters, the expression shows that bits assigned to the initial filter (the bits) reduce false positives arising from the learned function (the term) as well as false positives arising subsequent to the learned function (the term), while the backup filter only reduces false positives arising subsequent to the learned function. Initially we would provide bits to the backup filter to reduce the rate of false positives subsequent to the learned function. Indeed, bits in the backup filter drive down this term rapidly, because the backup filter holds fewer keys from the original set, leading to the (instead of just a ) in the exponent in the expression . Once the false positives coming through the backup Bloom filter reaches an appropriate level, which, by plugging in the determined optimal value for , we find is
then the tradeoff changes. At that point the gains from reducing the false positives from the backup Bloom filter are smaller than the gains obtained by using the initial Bloom filter.
As an example, using numbers roughly corresponding to settings tested in , suppose we have a learned function where and . For convenience we consider (that corresponds to perfect hash function based Bloom filters). Then
Depending on our Bloom filter budget parameter , we obtain different levels of performance improvement by using the initial Bloom filter. At bits per key, the false positive rate drops from approximately to , over an order of magnitude. Even at bits per key, the false positive rate drops from approximately to .
If one wants to consider a fixed false positive rate and consider the space savings from using the sandwiched approach, that is somewhat more difficult. The primary determinant of the overall false positive rate is the oracle’s false positive probability . The sandwich optimization allows one to achieve better overall false positive with a larger ; that is, it can allow for weaker, and correspondingly smaller, oracles.
A possible further advantage of the sandwich approach is that it makes learned Bloom filters more robust. As discussed in , if the queries given to a learned Bloom filter do not come from the same distribution as the queries from the test set used to estimate the learned Bloom filter’s false positive probability, the actual false positive probability may be substantially larger than expected. The use of an initial Bloom filter mitigates this problem, as this issue affects the smaller number of keys that pass the initial Bloom filter.
In any case, we suggest that given that the sandwich learned Bloom filter is a relatively simple modification if one chooses to use a learned Bloom filter, we believe that the sandwiching method will allow greater application of the learned Bloom filter methodology.
-  A. Broder and M. Mitzenmacher. Network applications of bloom filters: A survey. Internet Mathematics, 1(4):485-509, 2004.
-  T. Kraska, A. Beutel, E. H. Chi, J. Dean, and N. Polyzotis. The Case for Learned Index Structures. https://arxiv.org/abs/1712.01208, 2017.
-  M. Mitzenmacher. A Model for Learned Bloom Filters and Related Structures. https://arxiv.org/abs/1802.00884, 2018.