Mosquitto MQTT Broker最大支持多少用户连接?
Hey there! Great question—let’s unpack how many concurrent connections Mosquitto can handle, since that’s critical for a chat app.
First off, Mosquitto doesn’t have a hard-coded maximum connection limit out of the box. The actual number you can support depends almost entirely on your server’s resources and how you configure the broker. That 100k figure you’ve seen? It’s not just a random number—it’s a realistic upper bound for well-optimized setups with sufficient hardware.
Key Factors That Determine Connection Limits
Here’s what impacts how many concurrent users your Mosquitto instance can handle:
- File Descriptors: Every MQTT connection uses a file descriptor, and most OSes have a default limit (often 1024 per process) that’s way too low for high concurrency. You’ll need to bump this up using commands like
ulimit -n 65536(or higher) and update system-level configs (like/etc/security/limits.confon Linux) to make the change persistent. - Memory: Each connection consumes a small amount of memory—roughly 10-20KB per client, depending on your setup. For 100k connections, that translates to 1-2GB of RAM just for holding connection states (not counting other broker overhead). Make sure your server has enough free memory to avoid swapping.
- Broker Configuration: While Mosquitto’s
max_connectionssetting defaults to0(meaning no enforced limit), you can tweak other settings to optimize performance:- Adjust
keepalive_intervalto reduce unnecessary traffic - Tune
listenersettings (likemax_queued_messages) to handle bursts - Disable unused features (like persistence if you don’t need it) to save resources
- Adjust
- Hardware & Network: Your server’s CPU (for handling connection handshakes and message routing) and network bandwidth (for transmitting messages between clients) will also cap the number of concurrent users. A dedicated server with fast networking is a must for scaling to 100k+ connections.
How to Validate Your Setup
If you want to test what your specific environment can handle, use tools like:
- Batch scripts with
mosquitto_pub/mosquitto_subto simulate hundreds/thousands of clients - Specialized MQTT load testers (like Eclipse Paho’s load testing tools) to push the broker to its limits
Just remember: real-world usage (with message traffic, not just idle connections) will lower the maximum number of concurrent clients compared to a pure connection test.
Final Takeaway
That 100k number is achievable, but it’s not a guarantee. Start with optimizing your system and broker config, test incrementally, and monitor resource usage (CPU, memory, file descriptors) as you scale up. For a chat app, you’ll also want to plan for message throughput alongside connection counts—since active chats will generate more traffic than idle connections.
内容的提问来源于stack exchange,提问作者Omid Aghakhani

