Safeguard
Security Guides•

Which MQTT broker limits stop one client from exhausting memory?

Packet size, queued messages, connections and memory caps each protect an MQTT broker. See Mosquitto defaults that may surprise you and how to tune them.

Safeguard Security Team
4 min read

Set a maximum packet size, cap queued messages per client, limit connections and put a hard memory limit on the broker. In Mosquitto several of these default to unlimited or generous values, so a single misbehaving or malicious client can use a lot of memory unless you change them. Test after tuning.

Why limits matter on a broker

A broker holds state on behalf of clients: sessions, subscriptions, and messages waiting for clients that are slow or offline. A client that never reads, or a publisher that floods a topic, can make that state grow. OWASP's API Security Top 10 treats missing limits on memory, upload size, processes and operations per request as the cause of unrestricted resource consumption, and the same thinking applies to brokers.

The Mosquitto settings and their defaults

The Mosquitto configuration manual documents these options. Defaults below come from that page, so confirm them against your own version.

SettingWhat it limitsDocumented default or note
max_packet_sizeSize of a whole incoming MQTT packet2000000 bytes since 2.1, no limit before
message_size_limitLargest publish payload accepted0, meaning all valid messages accepted
max_queued_messagesQoS 1 and 2 messages queued per client1000, with 0 meaning no maximum (not recommended)
max_queued_bytesQueued bytes per client0, meaning no maximum
max_inflight_messagesMessages in transit at once20
max_connectionsConnected clients per listenerMinus one means unlimited, set a number to cap
global_max_clientsClient sessions across the brokerMinus one, meaning unlimited
memory_limitHeap memory the broker may allocateSet a value to cap use, needs memory tracking compiled in

The manual also explains that a client session counts as connected clients plus persistent sessions that have disconnected but not yet expired. So a flood of persistent sessions can fill memory even with few connections.

The manual notes that when the queued bytes limit is reached, subsequent messages are silently dropped. That is a design choice you should understand: protecting memory means some messages are lost for slow clients.

What can go wrong

  • Large payloads. Without a payload limit, one client can publish very large messages that are then forwarded to every subscriber.
  • Slow subscribers. A client that stays connected but never acknowledges makes queues grow until a limit is hit.
  • Persistent sessions. Clients that connect with a persistent session and never return leave state behind.
  • Connection floods. Many cheap connections consume file descriptors and memory.

Steps to tune

  1. Measure normal traffic: largest legitimate payload, typical queue depth, number of devices.
  2. Set the payload and packet size limits just above the largest legitimate message.
  3. Set queue limits by count and by bytes, based on how long a device may be offline.
  4. Set per listener connection limits and a global client cap that leaves room to grow.
  5. Set a hard memory limit so the broker fails predictably instead of taking down the host.
  6. Put the broker in a container or service with memory and file descriptor limits too.
  7. Load test with a slow subscriber, a large publisher and a connection storm. Confirm the broker stays up and others are unaffected.
  8. Alert on dropped messages, queue size and session count.

Do not forget the software itself

Limits reduce the damage of abuse, but bugs in broker code can bypass them. Keep the broker and client libraries updated, and track advisories for them.

Try it yourself

Click through it at your own pace, or open it full screen.

Look at how a dependency inventory lists versions, and check whether your broker libraries would appear.

Quick check

Does Mosquitto cap queued messages by default?

It documents a default of 1000 queued QoS 1 and 2 messages per client, but no byte limit by default. Set both to match your devices.

What happens when the queue is full?

Per the manual, once the byte limit is reached, further messages are silently dropped. Monitor drops so you notice.

Safeguard scans your dependencies and SBOMs for known vulnerabilities, including broker and client libraries, and shows which are reachable. A free scan is at gold.safeguard.sh/scan.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.