Skip to content

Improve the cache quota system #97

Closed

Description

@coffeemug

We can't have the OOM killer wreaking havoc on our users' processes willy-nilly. We need to have a smarter cache memory manager that limits the total cache size per machine to some reasonable number, and allows stealing cache memory between tables. We can discuss the details when we get to this feature.

Additionally, we need to do memory profiling on large queries -- it seems that memory usage is higher than it should be.

Activity

  1. alekstorm commented on Dec 13, 2012

    @alekstorm

    Er, hi. This may have been more appropriate on #98, but you could have the cache managed by another process, and communicate with it via some IPC mechanism. When the OOM killer goes hunting, it'll kill the cache process, since it's using a ton of memory, sparing the master process, which will detect the wanton slaughter and spawn a new cache process. Queries will slow down for a second or two during this time, but the system in general was probably already extremely slow due to memory pressure causing frequent page faults (if they have swap on, which a production system shouldn't).

    Who knows, you could even have a collection of cache processes, each managing, say, 1GB each, so the OOM killer will only need to sacrifice one of them.

    You could also keep the current monolithic process architecture, and set /proc/<pid>/oom_adj to -17 to prevent the OOM killer from ever selecting you, instead choosing something less critical, like a browser. However, you'd have to be absolutely sure you have zero memory leaks, or the system would have no recourse to stop you from eating all the memory, and causing... a kernel panic? I'm honestly not sure what happens.

    Sorry if the speculation is unproductive; this is literally the first issue I came across - considering getting involved in the project.

  2. coffeemug commented on Dec 13, 2012

    @coffeemug
    ContributorAuthor

    Thanks!! It would be pretty hard to break out the caching system into another process, but it's a really interesting idea.

    I think the solution we'll go with here is to limit the cache size on a per-machine basis, and allow tables to steal cache space from each other (this is the way all other db systems with a custom caching scheme do it). This isn't too difficult to do, and really is just a matter of doing it :)

  3. ghost assigned on Dec 22, 2012
  4. jdoliner commented on Feb 7, 2013

    @jdoliner
    Contributor

    This is pretty obviously not going in to 1.4 at this point so I'm going to push it to backlog and we'll see if we can get it in to 1.5.

  5. coffeemug commented on Feb 12, 2013

    @coffeemug
    ContributorAuthor

    Moving to 1.5 so it doesn't slip through the cracks -- this is pretty important because it's one of the main reasons the OOM killer unleashes on the rethinkdb process and confuses everyone.

  6. danielmewes commented on Apr 27, 2013

    @danielmewes
    Member

    This being in backlog once again, I thought I would add my humble suggestion on this.

    I believe the problem could be strongly reduced if the following could be implemented:

    1. An interface in the buffer cache to change the max cache size online. I don't think this would be very difficult to do, would it? If the max_size is decreased, the page replacement should be invoked once to actually free the memory.
    2. Exposing an option to the user (ideally in the web UI) to change the cache quota of a table. Other cluster nodes would update their buffer cache instances when receiving the metadata update.

    This would already be very useful I think. In fact I believe that making this option configurable at runtime is quite crucial. The current system works for developers, in some cases only just. For anything that's in production, being able to change the cache size would be absolutely crucial I imagine. Plus, - even if you try - you cannot realistically set cache quotas properly when creating the tables, because as soon as you add an additional one, you would run out of memory. Or you might upgrade memory in the machines later on. Or your load might change and different tables might then require different amounts of cache. So currently, the only feasible way is to be extremely conservative on cache values. In many applications, this can degrade performance by magnitudes.

    Optionally, one could think about adding quota configurability on a per-machine level. If the cluster is not homogeneous, something like that would be useful. However I suppose that this should be done properly by means of the general overhaul of the cache quota system that @coffeemug initially mentioned, as there are many different configurations and use-cases to be considered.

  7. coffeemug commented on Apr 29, 2013

    @coffeemug
    ContributorAuthor

    @danielmewes -- we decided to adopt a simple solution to this. The cache will be capped to some proportion of total available memory (or as set by the user on process startup time), and the tables will steal memory from each other. It's just a matter of doing it, we should be able to get to it ASAP.

  8. danielmewes commented on Apr 30, 2013

    @danielmewes
    Member

    @coffeemug Well, that's the more complex solution, as it requires inter-thread communication with all the questions regarding performance and race conditions that come with it. Also it doesn't seem obvious (to me) when one table should "steal" space from another. Should it give it back when the other table needs the memory again? When a request requires cache memory, at what priority should it evict one of its own pages vs. ask another table to provide more memory?
    In case you have that all worked out, please ignore my concerns.

    If it's easy enough to implement, it's seems like the superior solution. (though there probably are still a handful of cases where the admin would like to specify per-table cache limits or "cache priorities" manually)

  9. danielmewes commented on Jun 19, 2013

    @danielmewes
    Member

    Bump

    I believe that we really really should make the quota reconfigurable for existing tables. This should be very very easy to implement (see #97). The "real" solution which @coffeemug seems to be waiting for to be implemented at some point in the potentially distant future is absolutely non-trivial. Despite that, people might actually prefer explicit control over per-table cache sizes over the magic proposed in the long-term solution.

    This issue will have a huge impact on anyone who tries to use RethinkDB for a production system.
    The problem being that once you reach a certain table size, your queries will become incredibly slow even on systems with much memory. The only way to make them fast again is to re-create the tables, resulting in downtime and frustration.

    On the other hand, you might have a system running absolutely fine for months. Then you add another table and suddenly RethinkDB becomes unusable because it runs out of memory. Again, no chance to do anything about it except for re-creating all of the tables.

  10. coffeemug commented on Jun 19, 2013

    @coffeemug
    ContributorAuthor

    This isn't going to make it into the 1.7 sprint, but we'll absolutely make it a part of 1.8.

  11. ArtemGr commented on Jun 20, 2013

    @ArtemGr

    once you reach a certain table size, your queries will become incredibly slow even on systems with much memory

    BTW, is there an issue to make the direct-io optional? What if I want the filsystem cache to serve as a secondary cache?

  12. coffeemug commented on Jun 20, 2013

    @coffeemug
    ContributorAuthor

    We already turn off DIRECT_IO on systems that don't support it (e.g.
    encrypted file systems), but there is currently no setting to explicitly
    turn it off. Normally you wouldn't want to do that because the RethinkDB
    caching system outperforms the page cache for db use cases (because it's
    got more information than the page cache). Is there an example use case for
    why you might want to turn it off?

    On Thu, Jun 20, 2013 at 5:42 AM, Artem [email protected] wrote:

    once you reach a certain table size, your queries will become incredibly
    slow even on systems with much memory

    BTW, is there an issue to make the direct-io optional? What if I want the
    filsystem cache to serve as a secondary cache?

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/97#issuecomment-19749605
    .

  13. ArtemGr commented on Jun 20, 2013

    @ArtemGr

    Normally you wouldn't want to do that because the RethinkDB
    caching system outperforms the page cache for db use cases (because it's
    got more information than the page cache).

    No, normally I would want to do that because I don't want to micromanage the RethinkDB cache.
    The internal cache access is, of course, much faster than a context switch and a copy required to get the page from the filesystem cache (unless, of course, you're memory-mapping), but the operating system might have a lot of memory that I'm not ready to pin to RethinkDB but that might help RethinkDB fetch data from disk under a certain load.
    That's the way most databased are implemented, e.g. MySQL, Postgresss, SQLite, Berkeley DB, etc, they all have an internal cache but allow the operating system to help alleviate the disk load using it's large cache as necessary.

    Is there an example use case for why you might want to turn it off?

    Sure. Suppose I have several databases on a host and batch operations which are going on from cron. If I give most memory to RethinkDB then the cron job using another database will become unnecessary slow, and if I give most memory to the OS then the RethinkDB operations will become unnecessary slow. The usual way to solve this is by letting the OS cache adapt to the load with its LRU or whatever.

    Generally, any setup where a RethinkDB table cache is significantly smaller than the filesystem cache can benefit from the secondary caching provided by the OS.

  14. coffeemug commented on Jun 20, 2013

    @coffeemug
    ContributorAuthor

    Ok, that makes a lot of sense, thanks for the explanation. See #1051.

  15. 89 remaining items

  16. coffeemug commented on Mar 11, 2014

    @coffeemug
    ContributorAuthor

    @Tryneus -- I originally thought it should be the same units the user passes on the command line, but I'm not overly attached to that. I think our directory/stats/etc currently have no standard -- they sometimes return bytes, sometimes blocks, sometimes megabytes. We should standardize it all (and will) for monitoring, but for the moment you should use whatever makes the most sense to you.

  17. coffeemug commented on Mar 13, 2014

    @coffeemug
    ContributorAuthor

    @Tryneus -- what's the state of this issue?

  18. Tryneus commented on Mar 13, 2014

    @Tryneus
    Member

    @coffeemug, I've addressed all the comments in the code review, just waiting from @srh to look at it again.

  19. srh commented on Mar 17, 2014

    @srh
    Contributor

    I have merged grey_issue_97 into next as of ecf1884.

  20. AtnNn commented on Mar 18, 2014

    @AtnNn
    Member

    I have waited a long time for this! Thanks @srh and @Tryneus.

  21. boltonbytes commented on Mar 18, 2014

    @boltonbytes

    wow is this finally complete? amazingness

  22. coffeemug commented on Mar 18, 2014

    @coffeemug
    ContributorAuthor

    Wow, a year in development. Congrats everyone! (well, we're not quite over the hill yet; still have to ship 1.12)

  23. hokkaido commented on Mar 19, 2014

    @hokkaido

    Nice!

  24. stuartpb commented on Apr 10, 2014

    @stuartpb

    Should the FAQ be amended now?

  25. coffeemug commented on Apr 10, 2014

    @coffeemug
    ContributorAuthor

    @stuartpb -- good catch, thanks! I opened a pull request for that -- rethinkdb/docs#261.

  26. coffeemug commented on Jul 7, 2014

    @coffeemug
    ContributorAuthor

    Thanks, I opened rethinkdb/docs#403 to track this.

  27. added 2 commits that reference this issue on Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions