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.
| Concern | Package | What is shared |
|---|---|---|
| Cache | cache-redis, cache-memcached | entries, with expiry |
| Rate limits | throttler-redis | atomic counters, so the limit is global |
| Sessions | session-redis | any instance can serve any request |
| Queues | queue-redis | waiting, delayed and repeatable jobs, priorities, the rate limit |
| Migrations | the database | a lock lets instances start together |
| Messaging | the broker | each message to one consumer of a group |
A worker process
kest new jobs --template workerNo 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 withLayeredCacheStoreand a short near TTL. @Scheduledjobs (@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.