2.2 How ZODB Works

The ZODB is conceptually simple. Python classes subclass a Persistent class to become ZODB-aware. Instances of persistent objects are brought in from a permanent storage medium, such as a disk file, when the program needs them, and remain cached in RAM. The ZODB traps modifications to objects, so that when a statement such as obj.size = 1 is executed, the modified object is marked as ``dirty''. On request, any dirty objects are written out to permanent storage; this is called committing a transaction. Transactions can also be aborted or rolled back, which results in any changes being discarded, dirty objects reverting to their initial state before the transaction began.

The term ``transaction'' has a specific technical meaning in computer science. It's extremely important that the contents of a database don't get corrupted by software or hardware crashes, and most database software offers protection against such corruption by supporting four useful properties, Atomicity, Consistency, Isolation, and Durability. In computer science jargon these four terms are collectively dubbed the ACID properties, forming an acronym from their names. The definitions of the ACID properties are:

Atomicity
means that any changes to data made during a transaction are all-or-nothing. Either all the changes are applied, or none of them are. If a program makes a bunch of modifications and then crashes, the database won't be partially modified, potentially leaving the data in an inconsistent state; instead all the changes will be forgotten. That's bad, but it's better than having a partially-applied modification put the database into an inconsistent state.

Consistency
means that the data cannot be placed into a logically invalid state; sanity checks can be written and enforced. Usually this is done by defining a database schema, and requiring the data always matches the schema. For example, this might enforce that the order_number attribute is always an integer, and not a string, tuple, or other object.

Isolation
means that two programs or threads running in two different transactions cannot see each other's changes until they commit their transactions.

Durability
means that once a transaction has been committed, a subsequent crash will not cause any data to be lost or corrupted.

The ZODB provides 3 of the ACID properties. Only Consistency is not supported; the ZODB has no notion of a database schema, and therefore has no way of enforcing consistency with a schema.