System Design Quiz Practice
Active Recall Drill Session
← Exit SessionQuestion 1 of 1
mediumSystem Design
Given this ReplicatedStore in CP mode, what happens when readFromReplica() is called during a simulated partition?
code
class ReplicatedStore {
constructor(mode) { this.mode = mode; this.replicaValue = null; this.partitioned = false; }
write(v) { if (!this.partitioned) this.replicaValue = v; }
simulatePartition(p) { this.partitioned = p; }
readFromReplica() {
if (this.partitioned) {
if (this.mode === "CP") throw new Error("Unavailable");
return { value: this.replicaValue, stale: true };
}
return { value: this.replicaValue, stale: false };
}
}
const s = new ReplicatedStore("CP");
s.write("v1");
s.simulatePartition(true);
s.readFromReplica();