https://slatedb.io logo
🤯 in-memory block cache PR from <@69240463826327...
# general
c
🤯 in-memory block cache PR from @macha____ is up: https://github.com/slatedb/slatedb/pull/137 👀 needed 😄
m
@criccomini trying to figure out how to solve the range scan with cache
What do you think about storing metadata in memory as well and scan it to figure out which blocks we can fetch from s3
I believe rockset does the same as well but since they preemptively fetch data to store on disk they wouldn’t need to do a lot of s3 calls
c
> storing metadata in memory as well Can you clarify this? Not sure which metadata you mean.
Ya, the crossed my mind. Basically having the page cache sit atop the in-disk SSTs. Then you never pay for a bunch of GETs—just one per SST. But it would be nice to front got S3 pages in memory while the SST is being downloaded to disk. 🤔
I feel like caching ranges must be well researched and there must be standard data structures and algorithms for it…
Maybe the best/easiest thing to do is to have the range read check which blocks are cached and call parallel GETs for each missing subrange…
r
> Maybe the best/easiest thing to do is to have the range read check which blocks are cached and call parallel GETs for each missing subrange… +1 this is what I was imagining we could start with. We first identify the SSTs that we need from a given sorted run. Then we'd need to get to the indexes for the first and last SSTs to see what blocks we need (we'd need all blocks for the other SSTs). For now the indexes are in-memory, but with https://github.com/slatedb/slatedb/issues/88 we'd treat the index like a block. So we'd need to load it from in-memory/disk cache or s3. Then, we know the blocks we need and can get those from cache or s3 as needed. I don't think we need to cache entire ranges necessarily. I feel like most of the work on caching ranges deals with the fact that ranges are not aligned to anything in particular. But in our case we are always dealing with "blocks" as a unit.
c
One thing right now is the API is a little different from what you describe. The TableStore has ReadBlocks for an SSTableId and a block range. And the cache would be on table store. But the thing that knows about the full scan (multiple SSTs) is the SSTableIterator.
r
yeah but it interacts with table store and reads block ranges
as it's main i/o interface
c
Ah gotcha. I misread something you said.
What do you mean by the “I don’t think we need to cache the entire range necessarily”?
Like, skip in memory caching for range wueries?
r
Ah no - I meant more we don't need to treat ranges as the entities that we cache - just blocks
c
Ahh gotcha. Yes agreed.
I think we’re saying the same thing then
Basically figure out which blocks we need to cache from S3 (by checking what’s in the cache), fetch those, merge with cached blocks to construct the full range
r
yep
c
👍
Fun part is coming up with the algo to figure out the sub ranges to fetch 😀
r
ah yeah like if we have one block cached in the middle of a 10 block range, do we split it into 2 fetches vs 1 request to fetch the whole range?
c
Exactly haha
Some cost function…
Should probably do the naive thing for now 😅
m
@criccomini updated PR with a better fetch for read_blocks, now just fetches the range that is not available in cache
c
😀 nice! Will take a look soon
@macha____ left some comments! High-level looks good, but I think the range algorithm needs some work. I suggested two potential alternatives.
@Rohan might be good to get your 👀 on this one too.. it's got some complex stuff in
read_blocks
, which is critical.
m
@criccomini I might be misunderstanding something but I did the (min, max) in range because we would be fetching the same SST if the sst includes (0,5) and we fetch (1,2) and (3,4) right?
c
Mmm..
So, if someone calls read_blocks(.. [0, 5]) then we need blocks 1 2 3 and 4
And if block 3 is in the cache, I think this should result in 2 table store reads: one for blocks 1 and 2, and the other for block 4. And we should use block 3 from the cache.
as I read the code now, you'd have uncached_blocks = 1, 2, 4 right?
I could be misreading
m
yeah that's right so I do a read_blocks on sst for (1, 4) (which I now realize is wrong for a different reason, need to use index -1 and index + 1). But if you do obj.read_range(0, 5) it will give you 1,2,3,4 blocks in one call right why make 2 seperate calls with ranges(1,3) and range(3,5)?
c
So, I think after this, what we want to do is add some form of a cost-based optimizer to determine when to ignore cached blocks vs. when to use them. In the example I gave, it'd make more sense to just do one fetch. But if the range is very large and 90% of the blocks are in cache, it would make sense to do multiple parallel GETs rather than do a full download.
So the approach Rohan and I were thinking of was to implement the naive way in this GH issue (the naive way just do parallel GETs for all subranges), then follow up with a separate PR that adds the optimizer to determine when to merge fetches and disregard some of the cache.
I'm open to alternative approaches if you can think of them, but they should work well with the various workloads we have (including large multi-gig SSTs with a lot cached, a little cached, and some cached but a lot of non-contiguous blocks)
Something we're talking about in the on-disk SSTs is to cache SST parts rather than the whole SST on disk
cc @flaneur233 in case you have other thoughts here..
Rohan and I discussed caching parts in memory, but thought the blocks would already work OK.
here ^
m
Okay this makes sense I will do a naive range fetch and can build on that as required
c
Thanks! Really appreciate your help. Things are a lot less daunting with y'all helping out 🙂
m
@criccomini updated code if you wanna have a look
c
Thanks! Will take a look in a few.