State Management via CRDTs:
- We'll use a CRDT that can represent a set of scheduled jobs (each job having a unique ID) and their states (like enabled/disabled, last run, next run, etc.).
- Since we are distributing the state, we can use an OR-Set (Observed-Remove Set) for the set of jobs, and for each job's mutable state (like configuration) we can use a LWW-Register (Last-Write-Wins) or a multi-value register if we want to keep all versions and resolve later.
- However, note that cron jobs have a schedule and we need to compute the next run time. This is a function of the schedule and the last run time. But if multiple nodes can update the schedule, we need to merge the schedules. Alternatively, we can treat the job configuration (including schedule) as an opaque blob and use LWW-Register for each job's config. But then if two nodes update the schedule at the same time, we lose one. Alternatively, we can use a more sophisticated CRDT for the schedule that can merge two schedules (like a set of times). But cron schedules are complex (with minute, hour, day of month, etc.). We might break it down.
Alternatively, we can model each job as a CRDT itself. Let's think:
Job CRDT:
- id: unique
- schedule: we can represent as a string (cron expression) and use LWW-Register for the entire string. But then two concurrent updates to different parts of the schedule might overwrite each other. Alternatively, we can break the schedule into fields and use a register for each field? But then we have to update multiple fields atomically? Not possible in CRDT without a transaction.
Given the complexity, perhaps we treat the job configuration (including schedule) as an immutable object once set? But then how do we update? We can version the job configuration and then have a separate CRDT for the current version. But then we need to merge two different versions. We can use an OR-Set for the configurations and then have a pointer to the current one? That seems heavy.
Another idea: We can use a LWW-Element-Set for the jobs, where each job is a tuple (id, schedule, ...). But then updating one job requires removing the old and adding the new. And if two nodes do that concurrently, we might end up with two versions? Actually, in an OR-Set, we can have multiple copies of the same element if the add and remove are concurrent? But we want to update the job, so we need to remove the old version and add the new. But if two nodes do that, we might get both added and then one removed? The OR-Set will keep the one that was added and not removed? Actually, the OR-Set with unique tags (like in the original paper) can handle this: each add has a unique tag, and remove removes a specific tag. Then if two adds happen, we have two tags. Then if one remove happens, it removes one tag and the other remains. But then we have two versions of the same job? That's not what we want.
Alternatively, we can use a 2P-Set (two-phase set) for the set of job IDs, and then for each job ID we have a separate CRDT (like a LWW-Register) for the configuration. Then updating a job means updating its configuration register. But then if two nodes update the same job's configuration, we take the last write. That's acceptable if we don't require strong consistency for the configuration? But we might have a problem: if two nodes update the schedule at the same time, we lose one. However, in practice, we might have a single source of truth for job configurations (like via an API) and then the distributed nodes just replicate. But if we allow multiple nodes to update, then we need to decide how to merge.
Given the complexity, let's assume that job configurations are updated by an external source (like an admin) and then propagated to all nodes. And if two updates happen, we take the one with the higher timestamp (using LWW). But what if the clocks are not synchronized? We can use hybrid logical clocks or vector clocks? But then we have to store the vector clock for each job and resolve conflicts when reading? That might be heavy.
Alternatively, we can use a state-based CRDT that is a map from job ID to job configuration, and the map itself is a CRDT (like a LWW-Map). Then each job configuration is a LWW-Register. Then the map allows us to update individual jobs.
So:
System state = Map<JobID, JobConfig> where JobConfig is a LWW-Register (with timestamp and value).
The JobConfig contains:
- schedule (cron expression)
- command (or script)
- other metadata (like timezone, etc.)
Then, when a node wants to update a job, it writes a new JobConfig with a timestamp. The LWW-Register will keep the one with the highest timestamp (using a total order, e.g., (timestamp, node_id) to break ties).
But note: the schedule is used to compute the next run time. However, if we update the schedule, the next run time might change. We can compute the next run time on the fly from the schedule and the last run time? But the last run time is also part of the state? Actually, we might not want to store the next run time because it's derived. Instead, we store:
- last_run_time (a timestamp)
- schedule (cron expression)
Then each node can compute the next run time from the schedule and the last_run_time (and current time). But if the schedule is updated, then the next run time might be different. However, if two nodes update the schedule at the same time, we take the last write. But then what about the last_run_time? We might also update the last_run_time when the job runs. So we have two fields that are updated independently? We can have two LWW-Registers per job: one for schedule and one for last_run_time? But then they are independent and might get out of sync? Actually, when a job runs, we update the last_run_time. But the schedule might have been updated by another node in the meantime. That's okay because the schedule is used for future runs.
Alternatively, we can have a single LWW-Register for the entire job configuration that includes both schedule and last_run_time? But then if two nodes update different fields, we lose one. So we need to break it down.
Let's break the job state into:
- schedule: LWW-Register (cron expression)
- last_run_time: LWW-Register (timestamp)
But then when we update the schedule, we don't want to lose the last_run_time. So we need to update the schedule without affecting the last_run_time. So we have two separate registers.
However, note that the job configuration (schedule) and the job execution state (last_run_time) are updated by different processes: the schedule by admin, the last_run_time by the executor. So it's acceptable to have them separate.
But then we have two independent LWW-Registers. How do we merge them? They are independent, so we just take the latest for each. That's fine.
But what about the job command? That is part of the configuration and updated by admin. So we can have:
JobState:
schedule: LWW-Register
command: LWW-Register
last_run_time: LWW-Register
But then if we update the command and schedule at the same time, we might update two different registers. That's okay because they are independent. However, if we want to update both atomically (as a single configuration update), we cannot with this design. But in practice, we might update them one by one? Or we can have a single configuration blob and use a version number? But then we are back to the same problem.
Alternatively, we can have a single LWW-Register for the entire job configuration (including schedule and command) and then a separate LWW-Register for last_run_time. Then when we update the configuration, we update the entire configuration blob. This way, the schedule and command are updated together. And the last_run_time is updated separately.
So:
JobState = {
config: LWW-Register // JobConfig has schedule and command
last_run_time: LWW-Register
}
But then if two nodes update the config at the same time, we take the last write. That's acceptable because the admin should update from a single source? But if we allow multiple admins, then we need to decide how to merge. We can use the same LWW rule.
However, note that the job configuration might have many fields. If we update only one field (like the schedule) we have to send the entire config? That's inefficient but acceptable.
Alternatively, we can break the config into multiple LWW-Registers (one for schedule, one for command, etc.) and then update them independently. Then if two nodes update different fields, both updates are kept. But then we have to read all the registers to get the full config. That's acceptable.
Given the trade-offs, I think having a single config register is simpler for atomic updates of the entire configuration. But if we want to allow partial updates (like only schedule) without overwriting the command, then we need multiple registers.
Let's assume we want partial updates. Then we break the config:
JobState = {
schedule: LWW-Register
command: LWW-Register
// ... other fields as needed
last_run_time: LWW-Register
}
Then when updating the schedule, we only update the schedule register and leave the command untouched. This way, concurrent updates to different fields are merged.
But note: if two nodes update the same field (say schedule) at the same time, we take the last write. That's acceptable.
So we can represent the entire system state as a CRDT map from JobID to JobState (which is a record of LWW-Registers).
We can implement this as a LWW-Map where the value is a record of LWW-Registers? But note: the LWW-Map typically has a single register per key. We want multiple registers per key. So we need a nested CRDT.
We can define:
SystemState = Map<JobID, JobState>
JobState = {
schedule: LWW-Register,
command: LWW-Register,
last_run_time: LWW-Register
}
Then the SystemState can be a CRDT map that uses a LWW-Element-Set for the keys? But we want to update the values independently. So we need a map CRDT that allows updating values independently. We can use a LWW-Map where the value is a CRDT itself? Actually, in CRDTs, we can have nested CRDTs. The outer map can be a LWW-Map, but then the value is a CRDT that is itself a collection of registers. But the LWW-Map only allows one value per key, and that value is a CRDT that we update. So when we update the schedule, we update the JobState's schedule register. But the outer map doesn't change. So we don't need to update the outer map. So we can have:
SystemState = LWW-Map<JobID, JobState>
But then if we update a field in JobState, we are updating the value of the map. But in a LWW-Map, when we update a value, we are replacing the entire value. So we would lose the other fields? Actually, no: because JobState is a CRDT itself? Wait, if JobState is a CRDT (a record of LWW-Registers) then updating one register in JobState is an update to the JobState CRDT. But in a LWW-Map, the value is a black box. We cannot update part of it without replacing the entire value. So we have two options:
Option 1: Make the outer map a CRDT that supports nested CRDTs, like a CRDT map that uses a two-phase approach? Actually, we can use a CRDT that is a map from key to a CRDT value, and then when we update a field in the value, we send the updated value CRDT. But then the outer map must merge the updated value CRDT with the existing one. How?
Alternatively, we can use a different outer map CRDT that is a set of (key, value) pairs where the value is a CRDT and we can update the value CRDT independently. This is like a map where each key's value is a CRDT and we can update that CRDT without affecting other keys. We can use a CRDT that is a set of entries, each entry being (key, valueCRDT). But then we need to be able to update the valueCRDT for a given key. We can do:
SystemState = Set<Entry> where Entry = (key, valueCRDT)
Then to update a field in a job, we:
- find the entry for the job id (if exists)
- update the valueCRDT (which is a JobState) by updating the appropriate register.
- then we have a new entry (key, newValueCRDT) and we add it to the set? But then we have multiple entries for the same key? That's not allowed because the set is a set of entries and we are using the key as part of the entry. So we need to remove the old entry and add the new one? But then we lose the other fields if we don't merge the valueCRDT properly.
Alternatively, we can use a CRDT that is a map from key to a CRDT value, and the map itself is a LWW-Map. Then when we update a field in the value, we have to create a new value CRDT (by merging the old one with the update) and then set that as the new value for the key. But then we are replacing the entire value CRDT, which is okay because the value CRDT is mergeable. So:
Let the outer map be a LWW-Map<JobID, JobState>.
JobState is a CRDT that is a record of LWW-Registers (or we can implement it as a map from field name to LWW-Register?).
Then to update the schedule for job X:
- we read the current JobState for X (from the LWW-Map) -> let's call it J.
- we update J.schedule with the new schedule (using LWW-Register update).
- then we set the LWW-Map at key X to J (with a new timestamp for the map?).
But note: the LWW-Map has its own timestamp for each key. When we update the JobState (which is a nested CRDT) we are not updating the timestamp of the outer map? Actually, in a LWW-Map, each key has a timestamp and a value. When we set a key, we set the timestamp and the value. So if we update the nested JobState, we are not updating the outer map's timestamp? We have to update the outer map's timestamp as well? Because if another node updates a different field in the same job at the same time, we want both updates to be merged? But the outer map's value is the entire JobState. So if we update the schedule, we create a new JobState (with updated schedule and same command and last_run_time) and then set the outer map's value to that new JobState with a new timestamp. But then if another node updates the command, it will also set the outer map's value to a new JobState (with updated command and old schedule) with its timestamp. Then when merging, we take the one with the higher timestamp for the entire JobState? That would lose one of the updates.
So we see that with a LWW-Map, we cannot update nested CRDTs independently because the outer map's value is replaced as a whole.
Therefore, we need a different outer map CRDT that allows updating the value CRDT without replacing the entire value. We can use a CRDT that is a map where each key's value is a CRDT and we can update that CRDT by sending a delta (update to the value CRDT). This is like a delta-state CRDT.
Alternatively, we can avoid the outer map and use a set of (key, field, value) with timestamps? But that becomes complex.
Given the complexity, perhaps we should reconsider the requirement: do we really need to update individual fields of a job independently? In practice, when updating a job, we might update the entire configuration (schedule, command, etc.) at once. And for last_run_time, that's updated by the executor. So we have two types of updates:
- Configuration updates (which update schedule and command together) -> we can treat the entire configuration as a single blob and use a LWW-Register for the configuration, and a separate LWW-Register for last_run_time.
Then the JobState becomes:
config: LWW-Register<JobConfig> // JobConfig has schedule and command
last_run_time: LWW-Register<timestamp>
Then when we update the configuration, we update the config register. And when the job runs, we update the last_run_time.
And the outer map: we can have a set of job IDs (OR-Set) and then for each job ID we have the JobState? But then how do we store the JobState per job? We can have a separate CRDT for each job? Actually, we can have:
SystemState = {
jobs: OR-Set<JobID>, // the set of jobs that exist
jobStates: Map<JobID, JobState> // but again, we have the same problem with the map.
}
Alternatively, we can have a single CRDT that is a map from JobID to JobState, and we use a map CRDT that supports value updates without replacing the entire value? We can use a CRDT that is a map of LWW-Registers for each field? But then we have to define the schema.
Let's step back. We are using CRDTs to replicate state. We have two types of state:
- The set of jobs (which jobs are defined). This is a set of job IDs.
- For each job, we have two pieces of state: configuration (which includes schedule and command) and last_run_time.
We can model:
SystemState = {
jobs: OR-Set,
configs: LWW-Map<JobID, JobConfig>, // JobConfig is an immutable object (schedule, command, etc.)
lastRunTimes: LWW-Map<JobID, timestamp>
}
Then when we add a job, we add the job ID to the jobs set and set the config and lastRunTime for that job (with initial values). When we update the configuration, we update the configs map. When we update the last_run_time, we update the lastRunTimes map.
But note: when we remove a job, we remove the job ID from the jobs set, and we can also remove the entries in configs and lastRunTimes? Or we can leave them? We might want to keep the history? Probably we remove them.
However, the problem with this design is that we have three separate CRDTs and we need to keep them in sync. For example, if a job is removed, we should remove its config and lastRunTime. But if we remove from the jobs set, and then later we see a config update for that job (from a node that hasn't seen the removal yet), we might re-add the job? We need to define the semantics.
Alternatively, we can have a single CRDT that is a map from JobID to a record that has:
exists: boolean (but we don't need exists because if the job is not in the map, it doesn't exist) and then config and lastRunTime.
But then we are back to the map problem.
Let's use a CRDT map that is a LWW-Map, but the value is a record with two LWW-Registers. Then when we update the config, we have to:
current = map.get(jobId)
newValue = current with config updated (using LWW-Register update for config)
map.put(jobId, newValue, newTimestamp)
But then the entire record is replaced. And if two nodes update different fields (config and lastRunTime) at the same time, we will have two different records and the one with the higher timestamp wins, losing the other update.
So we need a way to update the nested LWW-Registers without updating the outer map's timestamp? Actually, the outer map's timestamp is for the entire value. So we cannot.
Therefore, we need a map CRDT that allows updating the value CRDT without replacing the entire value. We can use a CRDT that is a set of (key, valueCRDT) where the valueCRDT is updated independently and the set uses OR-Set semantics? But then we have to merge the valueCRDTs for the same key.
Alternatively, we can use a CRDT that is a map from key to a CRDT, and the map itself is implemented as an OR-Set of (key, value) but then the value is a CRDT and we can have multiple values for the same key? And then we merge the value CRDTs? That's getting complex.
Given the time, perhaps we can accept that the entire job state (config and last_run_time) is updated as a whole? But then when the job runs, we update the last_run_time, which is part of the job state. So we would be updating the entire job state (which includes config and last_run_time) and if at the same time an admin updates the config, we lose one.
This is not acceptable because job runs are frequent and configuration changes are infrequent. We want to allow concurrent configuration updates and job runs.
So we must separate the two.
How about:
SystemState = {
jobs: OR-Set,
configs: LWW-Map<JobID, JobConfig>,
lastRunTimes: LWW-Map<JobID, timestamp>
}
And we add an invariant: for any job in the jobs set, there must be an entry in configs and lastRunTimes. And if a job is removed from jobs, we eventually remove the entries in configs and lastRunTimes. But during convergence, we might have a job in jobs but no config? That would be invalid. So we need to ensure that when we add a job, we add to all three. And when we remove, we remove from all three.
But then we have to update three CRDTs atomically? We cannot. So we might have temporary inconsistencies.
Alternatively, we can have a single CRDT that is a set of jobs, and each job is a CRDT that has two fields: config and lastRunTime. And the set uses OR-Set. Then when we add a job, we add a job CRDT (with initial config and lastRunTime). When we update the config, we update the job CRDT's config register. When we update lastRunTime, we update the job CRDT's lastRunTime register. And when we remove, we remove the job from the set.
But how do we represent the set? We can have:
SystemState = OR-Set
JobCRDT = {
id: JobID,
config: LWW-Register,
lastRunTime: LWW-Register
}
But then two nodes might add the same job with different configs? We have to merge the JobCRDTs. How do we merge two JobCRDTs? We need to define a merge function for JobCRDT. Since JobCRDT is a record of two LWW-Registers, we can merge by merging each register independently (taking the LWW for each). But note: the id must be the same? Actually, in an OR-Set, each element has a unique tag (or multiple tags if added multiple times). But we are adding JobCRDTs with the same id? That would be two different elements in the set? But we want to have at most one job per id. So we need to use a set that is keyed by job id. That is, we want a map from job id to JobCRDT.
So we are back to a map.
Given the complexity, I think we should use a CRDT library that provides a map with nested CRDTs and allows updating nested values without replacing the entire value. For example, in Rust we have crdts crate that has Map and Register and we can have Map<JobID, (Register<JobConfig>, Register<timestamp>)>. But then the map is a LWW-Map? And when we update the inner register, we don't update the outer map's timestamp? Actually, the inner registers are updated independently and the outer map's value is a pair of registers. When we update one of the inner registers, we are updating the value of the outer map? Because the value (the pair) changes. So we have to update the outer map's timestamp? Then we are back to the same problem.
Wait, if the outer map is a LWW-Map, then the value is a pair of LWW-Registers. When we update one of the inner registers, we create a new pair (with the updated inner register and the same other inner register) and then set the outer map to that new pair with a new timestamp. Then if another node updates the other inner register at the same time, we have two different pairs and the one with the higher timestamp wins, losing the other update.
So we need an outer map that does not use LWW for the entire value, but rather allows the inner registers to be updated independently and the outer map's timestamp is not used for the inner values? That doesn't make sense because the outer map's value is the pair of registers. We want to update the inner registers without affecting the other inner register and without having to update the outer map's timestamp? That's not possible with a LWW-Map because the value is replaced.
Therefore, we must use a different map CRDT. We can use a CRDT that is a map where each key's value is a CRDT and the map itself is an OR-Set of (key, value) where the value is a CRDT and we can have multiple (key, value) pairs for the same key? Then we merge the value CRDTs for the same key. This is like a multi-value map.
Let me define:
SystemState = OR-Set where Entry = (JobID, JobState)
JobState = {
config: LWW-Register,
lastRunTime: LWW-Register
}
Then when we want to update the config for job X, we:
-
create a new JobState with the same lastRunTime (from the current one) and the new config? But we don't have the current one? We have to read the current state for job X from the OR-Set? Actually, we can:
current = the merged JobState for job X (by taking all the Entries with key X and merging the JobStates by merging the config registers and lastRunTime registers independently).
Then we create a new JobState with updated config and same lastRunTime? But we don't have the current lastRunTime? We do from the current merged state.
Then we create a new Entry (X, newJobState) and add it to the OR-Set. But then we have multiple entries for the same key? Yes, and when we query, we merge all entries for the same key.
But then how do we remove a job? We remove all entries for that job? But we cannot remove specific entries in an OR-Set? We can remove by tag. So we need to track the tags for each entry? Actually, in an OR-Set, we add with a unique tag and remove by tag. So if we update a job, we add a new entry with a new tag and remove the old entry? But then we have to know the old tag. That's not practical.
Alternatively, we can use a CRDT that is a map from key to a CRDT value, and the map is implemented as a set of (key, value) with unique tags for each (key, value) pair? Then updating a key means adding a new (key, value) and removing the old one? But then we have to know the old tag.
This is getting too complex.
Given the time, I think we should simplify:
We assume that job configurations are updated by a single source (like an admin API) and then propagated to all nodes. And job runs are updating the last_run_time. We can have:
SystemState = {
jobs: OR-Set,
configs: LWW-Map<JobID, JobConfig>,
lastRunTimes: LWW-Map<JobID, timestamp>
}
And we enforce that for any job in the jobs set, there is an entry in configs and lastRunTimes. And we have a background process that removes entries in configs and lastRunTimes for jobs not in the jobs set.
But then when we add a job, we add to jobs, and set config and lastRunTime. When we update config, we update configs. When we update lastRunTime, we update lastRunTimes. And when we remove a job, we remove from jobs and eventually from configs and lastRunTimes.
The problem is that if we update config and lastRunTime concurrently, they are in two separate maps and we don't have a conflict because they are independent. That's acceptable.
But what if we update the config and at the same time remove the job? Then we might add the config after the job is removed? We need to define the order: when we remove a job, we remove from jobs, and then we remove from configs and lastRunTimes. But if after removing from jobs, we get a config update (from a node that hasn't seen the removal) then we will add the job again? Because the job is not in the jobs set, but we update configs for that job. Then we have a config for a job that is not in the jobs set? That's inconsistent.
To avoid that, we can say that the set of jobs is the source of truth. If a job is not in the jobs set, then we ignore any updates to configs and lastRunTimes for that job. And when we add a job, we must add to the jobs set and then set the config and lastRunTime. But if we set the config and lastRunTime before adding to the jobs set, then we have config and lastRunTime for a job that is not in the jobs set? So we should only allow updates to config and lastRunTime for jobs that are in the jobs set.
So the algorithm for a node:
When receiving an update for a job's config:
if the job is in the jobs set (local view) then update the configs map.
else, ignore.
Similarly for lastRunTime.
When adding a job:
add to jobs set, and then set config and lastRunTime (if not set, set to default?).
When removing a job:
remove from jobs set, and then remove from configs and lastRunTimes? But we might have concurrent updates to config and lastRunTime for that job? We can remove from configs and lastRunTimes after removing from jobs, but if we receive an update for config after removing from jobs, we ignore because the job is not in jobs.
This way, the jobs set controls the existence of the job.
But note: the jobs set is an OR-Set. So if two nodes add the same job at the same time, we will have two adds and then when we remove, we have to remove both? Actually, in an OR-Set, we remove by tag. So if we add the same job twice (with two different tags) then we have two entries. Then when we remove, we remove one tag? But we want to remove the job entirely. So we need to remove all tags for that job? But the OR-Set doesn't support removing by element, only by tag. So we need to track the tags for each job? That's not provided by the OR-Set.
Alternatively, we can use a different set: a LWW-Set for the jobs? Then we add the job with a timestamp. If two nodes add the same job, we take the one with the higher timestamp? But then we lose one? Actually, we want to allow multiple adds? But then we have duplicates? We don't want duplicates. So we want a set of unique job IDs. So we can use a LWW-Set for the jobs. Then if two nodes add the same job, the one with the higher timestamp wins, but the job is still in the set. That's acceptable.
But then when we remove, we remove the job (using LWW-Remove). Then if two nodes remove at the same time, we take the one with the higher timestamp? But then the job is removed. And if one node adds and another removes at the same time, we take the one with the higher timestamp? So we need to compare the timestamps of add and remove? In a LWW-Set, we have a timestamp for each element (when it was added) and a timestamp for the remove (if removed). Then we decide: if the remove timestamp is greater than the add timestamp, then the element is removed. Otherwise, it's present.
So we can use:
jobs: LWW-Set
configs: LWW-Map<JobID, JobConfig>
lastRunTimes: LWW-Map<JobID, timestamp>
Then the rule: for a job to be considered active, it must be in the jobs set. And we only process configs and lastRunTimes for jobs that are in the jobs set.
But then when we add a job, we add to the jobs set and set config and lastRunTime. When we update config, we update configs (only if the job is in the jobs set). When we update lastRunTime, we update lastRunTimes (only if the job is in the jobs set). When we remove a job, we remove from the jobs set. And then we can eventually remove from configs and lastRunTimes (or leave them and ignore).
This seems workable.
However, note that the configs and lastRunTimes maps might have entries for jobs that are not in the jobs set. We can clean them up periodically.
Now, about the schedule: we store it in the config as a cron expression. Each node will compute the next run time for each active job by:
next_run = compute_next_run(job.config.schedule, job.lastRunTime, current_time)
But note: if the schedule is updated, then the next run time might change. And if the lastRunTime is updated (by a job run), then the next run time is recomputed.
However, if two nodes compute the next run time at the same time, they might both think they are the one to run the job? We need a leader election or a way to assign jobs to nodes.
We haven't discussed how jobs are executed.