Hacker News new | ask | show | jobs
by St-Clock 5392 days ago
The system you describe, recording events and replaying events to get the actual state of the system, usually assumes that you can only write one event at a time (writes need to be super cheap). If you can write multiple events in parallel, you won't have isolation and you may read tension documents that will be voided (in your example).

You could use optimistic concurrency control to ensure that only one tension document is written at a time while ensuring that your constraints are respected:

  current_id = atomic increment global state_id
  create tension document (but with active bit set to false)
  check constraints
  atomic:
    if global state_id = current_id
    set active bit to true
  if active bit is false:
    delete tension document and report error
This would provide isolation (you don't see tension documents that are not yet validated) and consistency (your balance is respected).

The atomic operation in RDBMS is usually implemented in one very simple and fast SQL UPDATE query. I believe mongo must provide something similar too.

Although my bank would allow me to overdraw from my checking account, I have a higher margin/tolerance than my brother, so they must perform some constraints check ;-)