## Immutable SQL Database (Kinda)

From: andrew cooke <andrew@...>

Date: Sun, 8 Mar 2015 20:33:05 -0300

I want to share an idea that has proved extremely useful - having a database
that never changes existing values.

The context is a suite of programs used to calibrate seismometers.  These
follow the "unix pattern" of being command line tools with small actions,
which need to be used together, in certain sequences, to achieve the required
result.

The commands share a fair amount of state (for example, the definition of "the
current calibration"), which is written to a database.

In addition, there's a background process that can schedule new tasks to run,
which removes the need for the operator to manually execute all tasks.

So in a typical use case, the operator will run separate commands to:

1 - indicate that a new calibration is being defined
2 - add signals to the calibration (a "signal" is used to measure the
response of the equipment - typically they are sine or random)
3 - indicate when the calibration should start.

the background scheduler can then take over and execute commands to:

4 - trigger the signals by sending appropriate commands to hardware
5 - collect the resulting data
6 - calculate the system response from the data
7 - compare the system response with the nominal response
8 - do end-of-calibration housekeeping tasks (like email results)

Clearly steps 1-3 define entries in the database, which are used by later
tasks.  But later steps can also add information - for example, it is only
after triggering the hardware that we get exact timing data.

Now, the trick is, that rather than modify the existing information, later

parameter has to read back through the task history to find the most recent
value.  But that code can be automated and becomes a 1-off cost.

The advantage is that it is always possible to restart the process from some
known point.  This is HUGELY useful for debugging, which would otherwise be
extremely time consuming (since you would need to either try to reverse and
database mutations or re-run the entire series of commands).

One obvious objection is that the database grows over time.  But the amount of
data is so low that this is not an issue in practice (and, if it becomes one,
it would be relatively simple to have a regular purge of old data).

Andrew