The NATS server rejected a message because its payload exceeded the configured maximum size.

Common Causes and Fixes

1. Exceeding max_payload on the NATS Server

  • Diagnosis: Check the NATS server’s configuration file (typically nats-server.conf) for the max_payload setting. You can also query a running server using the monitoring endpoint. For example, if your server is on localhost:8222, you can curl http://localhost:8222/varz and look for the max_payload field.
  • Fix: Increase the max_payload value in your nats-server.conf. For instance, if the error indicates a message size of 1.5MB and your max_payload is 1MB (1048576 bytes), you might set it to 2097152 (2MB) or higher. Restart the NATS server for the change to take effect.
    # nats-server.conf
    max_payload: 2097152
    
  • Why it works: The max_payload setting is a hard limit enforced by the NATS server on individual messages to prevent resource exhaustion. Increasing this value allows larger messages to be processed.

2. Exceeding max_msg_size in NATS JetStream

  • Diagnosis: If you are using NATS JetStream, the limit is controlled by max_msg_size within the stream configuration. You can inspect this using the NATS CLI: nats stream info <stream_name>.
  • Fix: Update the stream configuration to allow larger messages. Use the NATS CLI to modify the stream:
    nats stream update <stream_name> --max-msg-size 2097152
    
    Replace 2097152 with your desired maximum message size in bytes.
  • Why it works: JetStream streams have their own message size limits, independent of the server-wide max_payload, to manage storage and throughput for durable message queues.

3. Client Library Default Limits

  • Diagnosis: Some NATS client libraries might have their own default maximum message size limits that are lower than the server’s max_payload or JetStream’s max_msg_size. This is less common but can occur if the client is configured with specific constraints. Check the documentation for your specific NATS client library (e.g., Go, Node.js, Python) for any relevant configuration options related to message size.
  • Fix: When initializing your NATS client, look for options to set or increase the maximum message size. For example, in some Go clients, you might configure this during connection:
    import "github.com/nats-io/nats.go"
    
    // ...
    
    nc, err := nats.Connect(nats.DefaultURL, nats.WithMaxPayload(2097152))
    if err != nil {
        log.Fatal(err)
    }
    defer nc.Close()
    
    Consult your client library’s documentation for precise syntax.
  • Why it works: This ensures that the client itself doesn’t prematurely reject or fail to send messages that are within the server’s acceptable limits.

4. Message Serialization Overhead

  • Diagnosis: The size reported in the error might be the serialized size of your message, which includes headers, encoding (like JSON or Protobuf), and potentially wrapper objects, not just the raw data. If you’re close to the max_payload limit, a small increase in data might push it over due to serialization.
  • Fix: Review your message structure and serialization format. Consider using more efficient serialization methods (e.g., Protocol Buffers over JSON) or compressing the message payload before sending it. You might need to adjust the max_payload or max_msg_size to accommodate the compressed size.
    // Example using Go's gzip compression
    import (
        "bytes"
        "compress/gzip"
        "io"
        // ... nats client imports
    )
    
    func compress(data []byte) ([]byte, error) {
        var buf bytes.Buffer
        zw := gzip.NewWriter(&buf)
        _, err := zw.Write(data)
        if err != nil {
            return nil, err
        }
        err = zw.Close()
        if err != nil {
            return nil, err
        }
        return buf.Bytes(), nil
    }
    
    // ... in your publisher
    originalData := []byte(`{"key": "very large value..."}`)
    compressedData, err := compress(originalData)
    if err != nil {
        // handle error
    }
    // Publish compressedData, ensuring max_payload is large enough for it
    
  • Why it works: By reducing the byte count of the message payload through compression or more efficient encoding, you can fit larger logical amounts of data within the fixed byte limits.

5. Incorrect Unit Interpretation

  • Diagnosis: Ensure you are consistently interpreting message sizes and configuration values in bytes. Some tools or logs might display sizes in KB or MB, leading to miscalculations. For example, 1MB is 1024 * 1024 = 1048576 bytes. A common mistake is to use 1000000 bytes for 1MB.
  • Fix: Always double-check your calculations. If a limit is stated as 1MB, ensure your max_payload is set to at least 1048576 bytes. If the error message shows a size like 1572864 bytes, and your max_payload is set to 1048576, you know it’s too small. Set max_payload: 2097152 to be safe.
  • Why it works: Accurate unit conversion prevents subtle configuration errors where the intended limit is technically met, but the byte count falls short due to different interpretations of units.

6. System Resource Constraints on the Publisher

  • Diagnosis: While the error is reported by the NATS server, a publisher might be struggling to construct or send a very large message due to its own memory or CPU limitations. This can manifest as the publisher taking an excessively long time to serialize or send, potentially leading to timeouts or outright failures before the server even fully receives the message. Monitor the publisher’s resource usage.
  • Fix: Optimize the publisher’s message construction and serialization process. If the publisher is a microservice, consider scaling up its resources (CPU, RAM) or optimizing the code that generates the large message payload. In some cases, breaking down a very large logical message into smaller, sequential messages might be a viable architectural change.
  • Why it works: Ensuring the publisher has sufficient resources to prepare and send the message correctly is a prerequisite for the NATS server to be able to receive and process it.

The next error you’ll likely encounter if you’ve fixed message size issues but haven’t addressed payload content is a "payload validation failed" or similar error, indicating the content of the message is malformed or unexpected by the consumer.

Want structured learning?

Take the full Nats course →