Memory Image Pattern

Memory Image is a programming pattern in which data stored on the database resides in memory. This means data access doesn’t require a round-trip to the database. As a consequence, the application code is responsible for:

  1. handling concurrent modification attempts, and

  2. keeping the consistency across nodes running the application.

Sharded Entities

One way to meet the two requirements above is to ensure only one copy of the data is in memory at any given time. That’s also known as isolating the mutations new tab. Then, because your data may exceed the capacity of your memory, you can shard the data and keep a chunk on each of your application nodes. Finally, each shard of data uses mutual exclusion mechanisms to ensure each mutation happens sequentially.

Akka actors new tab provide serial access to mutating data out of the box. If an actor, is a direct image of some persistent data it becomes a memory image. Finally, Akka Cluster provides the means to implement the "Sharded Singletons" approach by sharding new tab all your actors.

Using a single memory instance for each of the entities in the database ensures we are compliant with the Single Writer Principle. Since there is only one instance in the cluster, we can guarantee there will only be one writer for each entity therefore we don’t need to implement locking mechanisms on the database access.

The Implementing Microservices with Akka tutorial teaches how to create a microservice that uses Event Sourced persistent sharded actors. In the Event Sourced entity step you will create a memory image of a Shopping Cart persisted using Event Sourcing.

Replicated Images

The Sharded Singletons approach to Memory Image prefers Consistency to Availability. You may decrease the Consistency guarantees in favor of Availability if you implement your memory images in a way that concurrent mutation may converge.

Akka Persistence supports Replicated Images for Event Sourced actors in the Replicated Event Sourcing API new tab.

Learn more

Learn more about the Memory Image pattern at Martin Fowler’s bliki post new tab.