Skip to main content

Agent Connections

An agent connection is where jobs pass through on their way to the workers. It's ephemeral: it serves only as a transport (a newly scheduled job is saved there first) and for execution (the jobs a worker is about to run are handed to it there). Nothing durable is kept on it: jobs, history and logs live in the cluster's master database, so an agent connection can be replaced.

Adding a Connection​

config.AddAgentConnectionConfig("agent-1")
.UsePostgresForAgent("Host=db;Database=agent_1");

A cluster can have more than one agent connection, to spread the load, and they can use different providers. An agent connection can use the same database as the master, or a database of its own, and several agent connections can share a database, as long as each one has a unique name.

Workers then use it by name: config.AddWorker().AgentConnName("agent-1"). Options for each provider are in Providers.

The name ("agent-1") is letters, numbers, - and _, at most 50 characters.

Hybrid Connections​

A hybrid connection uses one provider for transport and another for execution, so that running jobs never slows down taking in new ones:

  • Transport: where newly scheduled jobs and recurring schedules are saved first.
  • Execution: where jobs are handed to workers and run.

The typical setup: NATS JetStream for transport, to take in a high volume of scheduling calls fast, and a database for execution. A database suits execution better than a message broker when jobs run long (more than about 3 minutes):

config.AddAgentConnectionConfig("hybrid-1")
.UseHybrid()
.Transport(t => t.UseNatsJetStream("nats://nats:4222"))
.Execution(e => e.UseRavenDB("Urls=http://db:8080;Database=Agent1"));

Any agent provider works on either side. Workers use a hybrid connection by name, like any other.

  • Fingerprint: a hybrid connection's fingerprint combines both sides, so pointing either one at another database, or swapping the two roles, is caught by connection protection.
  • NATS and TransientThreshold: the 5-minute limit applies only when NATS is on the execution side. With NATS only for transport, the cluster can use a longer TransientThreshold.
  • JSON: in ConfigFromJson, give the connection a "Transport" and an "Execution" section (each with "RepositoryType" and "ConnectionString") instead of its own "RepositoryType" / "ConnectionString".

What Is Stored, and the Fingerprint​

The cluster's database never stores an agent connection's settings: no connection string, no credentials. It only keeps the connection's name, its provider type and a fingerprint. Each app brings the connection settings from its own configuration.

The fingerprint makes sure a name always points to the same database:

  • The first time a connection is used, JobMaster writes a random id into that agent database (for NATS, into a JetStream key-value store), and saves it, as the connection's fingerprint, in the cluster's database.
  • At every startup, each app reads the id from the database its connection string points to, and compares it with the stored fingerprint.
  • If an app's connection string points to another database under the same name (a typo, a copied config, a restored environment), the ids don't match, and connection protection stops it.

That matters because the name identifies the work handed to that connection: jobs in progress are found through it. The same name on a different database would lose them.

warning

Once jobs have run, keep both the name and the database it points to. To remove or replace a connection, see Decommissioning a Connection.

Connection Protection​

Protection is on by default:

config.AddAgentConnectionConfig("agent-1")
.UsePostgresForAgent(connectionString)
.ProtectConnectionChanges(true); // the default

At startup, when the fingerprint doesn't match:

ProtectConnectionChangesBehavior
true (default)The app fails to start
falseA warning is logged and the app starts

Leave it on in production.

A connection no app has used for over 30 minutes is removed from the cluster's database, once no work is left on it. With protection on, a connection that still has work when it goes silent also logs a Critical message: those jobs are only recovered if the connection comes back.

Producers and Consumers​

Producers and consumers work the same on a distributed cluster: a producer declares the agent connections but no worker, a consumer adds the workers.

// Producer: schedules only
config.AddAgentConnectionConfig("agent-1").UsePostgresForAgent(connectionString);

// Consumer: runs the jobs
config.AddAgentConnectionConfig("agent-1").UsePostgresForAgent(connectionString);
config.AddWorker().AgentConnName("agent-1");

A producer with the agent connections hands a new job straight to the agent connection, without waiting on the cluster's database: that's what lets many producers schedule at a high rate.

Every app of the cluster should declare all its agent connections, even the ones it has no worker on.

Decommissioning a Connection​

To remove or replace a connection without losing work:

  1. Remove the workers that use it, but keep the connection itself, so the work already on it can finish.
  2. Deploy. No new work goes to that connection, and the rest drains.
  3. Once no work is left on it, remove the AddAgentConnectionConfig entry in the next release.