Handling eventual consistencyThis page describes how to handle eventual consistency in Serialized in situations where you need to read data that has not yet been processed by the projection.
Since projections are eventually consistent, you might need to handle situations where you need to read data that has not yet been processed by the projection. This is especially true when you need to read data that has been written to an aggregate as a result of a user action, and the user expects to see the updated data immediately.
Waiting for the projection to be created
When you're expecting a new projection to be created, you can use the
awaitCreation parameter in Serialized. This will
wait for the projection to be created before returning the response, so the client will effectively hang until the data
In the following example, we're querying a new projection with name
45f184b5-f5a5-4c7e-88c0-5ed246675478 and waits up to 3 seconds for it to be created before cancelling the request:
var orderId = "45f184b5-f5a5-4c7e-88c0-5ed246675478";var request = ProjectionQueries.single("orders-by-id") .withId(orderId) .withAwaitCreation(Duration.ofSeconds(3)) .build(OrderProjection.class); // Will wait up to 3 secondsvar order = projectionClient.<OrderProjection>query(request).data();
Optimistically presenting transient data
Many applications can get away with presenting data in the UI before it has been persisted. This is a common pattern in many distributed systems to handle scalability and performance issues.