The Simplest Way to Make Jetty and Nats Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Running jetty and nats together lets web apps send fast messages between backend services. Jetty takes in web traffic while Nats moves small events across your servers. Teams use this pair to keep user requests fast and split up heavy work. The web server takes a request and drops a message onto a subject. Worker nodes pick up that message and finish the job in the background. This setup keeps web pages fast even when backend tasks take a long time. How data and permissions flow between jetty

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

Running jetty and nats together lets web apps send fast messages between backend services. Jetty takes in web traffic while Nats moves small events across your servers.

Teams use this pair to keep user requests fast and split up heavy work. The web server takes a request and drops a message onto a subject. Worker nodes pick up that message and finish the job in the background. This setup keeps web pages fast even when backend tasks take a long time.

How data and permissions flow between jetty and nats

Jetty runs your Java web code and listens for outside calls from users. When a call arrives, the app needs to talk to the message bus. It opens a persistent link to the Nats cluster using a client library. The web server logs in with a key file, a token, or a certificate.

Once the server logs in, Nats checks what that account can do. Each account holds rules that list which subjects the web app can use. A good setup lets the web tier publish to specific event subjects only. It blocks the web server from reading topics that belong to other backend workers.

Data flows in two main ways across this link. In a fire-and-forget flow, the web app sends an event and closes the call. In a request-reply flow, the web app sends a message and waits for an answer. The client holds a temporary inbox subject open until the worker replies or times out.

Common failure modes and how to scope roles

Many teams start by sharing one login key across all their web servers. If that single key leaks, an attacker can read or write to every subject. Some teams also leave wildcard permissions turned on in production by mistake. That lets any compromised web node read private messages from billing or user queues.

Another common bug happens when the web app opens a new link per request. Opening a fresh network link for every web hit will exhaust server ports fast. Instead, the web app must share one long-lived message client across all web threads. That client buffers messages in memory and sends them over a single socket.

End-to-End Encryption + NATS Security: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Unlogged traffic is another big risk when running fast message queues. If a worker drops a bad payload, you need to know who sent it. Give each web app service its own scoped user role and subject prefix. Set up strict network rules so only known web nodes can reach the message ports.

Keeping sensitive payload data safe in transit

Web servers often take in raw user data from forms and API calls. When the app pushes that payload onto a queue, private fields travel with it. Any worker listening to that subject will get the full message payload. If an engineer inspects the queue to debug an issue, raw data appears on screen.

To reduce risk, turn on TLS encryption between the web server and the cluster. This stops attackers on the local network from reading payloads on the wire. You should also strip or mask fields like emails, phone numbers, and card numbers. Doing this before messages hit the bus keeps private records out of worker logs.

Add runtime guardrails and masking beside your databases and APIs

Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits beside the service as an open-source proxy, a sidecar for your database or API.

Before a query runs, hoop.dev guardrails check the statement against your rules. An AI Analyzer you configure can score intent or hold risky calls for human review. Next, hoop.dev data masking rewrites sensitive fields in responses, and each verdict is logged per listener. Explore the MIT-licensed project on GitHub.

Frequently asked questions

Should Jetty open a new Nats connection for every HTTP request?

No, opening a new network link for every request will slow down your app. Create one shared client when the web server starts and reuse it across threads.

How do we restrict what subjects a Jetty app can use?

Give the web app its own account and role inside your message cluster settings. Set publish and subscribe rules on that role to match only the subjects it needs.

What happens if the Nats cluster restarts while Jetty is taking traffic?

The client library inside the web server will buffer messages in memory for a bit. It then reconnects to another healthy node in the cluster and sends the waiting messages.

The query runs. The emails stay hidden.