Comparison
Cache-asidevsIdentity map
Cache-aside
you check the cache, miss, go and read the database yourself, and write the value back before you return it.
The application owns the cache: on a miss it loads from the store and populates the cache itself. It is the usual default because the cache can go down without taking reads with it, and because nothing is cached that nobody asked for. The costs are that every miss pays database latency plus a write, and that two requests missing the same key at the same moment both hit the database.
Full entry →Identity map
you loaded the same customer twice in one request and got the same object, not two equal ones.
A per-transaction map from database identity to loaded object, so a row read twice produces one instance. It removes a whole class of bug where two copies of the same record drift apart, and it makes reference equality meaningful within a unit of work. It also explains a common confusion: a second query for a row already loaded may return the in-memory version, complete with your uncommitted changes.
Full entry →