Skip to content

Any command used in MULTI fails when used_memory > maxmemory #9926

Closed

Description

@markuszoeller

Edit: see current conclusion in #9926 (comment)

Describe the bug

I've created a scenario where used_memory is bigger than maxmemory
and where maxmemory-policy is set to noeviction. When I use a command
wrapped in MULTI, it behaves differently than when I use it standalone.

To reproduce

  • config set maxmemory-policy noeviction
  • config set maxmemory 12026784
  • write keys until OOM command not allowed when used memory > 'maxmemory'
  • ignore that error and brute force to write even more keys (some go through) to make sure there's really memory > maxmemory
  • try to delete a key wrapped in multi:
> MULTI
OK
> DEL foobar
(error) OOM command not allowed when used memory > 'maxmemory'.
> EXEC
(error) EXECABORT Transaction discarded because of previous errors.

Expected behavior

I would expect any non-write command to work in MULTI. It only works without MULTI:

> DEL foobar
(integer) 1

Same behavior with INFO. There are probably more commands where it would make sense to allow them in MULTI in that scenario (In my case CLIENT PAUSE was part of it, but given that it buffers requests, I can see why it wouldn't be allowed).

Additional information

Tested in Redis 5.0.14

Activity

  1. itamarhaber commented on Dec 10, 2021

    @itamarhaber
    Member

    A quicker way to reproduce (also on unstable): set maxmemory 1mb :)

  2. markuszoeller commented on Dec 10, 2021

    @markuszoeller
    Author

    A quicker way to reproduce (also on unstable): set maxmemory 1mb :)

    haha, yeah, I just used the value I had in my test :)

  3. madolson commented on Dec 10, 2021

    @madolson
    Contributor

    I believe this is intended behavior because multi-execs consume additional memory when building up the object, so we don't want people building up objects when you're at maxmemory.

    I don't know if I agree with this behavior, it's really no different than accumulating commands in the query buffer or loading up commands into a lua script. I would prefer we just make sure there are no "use-memory" commands inside the multi-exec.

  4. oranagra commented on Dec 12, 2021

    @oranagra
    Member

    Indeed.. This behavior was added on purpose in redis 5.0, see #5454

  5. added
    state:to-be-closed
    on Dec 12, 2021
  6. markuszoeller commented on Dec 13, 2021

    @markuszoeller
    Author

    Indeed.. This behavior was added on purpose in redis 5.0, see #5454

    Does it make sense in it's current form though?

    I was also under the impression that maxmemory is only limiting the keyspace and not memory the Redis process would need to respond to something, like multi-execs. Is that assumption not correct?

  7. oranagra commented on Dec 13, 2021

    @oranagra
    Member

    nothing major was changed since then in that respect, so this change makes as much sense now as it did then.

    redis counts memory usage of (almost) everything, including client buffers and other non-keyspace allocations.
    the maxmemory limit however, is enforced by evicting keys, or rejecting commands.
    redis does avoid rejecting some commands, namely read command and delete commands, or actually ones that it thinks are gonna increase the memory usage.

    i think the reason MULTI queuing is rejected since the client could otherwise abuse redis but sending more and more commands to be queued, and never send EXEC, which will result in an unbound memory usage.

    maybe it makes sense to give the queuing some grace, and not reject them too soon, so that we don't interrupt naive workloads by a temporary memory overuse.
    however, if the queued commands contain any "use-memory" commands, then EXEC will fail when it arrives, so it might not help much (other than for read / delete workloads)

  8. markuszoeller commented on Dec 14, 2021

    @markuszoeller
    Author

    redis counts memory usage of (almost) everything, including client buffers and other non-keyspace allocations.
    the maxmemory limit however, is enforced by evicting keys, or rejecting commands.

    I understand how it's enforced, but it's not 100% clear to me what actions exactly increase/decrease the memory count which is limited by maxmemory. I was trying to say, that I though only actions like set foo bar (keyspace actions) increase that portion of memory usage which is limited by maxmemory, that's why I was surprised to see "non-keyspace actions" like INFO to be limited by multi-exec.

    (other than for read / delete workloads)

    That would already help I think. Not being able to call INFO or DEL in multi-exec feels a bit too restrictive.

  9. removed
    state:to-be-closed
    on Dec 14, 2021
  10. added this to the Next minor backlog milestone on Dec 14, 2021
  11. moved this to backlog in Redis Backlogon Jul 6, 2022
  12. eladbern commented on Dec 25, 2023

    @eladbern

    Now that we have client eviction that accounts for multi memory (via mstate.argv_len_sums), should we just allow non-'denyoom' transactions?
    That would allow performing reads, deletes, etc. inside transactions.

  13. 16 remaining items

  14. soloestoy commented on Jan 16, 2024

    @soloestoy
    Contributor

    so if a user can do GETDEL in OOM state, i'd like them to also be able do the similar MULTI based one.

    I believe the root of the problem lies not in the size of the transaction, but in whether to allow transaction caching during OOM. If executing small transactions is allowed, then in my opinion, executing large transactions should also be allowed. It is different from the case where unauthenticated clients trigger a DOS attack with large parameters. If we think that OOM should focus on command attributes (whether they are of CMD_DENYOOM type) rather than transaction caching, then I think we can directly revert #5454, I do not object to doing so. As I think it is unnecessary to limit the size of transactions during OOM either through configuration options or hard code.

  15. soloestoy commented on Jan 16, 2024

    @soloestoy
    Contributor

    Moreover, I believe the root cause is the lack of memory partitioning. The maxmemory limit restricts all the memory used by Redis, but the action to reduce memory is by deleting data, which is unfair.

    If we want to fundamentally address this issue, the theoretically correct approach would be to separate the memory into the data area (maxmemory-dataset) and the runtime area (maxmemory-runtime). Only when the data exceeds maxmemory-dataset should we reject CMD_DENYOOM commands and delete data. In other cases, when the runtime memory exceeds maxmemory-runtime, we should not delete data or reject CMD_DENYOOM commands.

    We have already taken a step in this direction by introducing maxmemory-clients. Perhaps in 8.0, we should enable maxmemory-clients by default, for example, setting the default configuration to 100%. In this way, even if we revert #5454 and do not limit transaction execution during OOM, maxmemory-clients will help disconnect connections with excessive transaction cached commands.

  16. oranagra commented on Jan 17, 2024

    @oranagra
    Member

    i'm ok reverting it, and i'm ok setting a hard coded sensible limit.
    what do you mean by setting maxmemory-clients to 100? 100 bytes? 100% of maxmeomry?

  17. soloestoy commented on Jan 18, 2024

    @soloestoy
    Contributor

    what do you mean by setting maxmemory-clients to 100? 100 bytes? 100% of maxmeomry?

    100% of maxmemory... 😅

  18. madolson commented on Jan 22, 2024

    @madolson
    Contributor

    In this way, even if we revert #5454 and do not limit transaction execution during OOM, maxmemory-clients will help disconnect connections with excessive transaction cached commands.

    The only thing that I don't like about this behavior is that you'll get a "disconnect" as opposed to an explicit error. Disconnects can be hard to root cause.

  19. soloestoy commented on Jan 23, 2024

    @soloestoy
    Contributor

    Disconnects can be hard to root cause.

    Good point. Disconnection typically occurs in three situations:

    1. Network broken
    2. Client-initiated disconnection
    3. Server-initiated disconnection

    Here, we are particularly concerned with the third case, which is server-initiated disconnection. This can occur when a client times out, exceeds buffer limits, or encounters other issues. Currently, there is no notification to the client about the reason for the disconnection.

    However, there is an interesting exception when a client establishes a new connection. If the number of connections exceeds the maxclients limit, Redis will first reply to the client with "-ERR max number of clients reached" before disconnecting. Maybe we can extend this mechanism (sending the reason to the client before disconnecting) to other scenarios of disconnection.

  20. madolson commented on Jan 23, 2024

    @madolson
    Contributor

    However, there is an interesting exception when a client establishes a new connection. If the number of connections exceeds the maxclients limit, Redis will first reply to the client with "-ERR max number of clients reached" before disconnecting. Maybe we can extend this mechanism (sending the reason to the client before disconnecting) to other scenarios of disconnection.

    We do this for ACL forced disconnection (if you delete your own user for example, you still get the OK before getting the disconnect). This normally causes a slight latency bump, as it's not till the next time the client is pulled out that it's checked and sees it is disconnected.

  21. soloestoy commented on Jan 23, 2024

    @soloestoy
    Contributor

    We do this for ACL forced disconnection (if you delete your own user for example, you still get the OK before getting the disconnect). This normally causes a slight latency bump, as it's not till the next time the client is pulled out that it's checked and sees it is disconnected.

    Yes, I know, and we have CLIENT_CLOSE_AFTER_REPLY mechanism in place for scenarios such as the QUIT command, CLIENT KILL command, and disconnecting blocked clients when the master becomes replica. However, these mechanisms still rely on satisfying the ping-pong protocol. But in cases like disconnection due to query or output buffer limit, it can lead to exceptional handling of client protocols. I'm wondering if we can have a more general approach to handle such scenarios, perhaps utilizing the PUSH message in RESP3.

  22. oranagra commented on Jan 23, 2024

    @oranagra
    Member

    This topic was discussed in a core-team meeting, and our conclusion was that:

    1. we wanna disconnect the tie of MULTI state memory usage and the server OOM state.
      meaning that we'll limit the total memory a single client can queue, and do that unconditionally regardless of the server being OOM or not.
    2. we don't wanna add a new config, and we can use the query buffer limit for that.
      the reasoning is that it's ok to send a single command with huge argument, and it's ok to send a long MULTI queue with many small commands. but it's probably not a practical use case to send a long MULTI queue with many commands carrying a huge argument.
    3. when the rules are violated, we think it's ok to disconnect the client (rather than return an OOM error), same as we do on query buffer size exhaustion.

    @soloestoy i suppose your PR is closer to that end goal?
    either way, let's make sure the above reasoning is reflected in the commit comment.

  23. added a commit that references this issue on Jan 25, 2024
    85a834b
  24. added a commit that references this issue on Feb 11, 2024
    e4c9ac2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    state:help-wanted

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions