Copy a running application, and run the copy
A host of two million actors makes a 165 MB snapshot in about a tenth of a second. Restore it in another process and rehearse a deploy on the copy.
Copy every actor into one buffer
host.snapshot() returns one Buffer holding every actor of the host, with its variables, its queued events and its pending timers.
The buffer also holds the hibernated actors' records, the compiled images, the system id and the host's time. Host.restore runs it again, in this process or another.
On a copy, add a new image, migrate, run the clock ahead, and look. Or capture a host when something goes wrong, and step through it.
A test can start from one prepared world, and a host can leave the process for a while.
Take a snapshot when the host is idle
snapshot() throws a TinyactorsError with TA_ERROR_STATE from a handler, and during a migration.
It throws the same while output or a typed method's reply waits for your handlers, so await host.idle() first.
Here a snapshot from a log handler throws, and one after idle() succeeds. A refused snapshot leaves the host running.
import { compileCharts, Host } from '@tinyactors/node';
const compiled = compileCharts([{ name: 'c.scxml', text: `
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0"
datamodel="ecmascript" name="c" initial="s">
<state id="s"><onentry><log expr="'hello'"/></onentry></state>
</scxml>` }]);
const host = new Host(compiled, {
system: 'demo',
log: (entry) => {
try {
host.snapshot();
} catch (error) {
console.log('from a handler:', error.status);
}
},
});
compiled.close();
host.spawn('c');
await host.idle();
console.log('after idle():', host.snapshot().length > 0);
host.close();from a handler: TA_ERROR_STATE
after idle(): truesnapshot() throws TA_ERROR_UNSUPPORTED when an actor holds what hibernation cannot store: on native, a function, Map or Date. The message names the variable.
Run the copy on a new clock
This reminder is half an hour from ringing when its host is snapshotted. Restored on a new clock that reads 0, it rings 30 minutes later.
import { compileCharts, Host, SimulatedClock } from '@tinyactors/node';
const compiled = compileCharts([{ name: 'reminder.scxml', text: `
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0"
datamodel="ecmascript" name="reminder" initial="waiting">
<datamodel><data id="note" expr="''"/></datamodel>
<state id="waiting">
<transition event="set">
<assign location="note" expr="_event.data"/>
<send event="ring" delay="3600s"/>
</transition>
<transition event="ring" target="done"/>
</state>
<final id="done"><onentry><log expr="'reminder: ' + note"/></onentry></final>
</scxml>` }]);
const image = compiled.image;
compiled.close();
const log = (entry) => console.log(entry.text);
const clock = new SimulatedClock();
const host = new Host(image, { system: 'demo', clock, log });
const reminder = host.spawn('reminder', { name: 'plants' });
reminder.send('set', 'water the plants');
await host.idle();
clock.increment(30 * 60_000); // half an hour passes
await host.idle();
const snapshot = host.snapshot(); // bytes: write them to a file, send them elsewhere
host.close();
const later = new SimulatedClock(); // a clock that reads 0
const copy = Host.restore(snapshot, image, { system: 'demo', clock: later, log });
console.log('same session:', copy.actor('plants').id === reminder.id);
await copy.idle();
console.log('due in', copy.nextDeadline / 60_000, 'minutes');
later.increment(30 * 60_000);
await copy.idle();
copy.close();same session: true
due in 30 minutes
reminder: water the plantsThe restored actor keeps its session id. The clock goes on from the snapshot's time, whatever reads it: a new SimulatedClock, or another machine's clock.
The snapshot's time is that of the snapshot() call. A timer with 6 seconds left then has 6 seconds left after the restore, however long the host was idle before.
Restore with the options the snapshot had
Pass Host.restore the charts and the options the snapshot was taken with. It checks system against the system id the snapshot holds.
The snapshot holds the host, not the options you passed it: give the copy its handlers, I/O processors and tick rate. Pass datamodel: 'quickjs' again for a quickjs host.
The snapshot holds its images, so pass only the charts it lacks. An image it holds is skipped, and a new version is added as new Host adds it.
- Restore with the same build.A snapshot holds raw memory, with a version of its layout.
Host.restorerefuses a snapshot of a build whose layout differs withTA_ERROR_INVALID: take a new one with the new build. - Keep your backups in your database.A snapshot looks like a backup, but durability is your database's job, and hibernation is how a running process keeps actors out of memory. A snapshot is a copy of the whole application, every chart version at once.
- Shared objects come back apart.Values are written through the value table, so two variables of one actor that shared one object no longer share it after a restore.
Start the invoked services again
The snapshot holds each running invocation, and the services you run stay in your program. The restored host's first turn announces each invocation that was running to the services handler you pass Host.restore.
Each comes with invocation.restored true and the same invokeid, src and params. Pass your real handlers to go on, or stand-ins for a copy that must not reach the real services.
A type with no handler gives its invokers error.communication, and nothing is thrown.
Give the copy its own store
The snapshot holds the hibernated actors' records, and their blobs stay in your store. The restored host loads them from the store you pass it.
A copy that hibernates actors writes and deletes blobs too, so give it a copy of the store.
Replay the inputs that came after
host.now() reads the host's clock, so a journal of the inputs that came after a snapshot records it with each.
A replay on a restored host with a SimulatedClock moves the clock to each reading before the input: clock.increment(entry.at - host.now()).
Rehearse a deploy on a copy
The snapshot program, tools/snapshot.mjs, rehearses a deploy on a live host of two million actors. At most 1000 are resident, and the rest are in SQLite.
Each resident actor has a delayed send pending, and half are paying through an invoked service that has not answered.
- It writes the snapshot to a file, copies the store, and restores a copy with its own store and clock.
- A stand-in payment service gets each payment in flight again,
restoredand with its amount, and declines it. - The copy's own snapshot must be the same bytes.
- The copy migrates to a new version while events come, and runs its clock past the delayed sends.
- Meanwhile the live host goes on with events of its own, its payments still in flight. The copy never reaches the real service.
With most actors hibernated, the snapshot is 165 MB, 82 B an actor. It takes 0.11 to 0.14 s to make, and 0.14 to 0.21 s to restore.
| runtime, datamodel | actors, resident | snapshot | taken | restored | migrate on the copy | peak resident (both hosts) |
|---|---|---|---|---|---|---|
| node, native | 2,000,000, 1000 | 165.0 MB (82 B per actor) | 0.14 s | 0.14 s | 244.9 s | 1261 MB |
| node, quickjs | 2,000,000, 1000 | 164.9 MB (82 B per actor) | 0.14 s | 0.14 s | 205.7 s | 1177 MB |
| bun, native | 2,000,000, 1000 | 165.0 MB (82 B per actor) | 0.12 s | 0.21 s | 208.1 s | 1534 MB |
| bun, quickjs | 2,000,000, 1000 | 164.9 MB (82 B per actor) | 0.11 s | 0.19 s | 185.9 s | 1559 MB |
| node, native | 100,000, 100,000 | 69.5 MB (695 B per actor) | 0.65 s | 2.00 s | 5.0 s | 522 MB |
| node, quickjs | 100,000, 100,000 | 63.7 MB (637 B per actor) | 1.01 s | 0.67 s | 3.4 s | 724 MB |
| bun, native | 100,000, 100,000 | 69.5 MB (695 B per actor) | 0.51 s | 1.05 s | 4.2 s | 941 MB |
| bun, quickjs | 100,000, 100,000 | 63.7 MB (637 B per actor) | 1.06 s | 0.71 s | 3.5 s | 800 MB |
Every run passed its checks:
- The copy's snapshot was the same bytes, and the copy migrated every actor to version 2. At two million, that migration made 3,688,631 loads from the copy's store.
- Every sampled actor had its count, delayed send and version on both hosts.
- The live store held only the live host's blobs.
- After every turn, no more actors were resident than the bound.
The host's region is 82 B an actor with most of them hibernated, and 490 B all resident. With all resident, the native datamodel's rows add about 58 B an actor over quickjs's state.
The logs are snapshot.log and snapshot-resident.log in tools/results/2026-11-20. The repository's rules for published numbers say when to run it again.