Kest

Search

Search the documentation

Microservices

Running several instances

Scale out: what is shared through Redis or the database, and worker processes.

State that must be shared between instances has an implementation backed by Redis or the database. Swap the store; your code does not change.

ConcernPackageWhat is shared
Cachecache-redis, cache-memcachedentries, with expiry
Rate limitsthrottler-redisatomic counters, so the limit is global
Sessionssession-redisany instance can serve any request
Queuesqueue-rediswaiting, delayed and repeatable jobs, priorities, the rate limit
Migrationsthe databasea lock lets instances start together
Messagingthe brokereach message to one consumer of a group
Loading diagram…

A worker process

kest new jobs --template worker

No HTTP server: Kest.run<AppModule>(args) starts queue processors, @Cron jobs, @OnEvent handlers and message consumers, then waits. Start as many as the load needs; they share queues and the repeat schedule through Redis. Give API and worker processes different concurrency settings to size them independently.

What is not shared

  • WebSocket rooms are per instance: a message sent to a room reaches the connections of this process only. Fan out through a broker or Redis pub/sub yourself.
  • In-process caches (CaffeineCacheStore, InMemoryCacheStore) differ between instances: put them in front of a shared store with LayeredCacheStore and a short near TTL.
  • @Scheduled jobs (@Cron, @Interval) run on every instance. For once-per-cluster, use a repeatable queue job: its occurrences are claimed in Redis.
  • Transactions do not span instances or databases.

Graceful shutdown

Kest.run registers a shutdown hook that closes the container: destroy and shutdown hooks run, queue workers stop taking jobs, the HTTP server stops accepting and drains. Send SIGTERM and give it a few seconds.

See Concurrency model for what happens inside one process.

Several processes on one machine

To use every core of one machine without a load balancer in front, start more processes from the same command: java -jar app.jar --processes=4, or set KEST_PROCESSES=4. The process you started supervises the others, restarts any that crash, and stops them all on SIGTERM. The workers share the HTTP port. See Concurrency model for the details, and for what runs once per machine and what runs once per cluster.