Skip to content

NSQ basics — distributed-by-design with nsqd and nsqlookupd

Installing NSQ as a distributed work queue — nsqd nodes, nsqlookupd discovery, channel fan-out, and the operational shape an MSP runs in production.

NSQ is a Go-written distributed message queue from the early Bitly stack — small daemons, no broker single-point-of-failure, and a discovery layer (nsqlookupd) that decouples producers and consumers from specific broker addresses. This article is the install we run for clients who want a simple, horizontally-scalable work queue without the operational complexity of Kafka or the cluster-mode nuance of RabbitMQ: stand up two or three nsqd nodes, one or more nsqlookupd discovery nodes, and a worker pool that auto-discovers them.

How to verify

NSQ ships as static Go binaries; confirm what is present and what ports would be needed:

which nsqd nsqlookupd nsqadmin
ss -ltnp | grep -E ':(4150|4151|4160|4161|4171)\b'
nsqd --version 2>/dev/null || echo "no nsqd binary yet"

Default ports: 4150 (nsqd TCP), 4151 (nsqd HTTP), 4160 (nsqlookupd TCP), 4161 (nsqlookupd HTTP), 4171 (nsqadmin web UI).

What’s happening

NSQ has three components:

  • nsqd — the broker daemon. Holds topics and channels in memory (with optional disk overflow), serves producers on TCP 4150, and serves consumers on the same port. There is no master nsqd — each runs independently. Topics are created on demand when a producer publishes.
  • nsqlookupd — the discovery service. nsqd nodes register their topics with nsqlookupd; consumers query nsqlookupd to find which nsqd nodes host the topic they want, then connect directly to those nsqd nodes.
  • nsqadmin — a web UI on 4171 that shows topics, channels, depths, consumer connection counts, and node lists by querying nsqlookupd.

The model is “producers connect to a local nsqd, consumers find all nsqd nodes via nsqlookupd and pull from each.” This means there is no central broker to scale; you horizontally scale by running more nsqd nodes on more boxes. The cost is no inter-node replication — a message published to a specific nsqd lives only on that nsqd until consumed (or until it overflows to disk). Losing a node loses the un-consumed messages on it.

Topics and channels. A topic is a named stream of messages. A channel is a consumer group on a topic — each message goes to one consumer per channel, but to all channels. So a payments topic with channels archive, notify, and audit delivers each payment message to exactly one archive worker, one notify worker, and one audit worker.

Durability is opt-in. By default, nsqd holds messages in memory until consumed; the --mem-queue-size flag controls how many messages per channel are held in RAM before overflow to disk. Disk overflow is a sequential file — recovery on restart re-loads the disk file but loses anything that was in memory.

The procedure

  1. Install the binaries. NSQ does not ship in the Ubuntu archive; grab the upstream release:

    NSQ_VER=1.3.0
    curl -fsSL "https://github.com/nsqio/nsq/releases/download/v${NSQ_VER}/nsq-${NSQ_VER}.linux-amd64.go1.21.5.tar.gz" \
      | sudo tar -xz -C /tmp
    sudo install -m 0755 /tmp/nsq-${NSQ_VER}.linux-amd64.go1.21.5/bin/* /usr/local/bin/
    nsqd --version
    nsqlookupd --version
  2. Create the data directory and a service user:

    sudo useradd --system --shell /usr/sbin/nologin --home /var/lib/nsq nsq
    sudo mkdir -p /var/lib/nsq /var/log/nsq
    sudo chown -R nsq:nsq /var/lib/nsq /var/log/nsq
  3. Write the nsqlookupd unit at /etc/systemd/system/nsqlookupd.service:

    [Unit]
    Description=nsqlookupd
    After=network-online.target
    
    [Service]
    Type=simple
    User=nsq
    Group=nsq
    ExecStart=/usr/local/bin/nsqlookupd \
      --tcp-address=0.0.0.0:4160 \
      --http-address=0.0.0.0:4161 \
      --log-level=info
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
  4. Write the nsqd unit at /etc/systemd/system/nsqd.service. Pointing it at one or more --lookupd-tcp-address entries is what makes it register topics with the discovery layer:

    [Unit]
    Description=nsqd
    After=network-online.target nsqlookupd.service
    
    [Service]
    Type=simple
    User=nsq
    Group=nsq
    ExecStart=/usr/local/bin/nsqd \
      --tcp-address=0.0.0.0:4150 \
      --http-address=0.0.0.0:4151 \
      --broadcast-address=$(hostname -f) \
      --data-path=/var/lib/nsq \
      --mem-queue-size=10000 \
      --max-msg-size=1048576 \
      --lookupd-tcp-address=nsqlookupd-01.internal:4160 \
      --lookupd-tcp-address=nsqlookupd-02.internal:4160 \
      --log-level=info
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target

    --broadcast-address is what nsqd tells nsqlookupd to advertise to consumers — must be resolvable from consumer hosts.

  5. (Optional) Write the nsqadmin unit:

    [Unit]
    Description=nsqadmin
    After=network-online.target
    
    [Service]
    Type=simple
    User=nsq
    Group=nsq
    ExecStart=/usr/local/bin/nsqadmin \
      --http-address=0.0.0.0:4171 \
      --lookupd-http-address=nsqlookupd-01.internal:4161 \
      --lookupd-http-address=nsqlookupd-02.internal:4161
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
  6. Enable and start everything:

    sudo systemctl daemon-reload
    sudo systemctl enable --now nsqlookupd nsqd nsqadmin
    curl -s http://localhost:4151/stats
    curl -s http://localhost:4161/nodes | python3 -m json.tool
  7. Produce and consume via HTTP for a smoke test:

    # produce a message
    curl -d 'hello nsq' "http://localhost:4151/pub?topic=greetings"
    
    # consume via the to_nsq utility or a Go client
    nsq_tail --lookupd-http-address=localhost:4161 --topic=greetings --channel=tail
  8. Lock the firewall. NSQ does not auth client connections — anyone reaching 4150 can publish or consume. Bind to a private network or front with a TLS proxy:

    sudo ufw allow from 10.10.0.0/24 to any port 4150 proto tcp
    sudo ufw allow from 10.10.0.0/24 to any port 4160 proto tcp
    sudo ufw allow from 10.10.10.0/24 to any port 4171 proto tcp
    sudo ufw deny 4150
    sudo ufw deny 4160
    sudo ufw reload

Common pitfalls

  • A message published to nsqd-01 stays on nsqd-01 — there is no replication. If you need every message to land in multiple places, your consumer is the one to dedupe (or run multiple producers writing to multiple nsqd nodes).
  • --mem-queue-size=0 forces every message to disk; useful for durability but cuts throughput. The default of 10000 is a sane middle ground.
  • nsqlookupd is a soft discovery layer — losing it does not stop in-flight consumers, but new consumers cannot find nsqd nodes. Run at least two.
  • Consumers must subscribe to a channel before messages flow; subscribing to a non-existent topic creates it as a side effect — beware typos.
  • nsqadmin is unauthenticated by default; never expose its 4171 to the internet. Bind to localhost or to a management VLAN.

Stack Harbor stands up NSQ as a deliberate choice for clients with a Go-heavy stack and a preference for horizontal scale over per-message durability — broker, lookup, and admin under systemd, dashboards built from nsqd’s /stats endpoint, and producer/consumer libraries pinned across services. If your NSQ install grew from a couple of binaries into the queue plumbing your platform relies on, this is the work covered by our Managed Operations practice.