liveBook · Manning

Side effect

As explained in Akka in Action, Second Edition

A side effect is an externally observable operation, such as an HTTP request or database insertion, performed in addition to updating persistent state.

A side effect is an externally observable operation performed alongside persistent-state updates. Typical examples include posting to an HTTP server or inserting a database record. In an Akka persistent actor, the command handler adds side effects to an effect such as persist with thenRun. section 9.2.1

Use side effects carefully around event persistence:

  • Run the callback after the event has been successfully persisted; callbacks appended to persist execute in order. section 9.2.3
  • Keep side effects out of the event handler. Recovery replays saved events through that handler, which would replay the side effects as well. section 9.2.1
  • Do not place persist after another effect when the earlier effect assumes persistence succeeded; a persistence failure could make the notification or operation false. section 9.2.3

Adding a side effect to persistence

A container command can produce Effect.persist([CargoAdded]).thenRun(callback). The callback can notify another actor or perform another external operation after the cargo event is saved. Use thenReply when the intended reaction is specifically a reply, and thenStop when the actor should stop after handling the command. section 9.2.3

How Akka in Action, Second Edition covers it

How deep it goes
Distinguishes side effects from actor effects, explains callback ordering and failure hazards, and stops short of broader delivery guarantees.
Examples
Uses HTTP posts, database inserts, actor notifications, and a persisted CargoAdded event in a container.
What it assumes
Assumes familiarity with commands, state, events, and persistent actors while distinguishing side effects from actor effects.
What you can do after
Apply it via Akka Persistence by attaching thenRun after persist while keeping replayed event handling free of side effects.
How it connects
Connects side effects to persistent actors, command handlers, event handlers, event sourcing, and event persistence.

Also explained in

Compare how 24 books explain side effect →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.