Here is a pure mutex system (no backwards writes). The key I think you missed it it's based on time not ticks.
- Claim the segment by writing your shard number and time + 3*N seconds
- Wait till you've claimed the segment for N seconds.
- If N < time < 2*N seconds then write data (also increment a sequence counter).
- Do not reclaim the segment for M seconds.
During all this every shard keeps its own copy of the shared memory. which it refreshes every tick. If their copy is more up to date overwrite the segment.
- N should to be your slowest tick + clock skew between machines.
- The safest M is # of shards * N. Smaller values allow write starvation of slow shards.
The easiest way to break this system is having a shards write get committed long after the script's tick is over (like > 3N after).
It is also possible for this scheme to break when ticks are > N. But even then a single slow tick doesn't break the system. Consistently slow ticks also don't break the system. You need inconsistent tick times.
- Shard A 2N slow tick while Shard B claims
- Shard B has a 3N slow tick while trying to write.
- Shard A claims and writes during B's slow tick.
- Both Shard A and Shard B think they have the next sequence.
Choosing N and M can be tricky but picking large values like 1 minute and 5 minutes respectively for the MMO server will just work.
The big problem with this scheme is it's SLOW. A shard could wait 100 ticks or more before acquiring the shared segment.