<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Zilverblog</title>
	<atom:link href="http://blog.zilverline.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.zilverline.com</link>
	<description>_Zilverline blog</description>
	<lastBuildDate>Wed, 09 Jan 2013 18:48:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Simple event sourcing &#8211; users, authentication, authorization (part 6)</title>
		<link>http://blog.zilverline.com/2013/01/09/simple-event-sourcing-users-authentication-authorization-part-6/</link>
		<comments>http://blog.zilverline.com/2013/01/09/simple-event-sourcing-users-authentication-authorization-part-6/#comments</comments>
		<pubDate>Wed, 09 Jan 2013 18:46:22 +0000</pubDate>
		<dc:creator>Erik Rozendaal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[authorization]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[event sourcing]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[immutability]]></category>
		<category><![CDATA[memory image]]></category>
		<category><![CDATA[play framework]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.zilverline.com/?p=1471</guid>
		<description><![CDATA[Previously we spend some time preparing the code to support multiple kinds of events and data, rather than just supporting blog posts. In this part we&#8217;ll add user accounts, together with the required authentication and authorization code. We&#8217;ll again use event sourcing and the memory image to keep track of all users and currently active <a href='http://blog.zilverline.com/2013/01/09/simple-event-sourcing-users-authentication-authorization-part-6/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>Previously we spend some time preparing the code to support multiple kinds of events and data, rather than just supporting blog posts. In this part we&#8217;ll add user accounts, together with the required authentication and authorization code. We&#8217;ll again use event sourcing and the memory image to keep track of all users and currently active sessions. But the biggest changes to the application are related to security, and authorization in particular. It turns out event sourcing allows for an additional layer of authorization which allows us to whitelist any change a particular user is allowed to make.<br />
<span id="more-1471"></span></p>
<h3>Other parts</h3>
<ul>
<li><a href="http://blog.zilverline.com/2012/07/04/simple-event-sourcing-introduction-part-1/">Part 1 &#8211; Introduction</a></li>
<li><a href="http://blog.zilverline.com/2012/07/23/simple-event-sourcing-consistency-part-2/">Part 2 &#8211; Consistency</a></li>
<li><a href="http://blog.zilverline.com/2012/07/30/simple-event-sourcing-redis-event-store-part-3/">Part 3 &#8211; Redis Event Store</a></li>
<li><a href="http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/">Part 4 &#8211; Conflict Resolution</a></li>
<li><a href="http://blog.zilverline.com/2012/08/26/simple-event-sourcing-refactoring-and-transactions-part-5/">Part 5 &#8211; Refactoring and Transactions</a></li>
<li>Part 6 &#8211; Users, Authentication, Authorization</li>
</ul>
<h3>Code</h3>
<p>You can find the code associated with this part at <a href="https://github.com/zilverline/event-sourced-blog-example/tree/part-6">github</a> on the <code>part-6</code> branch.</p>
<p>Since quite some time has past since the previous part some other upgrades were made as well:</p>
<ul>
<li>Scala 2.10.0 and Play 2.1-RC1 were released. The code has been updated accordingly. This mainly reduced the amount of code needed to work with JSON.</li>
<li>The stable version of Redis is now 2.6.7, which supports Lua scripts. So the 2.4.x Redis Watch/Multi/Exec event store implementation was removed.</li>
</ul>
<h2>User Accounts</h2>
<p>Most applications need some level of user authentication and authorization support. Since we&#8217;re building an event sourced example application we&#8217;ll first define the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-6/app/events/UserEvents.scala">events</a> we need for managing users and active sessions:</p>
<p>[code lang="scala"]<br />
sealed trait UserEvent extends DomainEvent {<br />
  def userId: UserId<br />
}<br />
case class UserRegistered(userId: UserId, email: EmailAddress, displayName: String, password: Password) extends UserEvent<br />
case class UserProfileChanged(userId: UserId, displayName: String) extends UserEvent<br />
case class UserEmailAddressChanged(userId: UserId, email: EmailAddress) extends UserEvent<br />
case class UserPasswordChanged(userId: UserId, password: Password) extends UserEvent<br />
case class UserLoggedIn(userId: UserId, token: AuthenticationToken) extends UserEvent<br />
case class UserLoggedOut(userId: UserId) extends UserEvent<br />
[/code]</p>
<p>From the event definitions it is quite clear what user related functionality our system supports. Besides registering, logging in and logging out, users can also change their profile information (display name), email address, and password.</p>
<p>To ensure we always hash passwords securely and never accidentally display them a custom <code>Password</code> class is defined, which uses the <a href="http://www.tarsnap.com/scrypt.html">scrypt</a> password hashing algorithm:</p>
<p>[code lang="scala"]<br />
case class Password private (hash: String) {<br />
  require(hash.startsWith("$s0$"), "invalid password hash")</p>
<p>  def verify(password: String): Boolean = SCryptUtil.check(password, hash)</p>
<p>  override def toString = "<PASSWORD-HASH>"<br />
}<br />
object Password {<br />
  def fromHash(hash: String): Password = Password(hash)<br />
  def fromPlainText(password: String): Password = Password(SCryptUtil.scrypt(password, 1 << 14, 8, 2))</p>
<p>  implicit val PasswordFormat: Format[Password] = valueFormat(fromHash)(_.hash)<br />
}<br />
[/code]</p>
<p>Similar classes are defined for <code>EmailAddress</code> and <code>AuthenticationToken</code>. The authentication token is stored in the user's session so we can lookup the current user when an HTTP request is made. This is implemented as part of the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-6/app/models/Users.scala#L115"><code>Users</code></a> class:</p>
<p>[code lang="scala"]<br />
case class Users(<br />
    private val byId: Map[UserId, RegisteredUser] = Map.empty,<br />
    private val byEmail: Map[EmailAddress, UserId] = Map.empty,<br />
    private val byAuthenticationToken: Map[AuthenticationToken, UserId] = Map.empty) {</p>
<p>  // [... code omitted ...]</p>
<p>  def findByAuthenticationToken(token: AuthenticationToken): Option[RegisteredUser] =<br />
    byAuthenticationToken.get(token).flatMap(byId.get)</p>
<p>  // [... code omitted ...]<br />
}<br />
[/code]</p>
<p>Now that we not only store posts but users as well, the global state of the application is now captured in the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-6/app/models/ApplicationState.scala"><code>ApplicationState</code></a> class, which simply combines the <code>Posts</code> and <code>Users</code> classes and dispatches incoming updates to its parts:</p>
<p>[code lang="scala"]<br />
case class ApplicationState(posts: Posts = Posts(), users: Users = Users()) {<br />
  def update(event: DomainEvent, revision: StreamRevision) = event match {<br />
    case event: PostEvent => copy(posts = posts.update(event, revision))<br />
    case event: UserEvent => copy(users = users.update(event, revision))<br />
    case _                => sys.error(s"unknown event: $event")<br />
  }</p>
<p>  def updateMany(events: Seq[(DomainEvent, StreamRevision)]) = events.foldLeft(this) {<br />
    case (state, (event, streamRevision)) => state.update(event, streamRevision)<br />
  }<br />
}<br />
[/code]</p>
<p>The <code>User</code> trait is defined as follows:</p>
<p>[code lang="scala"]<br />
sealed trait User {<br />
  def displayName: String</p>
<p>  /**<br />
   * @return `Some(registeredUser)` if this is a registered user,<br />
   *         `None` otherwise.<br />
   */<br />
  def registered: Option[RegisteredUser] = None</p>
<p>  // [... authorization code ommitted ...]<br />
}<br />
[/code]</p>
<p>By not having to worry about mapping classes to a (relational) database we can freely define our classes to match our application's needs. In this case there are four different implementations of this trait. They all have a <code>displayName</code> and <code>registered</code> method in common, but are used in different situations:</p>
<p>[code lang="scala"]<br />
/**<br />
 * A deleted or non-existent user.<br />
 */<br />
case class UnknownUser(id: UserId) extends User {<br />
  def displayName = "[deleted]"<br />
}</p>
<p>/**<br />
 * A user that we know the name of, but nothing else. Consider a comment posted<br />
 * by a guest (where they only have to specify their name).<br />
 */<br />
case class PseudonymousUser(displayName: String) extends User</p>
<p>/**<br />
 * A visitor to the site is represented by the guest user object.<br />
 */<br />
case object GuestUser extends User {<br />
  def displayName = "Guest"</p>
<p>  // [... authorization code ommitted ...]<br />
}</p>
<p>/**<br />
 * A registered user who may have an active session identified by the<br />
 * `authenticationToken`.<br />
 */<br />
case class RegisteredUser(<br />
    id: UserId,<br />
    revision: StreamRevision,<br />
    email: EmailAddress,<br />
    displayName: String,<br />
    password: Password,<br />
    authenticationToken: Option[AuthenticationToken] = None)<br />
  extends User {</p>
<p>  override def registered = Some(this)</p>
<p>  // [... authorization code ommitted ...]<br />
}<br />
[/code]</p>
<h2>The current user</h2>
<p>In most applications there is a <em>current user</em>. We want this user to be available to all our controller actions and views. We also want to store the current user's id as part of a commit to our event store, so that we can easily trace who performed a specific action.</p>
<p>Instead of reimplementing this in every controller action we'll add some helper methods, so that controller actions can stay focused. Play! makes it easy to do so using <a href="http://www.playframework.org/documentation/2.0/ScalaActionsComposition">action composition</a>.</p>
<p>First a new trait is defined so that we can decouple the controller from the raw memory image API. An instance of this trait will be passed to each controller when constructed:</p>
<p>[code lang="scala"]<br />
/**<br />
 * Actions available to controllers that make use of a memory image.<br />
 */<br />
trait ControllerActions[State, -Event] extends Controller { outer =><br />
  /**<br />
   * The type of blocks that only need to query the current state of the<br />
   * memory image.<br />
   */<br />
  type QueryBlock[A] = State => ApplicationRequest[A] => Result</p>
<p>  /**<br />
   * The type of blocks that wish to read and modify the memory image.<br />
   */<br />
  type CommandBlock[A] = State => ApplicationRequest[A] => Transaction[Event, Result]</p>
<p>  /**<br />
   * Runs the given `block` using the current state of the memory image.<br />
   */<br />
  def QueryAction(block: QueryBlock[AnyContent]): Action[AnyContent]</p>
<p>  /**<br />
   * Runs the given `block` using the current state of the memory image<br />
   * and applies the resulting transaction.<br />
   */<br />
  def CommandAction(block: CommandBlock[AnyContent]): Action[AnyContent]</p>
<p>  // [... code omitted ...]<br />
}<br />
[/code]</p>
<p>As you can see two kinds of actions are provided:</p>
<ol>
<li>Query actions - used when you want to access the current user and application state, but do not need to modify it.</li>
<li>Command actions - used when you want to generate and commit new events.</li>
</ol>
<p>Each action expects a callback block as a parameter. The block will be invoked with the current memory image state and an <code>ApplicationRequest</code>, which wraps the standard Play! <code>Request</code> and adds additional user related information:</p>
<p>[code lang="scala"]<br />
trait CurrentUserContext {<br />
  /**<br />
   * The current authenticated user or the guest user.<br />
   */<br />
  def currentUser: User<br />
}</p>
<p>trait UsersContext {<br />
  /**<br />
   * All known users and sessions.<br />
   */<br />
  def users: Users<br />
}</p>
<p>/**<br />
 * Context for use as an implicit parameter to views.<br />
 */<br />
trait ViewContext extends CurrentUserContext {<br />
  def flash: Flash<br />
}</p>
<p>/**<br />
 * Extend Play's `Request` with user context information.<br />
 */<br />
class ApplicationRequest[A](<br />
    request: Request[A],<br />
    val currentUser: User,<br />
    val users: Users)<br />
  extends WrappedRequest(request) with ViewContext with UsersContext<br />
[/code]</p>
<p>So instead of passing the Play! <code>Request</code> to views we now pass it a <code>ViewContext</code>, so that we can easily control which information is available to a view (in this case, just the current user and the flash message information).</p>
<p>The implementation of this trait uses a <code>MemoryImage</code> based on the <code>ApplicationState</code> class and can be found at <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-6/app/controllers/MemoryImageActions.scala">MemoryImageActions.scala</a>. Here's the implementation of the <code>QueryAction</code> method:</p>
<p>[code lang="scala"]<br />
/**<br />
 * Implements `ControllerActions` using the (global) memory image containing<br />
 * the `ApplicationState`.<br />
 */<br />
class MemoryImageActions<br />
    (memoryImage: MemoryImage[ApplicationState, DomainEvent])<br />
  extends ControllerActions[ApplicationState, DomainEvent] {</p>
<p>  override def QueryAction(block: QueryBlock[AnyContent]) = Action { request =><br />
    val state = memoryImage.get<br />
    block(state)(buildApplicationRequest(request, state))<br />
  }</p>
<p>  // [... CommandAction omitted ...]</p>
<p>  private def buildApplicationRequest[A](request: Request[A], state: ApplicationState) = {<br />
    val currentUser = request.session.get("authenticationToken")<br />
      .flatMap(AuthenticationToken.fromString)<br />
      .flatMap(state.users.findByAuthenticationToken)<br />
      .getOrElse(GuestUser)</p>
<p>    new ApplicationRequest(request, currentUser, state.users)<br />
  }<br />
}<br />
[/code]</p>
<p>As you can see a <code>QueryAction</code> is implemented as a Play! <code>Action</code> that reads the current state of the memory image and uses it to build the <code>ApplicationRequest</code> with the current user. If there is no authentication token the <code>GuestUser</code> is used instead. The provided block is then invoked. The <code>CommandAction</code> is similar, but uses the <code>MemoryImage.modify</code> to also commit the generated events.</p>
<p>Finally, we do not want to expose the entire application state to each controller. The <code>UsersController</code> only needs to see the users, while the <code>PostsController</code> only needs to see the posts. The <code>view</code> method on <code>ControllerActions</code> takes care of that:</p>
<p>[code lang="scala"]<br />
trait ControllerActions[State, -Event] extends Controller { outer =><br />
  // [... action methods omitted ...]</p>
<p>  /**<br />
   * Only expose part of the state of the memory image using the provided<br />
   * function `f`.<br />
   */<br />
  def view[S, E <: Event](f: State => S) = new ControllerActions[S, E] {<br />
    def QueryAction(block: QueryBlock[AnyContent]) = outer.QueryAction { state => request =><br />
      block(f(state))(request)<br />
    }<br />
    def CommandAction(block: CommandBlock[AnyContent]) = outer.CommandAction { state => request =><br />
      block(f(state))(request)<br />
    }<br />
  }<br />
[/code]</p>
<p>Controllers that use the memory image can now simply have an instance of <code>ControllerActions</code> passed in at construction time to define actions. For example, the PostsController.index action now becomes</p>
<p>[code lang="scala"]<br />
object PostsController<br />
  extends PostsController(Global.MemoryImageActions.view(_.posts))<br />
class PostsController(actions: ControllerActions[Posts, PostEvent]) {<br />
  // Import the action and controller methods directly.<br />
  import actions._</p>
<p>  /**<br />
   * Show an overview of the most recent blog posts.<br />
   */<br />
  def index = QueryAction { posts => implicit request =><br />
    Ok(views.html.posts.index(posts.mostRecent(20)))<br />
  }</p>
<p>  // [... other code omitted ...]<br />
}<br />
[/code]</p>
<h3>Uniqueness of email addresses</h3>
<p>One common requirement is that email addresses must be unique across all users. In an application backed by a relational database this is as easy as adding a <em>unique</em> constraint<a href="#unique-constraint" id="rev-unique-constraint"><sup>[1]</sup></a>, but an event store does not provide anything similar. We could use the email address as the user's event stream identifier, but that makes it hard for a user to change their email address without creating a brand new event stream. So what we'll do instead is to store a <em>map</em> of email address to user ids and fill this map on demand. For unit testing the following implementation works fine:</p>
<p>[code lang="scala"]<br />
val claimedEmailAddresses = collection.mutable.Map.empty[EmailAddress, UserId]<br />
def claim(email: EmailAddress, requestedUserId: UserId): UserId =<br />
  claimedEmailAddress.getOrElseUpdate(email, requestedUserId)<br />
[/code]</p>
<p>In other words, the first time an email address is used a new user id is assigned. Afterwards, the same user id is always returned. For the production implementation we use a Redis hash together with the <a href="http://redis.io/commands/hsetnx"><code>HSETNX</code></a> command:</p>
<p>[code lang="scala"]<br />
class RedisEmailRegistry(jedis: Jedis, redisKey: String) {<br />
  def claim(email: EmailAddress, requestedUserId: UserId): UserId = {<br />
    val result: Long = jedis.hsetnx(redisKey, email.toString, requestedUserId.toString)<br />
    result match {<br />
      case 0L =><br />
        val existingUserId = jedis.hget(redisKey, email.toString)<br />
        UserId.fromString(existingUserId).getOrElse(sys.error(s"cannot parse user id: $existingUserId"))<br />
      case 1L =><br />
        requestedUserId<br />
      case _ =><br />
        sys.error(s"unexpected Redis return value: $result")<br />
    }<br />
  }<br />
}<br />
[/code]</p>
<p>So when we register a new user we first map the email address to a user id and then generate the <code>UserRegistered</code> event. If the email address is already taken the event store will return a conflict, since we always expect the event stream to be empty for a new user:</p>
<p>[code lang="scala"]<br />
class UsersController(<br />
    actions: ControllerActions[Users, UserEvent],<br />
    claimEmailAddress: (EmailAddress, UserId) => UserId) {<br />
  import actions._</p>
<p>  // [... other actions omitted ...]</p>
<p>  def register = CommandAction { state => implicit request =><br />
    val form = registrationForm.bindFromRequest<br />
    form.fold(<br />
      formWithErrors =><br />
        abort(BadRequest(views.html.users.register(formWithErrors))),<br />
      registration => {<br />
        val (email, displayName, password) = registration<br />
        val userId = claimEmailAddress(email, UserId.generate)<br />
        Changes(<br />
            StreamRevision.Initial,<br />
            UserRegistered(userId, email, displayName, password): UserEvent)<br />
          .commit(<br />
            onCommit = Redirect(routes.UsersController.registered),<br />
            onConflict = _ => BadRequest(views.html.users.register(form.withGlobalError("duplicate.account"))))<br />
      })<br />
  }<br />
[/code]</p>
<h2>Authors and authorization</h2>
<p>When it comes to security <em>authentication</em> is often the "easy" part, mainly limiting itself to user registration and logging in. Authorization is often much harder, since it affects the entire application. Now that we have the concept of a user we change blog posts and comments to include the user id of the author or commenter. We then implement authorization rules to ensure only the author can delete a blog post, etc. We add the authorization methods the <code>User</code> trait:</p>
<p>[code lang="scala"]<br />
trait User {<br />
  // [... code omitted ...]</p>
<p>  def canAddPost: Boolean = false<br />
  def canAddComment(post: Post): Boolean = false<br />
  def canEditPost(post: Post): Boolean = false<br />
  def canDeletePost(post: Post): Boolean = false<br />
  def canDeleteComment(post: Post, comment: Comment): Boolean = false</p>
<p>  // [... code omitted ...]<br />
}<br />
[/code]</p>
<p>By default a user is not authorized to do anything, except for a <code>RegisteredUser</code> or a <code>GuestUser</code>:</p>
<p>[code lang="scala"]<br />
case object GuestUser extends User {<br />
  // [... other code omitted ...]</p>
<p>  override def canAddComment(post: Post) = true<br />
}</p>
<p>case class RegisteredUser(/* ... code omitted ... */) extends User {<br />
  // [... other code omitted ...]</p>
<p>  override def canAddPost = true<br />
  override def canAddComment(post: Post) = true<br />
  override def canEditPost(post: Post) = post.isAuthoredBy(this)<br />
  override def canDeletePost(post: Post) = post.isAuthoredBy(this)<br />
  override def canDeleteComment(post: Post, comment: Comment) =<br />
    post.isAuthoredBy(this) || comment.isAuthoredBy(this)</p>
<p>}<br />
[/code]</p>
<p>As you can see a guest user can only add comments to a post. Registered users can add posts, comments, and can edit or delete posts and comments authored by themselves.</p>
<p>Now controllers and views can easily invoke the correct authorization method to see if the user is allowed to see a certain button or to perform a certain action. Here's is an example when adding a post (from <code>PostsController</code>), which uses the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-6/app/controllers/ControllerActions.scala#L69"><code>AuthenticatedCommandAction</code></a> to require a logged in user:</p>
<p>[code lang="scala"]<br />
  def edit(id: PostId, expected: StreamRevision) = AuthenticatedCommandAction {<br />
    user => posts => implicit request =><br />
      posts.get(id).filter(user.canEditPost) map { post =><br />
        // [... code omitted ...]<br />
      } getOrElse {<br />
        abort(notFound)<br />
      }<br />
  }<br />
[/code]</p>
<h3>Event authorization</h3>
<p>Unfortunately, it is easy to forget to add the correct authorization check to a controller action, since any action is allowed by default. And an attacker only has to find a single mistake to break the security of an application. With event sourcing we can optionally add an additional layer of security: we can check the events being committed against the authorization level of a user. To do this we add one additional method to the <code>User</code> trait:</p>
<p>[code lang="scala"]<br />
trait User {<br />
  // [... code omitted ...]</p>
<p>  def authorizeEvent(state: ApplicationState): DomainEvent => Boolean =<br />
    event => false<br />
}<br />
[/code]</p>
<p>This method takes the current <code>ApplicationState</code> and returns a function that checks if this user is allowed to commit a specific domain event. The default implementation always forbids any change.</p>
<p>For guest users and registered users we override this method. Here's the code for the guest user:</p>
<p>[code lang="scala"]<br />
case object GuestUser extends User {<br />
  // [... code omitted ...]</p>
<p>  override def authorizeEvent(state: ApplicationState) = {<br />
    case event: UserRegistered => true<br />
    case event: UserLoggedIn   => true<br />
    case event: CommentAdded   => state.posts.get(event.postId).exists(this.canAddComment)<br />
    case _                     => false<br />
  }<br />
}<br />
[/code]</p>
<p>Now we have everything to implement the <code>CommandAction</code> helper method from the <code>MemoryImageActions</code> class:</p>
<p>[code lang="scala"]<br />
class MemoryImageActions(memoryImage: MemoryImage[ApplicationState, DomainEvent])<br />
    extends ControllerActions[ApplicationState, DomainEvent] {<br />
  // [... code omitted ...]</p>
<p>  override def CommandAction(block: CommandBlock[AnyContent]) = Action { request =><br />
    memoryImage.modify { state =><br />
      val applicationRequest = buildApplicationRequest(request, state)<br />
      val currentUser = applicationRequest.currentUser<br />
      val transaction = block(state)(applicationRequest)<br />
      if (transaction.events.forall(currentUser.authorizeEvent(state))) {<br />
        transaction.withHeaders(currentUser.registered.map(user => "currentUserId" -> user.id.toString).toSeq: _*)<br />
      } else {<br />
        Transaction.abort(notFound(request))<br />
      }<br />
    }<br />
  }</p>
<p>  // [... code omitted ...]<br />
}<br />
[/code]</p>
<p>As you can see it first builds the <code>ApplicationRequest</code>, just like the <code>QueryAction</code> method. But it then inspects the transaction returned by the provided block to see if the current user is authorized to commit all the generated events. If so, it adds the current user id as a header to the commit and applies it to the memory image. Otherwise, the transaction is aborted with a 404 Not Found response.</p>
<p>The main advantage of this level of authorization is that events must be explicitly whitelisted. So when new functionality is added it will simply not work until the whitelist is updated. This is the opposite of views and controllers, where new functionality works by default and authorization checks must be explicitly added. A step that is easy to forget!</p>
<h2>Summary</h2>
<p>Adding users to our example application caused some major re-design to manage authentication and authorization. Compared to this event sourcing was just a minor detail, the same kind of re-design would have been needed if users were stored in a traditional database. Event sourcing does provide an additional layer of security by allowing us to authorize committed events. We can also fully audit all changes, as every commit includes the committing user's id.</p>
<p>We have now build a fairly complete application. In the next part we'll start making use of our event sourced model to integrate with the outside world. We'll do this by reacting to the events as they are committed to the event store.</p>
<style>
#footnotes {
  font-size: smaller;
}
span:target {
  background-color: #F0F0F0;
}
</style>
<style media="only screen and (min--moz-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)">
.replace-2x {
	font-size: 1px;
}
</style>
<p><script type="text/javascript">
function highdpi_init() {
	if(jQuery('.replace-2x').css('font-size') == "1px") {
		var els = jQuery("img.replace-2x").get();
		for(var i = 0; i < els.length; i++) {
			var src = els[i].src
			src = src.replace(".png", "@2x.png");
			els[i].src = src;
		}
	}
}
jQuery(document).ready(function() {
	highdpi_init();
});
</script></p>
<div id="footnotes">
<p>Footnotes:</p>
<p><span id="unique-constraint"><a href="#rev-unique-constraint">[1]</a> Actually handling a unique constraint violation can be quite hard however. Often you'll need to catch an exception from an ORM framework when the transaction is committed and then try to derive which unique constraint was violated.</span></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.zilverline.com/2013/01/09/simple-event-sourcing-users-authentication-authorization-part-6/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to use your happiness metric as a information radiator?</title>
		<link>http://blog.zilverline.com/2012/09/27/how-to-use-your-happiness-metric-as-a-information-radiator/</link>
		<comments>http://blog.zilverline.com/2012/09/27/how-to-use-your-happiness-metric-as-a-information-radiator/#comments</comments>
		<pubDate>Thu, 27 Sep 2012 08:35:56 +0000</pubDate>
		<dc:creator>Mark Suurmond</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[happiness]]></category>
		<category><![CDATA[information radiator]]></category>
		<category><![CDATA[motivation]]></category>
		<category><![CDATA[team]]></category>
		<category><![CDATA[transparency]]></category>

		<guid isPermaLink="false">http://blog.zilverline.com/?p=1377</guid>
		<description><![CDATA[When I started recording the happiness of my team, I found it difficult to make this information transparent. I wanted to use it as a information radiator, so that everyone who looked at our Kanban or Scrum board could see (within 1 minute) how this particular team is doing, while on the other hand not <a href='http://blog.zilverline.com/2012/09/27/how-to-use-your-happiness-metric-as-a-information-radiator/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>When I started recording the happiness of my team, I found it difficult to make <a href="http://blog.zilverline.com/2012/08/21/how-to-fill-your-happiness-metric/" title="Happiness metric" target="_blank">this information</a> transparent. I wanted to use it as a information radiator, so that everyone who looked at our Kanban or Scrum board could see (within 1 minute) how this particular team is doing, while on the other hand not all team members were confident enough to have their personal grade for all to see.<br />
<span id="more-1377"></span><br />
As I found out, the happiness metric can also be used in a destructive way. When people don&#8217;t know the context in which a grade is given, they can overreact on those numbers and take actions that make sure team members will give &#8216;political correct&#8217; grades in the future.</p>
<p>In search for a good representation I didn&#8217;t want to use an average grade. All individual grades are too important to get lost in an average. Also a few 1&#8242;s and 6&#8242;s could give a very wrongful average of 3,5. So all numbers are important, but they need to be compressed in a &#8216;faster&#8217; information radiator.</p>
<p>What I tried, was to make a weather &#8216;backcast&#8217; out of it. I found some weather icons on the interwebz and translated the overall histogram to a description of our teams weather conditions. So for example, &#8220;Cloudy with here and there some sunshine&#8221; (a few 3 and 4&#8242;s and one 2) or &#8220;Sunny with some local thunderstorms&#8221; (mostly 2&#8242;s and one 5). In this way every observer can see what the happiness of this team is and if they want to get more detailed information, they have to ask a team member. This way we can make sure the correct context is given along with the current happiness. </p>
<p><img src="http://blog.zilverline.com/wp-content/uploads/2012/09/WheaterBackCast.jpg" alt="WeatherBackcast" /></p>
<p>Of course it is a good idea to put the actions that will improve the scores also on the board. You can add: &#8220;When we want the sun to shine tomorrow, we need the following improvements:&#8221; and then the list of actions. So they are visible for everyone, even (especially) for the &#8216;innocent&#8217; bystander. </p>
<p>Do you have any improvements?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zilverline.com/2012/09/27/how-to-use-your-happiness-metric-as-a-information-radiator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flowmulator &#8211; A Kanban flow simulator</title>
		<link>http://blog.zilverline.com/2012/09/21/flowmulator-a-kanban-flow-simulator/</link>
		<comments>http://blog.zilverline.com/2012/09/21/flowmulator-a-kanban-flow-simulator/#comments</comments>
		<pubDate>Fri, 21 Sep 2012 19:53:07 +0000</pubDate>
		<dc:creator>Mark Suurmond</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Kanban]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[flow]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[kanban]]></category>
		<category><![CDATA[simulator]]></category>
		<category><![CDATA[WIP]]></category>
		<category><![CDATA[work in progress]]></category>

		<guid isPermaLink="false">http://blog.zilverline.com/?p=1401</guid>
		<description><![CDATA[UPDATE: Now you can add workers to a column and see if it helps getting work done faster. Try experimenting with the numbers and get the best configuration! While reading blogs about Agile games, I came across this link of Karl Scotland. Because I always like combining programming with building something that gets across Agile <a href='http://blog.zilverline.com/2012/09/21/flowmulator-a-kanban-flow-simulator/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p><strong>UPDATE:</strong> Now you can add workers to a column and see if it helps getting work done faster. Try experimenting with the numbers and get the best configuration!</p>
<p>While reading blogs about Agile games, I came across <a href="http://availagility.co.uk/2008/12/22/the-kanban-flow-and-cadence-simulation/" target="_blank" title="The Kanban, Flow and Cadence Simulator">this link</a> of Karl Scotland.<br />
Because I always like combining programming with building something that gets across Agile principles (our <a href="http://blog.zilverline.com/2012/04/01/the-power-of-feedback-in-scrum/" target="_blank" title="The power of feedback in Scrum">Battleship game</a> is also a good example) this seemed like a good new project for our Fridays at Zilverline HQ. Bob Forma and me started this and within a few days we came up with <a href="http://zilverline.github.com/flowmulator/public/index.html" title="Flowmulator" target="_blank">this</a> (You can check the source code <a href="https://github.com/zilverline/flowmulator" title="Link to GitHub" target="_blank">here</a>)</p>
<p><span id="more-1401"></span></p>
<p>Each feature has a random generated effort which you can see on the &#8216;sticky&#8217;. Each day (if you press &#8216;Run 1 day&#8217;) for each stage (Analysis, Code, etc.) productivity is generated of 4, 6 or 8 hours. The stage then looks if there is ready work to be found in its preliminary stage, if there is, that work is pulled to &#8216;in progress&#8217; of course the &#8216;Work in progress Limit&#8217; is respected at all times. The velocity is then divided between the features that are &#8216;in progress&#8217;. Once a feature has zero remaining effort, it is moved to the &#8216;ready&#8217; column of that stage. </p>
<p>If you click on a feature, the text on it changes to &#8216;Workdays&#8217; which is time this feature spend in &#8216;in progress&#8217; and &#8216;Waitdays&#8217; which is time it has spend in a &#8216;Ready&#8217; column. I also added Totaldays which is a sum of these two. If you use &#8216;RESTART&#8217; the board gets reset and you can experiment with the work in progress limits and their effect on flow.</p>
<p>We hope you like it and feel free to ask for new options in the comments (or branch and add them yourself)!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zilverline.com/2012/09/21/flowmulator-a-kanban-flow-simulator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple event sourcing &#8211; refactoring and transactions (part 5)</title>
		<link>http://blog.zilverline.com/2012/08/26/simple-event-sourcing-refactoring-and-transactions-part-5/</link>
		<comments>http://blog.zilverline.com/2012/08/26/simple-event-sourcing-refactoring-and-transactions-part-5/#comments</comments>
		<pubDate>Sun, 26 Aug 2012 19:14:12 +0000</pubDate>
		<dc:creator>Erik Rozendaal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[event sourcing]]></category>
		<category><![CDATA[memory image]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.zilverline.com/?p=1315</guid>
		<description><![CDATA[In the previous part we added blog post comment functionality. In this part we&#8217;ll do some refactoring and change the memory image implementation to automatically retry domain logic on optimistic locking conflicts, giving us a simplified form of transactions. We&#8217;ll also change the event store to support multiple types of event streams in a single <a href='http://blog.zilverline.com/2012/08/26/simple-event-sourcing-refactoring-and-transactions-part-5/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>In the <a href="http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/">previous part</a> we added blog post comment functionality. In this part we&#8217;ll do some refactoring and change the memory image implementation to automatically retry domain logic on optimistic locking conflicts, giving us a simplified form of transactions. We&#8217;ll also change the event store to support multiple types of event streams in a single event store.</p>
<p><span id="more-1315"></span></p>
<h3>Other Parts</h3>
<ul>
<li><a href="http://blog.zilverline.com/2012/07/04/simple-event-sourcing-introduction-part-1/">Part 1 &#8211; Introduction</a></li>
<li><a href="http://blog.zilverline.com/2012/07/23/simple-event-sourcing-consistency-part-2/">Part 2 &#8211; Consistency</a></li>
<li><a href="http://blog.zilverline.com/2012/07/30/simple-event-sourcing-redis-event-store-part-3/">Part 3 &#8211; Redis Event Store</a></li>
<li><a href="http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/">Part 4 &#8211; Conflict Resolution</a></li>
<li>Part 5 &#8211; Refactoring and Transactions</li>
<li><a href="http://blog.zilverline.com/2013/01/09/simple-event-sourcing-users-authentication-authorization-part-6/">Part 6 &#8211; Users, Authentication, Authorization</a></li>
</ul>
<h3>Code</h3>
<p>You can find the code associated with this part at <a href="https://github.com/zilverline/event-sourced-blog-example/tree/part-5">github</a> on the <code>part-5</code> branch.</p>
<h2>Refactoring</h2>
<p>The main changes we want to make before improving the current <code>commit</code> code to handle transient conflicts is to move it out of the <code>PostsController</code>, so that we use it from other parts of the application. But the current implementation is tied directly to <code>Post</code>s and <code>PostEvent</code>s. So first we remove the following dependencies from this method:</p>
<ol>
<li><code>PostEvent</code> conflict resolution.</li>
<li>How to derive the <code>PostId</code> from the <code>PostEvent</code>.</li>
<li>How to turn a <code>PostId</code> into a event stream identifier (currently just using <code>toString</code>)</li>
</ol>
<p>To solve the first problem we introduce a new trait <code>ConflictsWith</code>:</p>
<p>[code lang=scala]<br />
/**<br />
 * Compares committed events against an attempted events to check for<br />
 * conflicts.<br />
 */<br />
trait ConflictsWith[-Event] {<br />
  /**<br />
   * Checks each committed event from `conflict` for conflicts with the `attempted` events.<br />
   * Any committed events that conflict are returned.<br />
   */<br />
  def conflicting[A <: Event, B <: Event]<br />
                 (conflict: Conflict[A], attempted: Seq[B]): Option[Conflict[A]]<br />
}<br />
object ConflictsWith {<br />
  /**<br />
   * Builds a new `ConflictsWith` based on the `conflicts` predicate.<br />
   */<br />
  def apply[Event](conflicts: (Event, Event) => Boolean) = new ConflictsWith[Event] {<br />
    override def conflicting[A <: Event, B <: Event]<br />
                            (conflict: Conflict[A], attempted: Seq[B]): Option[Conflict[A]] =<br />
      conflict.filter(a => attempted.exists(b => conflicts(a.event, b)))<br />
  }<br />
}<br />
[/code]</p>
<p><code>ConflictsWith</code> is a <a href="http://dcsobral.blogspot.com/2010/06/implicit-tricks-type-class-pattern.html">type class</a> that defines how to resolve conflicts for a certain type of events. By making it a separate trait (instead of a function) the Scala compiler can automatically pass implementations as an implicit parameter where needed. The definition of the <code>Conflict</code> class has also <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-5/app/eventstore/EventStore.scala#L96">changed slightly</a>, to make it easier to filter the events while preserving meta-information.</p>
<p>The <code>apply</code> method of the <code>ConflictsWith</code> companion object is helpful to create instances of <code>ConflictsWith</code> based on a simple predicate. The predicate must check if two events conflict or not. The <code>PostEvent</code> conflict resolution implementation becomes:</p>
<p>[code lang=scala]<br />
  implicit val PostEventConflictsWith: ConflictsWith[PostEvent] = ConflictsWith {<br />
    case (a: PostCommentEvent, b: PostCommentEvent) => a.commentId == b.commentId<br />
    case (_: PostCommentEvent, _)                   => false<br />
    case _                                          => true<br />
  }<br />
[/code]</p>
<p>We use Scala&#8217;s convenient <a href="http://blog.bruchez.name/2011/10/scala-partial-functions-without-phd.html">partial function syntax</a> to define all the cases, without having to explicitly use the <code>match</code> keyword.</p>
<p>To solve the second and third problem, and to support different type of event streams in a single event store, we introduce another trait <code>EventStreamType</code>:</p>
<p>[code lang=scala]<br />
/**<br />
 * Event stream type information.<br />
 */<br />
trait EventStreamType[StreamId, Event] {<br />
  /**<br />
   * Convert a stream identifier to a string. Used by the event store to persist<br />
   * the stream identifier.<br />
   */<br />
  def toString(streamId: StreamId): String</p>
<p>  /**<br />
   * Extract stream identifier from `event`.<br />
   */<br />
  def streamId(event: Event): StreamId</p>
<p>  /**<br />
   * Cast `event` to the `Event` type.<br />
   *<br />
   * @throws ClassCastException if `event` is not of type `Event`.<br />
   */<br />
  def cast(event: Any): Event<br />
}<br />
object EventStreamType {<br />
  def apply[StreamId, Event]<br />
           (writeStreamId: StreamId => String,<br />
            eventToStreamId: Event => StreamId)<br />
           (implicit manifest: Manifest[Event]) =<br />
     // [...]<br />
}<br />
[/code]</p>
<p>This trait captures the relationship between an event stream identifier type and the type of events that can be stored in the associated stream. It also provides methods to extract the event stream identifier from an event and to turn an event stream identifier into its string representation. Finally it allows casting a value of any type to the type of the event. With these two traits defined we can move the commit method from the <code>PostsController</code> into the <code>MemoryImage</code> class. But first we look into using the event store for different event stream types.</p>
<h3>Multiple event stream types</h3>
<p>Now that we&#8217;ve associated an event streams identifier type with the event type using the <code>EventStreamType</code> trait, we can make our event store <em>contra-variant</em>. In other words, when we have an event store of type <code>EventStore[DomainEvent]</code> we can use it as an <code>EventStore[PostEvent]</code>, if <code>PostEvent</code> is a subtype of <code>DomainEvent</code>.</p>
<p>But we can only do this by changing event store methods that return events to take into account the <code>EventStreamType</code>, otherwise the Scala compiler will not allow us to treat a <code>EventStore[DomainEvent]</code> as a <code>EventStore[PostEvent]</code> (since reading from such a store could return <code>DomainEvent</code>s, while the caller would only expect <code>PostEvent</code>s). So the definition of <code>CommitReader</code> becomes:</p>
<p>[code lang=scala]<br />
/**<br />
 * Reads commits from the event store.<br />
 */<br />
trait CommitReader[-Event] {<br />
  // [...]</p>
<p>  /**<br />
   * Reads all commits `since` (exclusive) up `to` (inclusive). Events are<br />
   * filtered to only include events of type `E`.<br />
   */<br />
  def readCommits[E <: Event]<br />
                 (since: StoreRevision, to: StoreRevision)<br />
                 (implicit manifest: Manifest[E]): Stream[Commit[E]]</p>
<p>  /**<br />
   * Reads all commits from the stream identified by `streamId` that occurred<br />
   * `since` (exclusive) up `to` (inclusive).<br />
   *<br />
   * @throws ClassCastException the stream contained commits that did not<br />
   *                            have the correct type `E`.<br />
   */<br />
  def readStream[StreamId, E <: Event]<br />
                (streamId: StreamId,<br />
                 since: StreamRevision = StreamRevision.Initial,<br />
                 to: StreamRevision = StreamRevision.Maximum)<br />
                (implicit descriptor: EventStreamType[StreamId, E]):<br />
                Stream[Commit[E]]<br />
}<br />
[/code]</p>
<p>Other methods in the event store traits are similarly changed. The methods can be used much like before, except that you&#8217;ll have to provide the expected event type (<code>readCommits</code>) or that Scala will look for an appropriate instance of <code>EventStreamType</code> (<code>readStream</code>):</p>
<p>[code lang=scala]<br />
implicit val PostEventStreamType: EventStreamType[PostId, PostEvent] =<br />
  EventStreamType(streamId => streamId.toString, event => event.postId)</p>
<p>val myCommits = eventStore.reader.readStream(myPostId)<br />
[/code]</p>
<p>Here the Scala compiler knows the type of <code>myPostId</code> and will infer that the events must be of type <code>PostEvent</code><a href="#boundary" id="rev-boundary"><sup>[1]</sup></a>, based on the existence of <code>PostEventStreamType</code>. So Scala will correctly infer the type of <code>myCommits</code> to be <code>Stream[Commit[PostEvent]]</code>, so you will not have to do any type casting.</p>
<h3>Commit parameters</h3>
<p>Another change is that we introduce a new <code>Changes</code> type that combines the three parameters provided to the event store committer&#8217;s <code>tryCommit</code> method (<code>streamId</code>, <code>expected</code> stream revision, and the <code>event</code> to append). The interface is:</p>
<p>[code lang=scala]<br />
/**<br />
 * Represents the changes that can be committed atomically to the event store.<br />
 */<br />
sealed trait Changes[Event] {<br />
  type StreamId<br />
  def eventStreamType: EventStreamType[StreamId, Event]</p>
<p>  def streamId: StreamId<br />
  def expected: StreamRevision<br />
  def events: Seq[Event]</p>
<p>  def withExpectedRevision(expected: StreamRevision): Changes[Event]<br />
}<br />
object Changes {<br />
  // [...]</p>
<p>  def apply[StreamId, Event]<br />
           (expected: StreamRevision, event: Event)<br />
           (implicit streamType: EventStreamType[StreamId, Event]): Changes[Event] =<br />
    apply(streamType.streamId(event), expected, event)<br />
}<br />
[/code]</p>
<p>So besides the three parameters it also captures the type of the stream identifier and the associated <code>EventStreamType</code> instance, so that only events of the correct type can be committed to the event stream. The <code>withExpectedRevision</code> returns a new copy of the changes with an updated expected <code>StreamRevision</code>, which is useful for retrying a commit in the case of resolved conflicts.</p>
<p>With all these changes in place we can get back to solving transient transaction conflicts.</p>
<h2>Atomic memory image modifications</h2>
<p>Going back to our application&#8217;s storage architecture the following picture emerges:</p>
<p><img src="http://blog.zilverline.com/wp-content/uploads/2012/08/storage-architecture.png" alt="" title="Storage architecture" width="368" height="99" class="alignnone size-full wp-image-1320 replace-2x" /></p>
<p>Remember that in the blog post application there is one event store, each with many memory images (one per application server), all concurrently committing new events, based on the actions performed by the users. We already detect the conflicts that can occur when users work with (potentially) outdated information by comparing the <em>expected</em> stream revision to the <em>actual</em> revision (see the <a href="http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/">previous part</a> for more details). Let&#8217;s call these kind of conflicts <em>user conflicts</em>.</p>
<p>But by adding the blog comment functionality we introduced a potential conflict in the interaction between the memory image and the event store. In the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-4/app/controllers/PostsController.scala#L126">add comment action of PostsController</a> we first read the current state of a blog post and use it to assign a <code>CommentId</code> to the new comment. We then commit a <code>CommentAdded</code> event. If multiple users happen to add comments to a blog post simultaneously, an unresolvable conflict will occur: both new comments will have the same id. This is a <em>transaction conflict</em><a href="#txn-conflict" id="rev-txn-conflict"><sup>[2]</sup></a><a href="#swp" id="rev-swp"><sup>[3]</sup></a>.</p>
<p>In the current version of the application the commit fails and we&#8217;ll notify the user that someone else added a comment with the same id. The user must then resubmit the comment, which will then succeed. But why ask the user to retry an action for these kind of <em>transient</em> conflicts, when computers are much better at doing repetitive work automatically?</p>
<p>What we have to do is to automatically rerun the code that reads the memory image and then generates the event. So we will replace the <code>MemoryImage</code>&#8216;s <code>tryCommit</code> method with a new <code>modify</code> method:</p>
<p>[code lang=scala]<br />
class MemoryImage[State, Event] /* [...] */ {<br />
  // [...]</p>
<p>  def modify[A](body: State => Transaction[Event, A]): A = ...<br />
}<br />
[/code]</p>
<p>The <code>body</code> parameter is a function that uses the current state of the memory image and returns a <code>Transaction</code> value. This transaction value contains all the information we need to try to commit to the event store. If it turns out that a transient transaction conflict occurred, we will rerun the <code>body</code> function and retry the commit. When the transaction succeeds we return the value returned by <code>body</code>.</p>
<p>The possible values for a <code>Transaction</code> are shown below:</p>
<p>[code lang=scala]<br />
/**<br />
 * The transaction to commit to the event store when modifying the memory image.<br />
 */<br />
sealed trait Transaction[+Event, +A] {<br />
  /**<br />
   * Maps the result of this transaction from `A` to `B` using `f`.<br />
   */<br />
  def map[B](f: A => B): Transaction[Event, B]<br />
}<br />
object Transaction {<br />
  /**<br />
   * Transaction result that completes with `onAbort` when run,<br />
   * without committing anything the event store.<br />
   */<br />
  def abort[A](onAbort: => A): Transaction[Nothing, A] =<br />
    new TransactionAbort(() => onAbort)</p>
<p>  implicit def ChangesOps[Event](changes: Changes[Event]) = new ChangesOps(changes)<br />
  class ChangesOps[Event](changes: Changes[Event]) {<br />
    /**<br />
     * Transaction result that will commit the  `changes` to the event store.<br />
     */<br />
    def commit[A](onCommit: => A, onConflict: Conflict[Event] => A)<br />
                 (implicit conflictsWith: ConflictsWith[Event]):<br />
                 Transaction[Event, A] =<br />
      new TransactionCommit(changes, () => onCommit, onConflict, conflictsWith)<br />
  }</p>
<p>  implicit def OptionTransactionOps[Event, A](m: Option[Transaction[Event, A]]) = new OptionTransactionOps(m)<br />
  class OptionTransactionOps[Event, A](value: Option[Transaction[Event, A]]) {<br />
    /**<br />
     * Turns an `Option[Transaction[Event, A]]` into `Transaction[Event, Option[A]]`.<br />
     */<br />
    def sequence: Transaction[Event, Option[A]] = value match {<br />
      case None              => abort(None)<br />
      case Some(transaction) => transaction.map(Some(_))<br />
    }<br />
  }<br />
}<br />
[/code]</p>
<p>The <code>TransactionCommit</code> and <code>TransactionAbort</code> (not listed) are simple case classes to hold parameters until we are ready to commit (or abort) the transaction. The <code>ChangesOps</code> <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=179766">enriches</a> the <code>Changes</code> class with a new <code>commit</code> method that instantiates a <code>TransactionCommit</code> (Scala 2.10 will offer a nicer syntax for &#8220;extending&#8221; existing classes with new methods using <a href="http://docs.scala-lang.org/sips/pending/implicit-classes.html">implicit classes</a>). Values of type <code>Option[Transaction[Event, A]]</code> are also extended with the <code>sequence</code> method, which flips the optionality to return a <code>Transaction[Event, Option[A]]</code></p>
<p>To modify the memory image safely, we can now write code like:</p>
<p>[code lang=scala]<br />
memoryImage.modify { posts => Changes(/* ... */).commit(/* ... */) }<br />
[/code]</p>
<p>The flow chart below describes the full <code>MemoryImage</code> <code>modify</code> algorithm, including user and transaction conflict resolution:</p>
<p><img src="http://blog.zilverline.com/wp-content/uploads/2012/08/transaction-flow.png" alt="" title="Transaction flow" width="469" height="714" class="alignnone size-full wp-image-1317 replace-2x" /></p>
<p>That&#8217;s quite a few steps! Let&#8217;s go through each step in the left column:</p>
<ol>
<li>Read the current state of the memory image.</li>
<li>Pass the state to the provided transaction body.</li>
<li>Is the result of the transaction an abort? If yes, return <code>onAbort</code> result and stop.</li>
<li>Try to commit the <code>Changes</code> to the event store using the <em>expected</em> stream revision.</li>
<li>Did the commit succeed? If yes, return the <code>onCommit</code> result and stop.</li>
<li>Was there an update to the event stream that was not yet applied to the memory image state used to run this transaction? If yes, we have a transaction conflict and go back to step 1 to retry.</li>
<li>Is there an unresolvable user conflict? If yes, return the result of the <code>onConflict</code> handler and stop.</li>
<li>Try to commit again, but now use the <em>actual</em> stream revision.</li>
<li>Did the commit succeed? If yes, return the <code>onCommit</code> result and stop. If not, we have a transaction conflict, so go back to step 1 to retry.</li>
</ol>
<p>This entire process is implemented in <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-5/app/eventstore/MemoryImage.scala#L74">MemoryImage.modify</a>. The method is a bit larger than I would like, but it seems to be one of those cases that don&#8217;t get any more understandable when split up into smaller pieces.</p>
<p>Now that we have basic transaction support in place we can modify the controller methods to work with this. Here&#8217;s the post edit action together with the <code>updatePost</code> helper method:</p>
<p>[code lang=scala]<br />
object edit {<br />
  // [...]<br />
  def submit(id: PostId, expected: StreamRevision) = Action { implicit request =><br />
    updatePost(id) { post =></p>
<p>      postContentForm.bindFromRequest.fold(<br />
        formWithErrors =><br />
          abort(BadRequest(views.html.posts.edit(id, expected, formWithErrors))),</p>
<p>        postContent =><br />
          Changes(expected, PostEdited(id, postContent): PostEvent).commit(<br />
            onCommit =<br />
              Redirect(routes.PostsController.show(id)).flashing("info" -> "Post saved."),<br />
            onConflict = conflict =><br />
              Conflict(views.html.posts.edit(id, conflict.actual, postContentForm.fill(postContent), conflict.events))))</p>
<p>    } getOrElse {<br />
      notFound<br />
    }<br />
  }<br />
}</p>
<p>/**<br />
 * Runs the transaction `body` against the post identified by `postId` and<br />
 * returns the result, if it exists. Otherwise `None` is returned.<br />
 */<br />
def updatePost[A](id: PostId)<br />
                 (body: Post => Transaction[PostEvent, A]): Option[A] =<br />
  memoryImage.modify { _.get(id).map(body).sequence }<br />
[/code]</p>
<p>As you can see, if the form validation fails, we <code>abort</code> the transaction and return a <code>BadRequest</code> as usual. Otherwise we return a commit result with the <code>PostEdited</code> event and the appropriate <code>onCommit</code> and <code>onConflict</code> handlers.</p>
<h2>Summary</h2>
<p>With the addition of the <code>modify</code> method to the <code>MemoryImage</code> we now have a place to put domain logic. Since the example application&#8217;s logic is still extremely simple, we can just keep it inside the <code>PostsController</code>. As your application gets more complicated, you should extract the domain logic into separate functions and classes.</p>
<p>The event store (and memory image) now also support multiple types of event streams. We&#8217;ll use that in the next part to add user accounts to the example application. We&#8217;ll also look into ensuring uniqueness of the email addresses associated with these user accounts.</p>
<style>
#footnotes {
  font-size: smaller;
}
span:target {
  background-color: #F0F0F0;
}
</style>
<style media="only screen and (min--moz-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)">
.replace-2x {
	font-size: 1px;
}
</style>
<p><script type="text/javascript">
function highdpi_init() {
	if(jQuery('.replace-2x').css('font-size') == "1px") {
		var els = jQuery("img.replace-2x").get();
		for(var i = 0; i < els.length; i++) {
			var src = els[i].src
			src = src.replace(".png", "@2x.png");
			els[i].src = src;
		}
	}
}
jQuery(document).ready(function() {
	highdpi_init();
});
</script></p>
<div id="footnotes">
<p>Footnotes:</p>
<p><span id="boundary"><a href="#rev-boundary">[1]</a> Of course, the event store as a <em>boundary</em>. The stored events our outside of the application's control and there is no guarantee that the events in a stream are of the correct type, or can be deserialized. This is why we fail fast when we get bad data out of the event store, so the problem can be detected and fixed quickly.</span></p>
<p><span id="txn-conflict"><a href="#rev-txn-conflict">[2]</a> Transient transaction conflicts are not specific to memory images or event stores. In relational database with multi-version concurrency control (such as Oracle or PostgreSQL) you can get a "non-serializable transaction" error, in which case you should also retry your transaction.</span></p>
<p><span id="swp"><a href="#rev-swp">[3]</a> Instead of trying to resolve transient conflicts, we can also <em>prevent</em> them by adhering to the <a href="http://mechanical-sympathy.blogspot.com/2011/09/single-writer-principle.html">Single Writer Principle</a>. By making sure only one application server can write to the event store, there can be no conflicts. All <code>Changes</code> can then be send to this writer process to be committed to the event store. This is actually a great solution, but does require some kind of cluster communication to decide which server is the "writer". Currently cluster communication (and leadership election) is not provided out-of-the-box by Play! or Akka, but Akka 2.1 should have built-in cluster management.</span></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.zilverline.com/2012/08/26/simple-event-sourcing-refactoring-and-transactions-part-5/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to fill your happiness metric</title>
		<link>http://blog.zilverline.com/2012/08/21/how-to-fill-your-happiness-metric/</link>
		<comments>http://blog.zilverline.com/2012/08/21/how-to-fill-your-happiness-metric/#comments</comments>
		<pubDate>Tue, 21 Aug 2012 07:23:45 +0000</pubDate>
		<dc:creator>Mark Suurmond</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Scrum]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[happiness]]></category>
		<category><![CDATA[opinions]]></category>
		<category><![CDATA[Retrospective]]></category>
		<category><![CDATA[scrum]]></category>
		<category><![CDATA[transparency]]></category>

		<guid isPermaLink="false">http://blog.zilverline.com/?p=1281</guid>
		<description><![CDATA[Knowing how people think or feel about a certain subject can be very helpful in building trust, creating a team and to reveal impediments. Of course it can be very difficult to get team members to express their real opinions, especially when a team has just started. I think with patience and the right approach <a href='http://blog.zilverline.com/2012/08/21/how-to-fill-your-happiness-metric/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>Knowing how people think or feel about a certain subject can be very helpful in building trust, creating a team and to reveal impediments. Of course it can be very difficult to get team members to express their real opinions, especially when a team has just started. I think with patience and the right approach trust can (and will) be built and it will be easier to get real issues out in the open. The following practice can help you start this and make explicit what individual opinions in a team are. </p>
<p>I have used the following retrospective practice for several years now:</p>
<p><span id="more-1281"></span></p>
<p>Ask your team members to be quiet for 5 minutes and write down a number between 1 and 6 on a piece of paper. The 1 usually stands for &#8216;something too good to be true&#8217; and the 6 stand for &#8216;total disaster&#8217;. So if you want to find out how people feel about being part of a certain team, 1 stands for &#8216;I want to be on this team for the rest of my life&#8217; and 6 stands for &#8216;I want to get the hell out of here, today!&#8217;. Also ask them to think about what has to happen to get your score up by one point (so towards 1):</p>
<p>Mark: So Bob what is your score?<br />
Bob: A 5, our code base is a disaster.<br />
Mark: Ok, what has to happen to make this a 4?<br />
Bob: Time to refactor during the sprint.</p>
<p>Repeat this for all team members and write them in a histogram, it will look something like this:</p>
<p> <img src='http://blog.zilverline.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
1<br />
2 XXX<br />
3 X<br />
4<br />
5 XX<br />
6<br />
 <img src='http://blog.zilverline.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>What change would improve your score?</p>
<p>5: Code-base is a disaster -> Time to refactor during the sprint.<br />
3: Nice team, project can go much faster -> More automated testing for iPhone.<br />
2: Traveling to work takes a lot of time, everything else is very cool! -> 1 Day to work from home.<br />
etc.</p>
<p>I use writing down a score to avoid socially acceptable answers and use the silence to &#8216;force&#8217; members to think about their grade and what to change. It seems to be very difficult for groups to keep silent for 5 minutes, but I really try to make a point of this. While silent, people don&#8217;t get distracted and can give the topic serious thought. It usually takes some time before you have disconnected from your daily work and are taking a step back to think about your opinion.</p>
<p>The numbers 1 and 6 are usually exaggerations because we tend not to give a 1 or a 6 anyway. Also there is no &#8216;middle number&#8217;. If you would use 1 to 5, it is very safe to give a 3. Although this might be comfortable, I think it is better to be a little bit optimistic/pessimistic (e.g. 3 or 4 on a 6 point scale). </p>
<p>Remember that there is no good or bad score. The most important part is that the team members realize what is needed to give a better grade and the possibility for anyone to address these problems. The second most important part is that after this session, everyone knows what the opinion is of his or her colleagues, people will have a better understanding of each other.<br />
There are also no bad improvement suggestions. Even things that seem unrealistic or just silly have to be written down. Things that seem silly to one person, might be normal to another. Things that might be unrealistic now, might be attainable tomorrow. Respect people by taking every suggestion serious.</p>
<p>A weak point of this exercise is that it is difficult to keep everybody&#8217;s attention during the discussion. A suggestion of a colleague of mine, was to let team members ask the &#8216;What has to happen to improve your grade?&#8217; question to each other (still one at a time).</p>
<p>Of course you can use different themes, &#8216;How likely do you think it is that this functionality will delight our customers&#8217;, &#8216;Will this Scrum-thingy work in this organization&#8217;, etc.</p>
<p>Was this post helpful?<br />
1: Totally! I will never read another blog.<br />
6: Not at all, why didn&#8217;t my spam filter block this? </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zilverline.com/2012/08/21/how-to-fill-your-happiness-metric/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Event Sourcing &#8211; conflict resolution (part 4)</title>
		<link>http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/</link>
		<comments>http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/#comments</comments>
		<pubDate>Wed, 08 Aug 2012 20:14:47 +0000</pubDate>
		<dc:creator>Erik Rozendaal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[event sourcing]]></category>
		<category><![CDATA[memory image]]></category>
		<category><![CDATA[play framework]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.zilverline.com/?p=1220</guid>
		<description><![CDATA[After our deep dive into a Redis event store implementation we&#8217;re now getting back to actually adding functionality to the blogging application. Like the Getting started with Rails guide we&#8217;ll add the capability to add comments to blog posts. Adding this functionality is straightforward, but it will require us to look into resolving conflicts when <a href='http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>After our deep dive into a <a href="http://blog.zilverline.com/2012/07/30/simple-event-sourcing-redis-event-store-part-3/">Redis event store implementation</a> we&#8217;re now getting back to actually adding functionality to the blogging application. Like the <a href="http://guides.rubyonrails.org/getting_started.html">Getting started with Rails</a> guide we&#8217;ll add the capability to add comments to blog posts. Adding this functionality is straightforward, but it will require us to look into resolving conflicts when multiple people make modifications to the same blog post or comment concurrently.</p>
<p><span id="more-1220"></span></p>
<h3>Other Parts</h3>
<ul>
<li><a href="http://blog.zilverline.com/2012/07/04/simple-event-sourcing-introduction-part-1/">Part 1 &#8211; Introduction</a></li>
<li><a href="http://blog.zilverline.com/2012/07/23/simple-event-sourcing-consistency-part-2/">Part 2 &#8211; Consistency</a></li>
<li><a href="http://blog.zilverline.com/2012/07/30/simple-event-sourcing-redis-event-store-part-3/">Part 3 &#8211; Redis Event Store</a></li>
<li>Part 4 &#8211; Conflict Resolution</li>
<li><a href="http://blog.zilverline.com/2012/08/26/simple-event-sourcing-refactoring-and-transactions-part-5/">Part 5 &#8211; Refactoring and Transactions</a></li>
<li><a href="http://blog.zilverline.com/2013/01/09/simple-event-sourcing-users-authentication-authorization-part-6/">Part 6 &#8211; Users, Authentication, Authorization</a></li>
</ul>
<h2>Events</h2>
<p>To add the new functionality to our application we will first define the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-4/app/events/PostEvents.scala#L38">new events</a> and supporting data types. Notice that by focusing on the events we are actually thinking about the <em>behaviour</em> of the application first, instead of just the data model. In such a simple application as this blogging application it&#8217;s a rather subtle distinction, but when your application is more complicated events can help you to get a good understanding of what your application is supposed to <em>do</em>. Here are the event definitions for adding and deleting comments:</p>
<p>[code lang="scala"]<br />
case class CommentId(value: Int)<br />
object CommentId {<br />
  implicit val CommentIdFormat: Format[CommentId] = valueFormat(apply)(unapply)<br />
  implicit val CommentIdOrdering: Ordering[CommentId] = Ordering.by(_.value)<br />
}</p>
<p>case class CommentContent(commenter: String, body: String)</p>
<p>// [...]</p>
<p>sealed trait PostCommentEvent extends PostEvent {<br />
  def commentId: CommentId<br />
}<br />
case class CommentAdded(postId: PostId, commentId: CommentId, content: CommentContent) extends PostCommentEvent<br />
case class CommentDeleted(postId: PostId, commentId: CommentId) extends PostCommentEvent</p>
<p>object PostEvent {<br />
  // [...]</p>
<p>  implicit val CommentContentFormat: Format[CommentContent] = objectFormat("commenter", "body")(CommentContent.apply)(CommentContent.unapply)</p>
<p>  implicit val PostEventFormat: Format[PostEvent] = typeChoiceFormat(<br />
    "PostAdded"      -> objectFormat("postId", "content")(PostAdded.apply)(PostAdded.unapply),<br />
    "PostEdited"     -> objectFormat("postId", "content")(PostEdited.apply)(PostEdited.unapply),<br />
    "PostDeleted"    -> objectFormat("postId")(PostDeleted.apply)(PostDeleted.unapply),<br />
    "CommentAdded"   -> objectFormat("postId", "commentId", "content")(CommentAdded.apply)(CommentAdded.unapply),<br />
    "CommentDeleted" -> objectFormat("postId", "commentId")(CommentDeleted.apply)(CommentDeleted.unapply))<br />
}<br />
[/code]</p>
<p>Since comments are always part of a blog post, we&#8217;ll can use a simple sequential comment identifier. The first comment of a post will have id <code>CommentId(1)</code>, the second one <code>CommentId(2)</code>, etc. Obviously, we could also generate UUIDs for comments (and it would actually simplify the code), but by working with a sequential identifier we&#8217;ll slowly introduce some &#8220;domain&#8221; logic into our example application. We&#8217;ll also use <code>CommentId</code> as a key in a sorted map, so we need to define the <a href="http://www.scala-lang.org/api/current/index.html#scala.math.Ordering">Ordering</a> (line 4), which is based on the underlying numeric value.</p>
<p><code>CommentContent</code> is a simple container for the name of the commenter and the comment text.</p>
<p>The <code>CommentAdded</code> and <code>CommentDeleted</code> events are both subtypes of the <code>PostCommentEvent</code> trait, which in turn extends <code>PostEvent</code>. This makes it easier to distinguish comment related events which will be useful for resolving conflicts automatically.</p>
<p>To <a href="http://blog.zilverline.com/2012/07/30/simple-event-sourcing-redis-event-store-part-3/#serialization">store the events</a> in the event store, we also define and extend the necessary <code>Format</code> instances.</p>
<h2>Models</h2>
<p>Now that we have defined the new events, we can adjust our view models to keep track of comments as part of the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-4/app/models/Post.scala#L10">post</a> class:</p>
<p>[code lang="scala"]<br />
case class Post(<br />
  id: PostId,<br />
  revision: StreamRevision,<br />
  content: PostContent,<br />
  nextCommentId: CommentId = CommentId(1),<br />
  comments: SortedMap[CommentId, CommentContent] = SortedMap.empty)</p>
<p>case class Posts(byId: Map[PostId, Post] = Map.empty, orderedByTimeAdded: Seq[PostId] = Vector.empty) {<br />
  // [...]</p>
<p>  def update(event: PostEvent, revision: StreamRevision): Posts = event match {<br />
    // [...]<br />
    case CommentAdded(id, commentId, content) =><br />
      modify(id) { post =><br />
        post.copy(<br />
          revision = revision,<br />
          nextCommentId = CommentId(commentId.value + 1),<br />
          comments = post.comments.updated(commentId, content))<br />
      }<br />
    case CommentDeleted(id, commentId) =><br />
      modify(id) { post =><br />
        post.copy(<br />
          revision = revision,<br />
          comments = post.comments - commentId)<br />
      }<br />
  }</p>
<p>  private[this] def modify(id: PostId)(f: Post => Post) =<br />
    this.copy(byId = byId.updated(id, f(byId(id))))<br />
}<br />
[/code]</p>
<p>The <code>Post</code> class is modified to keep track of the next available <code>CommentId</code> (starting at 1) and also tracks the comments, using a <code>SortedMap</code><a href="#sorted-map" id="rev-sorted-map"><sup>[1]</sup></a> from CommentId to CommentContent. Remember that the representation of the view model is not tied to any kind of database schema, so we can easily change it whenever we need to. It will be automatically rebuild whenever we restart the application!</p>
<p>The <code>update</code> method also has to be changed to match against the new events and update the <code>Post</code> class accordingly. Updating nested, immutable data structures is a bit more involved than the mutable equivalent, so we use a simple helper method to take care of the first two levels of nesting. There are more <a href="http://dl.dropbox.com/u/7810909/media/doc/asymmetric-lenses.pdf">general solutions (PDF)</a> to this problem, but for now the <code>modify</code> method will do.</p>
<h2>Views and controller</h2>
<p>Next we add the required routes to <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-4/conf/routes">conf/routes</a>:</p>
<p>[code]<br />
POST    /posts/:postId/comments                    &#x23CE;<br />
  controllers.PostsController.comments.add(postId: PostId, r: StreamRevision)<br />
POST    /posts/:postId/comments/:commentId/delete  &#x23CE;<br />
  controllers.PostsController.comments.delete(postId: PostId, r: StreamRevision, commentId: CommentId)<br />
[/code]</p>
<p>Listing, deleting, and adding comments is all part of the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-4/app/views/posts/show.scala.html#L1">show.scala.html</a> template.</p>
<p>[code lang="html"]<br />
  <!-- ... --></p>
<p>  @if(post.comments.nonEmpty) {</p>
<h2>Comments</h2>
<p>    @for((commentId, commentContent) <- post.comments) {<br />
      <br/></p>
<div style="display: inline-block;">
<form class="form-inline" style="display: inline-block;" action="@routes.PostsController.comments.delete(post.id, post.revision, commentId)" method="POST">
<fieldset><button>&times;</button></fieldset>
</p></form>
<p>        &nbsp; @commentContent.body -- <em>by @commentContent.commenter</em>
      </div>
<p>    }<br />
  }</p>
<p>  <br/></p>
<h2>Add a comment:</h2>
<p>  @helper.form(action = routes.PostsController.comments.add(post.id, post.revision)) {<br />
    @globalErrorsPanel(form)<br />
    @conflictsMessagePanel(conflicts)</p>
<fieldset>
      @helper.inputText(form("commenter"), '_label -> "Commenter", 'required -> "required")<br />
      @helper.textarea(form("body"), '_label -> "Body", 'cols -> 80, 'rows -> 10)<br />
    </fieldset>
<fieldset>
      <button class="btn btn-primary">Submit</button><br />
    </fieldset>
<p>  }<br />
[/code]</p>
<p>As you can see, the template is quite straightforward. The only thing that might be unfamiliar is the invocation of the <code>conflictsMessagePanel</code> template, which we&#8217;ll get back to later.</p>
<p>Finally, we need to implement two new controller methods, which can be found in the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-4/app/controllers/PostsController.scala#L118">comments</a> singleton object inside of <code>PostsController</code>:</p>
<p>[code lang="scala"]<br />
  // [...]</p>
<p>  private[this] def withPost(postId: PostId)(found: Post => Result)(implicit request: Request[_]) = {<br />
    posts().get(postId).map(found).getOrElse(notFound(request))<br />
  }</p>
<p>  object comments {<br />
    val commentContentForm = Form(mapping(<br />
      "commenter" -> trimmedText.verifying(minLength(3)),<br />
      "body" -> trimmedText.verifying(minLength(3)))(CommentContent.apply)(CommentContent.unapply))</p>
<p>    def add(postId: PostId, expected: StreamRevision) = Action { implicit request =><br />
      withPost(postId) { post =><br />
        commentContentForm.bindFromRequest.fold(<br />
          formWithErrors => BadRequest(views.html.posts.show(post, formWithErrors)),<br />
          commentContent =><br />
            commit(expected, CommentAdded(postId, post.nextCommentId, commentContent))(<br />
              onCommit = Redirect(routes.PostsController.show(postId)).flashing("info" -> "Comment added."),<br />
              onConflict = (actual, conflicts) => Conflict(views.html.posts.show(post, commentContentForm.fill(commentContent), conflicts))))<br />
      }<br />
    }</p>
<p>    def delete(postId: PostId, expected: StreamRevision, commentId: CommentId) = Action { implicit request =><br />
      withPost(postId) { post =><br />
        def deletedResult = Redirect(routes.PostsController.show(postId)).flashing("info" -> "Comment deleted.")<br />
        post.comments.get(commentId) match {<br />
          case None => deletedResult<br />
          case Some(comment) =><br />
            commit(expected, CommentDeleted(postId, commentId))(<br />
              onCommit = deletedResult,<br />
              onConflict = (actual, conflicts) => Conflict(views.html.posts.show(post, commentContentForm, conflicts)))<br />
        }<br />
      }<br />
    }<br />
  }</p>
<p>  // [...]<br />
[/code]</p>
<p>The code is a bit more complicated than the basic post actions, since adding and deleting comments requires the presence of a blog post instance. The <code>withPost</code> helper method is used to read the required post from the memory image, or render a <code>404 Not Found</code> result if the post is not present.</p>
<p>In the case of the <code>add</code> method we then validate the submitted form (line 14). If incorrect, we rerender the page with the error messages (line 15). Otherwise we commit a new <code>CommentAdded</code> event using the provided content and the next available comment id (line 17). If the commit succeeds without conflict, we redirect the user to the blog post with the added comment (line 18). In case there is a conflict, we rerender the form but add some information on the conflicts that occurred (line 19). The delete action is very similar.</p>
<p>This basically completes the addition of some new functionality. The effort required is comparable to a traditional database backed application, which is good to know. But there is one fly in the ointment we need to fix before we can call it a day&#8230;</p>
<h2>Conflict resolution</h2>
<p>Now that we can add comments to post you&#8217;ll soon discover that multiple concurrent users will quickly get a conflict. In <a href="http://blog.zilverline.com/2012/07/23/simple-event-sourcing-consistency-part-2/#posts-controller">part 2</a> we added conflict detection and rendered a placeholder page whenever a conflict occurred. Now that conflicts are more likely to occur, we need to be a bit smarter about resolving these conflicts.</p>
<p>The basic idea is that comment events usually do not conflict, even when they apply to the same post and therefore the same event stream. We can capture this knowledge in a <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-4/app/events/PostEvents.scala#L55">method</a> like this:</p>
<p>[code lang=scala]<br />
  def conflictsWith(committed: PostEvent, attempted: PostEvent) =<br />
    (committed, attempted) match {<br />
      case (a: PostCommentEvent, b: PostCommentEvent) => a.commentId == b.commentId<br />
      case (_: PostCommentEvent, _)                   => false<br />
      case _                                          => true<br />
    }<br />
[/code]</p>
<p>This method states that any two <code>PostCommentEvent</code>s only conflict when they affect the same comment (line 3). Furthermore, any new blog post related event does not conflict with an already committed <code>PostCommentEvent</code> (line 4). This allows the author to edit the blog post without getting conflicts on added or removed comments. Any other event combination is considered to conflict (line 5). So if you add a comment while someone edited the post, the system will give you a warning and ask you to resubmit your comment, if it is still applicable.</p>
<p>Notice that the main goal of this method is to decide whether we should ask the user for confirmation when a conflict occurs, or whether we should just proceed with the commit even though changes were made while the user was working on their request. This is rather subjective and the details will vary depending on your domain, your users, etc.</p>
<p>So now that we have a way to decide if two events conflict or not we need to modify our commit method to take this into account. This method is defined in the <code>PostsController</code>:</p>
<p>[code lang=scala]<br />
  /**<br />
   * Commits an event and applies it to the current state. If successful the<br />
   * provided `onCommit` callback is invoked and its result returned. Otherwise<br />
   * the `onConflict` is callback is invoked and its result returned.<br />
   */<br />
  private[this] def commit<br />
        (expected: StreamRevision, event: PostEvent)<br />
        (onCommit: => Result,<br />
         onConflict: (StreamRevision, Seq[PostEvent]) => Result): Result = {<br />
    def resolveConflict(committed: Seq[PostEvent], attempted: PostEvent) = {<br />
      val conflicting = committed.filter(PostEvent.conflictsWith(_, attempted))<br />
      if (conflicting.isEmpty) Right(attempted)<br />
      else Left(conflicting)<br />
    }</p>
<p>    @tailrec def run(expected: StreamRevision, event: PostEvent): Result =<br />
      memoryImage.tryCommit(event.postId.toString, expected, event) match {<br />
        case Right(commit) =><br />
          onCommit<br />
        case Left(conflict) =><br />
          resolveConflict(conflict.conflicting.flatMap(_.events), event) match {<br />
            case Right(event)      => run(conflict.actual, event)<br />
            case Left(conflicting) => onConflict(conflict.actual, conflicting)<br />
          }<br />
      }</p>
<p>    run(expected, event)<br />
  }<br />
[/code]</p>
<p>The new <code>commit</code> method implementation first defines a <code>resolveConflict</code> helper method (line 10), which takes a list of already committed, potentially conflicting events and uses the <code>PostEvent.conflictsWith</code> method to see if there are any real conflicts. If there are none, the attempted event is returned. Otherwise the conflicting events are returned.</p>
<p>The <code>run</code> method (line 16) runs in a (tail-recursive) loop. It tries to commit against the expected revision of the event stream. If there is no conflict, it returns the result of the provided <code>onCommit</code> callback. Otherwise it tries to resolve the conflicts. If this succeeds, it tries invokes itself again<a href="#retries" id="rev-retries"><sup>[2]</sup></a>, but now with the latest known event stream revision as the expected revision. If the conflict cannot be resolved, the result of the <code>onConflict</code> callback is returned instead.</p>
<p>Finally, line 27 simply kicks off the entire process using the provided revision and event.</p>
<p>With this in place actual conflicts should be quite rare. But we can still do better than just showing a generic &#8220;there was a conflict&#8221; error page. This is the job of the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-4/app/views/posts/conflictsMessagePanel.scala.html">conflictsMessagePanel.scala.html</a> template. This template shows a human readable version of the conflicts that occurred:</p>
<p><a href="http://blog.zilverline.com/wp-content/uploads/2012/08/Screen-Shot-2012-08-07-at-20.40.05-.png"><img src="http://blog.zilverline.com/wp-content/uploads/2012/08/Screen-Shot-2012-08-07-at-20.40.05-.png" alt="Conflict alert panel" title="Conflicts" width="1140" height="166" class="size-full wp-image-1225" /></a></p>
<h2>Summary</h2>
<p>Besides adding support for conflict resolution, the implementation of the blog post comment functionality was quite straightforward. In a blogging application conflicts are probably quite rare, so it may not make sense to build in extensive UI support for this, but having this as an example hopefully gives you some idea of what is possible. In more collaborative applications this kind of functionality is much more interesting and you may prefer to immediately push updates directly to the client, instead of waiting for the client to submit a form. An example of this is <a href="http://www.pivotaltracker.com">Pivotal Tracker</a> or <a href="http://incubator.apache.org/wave/">Apache Wave</a>.</p>
<p>If your application has extreme availability requirements, similar conflict resolution can also help you deal with recovering from network partitioning. Or applications that need to be able to run in disconnected mode. In these cases you will need to write an event stream function that merges divergent event streams. Version control systems are examples of this, and can be a source of inspiration, although they usually don&#8217;t have intention revealing events to work with.</p>
<p>But the main point is that the level of conflict resolution you need depends on your users and your application. Event sourcing gives you a great tool to make conflicts easier to resolve, without necessarily complicating your application if you do not need this kind of functionality.</p>
<p>In the next part we&#8217;ll take a look at another kind of concurrency conflict that can occur in the current application when two or more users committing events to the same event stream nearly simultaneously.</p>
<style>
#footnotes {
  font-size: smaller;
}
span:target {
  background-color: #F0F0F0;
}
</style>
<div id="footnotes">
<p>Footnotes:</p>
<p><span id="sorted-map"><a href="#rev-sorted-map">[1]</a> The immutable <code>SortedMap</code> implementation is rather <a href="https://issues.scala-lang.org/browse/SI-5331">broken</a> before Scala <a href="https://github.com/scala/scala/pull/82">2.10</a>, but it&#8217;ll probably do for this example.</span></p>
<p><span id="retries"><a href="#rev-retries">[2]</a> In practice you should put some limit on the number of retries, to avoid bugs causing infinite loops.</span></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple event sourcing &#8211; Redis event store (part 3)</title>
		<link>http://blog.zilverline.com/2012/07/30/simple-event-sourcing-redis-event-store-part-3/</link>
		<comments>http://blog.zilverline.com/2012/07/30/simple-event-sourcing-redis-event-store-part-3/#comments</comments>
		<pubDate>Mon, 30 Jul 2012 20:54:08 +0000</pubDate>
		<dc:creator>Erik Rozendaal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[event sourcing]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[memory image]]></category>
		<category><![CDATA[play framework]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.zilverline.com/?p=1075</guid>
		<description><![CDATA[In the previous part we developed a fake event store for our blogging application. This event store just kept all events in-memory, making it unsuitable for production use. But it did allow us to adapt our application to using an event store, and let us work out the details of the event store interface without <a href='http://blog.zilverline.com/2012/07/30/simple-event-sourcing-redis-event-store-part-3/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>In the <a href="http://blog.zilverline.com/2012/07/23/simple-event-sourcing-consistency-part-2/">previous part</a> we developed a fake event store for our blogging application. This event store just kept all events in-memory, making it unsuitable for production use. But it did allow us to adapt our application to using an event store, and let us work out the details of the event store interface without having to worry about an actual persistence mechanism.</p>
<p>In this part we&#8217;ll develop an event store implementation on top of <a href="http://redis.io/">Redis</a>, a key-value store with support for additional data structures (such as lists and hashes), publish/subscribe, and transactions. If you&#8217;re more interested in adding new application functionality, you can safely skip this part and go to <a href="http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/">part 4 &#8211; conflict resolution</a>.</p>
<p><span id="more-1075"></span></p>
<h3>Other Parts</h3>
<ul>
<li><a href="http://blog.zilverline.com/2012/07/04/simple-event-sourcing-introduction-part-1/">Part 1 &#8211; Introduction</a></li>
<li><a href="http://blog.zilverline.com/2012/07/23/simple-event-sourcing-consistency-part-2/">Part 2 &#8211; Consistency</a></li>
<li>Part 3 &#8211; Redis Event Store</li>
<li><a href="http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/">Part 4 &#8211; Conflict Resolution</a></li>
<li><a href="http://blog.zilverline.com/2012/08/26/simple-event-sourcing-refactoring-and-transactions-part-5/">Part 5 &#8211; Refactoring and Transactions</a></li>
<li><a href="http://blog.zilverline.com/2013/01/09/simple-event-sourcing-users-authentication-authorization-part-6/">Part 6 &#8211; Users, Authentication, Authorization</a></li>
</ul>
<h2>Running the application</h2>
<p>So we already have an <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/test/eventstore/EventStoreSpec.scala#L139">event store specification</a> and a <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/app/eventstore/fake/FakeEventStore.scala#L19">reference implementation</a>. Implementing a Redis-based event store should be pretty straight forward, right? Well, it mostly is, but before we dive into the details let&#8217;s take a look at what we&#8217;ll be able to do once everything is up and running.</p>
<p>First, make sure you have a working Redis installation (you can use the <code>redis-cli</code> command to connect to Redis and check if everything is working). Redis 2.6 introduced support for server-side Lua scripts which the event store implementation uses automatically if available. Otherwise, the event store falls back to using the <a href="http://redis.io/topics/transactions">WATCH/MULTI/EXEC</a> commands, so Redis 2.4 also works, although with a performance penalty when it comes to committing events.
<p class="note">If you use a Mac OS X with <a href="">homebrew</a> you can simple run <code>brew install redis</code> to install 2.4.15 or <code>brew install redis --devel</code> to install 2.6.0-RC5.</p>
<p>Now checkout the <code>part-3</code> branch of <a href="https://github.com/zilverline/event-sourced-blog-example.git">the project</a>, adopt <code>conf/application.conf</code> to match your Redis configuration, and start the application using <code>play run</code> or <code>sbt run</code>. Any blog posts you add, edit, or delete will now have the resulting events committed to Redis. If you restart the application the events are replayed on start-up, so you will no longer lose your data.</p>
<p>Start the Scala console (<code>play console</code> or <code>sbt console</code>) and paste the following Scala code (adjust the connection settings as needed).</p>
<p>[code lang=scala]<br />
import events._, eventstore._<br />
val es = redis.RedisEventStore[PostEvent]("blog", "localhost", 6379)<br />
es.publisher.subscribe(StoreRevision.Initial)(println)<br />
[/code]</p>
<p>This code connects to the event store and subscribes the <code>println</code> method. You&#8217;ll see that first all historical commits are printed, and as you use the application you&#8217;ll notice that new commits are immediately printed as well. This is one of the major differences with more traditional database backed systems: an event sourced system is <em>open for extension</em> and can communicate to the outside world what is going on right now, while a database based system is usually much more of a black box.</p>
<p>Now start a second instance of the application (<code>play 'run 9001'</code> or <code>sbt 'run 9001'</code>). This second instance also connects to the event store to replay and subscribe to the events being committed. The memory images of both instances stay synchronized as you make changes, while the Scala console keeps printing events as they are committed (make sure you close the event store before exiting the console, otherwise the process stays connected and will fail to terminate). The picture below shows the general setup:</p>
<p><img src="http://blog.zilverline.com/wp-content/uploads/2012/07/event-store.png" alt="" title="Event store" width="452" height="269" class="alignnone size-full wp-image-1272 replace-2x" /></p>
<p>So by using the Redis event store we can run multiple instances of the application, each running on a different server. Each commit from any of the application servers is pushed to all connected servers. This provides fault-tolerance and scalability at the application server level, and also makes it possible to upgrade application servers without affecting users (by doing rolling updates).</p>
<h2 id="serialization">Event serialization</h2>
<p>When data is stored in an external system we need to define some kind of serialization format. There is a wide variety of serialization formats and libraries available. For this example application we&#8217;ll use JSON. This may not be the most compact or fastest format, but is easy to use and read, which is great for this example. Play! defines the <a href="https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/play/api/libs/json/Format.scala">Format</a> trait for JSON serialization. We just have to provide an implementation of this trait for each data type that we want to serialize.</p>
<p>Although Play! predefines various JSON format implementations for many of the standard Scala classes, it does not include any helpers to make it easy to define formats for our own types. But it is easy to supply our own helpers, which you can find in the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/app/support/JsonMapping.scala#L6">JsonMapping</a> object.</p>
<p>The <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/app/support/JsonMapping.scala#L58"><code>objectFormat</code></a> methods are used to define the JSON format for case classes such as <code>PostContent</code>:</p>
<p>[code lang=scala]<br />
  implicit val PostContentFormat: Format[PostContent] =<br />
    objectFormat("author", "title", "body")(PostContent.apply)(PostContent.unapply)<br />
[/code]</p>
<p>Here we simply list the JSON field names and the Scala generated <code>apply</code> and <code>unapply</code> methods to construct and deconstruct <code>PostContent</code> instances. Formats for class hierarchies like <code>PostEvent</code> are a bit more difficult to get right, but easy enough to define with the <code>typeChoiceFormat</code> helper:</p>
<p>[code lang=scala]<br />
  implicit val PostEventFormat: Format[PostEvent] = typeChoiceFormat(<br />
    "PostAdded"   -> objectFormat("postId", "content")(PostAdded.apply)(PostAdded.unapply),<br />
    "PostEdited"  -> objectFormat("postId", "content")(PostEdited.apply)(PostEdited.unapply),<br />
    "PostDeleted" -> objectFormat("postId")(PostDeleted.apply)(PostDeleted.unapply))<br />
[/code]</p>
<p>This may not be quite as convenient as using a reflection based serialization library, but avoids having to deal with various JVM class loader problems and other reflection related troubles.</p>
<p>The formats are defined as <code>implicit</code> values in the companion objects of the classes that we want to serialize so that the Scala compiler can find these formats and automatically pass them as parameters where needed, so that we don&#8217;t have to. (Yes, the type &#8220;checker&#8221; is filling in the blanks for you.)</p>
<p>Writing tests for correct serialization and deserialization is rather tedious, but very important. Fortunately, <a href="https://github.com/rickynils/scalacheck">ScalaCheck</a> makes writing these kind of tests almost trivial:</p>
<p>[code lang=scala]<br />
  "Post events" should {<br />
    "convert to and from JSON" in forAll(eventsForMultiplePosts.arbitrary) { events =><br />
      Json.fromJson[List[PostEvent]](Json.toJson(events)) must_== events<br />
    }</p>
<p>    "parse example Post Added event" in {<br />
      val event = PostAdded(PostId(UUID.fromString("5ab11526-477b-43b9-8fe6-4bb25a3dfcc6")), PostContent(author = "Author", title = "Title", body = "Body"))<br />
      val json = """{"type":"PostAdded","data":{"postId":"5ab11526-477b-43b9-8fe6-4bb25a3dfcc6","content":{"author":"Author","title":"Title","body":"Body"}}}"""</p>
<p>      Json.fromJson[PostEvent](Json.parse(json)) must_== event<br />
    }<br />
  }<br />
[/code]</p>
<p>For the serialization tests I typically test using a few examples, but the main testing happens by defining the <em>property</em> <code>fromJson(toJson(events)) == events</code>. ScalaCheck will randomly generate data to verify that this property holds, so we can be confident serialization works as expected.</p>
<h2>The Redis event store implementation</h2>
<p>The Redis event store implementation uses the <a href="https://github.com/xetorthio/jedis">Jedis</a> client library. For reading and committing we use a <code>JedisPool</code> connection pool. To use a connection and return it to the pool we&#8217;ve implemented the <a href="http://books.google.nl/books?id=MFjNhTjeQKkC&#038;lpg=PA170&#038;ots=FLphXNHMou&#038;dq=loan%20pattern%20programming&#038;hl=nl&#038;pg=PA170#v=onepage&#038;q=loan%20pattern%20programming&#038;f=false">loan pattern</a> in the <code>withJedis</code> method:</p>
<p>[code lang=scala]<br />
class RedisEventStore[Event]<br />
  (name: String, host: String, port: Int, config: Config)<br />
  (implicit val eventFormat: Format[Event])<br />
extends EventStore[Event] {</p>
<p>  val jedisPool = new JedisPool(config, host, port)</p>
<p>  def withJedis[A](f: Jedis => A): A = {<br />
    val jedis = jedisPool.getResource<br />
    try {<br />
      f(jedis)<br />
    } finally {<br />
      jedisPool.returnResource(jedis: BinaryJedis)<br />
    }<br />
  }</p>
<p>  // [...]<br />
}<br />
[/code]</p>
<h3>Reading events</h3>
<p>To store the commits we&#8217;ll use two Redis data structures:</p>
<ol>
<li>A Redis <a href="http://redis.io/commands#hash">hash</a> that maps commit ids to serialized commits. The commit id is equal to the commits store revision. We use a hash instead of a list as Redis lists are <a href="http://redis.io/commands/lindex">O(n)</a> when accessing an arbitrary element, while hashes provide <a href="http://redis.io/commands/hget">O(1)</a> random element access.</li>
<li>One Redis <a href="http://redis.io/commands#list">list</a> for each event stream. Each element in the list is a commit id.</li>
</ol>
<p>This means that if we want to read all past commits we simply need to read each hash value starting with commit id 1 up to the commit id equal to the size of the hash. The code for this can be found in the reader implementation of <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/app/eventstore/redis/RedisEventStore.scala#L102">RedisEventStore.scala</a>:</p>
<p>[code lang=scala]<br />
override object reader extends CommitReader[Event] {<br />
  override def storeRevision = withJedis { jedis =><br />
    StoreRevision(jedis.hlen(CommitsKey))<br />
  }</p>
<p>  override def readCommits(since: StoreRevision, to: StoreRevision) = {<br />
    val current = storeRevision<br />
    if (since >= current) Stream.empty else {<br />
      val revisionRange = (since.value + 1) to (to.value min current.value)<br />
      doReadCommits(revisionRange.map(_.toString))<br />
    }<br />
  }</p>
<p>  // [...]<br />
}<br />
[/code]</p>
<p>The current store revision is equal to the size of the commits hash (<a href="http://redis.io/commands/hlen">HLEN</a>). The commits are all read in order using the <code>doReadCommits</code> method:</p>
<p>[code lang=scala]<br />
val KeyPrefix = name + ":"<br />
val CommitsKey: String = KeyPrefix + "commits"</p>
<p>def doReadCommits(commitIds: Seq[String]): Stream[Commit[Event]] = {<br />
  val chunks = commitIds.grouped(ChunkSize).map(_.toArray)<br />
  chunks.flatMap { chunk =><br />
    val serializedCommits = withJedis { _.hmget(CommitsKey, chunk: _*) }<br />
    serializedCommits.asScala.par.map(deserializeCommit)<br />
  }.toStream<br />
}</p>
<p>def deserializeCommit(serialized: String) =<br />
  Json.fromJson[Commit[Event]](Json.parse(serialized))<br />
[/code]</p>
<p>The list of commit ids to read is first split into <em>chunks</em>, each at most <code>ChunkSize</code> (10,000) elements in size. Each chunk is then read using a single <em>get multiple values from hash</em> (<a href="http://redis.io/commands/hmget">HMGET</a>) command. By reading many commits at a time we greatly reduce the number of process context switches. The commits are then deserialized using the <code>deserializeCommit</code> method, which is just a simple wrapper around the Play! JSON deserializer. Since deserialization is a CPU intensive process we use Scala&#8217;s parallel collections (<code>par</code>) to get a  nice speed-up on a multi-core machine.</p>
<p>The resulting commits are then transformed into a lazy <code>Stream</code>. This way we do not have to read all the commits into memory at once, but we&#8217;ll read them on-demand, one chunk at a time.</p>
<h3>Committing</h3>
<p>There are two implementations of the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/app/eventstore/EventStore.scala#L83">EventCommitter</a> interface. The <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/app/eventstore/redis/RedisWatchMultiExecEventCommitter.scala#L10">RedisWatchMultiExecEventCommitter</a> uses the Redis <code>WATCH/MULTI/EXEC</code> commands and is compatible with Redis 2.4 and higher. But it&#8217;s slower and more complicated than the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/app/eventstore/redis/RedisLuaEventCommitter.scala#L10">RedisLuaEventCommitter</a> that we&#8217;ll describe below.</p>
<p>The <code>RedisLuaEventCommitter</code> uses a <a href="http://www.lua.org">Lua</a> script to atomically commit events in Redis. The script is fairly similar to the commit implementation of the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/app/eventstore/fake/FakeEventStore.scala#L45">FakeEventStore</a>:</p>
<p>[code lang=scala]<br />
trait RedisLuaEventCommitter[Event] { this: RedisEventStore[Event] =><br />
  val TryCommitScript: String = """<br />
    | local commitsKey = KEYS[1]<br />
    | local streamKey = KEYS[2]<br />
    | local timestamp = tonumber(ARGV[1])<br />
    | local streamId = ARGV[2]<br />
    | local expected = tonumber(ARGV[3])<br />
    | local events = ARGV[4]<br />
    |<br />
    | local actual = tonumber(redis.call('llen', streamKey))<br />
    | if actual ~= expected then<br />
    |   return {'conflict', tostring(actual)}<br />
    | end<br />
    |<br />
    | local storeRevision = tonumber(redis.call('hlen', commitsKey))<br />
    | local commitId = storeRevision + 1<br />
    | local commitData = string.format('{"storeRevision":%d,"timestamp":%d,"streamId":%s,"streamRevision":%d,"events":%s}',<br />
    |   commitId, timestamp, cjson.encode(streamId), actual + 1, events)<br />
    |<br />
    | redis.call('hset', commitsKey, commitId, commitData)<br />
    | redis.call('rpush', streamKey, commitId)<br />
    | redis.call('publish', commitsKey, commitData)<br />
    |<br />
    | return {'commit', tostring(commitId)}<br />
    """.stripMargin</p>
<p>  // [...]<br />
}<br />
[/code]</p>
<p>The <code>RedisLuaEventCommitter</code> must be mixed into the <code>RedisEventStore</code> class, so we specify this using a <a href="http://stackoverflow.com/a/1991786">self type</a> (line 1). This gives us access to all <code>RedisEventStore</code> methods.</p>
<p>The first section of the Lua script reads the command arguments (line 3-8). The second section checks if the expected stream revision matches the actual stream revision. If not, a conflict is returned (line 10-13).</p>
<p>Otherwise the current store revision is determined. The commit id is set to the next store revision and the serialized commit JSON is generated (line 15-18). Then three commands are executed to:</p>
<ol>
<li>Store the serialized commit in the commits hash (line 20).</li>
<li>The <code>commitId</code> is appended to the commits of the affected stream (line 21).</li>
<li>The serialized commit is published to all subscribers (line 22). The name of the publication channel is the same as the key for the commits hash, but this is a rather arbitrary implementation detail.</li>
</ol>
<p>When starting the event store the above Lua script is uploaded to Redis (using <a href="http://redis.io/commands/script-load">SCRIPT LOAD</a>) and the resulting script identifier (the SHA-1 hash of the script contents) is saved:</p>
<p>[code lang=scala]<br />
  val TryCommitScriptId = withJedis { _.scriptLoad(TryCommitScript) }<br />
[/code]</p>
<p>The implementation of the <code>tryCommit</code> is now quite straightforward. We simple need to invoke the Lua script using the <a href="http://redis.io/commands/evalsha">EVALSHA</a> command and translate the results to Scala:</p>
<p>[code lang=scala]<br />
  object committer extends EventCommitter[Event] {<br />
    override def tryCommit(streamId: String, expected: StreamRevision, event: Event): CommitResult[Event] = {<br />
      // Prepare parameters.<br />
      val timestamp = DateTimeUtils.currentTimeMillis<br />
      val serializedEvents = Json.stringify(Json.toJson(Seq(event))(Writes.seqWrites(eventFormat)))</p>
<p>      // Invoke Lua script.<br />
      val response = withJedis { _.evalsha(TryCommitScriptId, 2,<br />
        /* KEYS */ CommitsKey, keyForStream(streamId),<br />
        /* ARGV */ timestamp.toString, streamId, expected.value.toString, serializedEvents)<br />
      }</p>
<p>      // Parse response.<br />
      try {<br />
        response.asInstanceOf[java.util.List[_]].asScala match {<br />
          case Seq("conflict", actual: String) =><br />
            val conflicting = reader.readStream(streamId, since = expected)<br />
            Left(Conflict(streamId, StreamRevision(actual.toLong), expected, conflicting))<br />
          case Seq("commit", storeRevision: String) =><br />
            Right(Commit(StoreRevision(storeRevision.toLong), timestamp, streamId, expected.next, Seq(event)))<br />
        }<br />
      } catch {<br />
        case e: Exception =><br />
          throw new EventStoreException("Error parsing response from Redis TryCommit script: " + response, e)<br />
      }<br />
    }<br />
  }<br />
[/code]</p>
<h3>Subscriptions</h3>
<p>Redis has built-in support for <a href="http://redis.io/commands#pubsub">publish/subscribe</a>, which we will use to notify each subscriber when new events are committed. But before we actually subscribe to Redis, we first need to replay historical commits. The reason to not subscribe before replaying is that replaying might take some time (if you have many historical commits) and we do not want any new commits to queue-up while we&#8217;re still processing the older commits:</p>
<p>[code lang=scala]<br />
  override object publisher extends CommitPublisher[Event] {<br />
    import reader._</p>
<p>    override def subscribe(since: StoreRevision)(listener: CommitListener[Event]): Subscription = {<br />
      @volatile var cancelled = false<br />
      @volatile var last = since<br />
      val unsubscribeToken = UUID.randomUUID.toString</p>
<p>      executor.execute(new Runnable {<br />
        private def replayCommitsTo(to: StoreRevision) {<br />
          if (last < to) {<br />
            Logger.info("Replaying commits since " + last + " to " + to)<br />
            readCommits(last, to).takeWhile(_ => !closed &#038;&#038; !cancelled).foreach(listener)<br />
            last = to<br />
          }<br />
        }<br />
      // [...]<br />
      }<br />
    }<br />
  }<br />
[/code]</p>
<p>The <code>last</code> variable contains the store revision of the last commit we passed to the listener so far. It is initialized to the <code>since</code> parameter. We then start a new thread to perform the actual commit replay and Redis subscription. This is consistent with our fake event store implementation.</p>
<p>The <code>cancelled</code> flag is used to help with unsubscribing when the subscription is cancelled. In addition to this, the <code>unsubscribeToken</code> is sent to the subscriber as well so that the subscriber wakes up when required.</p>
<p>After replaying (line 7 below) the historical commits we perform the actual subscription using a new Redis connection (line 12):</p>
<p>[code lang=scala]<br />
        override def run {<br />
          val currentRevision = storeRevision<br />
          if (last > currentRevision) {<br />
            Logger.warn("Last " + last + " is in the future, resetting it to current " + currentRevision)<br />
            last = currentRevision<br />
          } else {<br />
            replayCommitsTo(currentRevision)<br />
          }</p>
<p>          val jedis = new Jedis(host, port)<br />
          try {<br />
            jedis.subscribe(Subscriber, ControlChannel, CommitsKey)<br />
          } finally {<br />
            jedis.disconnect<br />
          }<br />
        }<br />
[/code]</p>
<p>The <code>Subscriber</code> object implements the <code>JedisPubSub</code> interface:</p>
<p>[code lang=scala]<br />
        object Subscriber extends JedisPubSub {<br />
[/code]</p>
<p>When the subscription is confirmed we check if we missed any commits that happened while we were performing the initial replay, but before completing the subscription:</p>
<p>[code lang=scala]<br />
          override def onSubscribe(channel: String, subscribedChannels: Int) = channel match {<br />
            case ControlChannel =><br />
              // We may have missed the cancellation token while subscribing, so check the flag.<br />
              if (closed || cancelled) unsubscribe<br />
            case CommitsKey =><br />
              // We may have missed some commits while subscribing, so replay missing if needed.<br />
              replayCommitsTo(storeRevision)<br />
            case _ =><br />
              Logger.warn("message received on unknown channel '" + channel + "'")<br />
          }<br />
[/code]</p>
<p>Once we&#8217;re subscribed we&#8217;ll receive a notification for each commit:</p>
<p>[code lang=scala]<br />
          override def onMessage(channel: String, message: String) = channel match {<br />
            case ControlChannel =><br />
              if (message == CloseToken || message == unsubscribeToken) {<br />
                unsubscribe<br />
              }<br />
            case CommitsKey =><br />
              val commit = deserializeCommit(message)<br />
              if (last.next < commit.storeRevision) {<br />
                Logger.warn("missing commits since " + last + " to " + commit.storeRevision + ", replaying...")<br />
                replayCommitsTo(commit.storeRevision)<br />
              } else if (last.next == commit.storeRevision) {<br />
                listener(commit)<br />
                last = commit.storeRevision<br />
              } else {<br />
                Logger.warn("Ignoring old commit " + commit.storeRevision + ", since we already processed everything up to " + last)<br />
              }<br />
            case _ =><br />
              Logger.warn("message received on unknown channel '" + channel + "'")<br />
          }<br />
[/code]</p>
<p>We deserialize and forward any new commit to the listener. In some case we may receive old commits, which we ignore. In case we miss any commits, we replay the missing ones. The control channel is monitored for event store closing or subscription cancellation messages.</p>
<p>This completes the implementation of the Redis event store. For full details, check the <a href="https://github.com/zilverline/event-sourced-blog-example/tree/part-3/app/eventstore/redis">eventstore.redis</a> package.</p>
<h3>Setup and configuration</h3>
<p>Now that we have multiple event store implementations we need to provide some configuration options. This is done in the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/app/controllers/Global.scala#L11">controllers.Global</a> object in the <code>onStart</code> method. The actual configuration can be found in the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/conf/application.conf#L33">conf/application.conf</a> file.</p>
<p>Since our event store exposes a blocking API (for ease of implementation) we also increase the Play! thread-pool sizes<a href="#async" id="rev-async"><sup>[1]</sup></a>.</p>
<h3>Redis durability</h3>
<p>The primary concern of an event store is to ensure events are written to durable storage, so no events get lost. To configure Redis for <a href="http://redis.io/topics/persistence">maximum reliability of written data</a> we need to edit the <code>redis.conf</code> file to:</p>
<ul>
<li>Turn off saving snapshots by commenting out all the <code>save</code> settings.</li>
<li>Turn on appendonly mode (<code>appendonly yes</code>).</li>
<li><a href="http://en.wikipedia.org/wiki/Sync_(Unix)">Sync to disk</a> on every transaction by setting <code>appendfsync always</code>.
<li>(Optional) disable automatic rewriting of the append-only file (<code>auto-aof-rewrite-percentage 0</code>). We&#8217;ll only ever be adding new data to Redis, so rewriting will not result in a smaller append-only file anyway.</li>
</ul>
<h2>Performance</h2>
<p>To test and compare the performance of the various event store implementations the following setup was used:</p>
<ul>
<li>The application and Redis run on <code>deler</code> (dutch for denominator), a 2010 dual-core 2.66GHz Core i7 with hyper-threading and a 7200-RPM HDD. The server was started using the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/start-server.sh">start-server.sh</a> script.</li>
<li>The performance test client runs on <code>noemer</code> (dutch for numerator), a 2012 quad-core 2.6GHz Core i7 with hyper-threading. The benchmark was started using the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/run-benchmark.sh">run-benchmark.sh</a> script with the following arguments: <code>http://deler.local:9000 50 500000</code>.</li>
</ul>
<p>The server and client machine were directly connected with a 1 Gigabit ethernet cable and were otherwise idle. This setup ensures that neither the client nor the network was a bottleneck so that we can fully test the performance of the server code. Both computers were running the <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java SE 7u5 JDK</a>.</p>
<p>The <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-3/test/performance/PerformanceTest.scala#L148">benchmark</a> itself consists of a warmup phase (running 20,000 iterations of adding/listing/editing/reading/deleting blog posts) to ensure the server JVM has ample opportunity to optimize the generated code, and then 50 concurrent clients completed a total of 500,000 iterations (100,000 iterations for the Redis WATCH/MULTI/EXEC event store implementation) of benchmarking. Note that this is a <em>full-stack</em> benchmark, including the network, HTTP protocol, request routing, the event store, and template rendering.</p>
<p>The results are summarized below (first number is total iterations per second, second number is the 99th percentile latency in milliseconds):</p>
<style>
.benchmark table {
border-collapse:collapse;
margin-bottom:1.5em;
}
.benchmark table, .benchmark th, .benchmark td
{
  border: 1px solid black;
  padding: 5px;
}
.benchmark th {
  background-color: #f0f0f0;
  text-align: left;
}
.benchmark td {
  text-align: center;
}
</style>
<div class="benchmark">
<table width=100% border-width=1>
<thead>
<tr>
<th>Event store</th>
<th style="text-align:center;">None</th>
<th style="text-align:center;">Fake</th>
<th style="text-align:center;">Redis Lua</th>
<th style="text-align:center;">Redis WME</th>
</tr>
<tr>
<th>Iterations</th>
<td colspan=3>500,000</td>
<td>100,000</td>
</thead>
<tbody>
<tr>
<th>Add post (<small>POST</small>)</th>
<td>5330.5/s (47 ms)</td>
<td>5154.2/s (59 ms)</td>
<td>2617.4/s (64 ms)</td>
<td>583.7/s (762 ms)</td>
</tr>
<tr>
<th>Read post (<small>GET</small>)</th>
<td>8284.1/s (31 ms)</td>
<td>8176.1/s (32 ms)</td>
<td>6755.8/s (36 ms)</td>
<td>6641.7/s (38 ms)</td>
</tr>
<tr>
<th>Edit post (<small>GET-POST-GET</small>)</th>
<td>2145.0/s (55 ms)</td>
<td>1845.1/s (64 ms)</td>
<td>1178.7/s (96 ms)</td>
<td>359.5/s (511 ms)</td>
</tr>
<tr>
<th>List posts (<small>GET</small>)</th>
<td>1290.8/s (98 ms)</td>
<td>1259.6/s (147 ms)</td>
<td>1174.7/s (155 ms)</td>
<td>1165.5/s (150 ms)</td>
</tr>
<tr>
<th>Java memory usage</th>
<td>700 MB</td>
<td>1750 MB</td>
<td>700 MB</td>
<td>&nbsp;</td>
</tr>
<tr>
<th>Redis memory usage</th>
<td>N/A</td>
<td>N/A</td>
<td>958 MB</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
</div>
<p>As you can see there is only a slight difference between having no event store at all or the fake event store. The switch to the Lua-based Redis event store causes a much bigger change. Committing events now requires us to wait for a disk write, and also adds the overhead of process switching and commit serialization and deserialization. Still, the performance is not bad at all. When falling back to the WATCH/MULTI/EXEC instructions commit performance drops again. With the Lua implementation Redis was still able to write multiple commits to disk using a single <code>fsync</code><a href="#write-combining" id="rev-write-combining"><sup>[2]</sup></a>, without Lua each commit needs to wait for a full <code>fsync</code> to complete due to the use of optimistic locking.</p>
<p>When a more complex page is rendered (listing the last 20 blog posts) the performance differences become negligible. It seems the bottleneck here is the speed at which we can render the page. I believe the Play! 2.0 template engine implementation could use a bit more optimization here.</p>
<p>After completing a benchmark run there are 1,060,000 events in the event store, almost 1 Gigabyte of data. How much time does it take to replay these events on startup? On <code>deler</code> it takes about 26 seconds to fully restore the memory image from the committed events (about 40,500 commits per second). <code>Noemer</code> is more than twice as fast, since it has more and faster processors to perform event deserialization, the primary bottleneck.</p>
<h2>Summary</h2>
<p>Now that we have a working event store implementation our application could go into production, except for the obvious lack of features. There are obviously some enhancements that could be made to the event store:</p>
<ul>
<li>Automatic recovery from Redis connection failures.</li>
<li>Adding arbitrary <em>headers</em> to a commit. These can be used to store metadata like client IP, application server node, authenticated user, etc.</li>
<li>Performance metrics, such as latency from commit to notification. Raise alerts when latency gets to high.</li>
<li>Etc.</li>
</ul>
<p>But for now this event store implementation is good enough.</p>
<p>If you feel that we needed to do a lot of work to get to this place, you&#8217;re partly right.  But we did gain complete control and understanding of the basic infrastructure of our application! Compare this with having to understand SQL, database schemas, JDBC, your favorite Object-Relational Mapper, transaction managers, etc.</p>
<p>Of course, it still remains to be seen how easy or hard it is to add new application functionality. So in the next parts we&#8217;ll focus on adding new features, and some of the problems (with solutions) that you will quickly encounter when using event sourcing.</p>
<style>
#footnotes {
  font-size: smaller;
}
span:target {
  background-color: #F0F0F0;
}
</style>
<style media="only screen and (min--moz-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)">
.replace-2x {
	font-size: 1px;
}
</style>
<p><script type="text/javascript">
function highdpi_init() {
	if(jQuery('.replace-2x').css('font-size') == "1px") {
		var els = jQuery("img.replace-2x").get();
		for(var i = 0; i < els.length; i++) {
			var src = els[i].src
			src = src.replace(".png", "@2x.png");
			els[i].src = src;
		}
	}
}
jQuery(document).ready(function() {
	highdpi_init();
});
</script></p>
<div id="footnotes">
<p>Footnotes:</p>
<p><span id="async"><a href="#rev-async">[1]</a> A good exercise is to modify the event store's <code>tryCommit</code> method to return the result asynchronously. The implementation can then use a dedicated thread pool for performing the actual commits, leaving the Play! action thread pool available for handling web requests.</span></p>
<p><span id="write-combining"><a href="#rev-write-combining">[2]</a> Another good exercise is to implement our own write-combining of commits. This should be pretty straightforward with the Lua event store implementation. All you need is a single, dedicated thread that continuously gathers all pending commits and sends these to Redis. Redis <a href="http://redis.io/topics/pipelining">pipelining</a> and the <a href="http://code.google.com/p/disruptor/">disruptor</a> are a perfect match for this!</span></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.zilverline.com/2012/07/30/simple-event-sourcing-redis-event-store-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple event sourcing &#8211; consistency (part 2)</title>
		<link>http://blog.zilverline.com/2012/07/23/simple-event-sourcing-consistency-part-2/</link>
		<comments>http://blog.zilverline.com/2012/07/23/simple-event-sourcing-consistency-part-2/#comments</comments>
		<pubDate>Mon, 23 Jul 2012 20:15:23 +0000</pubDate>
		<dc:creator>Erik Rozendaal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[event sourcing]]></category>
		<category><![CDATA[memory image]]></category>
		<category><![CDATA[play framework]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.zilverline.com/?p=1012</guid>
		<description><![CDATA[In the first part of this series we developed a simple blogging application that uses event sourcing to keep track of all changes made by the users. However, we did not yet write these events to durable storage. This means all data is lost when the application is restarted, which is clearly not acceptable. Saving <a href='http://blog.zilverline.com/2012/07/23/simple-event-sourcing-consistency-part-2/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>In the <a href="http://blog.zilverline.com/2012/07/04/simple-event-sourcing-introduction-part-1/">first part</a> of this series we developed a simple blogging application that uses event sourcing to keep track of all changes made by the users. However, we did not yet write these events to durable storage. This means all data is lost when the application is restarted, which is clearly not acceptable. Saving and restoring events will be the responsibility of the <em>event store</em>, which we&#8217;ll start implementing in this part. But before we get to actually writing events to disk, we must first tackle the problem of maintaining data consistency when using event sourcing.</p>
<p><span id="more-1012"></span></p>
<h3>Other Parts</h3>
<ul>
<li><a href="http://blog.zilverline.com/2012/07/04/simple-event-sourcing-introduction-part-1/">Part 1 &#8211; Introduction</a></li>
<li>Part 2 &#8211; Consistency</li>
<li><a href="http://blog.zilverline.com/2012/07/30/simple-event-sourcing-redis-event-store-part-3/">Part 3 &#8211; Redis Event Store</a></li>
<li><a href="http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/">Part 4 &#8211; Conflict Resolution</a></li>
<li><a href="http://blog.zilverline.com/2012/08/26/simple-event-sourcing-refactoring-and-transactions-part-5/">Part 5 &#8211; Refactoring and Transactions</a></li>
<li><a href="http://blog.zilverline.com/2013/01/09/simple-event-sourcing-users-authentication-authorization-part-6/">Part 6 &#8211; Users, Authentication, Authorization</a></li>
</ul>
<h2>The code</h2>
<p>You can find the code at <a href="https://github.com/zilverline/event-sourced-blog-example/tree/part-2">github</a> in the <code>part-2</code> branch. Use <code>git clone -b part-2 https://github.com/zilverline/event-sourced-blog-example.git</code> to checkout the correct branch. If you already retrieved the previous part, then using <code>git pull</code> and <code>git checkout part-2</code> should do the trick.</p>
<h2>Living in a distributed world</h2>
<p>Something you&#8217;ll quickly have to learn when you develop web applications is that the internet is a truly distributed environment. You&#8217;ll often run your application across multiple servers (for performance and/or fault-tolerance), while users access your application from different devices all across the globe. All of these servers and devices will have a different view of the world. In a distributed world there is no such thing as a single, global truth. As soon as a web page is rendered by a server and the page is displayed by the client, the data on the page is already out of date. If you do not handle this correctly, some surprising things may happen.</p>
<p>An easy way to see this is to start editing a blog post, then use another browser window to delete the same blog post, and then submit the original edit form. In the application from part 1 this results in a <code>NoSuchElementException</code> while processing the event to update the current state. In the Rails <a href="http://guides.rubyonrails.org/getting_started.html">getting started application</a> you get a <code>ActiveRecord::RecordNotFound</code>. Other frameworks that I know of aren&#8217;t any better at handling this. Back button usage and double-clicking also causes many of the same problems. And the more collaberative your application is, the more likely it is that these inconsistencies and conflicts will occur for real.</p>
<p>Many current web applications deal with this by relying on a combination of ad-hoc solutions, prayer, and luck<a href="#consistency" id="rev-consistency"><sup>[1]</sup></a>. You just have to hope that the occasional lost update, exception or other glitch won&#8217;t cause too much trouble.</p>
<h3>Write consistency</h3>
<p>Since events are forever, we cannot afford to let these glitches go undetected. Otherwise we could get strange, meaningless event sequences, such as having a blog post being edited after it has been deleted! So one major responsibility of an event store is to detect these conflicts and prevent storing inconsistent events.</p>
<p>To implement this our event store is going to keep track of <em>multiple</em> event streams. Each event stream has an associated <em>stream revision</em>, which is defined to be equal to the number of commits for that particular event stream. This makes the stream revision a gapless, strictly increasing sequence.</p>
<p>When you commit additional events to an event stream, you will have to specify the stream revision that you expect the stream to currently have. If the expected stream revision is lower than the current stream revision, there is a conflict. This approach is also known as <em>optimistic concurrency control</em>.</p>
<p>We use multiple event streams so that commits that go to different event streams do not conflict: each event stream defines a consistency boundary. In our example application we&#8217;ll map each blog post to its own event stream. So editing or deleting two different posts never cause conflicts.</p>
<h3>Read consistency</h3>
<p>There is another problem that we have to solve, related to using a memory image. In a database oriented application the central database is considered to give a single, up-to-date, consistent view of the current state of the application. Database transactions are used to control concurrency conflicts (but make sure you choose the correct <a href="http://en.wikipedia.org/wiki/Isolation_(database_systems)">isolation level</a>!). Since the database is a single, centralized component any read after a transaction commit will return the correct data (at least until you introduce master-slave replication or caching).</p>
<p><img src="http://blog.zilverline.com/wp-content/uploads/2012/07/database-centered.png" alt="" title="Central database" width="454" height="287" class="alignnone size-full wp-image-1269 replace-2x" /></p>
<p>With a memory image the situation is slightly different. After changes are successfully committed to the event store, the memory images of every application server is not immediately updated! The newly committed events must first be transferred to the application servers and applied to the memory image before the effects of the change becomes visible:</p>
<p><img src="http://blog.zilverline.com/wp-content/uploads/2012/07/event-store.png" alt="" title="Event store" width="452" height="269" class="alignnone size-full wp-image-1272 replace-2x" /></p>
<p>So to avoid having a fast client not seeing the results of the action they just performed, we&#8217;ll also keep track of the <em>event store revision</em>, which is defined to be equal to the total number of commits to the event store. Again, this produces a gapless, strictly increasing sequence. Whenever a client is redirected to see the results of a commit, we&#8217;ll use the store revision to check if our memory image is up-to-date.</p>
<p>There are a couple of ways to implement this:</p>
<ul>
<li>Don&#8217;t track the store revision at all. The application servers will usually be notified of the change before the client completes the HTTP redirect, so the client will usually see the changes it just made. This may be good enough in situations where highly volatile data is the norm, such as financial markets. For a blog posting application, users expect their changes to be immediately visible, so we need to look at other solutions.</li>
<li>Only run a single application server or ensure all clients always access the same application server (for example, by using sticky sessions, or having one primary server and another server just for failover purposes). The client should always see the result of its actions if we update the server&#8217;s memory image before responding.</li>
<li>Save the store revision as part of the user&#8217;s session state, such as a cookie. Before accessing the memory image ensure it is up-to-date with respect to the revision saved in the cookie or session. Unfortunately, using mutating cookies can lead to <a    href="http://stackoverflow.com/questions/7002215/strategy-for-mitigating-race-conditions-when-updating-cookies">race conditions</a>.</li>
<li>Pass the most recent store revision as a parameter in the redirect URL. This means that every POST action needs to take care of correctly passing in the new store revision whenever a commit succeeds.</li>
<li>Read the current event store revision whenever the memory image is accessed. Wait until at least this many commits have been applied to  the memory image before rendering the response. This is the easiest to implement, so we&#8217;ll use this. It is however not the most performant!</li>
</ul>
<h2>The Event Store API</h2>
<p>If the above got you thoroughly confused, don&#8217;t worry too much. The event store implementation is actually not that complicated. To make the first implementation easier to understand we will not worry about durability yet. In the next part of this series we&#8217;ll build an implementation that uses an actual database (<a href="http://redis.io/">Redis</a>). We&#8217;ll then compare the fake implementation to the Redis implementation to check that both behave the same.</p>
<h3>Store and stream revisions</h3>
<p>The store and stream revisions are represented by the <code>StoreRevision</code> and <code>StreamRevision</code> classes and can be found in <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-2/app/eventstore/EventStore.scala">EventStore.scala</a>. They simply wrap a <code>Long</code> value. The main reason not to use plain <code>Long</code>s is to avoid accidental mixups. Plain numbers that are related but actually mean different things are hard to keep straight otherwise!</p>
<h3>Conflicts and commits</h3>
<p>Committing events to the event store uses the <code>EventCommitter</code> interface:</p>
<p>[code lang=scala]<br />
/**<br />
 * Commits events to an event store.<br />
 */<br />
trait EventCommitter[Event] {<br />
  def tryCommit(streamId: String, expected: StreamRevision, event: Event): CommitResult[Event]<br />
}<br />
[/code]</p>
<p>The <code>tryCommit</code> method will attempt to commit the given event to the event stream. The result type is defined as follows:</p>
<p>[code lang=scala]<br />
/**<br />
 * The result of a commit attempt is either a `Conflict` or a successful `Commit`.<br />
 */<br />
type CommitResult[+Event] = Either[Conflict[Event], Commit[Event]]</p>
<p>/**<br />
 * A successful commit to `streamId`.<br />
 */<br />
case class Commit[+Event](<br />
  storeRevision: StoreRevision,<br />
  timestamp: Long,<br />
  streamId: String,<br />
  streamRevision: StreamRevision,<br />
  events: Seq[Event])</p>
<p>/**<br />
 * The conflict that occurred while trying to commit to `streamId`.<br />
 */<br />
case class Conflict[+Event](<br />
  streamId: String,<br />
  actual: StreamRevision,<br />
  expected: StreamRevision,<br />
  conflicting: Seq[Commit[Event]])<br />
[/code]</p>
<p>In other words, <code>tryCommit</code> returns <em>either</em> a <code>Conflict</code> <em>or</em> a <code>Commit</code>. The conflict will contain the <em>actual</em> stream revision and all conflicting commits (any commit that happened since the <em>expected</em> stream revision). When a commit succeeds, it will contain the committed event as well as some metadata, such as the timestamp of the commit.</p>
<p>You may have noticed that the <code>Commit</code> class allows for multiple events in a single commit. This can often be useful if you want a group of events to be treated atomically by subscribers. Another reason for this is that as your application evolves some events may no longer be of interest. These events can then be filtered out without altering the store or stream revisions the commit is part of. In other words, you may have commits that no longer hold any events!</p>
<h3>Event notification</h3>
<p>To rebuild the memory image and receive commits as they happen the <code>CommitPublisher</code> interface is defined:</p>
<p>[code lang=scala]<br />
/**<br />
 * Publishes successful commits to subscribers.<br />
 */<br />
trait CommitPublisher[Event] {<br />
  /**<br />
   * Notifies `listener` of all commits that happened `since`. Notification happens asynchronously.<br />
   */<br />
  def subscribe(since: StoreRevision)(listener: Commit[Event] => Unit): Subscription<br />
}</p>
<p>/**<br />
 * A subscription that can be cancelled.<br />
 */<br />
trait Subscription {<br />
  def cancel(): Unit<br />
}<br />
[/code]</p>
<p>After subscribing to the event store all commits that happened after the <code>since</code> store revision will be passed to the <code>listener</code> callback. The event store will ensure that commits are send to the listener in-order and without any gaps or duplicates. Notice that notification of commits happens asynchronously.</p>
<h3>Reading events and the event store API</h3>
<p>Two more interfaces are defined that complete the event store API:</p>
<p>[code lang=scala]<br />
/**<br />
 * Reads commits from the event store.<br />
 */<br />
trait CommitReader[Event] {<br />
  def storeRevision: StoreRevision<br />
  def readCommits(since: StoreRevision, to: StoreRevision): Stream[Commit[Event]]<br />
  def streamRevision(streamId: String): StreamRevision<br />
  def readStream(streamId: String, since: StreamRevision, to: StreamRevision): Stream[Commit[Event]]<br />
}</p>
<p>/**<br />
 * The event store API.<br />
 */<br />
trait EventStore[Event] {<br />
  def reader: CommitReader[Event]<br />
  def committer: EventCommitter[Event]<br />
  def publisher: CommitPublisher[Event]<br />
  def close(): Unit<br />
}<br />
[/code]</p>
<p>The <code>CommitReader</code> interface allows access to stored commits (using Scala&#8217;s lazy <code>Stream</code>s to avoid having to load all commits in memory at once) and the <code>EventStore</code> interface simply combines the other interfaces and a <code>close</code> method into a single unit.</p>
<h2>The fake event store implementation</h2>
<p>The <code>FakeEventStore</code> implementation can be found in <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-2/app/eventstore/fake/FakeEventStore.scala">FakeEventStore.scala</a>. It uses a simple <code>Vector</code> (Scala&#8217;s immutable equivalent to Java&#8217;s <code>ArrayList</code>) to store <em>all</em> events committed to the store and a map of <code>streamId</code> to <code>Vector</code> for the commits associated with each event stream. All commits are stored in their original order. Scala&#8217;s Software Transactional Memory (STM) is again used to ensure consistency:</p>
<p>[code lang=scala]<br />
class FakeEventStore[Event] extends EventStore[Event] {<br />
  // [...]<br />
  private[this] val commits = Ref(Vector.empty[Commit[Event]]).single<br />
  private[this] val streams = Ref(Map.empty[String, Vector[Commit[Event]]]).single</p>
<p>  override object reader extends CommitReader[Event] {<br />
    override def storeRevision = StoreRevision(commits().size)</p>
<p>    override def readCommits(since: StoreRevision, to: StoreRevision): Stream[Commit[Event]] = {<br />
      commits().slice(<br />
        (since.value min Int.MaxValue).toInt,<br />
        (to.value min Int.MaxValue).toInt).toStream<br />
    }<br />
    // [...]<br />
  }<br />
  // [...]<br />
}<br />
[/code]</p>
<p>The <code>CommitReader</code> implementation just uses regular Scala collection manipulation functions. Notice that the implementation of <code>storeRevision</code> is precisely the definition of a store revision as discussed in &quot;read consistency&quot; above! The <code>streamRevision</code> and <code>readStream</code> methods (not shown) are implemented similarly.</p>
<p>The <code>EventCommitter</code> <code>tryCommit</code> method is also straightforward:</p>
<p>[code lang=scala]<br />
override def tryCommit(streamId: String, expected: StreamRevision, event: Event): CommitResult[Event] = {<br />
  require(Txn.findCurrent.isEmpty, "the fake event store cannot participate in an STM transaction, just like a real event store")</p>
<p>  atomic { implicit txn =><br />
    val actual = streamRevision(streamId)</p>
<p>    if (expected < actual) {<br />
      val conflicting = readStream(streamId, since = expected)<br />
      Left(Conflict(streamId, actual, expected, conflicting))<br />
    } else if (expected > actual) {<br />
      throw new IllegalArgumentException("expected revision %d greater than actual revision %d" format (expected.value, actual.value))<br />
    } else {<br />
      val commit = Commit(storeRevision.next, DateTimeUtils.currentTimeMillis, streamId, actual.next, Seq(event))<br />
      commits.transform(_ :+ commit)<br />
      streams.transform(streams => streams.updated(streamId, streams.getOrElse(streamId, Vector.empty) :+ commit))<br />
      Right(commit)<br />
    }<br />
  }<br />
}<br />
[/code]</p>
<p>First a check is done to ensure that no STM transaction is currently active, to make sure that we correctly mimic a real event store. Then the expected stream revision is checked against the actual revision. If there is a mismatch a conflict or error is reported. If they match, a new <code>Commit</code> is instantiated and appended to the <code>commits</code> and <code>streams</code> collections. The new commit is then returned. All this is done inside a STM transaction to ensure atomicity.</p>
<p>The final part of the event store implementation deals with notification and is the most tricky. STM again helps to keep complexity under control:</p>
<p>[code lang=scala]<br />
private[this] val closed = Ref(false).single<br />
private[this] val executor = Executors.newCachedThreadPool</p>
<p>override object publisher extends CommitPublisher[Event] {<br />
  override def subscribe(since: StoreRevision)(listener: Commit[Event] => Unit): Subscription = {<br />
    val cancelled = Ref(false).single<br />
    val last = Ref(since).single</p>
<p>    executor.execute(new Runnable {<br />
      @tailrec override def run {<br />
        // Wait for new commits or subscription termination.<br />
        val pending = atomic { implicit txn =><br />
          if (closed() || cancelled()) None else {<br />
            val pending = commits().drop(last().value.toInt)<br />
            if (pending.isEmpty) retry<br />
            else Some(pending)<br />
          }<br />
        }<br />
        pending match {<br />
          case None => // Stop.<br />
          case Some(commits) =><br />
            // Notify listener and go back to run.<br />
            commits.foreach { commit =><br />
              listener(commit)<br />
              last() = commit.storeRevision<br />
            }<br />
            run<br />
        }<br />
      }<br />
    })</p>
<p>    // Return a subscription instance that can be used for cancellation.<br />
    new Subscription {<br />
      override def cancel() = cancelled.set(true)<br />
      override def toString = "Subscription(" + last() + ", " + cancelled() + ", " + FakeEventStore.this + ")"<br />
    }<br />
  }<br />
}<br />
[/code]</p>
<p>The <code>closed</code> reference is used to communicate that the event store has been closed. The <code>executor</code> thread pool is used to run each subscription on its own background thread<a href="#threading" id="rev-threading"><sup>[2]</sup></a> so that we can correctly mimic a real event store.</p>
<p>When a subscription is made, we create two more references: <code>cancelled</code> to communicate that this subscription has been cancelled and <code>last</code> to keep track of the last commit that has been passed to the <code>listener</code>. </p>
<p>A notification thread is then started that continuously checks if there are any new commits that the listener needs to be notified of. If there are none, the STM transaction is <em>retried</em>. <a href="http://nbronson.github.com/scala-stm/quick_start.html#retry">Retry</a> internally uses blocking to avoid needless looping to check for new event store commits.</p>
<p>If the event store is closed or the subscription is cancelled the subscription thread terminates.</p>
<p>The tests for the event store can be found in <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-2/test/eventstore/EventStoreSpec.scala#L108">EventStoreSpec.scala</a>. The tests are implemented as a trait and the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-2/test/eventstore/fake/FakeEventStoreSpec.scala">FakeEventStoreSpec</a> simply extends this trait. This allows us to use the same tests for all event store implementations.</p>
<h2>Memory Image</h2>
<p>The memory image itself is now implemented in its <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-2/app/eventstore/MemoryImage.scala">own class</a>. It basically wraps an <code>EventStore</code> while allowing access to the current state.</p>
<p>[code lang=scala]<br />
/**<br />
 * A `MemoryImage` tracks an underlying event store and uses the provided<br />
 * `initialState` and `update` to project the committed events onto the<br />
 * current state.<br />
 */<br />
class MemoryImage[State, Event] private<br />
  (eventStore: EventStore[Event])<br />
  (initialState: State)<br />
  (update: (State, Commit[Event]) => State)<br />
extends EventCommitter[Event] {<br />
  private[this] val state = Ref(initialState)<br />
  private[this] val revision = Ref(StoreRevision.Initial)</p>
<p>  /**<br />
   * The current state of the memory image with at least all commits applied<br />
   * that have been committed to the underlying event store.<br />
   */<br />
  def get: State = {<br />
    val minimum = eventStore.reader.storeRevision<br />
    atomic { implicit txn =><br />
      if (revision() < minimum) retry<br />
      else state()<br />
    }<br />
  }</p>
<p>  /**<br />
   * Commits an event to the underlying event store. The memory image will be<br />
   * updated if the commit succeeds.<br />
   */<br />
  override def tryCommit(streamId: String, expected: StreamRevision, event: Event): CommitResult[Event] =<br />
    eventStore.committer.tryCommit(streamId, expected, event)</p>
<p>  override def toString = "MemoryImage(%s, %s)".format(revision.single.get, eventStore)</p>
<p>  // Subscribe to the underlying event store and apply every commit to the<br />
  // current state using the provided `update` function.<br />
  eventStore.publisher.subscribe(StoreRevision.Initial) { commit =><br />
    atomic { implicit txn =><br />
      require(revision().next == commit.storeRevision, "expected: " + revision().next + ", got " + commit.storeRevision)</p>
<p>      state.transform(s => update(s, commit))<br />
      revision() = commit.storeRevision<br />
    }<br />
  }<br />
}<br />
[/code]</p>
<p>The memory image takes three constructor parameters: the event store, the initial state, and the update function that takes the current state and a commit to produce the updated state. The memory image also implements the <code>EventCommitter</code> interface and simply passes any commit attempts to the underlying event store.</p>
<p>The <code>get</code> method first checks if the current memory image is up-to-date. If it isn&#8217;t, it blocks until the required number of commits have been applied. Otherwise it simply returns the current state. This ensures the required level of read consistency.</p>
<p>When the memory image is instantiated it also subscribes to the underlying event store and uses the provided <code>update</code> function to ensure the current state reflects all commits received from the event store.</p>
<h2 id="posts-controller">The Posts Controller</h2>
<p>Now that we have a (fake) implementation of the event store, we can start adjusting the <code>PostsController</code> to use it to store events. Before we simply had a <code>posts</code> reference to the latest data and a <code>commit</code> method to commit events. We&#8217;ll re-implement these to use the memory image:</p>
<p>[code lang=scala]<br />
class PostsController(memoryImage: MemoryImage[Posts, PostEvent]) extends Controller {<br />
  /**<br />
   * The current blog posts from the memory image.<br />
   */<br />
  def posts(): Posts = memoryImage.get</p>
<p>  /**<br />
   * Commits an event and applies it to the current state. If successful the<br />
   * provided callback `f` is applied to the `commit`. Otherwise a conflict<br />
   * result is returned.<br />
   */<br />
  private[this] def commit(expected: StreamRevision, event: PostEvent)<br />
                          (f: Commit[PostEvent] => Result): Result = {<br />
    memoryImage.tryCommit(event.postId.toString, expected, event) match {<br />
      case Left(conflict) => Conflict(todo())<br />
      case Right(commit)  => f(commit)<br />
    }<br />
  }<br />
[/code]</p>
<p>As you can see the <code>commit</code> method parameter list was expanded to include the expected stream revision and a callback used to render an HTTP response if the commit succeeds. However, if a conflict is detected an HTTP 409 (Conflict) status code is returned. Giving the client detailed information on the conflict is not implemented yet, so we simply render a <code>todo</code> page.</p>
<p>The controller actions that generate events are all related to HTTP POST requests and need to be adjusted to handle the new expected stream revision parameter and conflict response. Since only the client knows the revision used to render the page by the time the POST request is submitted, the expected stream revision is added as a new URL parameter. Listed below are the <code>show</code> and <code>submit</code> actions related to the &quot;edit post&quot; page:</p>
<p>[code lang=scala]<br />
def show(id: PostId) = Action { implicit request =><br />
  posts().get(id) match {<br />
    case Some(post) => Ok(views.html.posts.edit(post.id, post.revision, postContentForm.fill(post.content)))<br />
    case None       => NotFound(notFound(request, None))<br />
  }<br />
}</p>
<p>def submit(id: PostId, expected: StreamRevision) = Action { implicit request =><br />
  postContentForm.bindFromRequest.fold(<br />
    formWithErrors => BadRequest(views.html.posts.edit(id, expected, formWithErrors)),<br />
    postContent =><br />
      commit(expected, PostEdited(id, postContent)) { commit =><br />
        Redirect(routes.PostsController.show(id)).flashing("info" -> "Post saved.")<br />
      })<br />
}<br />
[/code]</p>
<p>Compared to the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-1/app/controllers/PostsController.scala#L75">previous version</a> there are two main changes:</p>
<ol>
<li>The revision of the post is passed to the &quot;edit post&quot; view.</li>
<li>The expected revision is added as a parameter to the form submit action, so will need to be passed in as part of the URL. This is   configured in the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-2/conf/routes#L13">conf/routes</a> file.</li>
</ol>
<p>Since Play! 2 view templates and URLs are strongly typed it is also impossible to forget to pass this additional expected revision parameter, something that can happen easily with many other solutions, such as hidden &quot;version&quot; form fields.</p>
<h3>The blog posts models</h3>
<p>Since we need to know the current revision of a blog post when rendering the edit or delete actions we need to keep track of this in our model classes. This is done by adding a new field to the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-2/app/models/Post.scala#L14">Post</a> class and setting this based on the stream revision associated with the event:</p>
<p>[code lang=scala]<br />
  def update(event: PostEvent, revision: StreamRevision): Posts = event match {<br />
    case PostAdded(id, content) =><br />
      this.copy(byId = byId.updated(id, Post(id, revision, content)), orderedByTimeAdded = orderedByTimeAdded :+ id)<br />
    case PostEdited(id, content) =><br />
      this.copy(byId = byId.updated(id, byId(id).copy(revision = revision, content = content)))<br />
    case PostDeleted(id) =><br />
      this.copy(byId = byId - id, orderedByTimeAdded = orderedByTimeAdded.filterNot(_ == id))<br />
  }<br />
[/code]</p>
<p>This concludes our tour of the code. There were only minor changes to the code related to the application functionality, as most of the code is related to introducing the event store as a piece of application infrastructure.</p>
<h2>Performance</h2>
<p>Introducing the fake event store causes about a 3%-10% drop in performance compared to having no event store at all. Writes were affected the most, while read performance is almost the same. This is to be expected, since writes now use a background thread to update the memory image, while reads only add a quick check to the memory image to see if it is up-to-date with respect to the event store. Of course, we&#8217;re still not persisting the events to durable storage so we are mostly CPU bound. Writing to disk will obviously introduce new bottlenecks and we&#8217;ll go into a more detailed performance analysis in the next part.</p>
<h2>Summary</h2>
<p>With the introduction of the fake event store implementation our application&#8217;s architecture is now in place. The fake event store has the same behavior as a real event store (except for durability) and is very useful in combination with automated testing. The tests for the fake event store will also be used to check that the real event store behaves correctly. No architectural changes will be needed to our application when this new event store is available.</p>
<p>In the next part we&#8217;ll use <a href="http://redis.io/">Redis</a> to implement an event store that will actually write the events to durable storage and will be used to replay the events to restore the memory image when the application server is started.</p>
<style>
#footnotes {
  font-size: smaller;
}
span:target {
  background-color: #F0F0F0;
}
</style>
<style media="only screen and (min--moz-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)">
.replace-2x {
	font-size: 1px;
}
</style>
<p><script type="text/javascript">
function highdpi_init() {
	if(jQuery('.replace-2x').css('font-size') == "1px") {
		var els = jQuery("img.replace-2x").get();
		for(var i = 0; i < els.length; i++) {
			var src = els[i].src
			src = src.replace(".png", "@2x.png");
			els[i].src = src;
		}
	}
}
jQuery(document).ready(function() {
	highdpi_init();
});
</script></p>
<div id="footnotes">
<p>Footnotes:</p>
<p><span id="consistency"><a href="#rev-consistency">[1]</a> Primarily by defining various database constraints and by using row-level optimistic locking. These can help detect and prevent consistency problems, but do not help much with resolving these conflicts, something we'll come back to in a later part of this series.</span></p>
<p><span id="threading"><a href="#rev-threading">[2]</a> Since we do not expect to have many concurrent subscriptions to a single event store from a single JVM, threads are perfectly fine and do not limit scalability or performance. If you need many concurrent subscribers (for example, to push events to clients) then you should probably put an event bus or actor between the event store subscription and the clients.</span></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.zilverline.com/2012/07/23/simple-event-sourcing-consistency-part-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Simple event sourcing &#8211; introduction (part 1)</title>
		<link>http://blog.zilverline.com/2012/07/04/simple-event-sourcing-introduction-part-1/</link>
		<comments>http://blog.zilverline.com/2012/07/04/simple-event-sourcing-introduction-part-1/#comments</comments>
		<pubDate>Wed, 04 Jul 2012 19:08:52 +0000</pubDate>
		<dc:creator>Erik Rozendaal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[event sourcing]]></category>
		<category><![CDATA[memory image]]></category>
		<category><![CDATA[play framework]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.zilverline.com/?p=942</guid>
		<description><![CDATA[This is the first part of a series on building an event sourced application. We&#8217;ll build a simple blogging application (inspired by the Ruby on Rails &#8220;Getting Started&#8221; tutorial), so the domain should be familiar. This allows us to focus on implementing a memory image based architecture using event sourcing. Another goal is to show <a href='http://blog.zilverline.com/2012/07/04/simple-event-sourcing-introduction-part-1/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>This is the first part of a series on building an event sourced application. We&#8217;ll build a simple blogging application (inspired by the <a href="http://rubyonrails.org/">Ruby on Rails</a> &#8220;<a href="http://guides.rubyonrails.org/getting_started.html">Getting Started</a>&#8221; tutorial), so the domain should be familiar. This allows us to focus on implementing a memory image based architecture using event sourcing. Another goal is to show that this kind of architecture is not more complex (and arguably, simpler) than those implemented by traditional database centered applications. The example code is in Scala using the <a href="http://www.playframework.org/">Play!</a> web framework, but should be understandable enough if you come from a Java (or similar) background.<br />
<span id="more-942"></span></p>
<h3>Other Parts</h3>
<ul>
<li>Part 1 &#8211; Introduction</li>
<li><a href="http://blog.zilverline.com/2012/07/23/simple-event-sourcing-consistency-part-2/">Part 2 &#8211; Consistency</a></li>
<li><a href="http://blog.zilverline.com/2012/07/30/simple-event-sourcing-redis-event-store-part-3/">Part 3 &#8211; Redis Event Store</a></li>
<li><a href="http://blog.zilverline.com/2012/08/08/simple-event-sourcing-conflict-resolution-part-4/">Part 4 &#8211; Conflict Resolution</a></li>
<li><a href="http://blog.zilverline.com/2012/08/26/simple-event-sourcing-refactoring-and-transactions-part-5/">Part 5 &#8211; Refactoring and Transactions</a></li>
<li><a href="http://blog.zilverline.com/2013/01/09/simple-event-sourcing-users-authentication-authorization-part-6/">Part 6 &#8211; Users, Authentication, Authorization</a></li>
</ul>
<h2>Memory Image and Event Sourcing</h2>
<p>The example application is build using a <a href="http://martinfowler.com/bliki/MemoryImage.html">Memory Image</a>. This means that the current state of the application is <em>not stored in a database</em>, but kept in main memory instead. This immediately raises the question of how our state is going to survive application restarts or crashes. Since we cannot save the entire state of our application on every change, we&#8217;ll need to keep something like a durable transaction log, just like databases do. To implement this every change our application makes is first represented as a <em>domain event</em>. These domain events are stored as our application&#8217;s transaction log, using an event store.</p>
<p>After a restart, we can replay the saved domain events to rebuild the in-memory data structure. This concept is known as <a href="http://martinfowler.com/eaaDev/EventSourcing.html">event sourcing</a>. Keeping this durable record of domain events also means we will continue to have access to all historical information, which is very useful for auditing, troubleshooting, or data mining purposes.</p>
<p>Keeping our current state in memory has some obvious advantages:</p>
<ul>
<li>No need to perform mapping between the in-memory model and a database model. This allows you to quickly evolve your application without worrying about database schema migrations.</li>
<li>No need to access the database to answer a query or render a view. The database should no longer be the first scalability bottleneck you hit.</li>
</ul>
<p>There are some disadvantages too:</p>
<ul>
<li>Your data must fit in memory. Fortunately, you can start out with a pure in-memory implementation and later refactor your biggest data into a database. Being able to safely delay major architectural decisions until you really need to decide is core to agile architecture and development.</li>
<li>You must be able to safely and efficiently manage this memory. Garbage collection pause times can become problematic. With the OpenJDK Java VM you can probably get up to 10 Gigabyte or so, and <a href="http://www.azulsystems.com/products/zing/virtual-machine">Azul&#8217;s Zing JVM</a> will go all the way to 512 Gigabyte. The latter should be enough to handle tens of millions of blog posts.</li>
</ul>
<h2>Running the application</h2>
<p>The code for this part can be found on <a href="https://github.com/zilverline/event-sourced-blog-example/tree/part-1">https://github.com/zilverline/event-sourced-blog-example</a>. You can use <code>git clone -b part-1 https://github.com/zilverline/event-sourced-blog-example</code> command to clone and checkout the code that matches the contents of this part.</p>
<p>To run the example you need to install either <a href="http://www.playframework.org/">Play!</a> 2.0.2 (or later) or <a href="https://github.com/harrah/xsbt/wiki">sbt</a> 0.11.3 or later. If you have a Mac and use <a href="http://mxcl.github.com/homebrew/">homebrew</a> you can simply install these using <code>brew install play</code> or <code>brew install sbt</code>.</p>
<p>After installing execute <code>play run</code> or <code>sbt run</code> to start the application in development mode. After downloading all required dependencies and compilation, the application should be available on <a href="http://localhost:9000/">http://localhost:9000/</a>.</p>
<p>Click around a bit to see how everything works. The functionality is pretty minimal, so this shouldn&#8217;t take long.</p>
<h2>The domain events</h2>
<p>The domain events (and supporting classes) for blog postings are defined in <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-1/app/events/PostEvents.scala">PostEvents.scala</a>. Since the application is (still) extremely simple, the events basically model adding a new blog posts, and editing or deleting an existing one. These basically mimic the typical create/update/delete actions, but are named in terms our users understand. This way we also capture the intent of the user, not just the effects of the action they just performed. Later we&#8217;ll add more events, such as <em>Post Published</em> and <em>Comment Added</em>.</p>
<p>[code lang=scala]<br />
sealed trait PostEvent {<br />
  def postId: PostId<br />
}<br />
case class PostAdded(postId: PostId, content: PostContent) extends PostEvent<br />
case class PostEdited(postId: PostId, content: PostContent) extends PostEvent<br />
case class PostDeleted(postId: PostId) extends PostEvent<br />
[/code]</p>
<p>Notice that events are named in the past tense. This can be a bit confusing when writing code that actually generates the events, but at all other times events have always happened in the past so this naming convention makes sense.</p>
<p>The events are implemented using Scala <a href="http://www.scala-lang.org/node/107">case classes</a>. The Scala compiler will translate these into ordinary JVM classes but will add the fields specified in the constructor, accessors, field based <code>equals</code>/<code>hashCode</code> implementations, and a factory method so that you do not need to use the <code>new</code> keyword when instantiating an object. Case classes can also be used with Scala&#8217;s <code>match</code> expression.</p>
<p>In Scala the type is written <em>after</em> the name of a variable or parameter. Explicit types are usually only required on parameters, as the Scala compiler can usually figure out the type in other cases by itself.</p>
<p>The events all extend the <code>PostEvent</code> <a href="http://www.scala-lang.org/node/126">trait</a> (which is translated into a JVM interface). The <code>PostEvent</code> trait is marked as <code>sealed</code> so that it can only be extended in the same source file. This allows the Scala compiler to do compile-time  analysis of match statements to ensure all possible cases are covered. This is very useful when new events are added later!</p>
<p>The events also use two support classes to represent the blog post&#8217;s identifier and content, respectively:</p>
<p>[code lang=scala]<br />
case class PostContent(author: String, title: String, content: String)</p>
<p>case class PostId(uuid: UUID)<br />
object PostId {<br />
  def generate(): PostId = PostId(UUID.randomUUID())</p>
<p>  def fromString(s: String): Option[PostId] = s match {<br />
    case PostIdRegex(uuid) => catching(classOf[RuntimeException]) opt { PostId(UUID.fromString(uuid)) }<br />
    case _                 => None<br />
  }</p>
<p>  private val PostIdRegex = """PostId\(([a-fA-F0-9-]{36})\)""".r<br />
}<br />
[/code]</p>
<p>The <code>PostContent</code> class is a simple case class with three fields, while the <code>PostId</code> class has a <a href="http://daily-scala.blogspot.nl/2009/09/companion-object.html">companion object</a> with a <code>generate</code> factory method and <code>fromString</code> parse method.</p>
<p>Since events make up the durable record of everything that happened in our domain, they need to be <em>stable</em>. Stability can be achieved by getting the design right (<em>hard!</em>) and ensuring the events related definitions have very few dependencies on other code. See <a href="http://www.objectmentor.com/resources/articles/stability.pdf">Stability (PDF)</a> by Robert C. Martin for a great explanation of managing stability and dependencies within a program.</p>
<h2>Keeping track of the current state</h2>
<p>Although events are very useful to track transactional and historical information, they cannot easily be used to determine the current state. So in addition to capturing the events, we also need to <em>derive</em> the current state from these events. We do not necessarily need to track every piece of data, only what we need for our application to make decisions (by creating new events) or to show information to the user (by rendering views, or sending out emails, etc). The state needed for the blogging application is implemented in <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-1/app/models/Post.scala">Post.scala</a>:</p>
<p>[code lang=scala]<br />
/**<br />
 * A specific blog post with its current content.<br />
 */<br />
case class Post(id: PostId, content: PostContent)</p>
<p>/**<br />
 * The current state of blog posts, derived from all committed PostEvents.<br />
 */<br />
case class Posts(byId: Map[PostId, Post] = Map.empty, orderedByTimeAdded: Seq[PostId] = Vector.empty) {<br />
  def get(id: PostId): Option[Post] = byId.get(id)<br />
  def mostRecent(n: Int): Seq[Post] = orderedByTimeAdded.takeRight(n).reverse.map(byId)</p>
<p>  def apply(event: PostEvent): Posts = event match {<br />
    case PostAdded(id, content) =><br />
      this.copy(byId = byId.updated(id, Post(id, content)), orderedByTimeAdded = orderedByTimeAdded :+ id)<br />
    case PostEdited(id, content) =><br />
      this.copy(byId = byId.updated(id, byId(id).copy(content = content)))<br />
    case PostDeleted(id) =><br />
      this.copy(byId = byId - id, orderedByTimeAdded = orderedByTimeAdded.filterNot(_ == id))<br />
  }<br />
}<br />
object Posts {<br />
  def fromHistory(events: PostEvent*): Posts = events.foldLeft(Posts())(_ apply _)<br />
}<br />
[/code]</p>
<p>Since the model classes are in-memory only, we&#8217;re free to use regular Scala data structure classes to implement whatever query capabilities we need. There is no need to worry about Object-Relational Mapping, etc. Here we use a map to track each blog post by its identifier and a simple <code>Seq</code> (ordered collection or list) to keep track of the order that posts were added. Notice that this entire model is represented using immutable values, which allows us to safely share the current state between multiple concurrent requests.</p>
<p>The <code>Posts.get</code> method simply looks up a post by its identifier. The <code>Posts.mostRecent</code> method takes the last <code>n</code> added post identifers, reverses the result (so the most recently added post is first), and translates each identifier into a post using the <code>byId</code> map.</p>
<p>The <code>Posts.apply</code> method implements updating the current state based on an event. It basically matches on the type of event and updates its state accordingly. <code>Posts.fromHistory</code> builds up the current state by folding a sequence of events using the <code>Posts.apply</code> method. As with all immutable structures, an <em>update</em> results in a <em>new copy</em> of the original state to be returned with the necessary changes applied. Fortunately we do not need to copy <em>everything</em> to apply changes, only the parts that are changed need to be copied. This makes immutable data structures efficient enough to be practical.</p>
<p>Another advantage of using an in-memory model is that it can be thoroughly tested, with no need to start or stub a database. <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-1/test/models/PostsSpec.scala">PostsSpec.scala</a> uses both manually written examples and <a href="https://github.com/rickynils/scalacheck/wiki/User-Guide">randomly generated data</a> to test the model. Using the randomly generated date we can run hundreds of tests in less than a second.</p>
<h2>Putting it all together: the UI</h2>
<p>The UI pulls everything together into a working application. In this case it is a standard Play! 2.0 Scala application and the main work is done by the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-1/app/controllers/PostsController.scala">PostsController</a>.</p>
<p>In this example application the <code>PostsController</code> keeps a <a href="http://nbronson.github.com/scala-stm/quick_start.html">Software Transactional Memory (STM) reference</a> to the current state of the application. This reference is our <em>only</em> piece of mutable data in the entire application!</p>
<p>[code lang=scala]<br />
object PostsController extends Controller {<br />
  /**<br />
   * A Scala STM reference holding the current state of the application,<br />
   * which is derived from all committed events.<br />
   */<br />
  val posts = Ref(Posts()).single<br />
[/code]</p>
<p>By using an STM reference any controller method can always access the current state simply by reading from the reference. New events are applied to the current state using the <code>commit</code> method:</p>
<p>[code lang=scala]<br />
  /**<br />
   * Commits an event and applies it to the current state.<br />
   */<br />
  def commit(event: PostEvent): Unit = {<br />
    posts.transform(_.apply(event))<br />
    Logger.debug("Committed event: " + event)<br />
  }<br />
[/code]</p>
<p>The <code>posts</code> reference is updated by using the <code>transform</code> method. This ensures the update occurs in a concurrency safe manner. In this version of the application the events are not yet saved to durable storage, but to help you see what is happening while running the application the event is printed to the debug log instead. Later we&#8217;ll implement saving the committed events to durable storage before updating the current state. The <code>commit</code> method returns <a href="http://en.wikipedia.org/wiki/Unit_type"><code>Unit</code></a>, which is similar to <code>void</code> in Java and provides no useful information to the caller.</p>
<p>The <code>PostsController.index</code> method is invoked by Play! to render a list of posts (the mapping from URL to a controller method is configured in the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-1/conf/routes">routes</a> file). The <code>index</code> method returns a Play! <a href="http://www.playframework.org/documentation/2.0/ScalaActions">action</a> which generates an HTTP response from an HTTP request. In this case the response is an HTTP OK (200) containing the most recent 20 posts as rendered by the <a href="https://github.com/zilverline/event-sourced-blog-example/blob/part-1/app/views/posts/index.scala.html">index template</a>. Notice the use of the parentheses to read the current value of the <code>posts</code> reference:</p>
<p>[code lang=scala]<br />
  /**<br />
   * Show an overview of the most recent blog posts.<br />
   */<br />
  def index = Action { implicit request =><br />
    Ok(views.html.posts.index(posts().mostRecent(20)))<br />
  }<br />
[/code]</p>
<p>To change an existing post we need to implement a GET to render an HTML form, and a HTTP POST to validate the form and update the post. The <code>postContentForm</code> defines the mapping between the HTML add/edit form and the <code>PostContent</code> class using a <a href="http://www.playframework.org/documentation/2.0.2/ScalaForms">Play! form</a>:</p>
<p>[code lang=scala]<br />
  /*<br />
   * Blog content form definition.<br />
   */<br />
  private val postContentForm = Form(mapping(<br />
    "author"  -> trimmedText.verifying(minLength(3)),<br />
    "title"   -> trimmedText.verifying(minLength(3)),<br />
    "content" -> trimmedText.verifying(minLength(3)))(PostContent.apply)(PostContent.unapply))<br />
[/code]</p>
<p>The <code>show</code> method first tries to look up the specified post by its id. If successful it fills the form with the current contents of the post and renders the <code>views.html.posts.edit</code> template. Otherwise it returns an HTTP 404 (Not Found) response:</p>
<p>[code lang=scala]<br />
  def show(id: PostId) = Action { implicit request =><br />
    posts().get(id) match {<br />
      case Some(post) => Ok(views.html.posts.edit(id, postContentForm.fill(post.content)))<br />
      case None       => NotFound(notFound(request, None))<br />
    }<br />
  }<br />
[/code]</p>
<p>When the user has made the modifications and hits the <em>save</em> button the <code>submit</code> method is invoked. First we bind the HTTP POST parameters to the form (<code>bindFromRequest</code>) and then perform validation (<code>fold</code>). If form validation succeeds it commits a new <code>PostEdited</code> event and redirects the browser to the <code>PostsController.show</code> action to show the updated post. Otherwise the method rerenders the HTML form together with the validation errors:</p>
<p>[code lang=scala]<br />
  def submit(id: PostId) = Action { implicit request =><br />
    postContentForm.bindFromRequest.fold(<br />
      formWithErrors => BadRequest(views.html.posts.edit(id, formWithErrors)),<br />
      postContent => {<br />
        commit(PostEdited(id, postContent))<br />
        Redirect(routes.PostsController.show(id)).flashing("info" -> "Post saved.")<br />
      })<br />
  }<br />
[/code]</p>
<p>Note that our &#8220;domain logic&#8221; is implemented directly in the controller. This is fine for simple applications like this example, but a <a href="http://blog.zilverline.com/2011/02/01/towards-an-immutable-domain-model-introduction-part-1/">rich domain model</a> is usually introduced when the logic to generate the events becomes less straightforward.</p>
<h2>Performance</h2>
<p>Like we discusses above one of the advantages of an in-memory model is that there is no need to talk to a database just to render a view. Even though this application  has not been performance tuned, it is still interesting to get a basic idea of the performance. I ran some trivial benchmarks on my laptop (an early 2010 MacBook Pro with 2.66 GHz dual core i7 with hyper-threading). </p>
<p>Just rendering the blog posts index view runs at about 5,500 GETs per second (with the three example blog posts you see when you start the application). This was measured with <a href="http://jmeter.apache.org/">Apache JMeter</a> using 25 concurrent client threads running on the same machine as the server.</p>
<p>Just submitting the blog post edit form runs at 4,200 HTTP POSTs per second or so. </p>
<p>In both tests the server is basically CPU bound, since we&#8217;re not performing any disk I/O. It will be interesting to see how well we can do with an actual event store implementation. The server was running on JDK 1.7.0u5 with the Garbage First GC (JVM option <code>-XX:+UseG1GC</code>).</p>
<h2>Conclusion</h2>
<p>The example application shows a simplified implementation of the event sourcing and memory image principles. Domain events are generated and used to apply updates to the memory image, which is then used to render views, without the need for a traditional database.</p>
<p>Hopefully you&#8217;ve noticed that none of the code is very complex. Each part (events, model, controller actions) simply focuses on a single task, without any of the &#8220;magic&#8221; that is so common with many web- or CRUD-frameworks. Even the most complicated method (<code>Posts.apply</code>) is a rather straightforward translation of events into updates to the current state and is easy to thoroughly test.</p>
<p>But the events are not yet committed to durable storage, which is necessary to build a production-ready application. In the next part we&#8217;ll see what is needed to capture the events correctly so that we can start writing the events to durable storage.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zilverline.com/2012/07/04/simple-event-sourcing-introduction-part-1/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Architect in Scrum</title>
		<link>http://blog.zilverline.com/2012/04/16/architect-in-scrum/</link>
		<comments>http://blog.zilverline.com/2012/04/16/architect-in-scrum/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 09:05:59 +0000</pubDate>
		<dc:creator>Michael Franken</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Scrum]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[architect]]></category>
		<category><![CDATA[architecting]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[lean]]></category>
		<category><![CDATA[scrum]]></category>

		<guid isPermaLink="false">http://blog.zilverline.com/?p=917</guid>
		<description><![CDATA[Last friday I gave a Masterclass called &#8216;Lean Agile Architecting&#8217; to architects. Very interesting masterclass and a couple of things struck me. The issue for architects in an Agile environment is their position and responsibility. The thing with the change from waterfall to agile is that architects feel their role is being undercut, the team just <a href='http://blog.zilverline.com/2012/04/16/architect-in-scrum/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>Last friday I gave a Masterclass called &#8216;Lean Agile Architecting&#8217; to architects. Very interesting masterclass and a couple of things struck me. <em>The</em> issue for architects in an Agile environment is their position and responsibility.</p>
<p>The thing with the change from waterfall to agile is that architects feel their role is being undercut, the team just goes fast and are only paying attention to the Product Owner. The standard answer they seem to get is: &#8216;then join the team&#8217;, but they feel reluctant to do so, and most of the times they can not fully commit (full time). So they pass, and they feel miserable about it, since now this Agile project is going to make mistakes, and can not learn from past experiences and their expertise.</p>
<p>The answer lies in the closer observation of the definition of Agile and Architecture.<br />
<span id="more-917"></span></p>
<p>Going agile is not the same as going fast, as the Agile Manifesto&#8217;s first principle says:</p>
<blockquote><p>Our highest priority is to satisfy the customer through <em>early</em> and <em>continuous</em> delivery of valuable software.</p></blockquote>
<p>So not just fast (early), but continuous too! And that&#8217;s the whole point of the position of architecture in Scrum/XP/Agile. In order to be able to continue to go fast, we have to make sure we mind our architecture, since architecture is:</p>
<blockquote><p>The set of design decisions that minimize the cost of development, maintenance and use (my definition, close enough to Grady Booch&#8217;s)</p></blockquote>
<p>So the whole point is that architects make sure that teams can go fast now and ever after, and that the cost of use and maintenance is proportional to the development investment. So architecture is not a stable deliverable or document, much better to speak about architecting; the process of keeping the architecture aligned with the product development and agile process.</p>
<p>Architecting is about identifying stakeholders that might be overlooked by the team and Product Owner, such as Support, Legal and 3rd party vendors. Architecting is about challenging the Product Owner and team to go slow, in order to be able to go fast by high quality standards, such as readability, 95%+ test coverage, early integration and performance testing, refactoring and the boy scout rule.</p>
<p>So architects, make sure you identify and represent stakeholders, and get their points on the Product Backlog (yes, it may contain non-functionals) by working closely with the Product Owner. You are a major stakeholder! Assist the team by making them go slow for a good reason, namely to serve previously overlooked stakeholders. There is a subtle balance here, if in doubt go fast with the Product Owner, and only address the issue when velocity drops because of technical debt.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zilverline.com/2012/04/16/architect-in-scrum/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
