{"action":"created","comment":{"url":"https://api.github.com/repos/akka/akka/pulls/comments/37057022","id":37057022,"diff_hunk":"@@ -0,0 +1,471 @@\n+.. _persistence-schema-evolution-scala:\n+\n+##############################\n+Persistence - Schema Evolution\n+##############################\n+\n+When working on long running projects using :ref:`persistence-scala`, or any kind of `Event Sourcing`_ architectures,\n+schema evolution becomes one of the more important technical aspects of developing your application.\n+The requirements as well as our own understanding of the business domain may (and will) change in time.\n+\n+In fact, if a project matures to the point where you need to evolve its schema to adapt to changing business\n+requirements you can view this as first signs of its success – if you wouldn't need to adapt anything over an apps\n+lifecycle that could mean that no-one is really using it actively.\n+\n+In this chapter we will investigate various schema evolution strategies and techniques from which you can pick and\n+choose the ones that match your domain and challenge at hand.\n+\n+.. note::\n+  This page proposes a number of possible solutions to the schema evolution problem and explains how some of the\n+  utilities Akka provides can be used to achieve this, it is by no means a complete (closed) set of solutions.\n+\n+  Sometimes, based on the capabilities of your serialization formats, you may be able to evolve your schema in\n+  different ways than outlined in the sections below. If you discover useful patterns or techniques for schema\n+  evolution feel free to submit Pull Requests to this page to extend it.\n+\n+\n+Schema evolution in event-sourced systems\n+=========================================\n+\n+In recent years we have observed a tremendous move towards immutable append-only datastores, with event-sourcing being\n+the prime technique successfully being used in these settings. For an excellent overview why and how immutable data makes scalability\n+and systems design much simpler you may want to read Pat Helland's excellent `Immutability Changes Everything`_ whitepaper.\n+\n+Since with `Event Sourcing`_ the **events are immutable** and usually never deleted – the way schema evolution is handled\n+differs from how one would go about it in a mutable database setting (e.g. in typical CRUD database applications).\n+The system needs to be able to continue to work in the presence of \"old\" events which were stored under the \"old\" schema.\n+We also want to limit complexity in the business logic layer, exposing a consistent view over all of the events of a given\n+type to :class:`PersistentActor` s and :ref:`persistence queries <persistence-query-scala>`. This allows the business logic layer to focus on solving business problems\n+instead of having to explicitly deal with different schemas.\n+\n+The system needs to be able to continue to work in the presence of \"old\" events which were stored under the \"old\" schema,\n+and we want to limit the complexity to the data layer, exposing a consistent view over all of the events of a given type\n+to :class:`PersistentActor` s and persistence queries, which allows these layers to focus on the business problems instead\n+handling the different schemas explicitly in the business logic layers.\n+\n+\n+In summary, schema evolution in event sourced systems exposes the following characteristics:\n+  - Allow the system to continue operating without large scale migrations to be applied,\n+  - Allow the system to read \"old\" events from the underlying storage, however present them in a \"new\" view to the application logic,\n+  - Transparently promote events to the latest versions during recovery (or queries) such that the business logic need not consider multiple versions of events\n+\n+.. _Immutability Changes Everything: http://www.cidrdb.org/cidr2015/Papers/CIDR15_Paper16.pdf\n+.. _Event Sourcing: http://martinfowler.com/eaaDev/EventSourcing.html\n+\n+Types of schema evolution\n+-------------------------\n+Before we explain the various techniques that can be used to safely evolve the schema of your persistent events\n+over time, we first need to define what the actual problem is, and what the typical styles of changes are.\n+\n+Since events are never deleted, we need to have a way to be able to recover (read) old events, in such way\n+that does not force the ``PersistentActor`` to be aware of all possible versions of an event that it may have\n+persisted in the past. Instead, we want the Actors to work on some form of \"latest\" version of the event and provide some\n+means of either converting old \"versions\" of stored events into this \"latest\" event type, or constantly evolve the event\n+definition - in a backwards compatible way - such that the new deserialization code can still read old events.\n+\n+The most common schema changes you will likely are:\n+\n+- :ref:`adding a field to an event type <add-field-scala>`,\n+- :ref:`remove or rename field in event type <rename-field-scala>`,\n+- :ref:`remove event type <remove-event-class-scala>`,\n+- :ref:`split event into multiple smaller events <split-large-event-into-smaller-scala>`.\n+\n+The following sections will explain some patterns which can be used to safely evolve your schema when facing those changes.\n+\n+Picking the right serialization format\n+======================================\n+\n+Picking the serialization format is a very important decision you will have to make while building your application.\n+It affects which kind of evolutions are simple (or hard) to do, how much work is required to add a new datatype, and,\n+last but not least, how serialization performance serialization.\n+\n+If you find yourself realising you have picked \"the wrong\" serialization format, it is always possible to change\n+the format used for storing new events, however you would have to keep the old deserialization code in order to\n+be able to replay events that were persisted using the old serialization scheme. It is possible to \"rebuild\"\n+an event-log from one serialization format to another one, however it may be a more involved process if you need\n+to perform this on a live system.\n+\n+Binary serialization formats that we have seen work well for long-lived applications include the very flexible IDL based:\n+`Google Protobuf`_, `Apache Thrift`_ or `Apache Avro`_ (for which schema evolution is more \"entire schema\" based, instead of","path":"akka-docs/rst/scala/persistence-schema-evolution.rst","position":89,"original_position":89,"commit_id":"ccca58db162aaf4acd4862ead2a0b5285ba95854","original_commit_id":"ccca58db162aaf4acd4862ead2a0b5285ba95854","user":{"login":"ktoso","id":120979,"avatar_url":"https://avatars.githubusercontent.com/u/120979?v=3","gravatar_id":"","url":"https://api.github.com/users/ktoso","html_url":"https://github.com/ktoso","followers_url":"https://api.github.com/users/ktoso/followers","following_url":"https://api.github.com/users/ktoso/following{/other_user}","gists_url":"https://api.github.com/users/ktoso/gists{/gist_id}","starred_url":"https://api.github.com/users/ktoso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ktoso/subscriptions","organizations_url":"https://api.github.com/users/ktoso/orgs","repos_url":"https://api.github.com/users/ktoso/repos","events_url":"https://api.github.com/users/ktoso/events{/privacy}","received_events_url":"https://api.github.com/users/ktoso/received_events","type":"User","site_admin":false},"body":"ok","created_at":"2015-08-14T08:13:34Z","updated_at":"2015-08-14T08:13:34Z","html_url":"https://github.com/akka/akka/pull/18084#discussion_r37057022","pull_request_url":"https://api.github.com/repos/akka/akka/pulls/18084","_links":{"self":{"href":"https://api.github.com/repos/akka/akka/pulls/comments/37057022"},"html":{"href":"https://github.com/akka/akka/pull/18084#discussion_r37057022"},"pull_request":{"href":"https://api.github.com/repos/akka/akka/pulls/18084"}}},"pull_request":{"url":"https://api.github.com/repos/akka/akka/pulls/18084","id":40998817,"html_url":"https://github.com/akka/akka/pull/18084","diff_url":"https://github.com/akka/akka/pull/18084.diff","patch_url":"https://github.com/akka/akka/pull/18084.patch","issue_url":"https://api.github.com/repos/akka/akka/issues/18084","number":18084,"state":"open","locked":false,"title":"+doc #17613 document persistence schema evolution","user":{"login":"ktoso","id":120979,"avatar_url":"https://avatars.githubusercontent.com/u/120979?v=3","gravatar_id":"","url":"https://api.github.com/users/ktoso","html_url":"https://github.com/ktoso","followers_url":"https://api.github.com/users/ktoso/followers","following_url":"https://api.github.com/users/ktoso/following{/other_user}","gists_url":"https://api.github.com/users/ktoso/gists{/gist_id}","starred_url":"https://api.github.com/users/ktoso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ktoso/subscriptions","organizations_url":"https://api.github.com/users/ktoso/orgs","repos_url":"https://api.github.com/users/ktoso/repos","events_url":"https://api.github.com/users/ktoso/events{/privacy}","received_events_url":"https://api.github.com/users/ktoso/received_events","type":"User","site_admin":false},"body":"Will resolve https://github.com/akka/akka/issues/17613\r\n\r\nI had a section on \"rename event\" but it was \"either protobuf does it for you, or refer to deleting events\" will work a bit more on covering all cases, need a break from this document for a while though.","created_at":"2015-07-28T10:57:45Z","updated_at":"2015-08-14T08:13:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c4e8d12147be59d99fb58e668b332644219b4bd4","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/akka/akka/pulls/18084/commits","review_comments_url":"https://api.github.com/repos/akka/akka/pulls/18084/comments","review_comment_url":"https://api.github.com/repos/akka/akka/pulls/comments{/number}","comments_url":"https://api.github.com/repos/akka/akka/issues/18084/comments","statuses_url":"https://api.github.com/repos/akka/akka/statuses/ccca58db162aaf4acd4862ead2a0b5285ba95854","head":{"label":"ktoso:wip-schema-migration-omg-its-a-small-book-ktoso","ref":"wip-schema-migration-omg-its-a-small-book-ktoso","sha":"ccca58db162aaf4acd4862ead2a0b5285ba95854","user":{"login":"ktoso","id":120979,"avatar_url":"https://avatars.githubusercontent.com/u/120979?v=3","gravatar_id":"","url":"https://api.github.com/users/ktoso","html_url":"https://github.com/ktoso","followers_url":"https://api.github.com/users/ktoso/followers","following_url":"https://api.github.com/users/ktoso/following{/other_user}","gists_url":"https://api.github.com/users/ktoso/gists{/gist_id}","starred_url":"https://api.github.com/users/ktoso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ktoso/subscriptions","organizations_url":"https://api.github.com/users/ktoso/orgs","repos_url":"https://api.github.com/users/ktoso/repos","events_url":"https://api.github.com/users/ktoso/events{/privacy}","received_events_url":"https://api.github.com/users/ktoso/received_events","type":"User","site_admin":false},"repo":{"id":4462204,"name":"akka","full_name":"ktoso/akka","owner":{"login":"ktoso","id":120979,"avatar_url":"https://avatars.githubusercontent.com/u/120979?v=3","gravatar_id":"","url":"https://api.github.com/users/ktoso","html_url":"https://github.com/ktoso","followers_url":"https://api.github.com/users/ktoso/followers","following_url":"https://api.github.com/users/ktoso/following{/other_user}","gists_url":"https://api.github.com/users/ktoso/gists{/gist_id}","starred_url":"https://api.github.com/users/ktoso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ktoso/subscriptions","organizations_url":"https://api.github.com/users/ktoso/orgs","repos_url":"https://api.github.com/users/ktoso/repos","events_url":"https://api.github.com/users/ktoso/events{/privacy}","received_events_url":"https://api.github.com/users/ktoso/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ktoso/akka","description":"Akka Project","fork":true,"url":"https://api.github.com/repos/ktoso/akka","forks_url":"https://api.github.com/repos/ktoso/akka/forks","keys_url":"https://api.github.com/repos/ktoso/akka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ktoso/akka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ktoso/akka/teams","hooks_url":"https://api.github.com/repos/ktoso/akka/hooks","issue_events_url":"https://api.github.com/repos/ktoso/akka/issues/events{/number}","events_url":"https://api.github.com/repos/ktoso/akka/events","assignees_url":"https://api.github.com/repos/ktoso/akka/assignees{/user}","branches_url":"https://api.github.com/repos/ktoso/akka/branches{/branch}","tags_url":"https://api.github.com/repos/ktoso/akka/tags","blobs_url":"https://api.github.com/repos/ktoso/akka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ktoso/akka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ktoso/akka/git/refs{/sha}","trees_url":"https://api.github.com/repos/ktoso/akka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ktoso/akka/statuses/{sha}","languages_url":"https://api.github.com/repos/ktoso/akka/languages","stargazers_url":"https://api.github.com/repos/ktoso/akka/stargazers","contributors_url":"https://api.github.com/repos/ktoso/akka/contributors","subscribers_url":"https://api.github.com/repos/ktoso/akka/subscribers","subscription_url":"https://api.github.com/repos/ktoso/akka/subscription","commits_url":"https://api.github.com/repos/ktoso/akka/commits{/sha}","git_commits_url":"https://api.github.com/repos/ktoso/akka/git/commits{/sha}","comments_url":"https://api.github.com/repos/ktoso/akka/comments{/number}","issue_comment_url":"https://api.github.com/repos/ktoso/akka/issues/comments{/number}","contents_url":"https://api.github.com/repos/ktoso/akka/contents/{+path}","compare_url":"https://api.github.com/repos/ktoso/akka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ktoso/akka/merges","archive_url":"https://api.github.com/repos/ktoso/akka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ktoso/akka/downloads","issues_url":"https://api.github.com/repos/ktoso/akka/issues{/number}","pulls_url":"https://api.github.com/repos/ktoso/akka/pulls{/number}","milestones_url":"https://api.github.com/repos/ktoso/akka/milestones{/number}","notifications_url":"https://api.github.com/repos/ktoso/akka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ktoso/akka/labels{/name}","releases_url":"https://api.github.com/repos/ktoso/akka/releases{/id}","created_at":"2012-05-27T14:05:17Z","updated_at":"2015-04-03T11:58:56Z","pushed_at":"2015-08-13T17:48:32Z","git_url":"git://github.com/ktoso/akka.git","ssh_url":"git@github.com:ktoso/akka.git","clone_url":"https://github.com/ktoso/akka.git","svn_url":"https://github.com/ktoso/akka","homepage":"http://akka.io","size":169254,"stargazers_count":1,"watchers_count":1,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"akka:master","ref":"master","sha":"a473ff23dc6c3f3ca08bc0dd7cb80aa1982ae280","user":{"login":"akka","id":1496237,"avatar_url":"https://avatars.githubusercontent.com/u/1496237?v=3","gravatar_id":"","url":"https://api.github.com/users/akka","html_url":"https://github.com/akka","followers_url":"https://api.github.com/users/akka/followers","following_url":"https://api.github.com/users/akka/following{/other_user}","gists_url":"https://api.github.com/users/akka/gists{/gist_id}","starred_url":"https://api.github.com/users/akka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akka/subscriptions","organizations_url":"https://api.github.com/users/akka/orgs","repos_url":"https://api.github.com/users/akka/repos","events_url":"https://api.github.com/users/akka/events{/privacy}","received_events_url":"https://api.github.com/users/akka/received_events","type":"Organization","site_admin":false},"repo":{"id":130013,"name":"akka","full_name":"akka/akka","owner":{"login":"akka","id":1496237,"avatar_url":"https://avatars.githubusercontent.com/u/1496237?v=3","gravatar_id":"","url":"https://api.github.com/users/akka","html_url":"https://github.com/akka","followers_url":"https://api.github.com/users/akka/followers","following_url":"https://api.github.com/users/akka/following{/other_user}","gists_url":"https://api.github.com/users/akka/gists{/gist_id}","starred_url":"https://api.github.com/users/akka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akka/subscriptions","organizations_url":"https://api.github.com/users/akka/orgs","repos_url":"https://api.github.com/users/akka/repos","events_url":"https://api.github.com/users/akka/events{/privacy}","received_events_url":"https://api.github.com/users/akka/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/akka/akka","description":"Akka Project","fork":false,"url":"https://api.github.com/repos/akka/akka","forks_url":"https://api.github.com/repos/akka/akka/forks","keys_url":"https://api.github.com/repos/akka/akka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/akka/akka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/akka/akka/teams","hooks_url":"https://api.github.com/repos/akka/akka/hooks","issue_events_url":"https://api.github.com/repos/akka/akka/issues/events{/number}","events_url":"https://api.github.com/repos/akka/akka/events","assignees_url":"https://api.github.com/repos/akka/akka/assignees{/user}","branches_url":"https://api.github.com/repos/akka/akka/branches{/branch}","tags_url":"https://api.github.com/repos/akka/akka/tags","blobs_url":"https://api.github.com/repos/akka/akka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/akka/akka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/akka/akka/git/refs{/sha}","trees_url":"https://api.github.com/repos/akka/akka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/akka/akka/statuses/{sha}","languages_url":"https://api.github.com/repos/akka/akka/languages","stargazers_url":"https://api.github.com/repos/akka/akka/stargazers","contributors_url":"https://api.github.com/repos/akka/akka/contributors","subscribers_url":"https://api.github.com/repos/akka/akka/subscribers","subscription_url":"https://api.github.com/repos/akka/akka/subscription","commits_url":"https://api.github.com/repos/akka/akka/commits{/sha}","git_commits_url":"https://api.github.com/repos/akka/akka/git/commits{/sha}","comments_url":"https://api.github.com/repos/akka/akka/comments{/number}","issue_comment_url":"https://api.github.com/repos/akka/akka/issues/comments{/number}","contents_url":"https://api.github.com/repos/akka/akka/contents/{+path}","compare_url":"https://api.github.com/repos/akka/akka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/akka/akka/merges","archive_url":"https://api.github.com/repos/akka/akka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/akka/akka/downloads","issues_url":"https://api.github.com/repos/akka/akka/issues{/number}","pulls_url":"https://api.github.com/repos/akka/akka/pulls{/number}","milestones_url":"https://api.github.com/repos/akka/akka/milestones{/number}","notifications_url":"https://api.github.com/repos/akka/akka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/akka/akka/labels{/name}","releases_url":"https://api.github.com/repos/akka/akka/releases{/id}","created_at":"2009-02-16T12:51:54Z","updated_at":"2015-08-13T23:00:27Z","pushed_at":"2015-08-14T08:09:29Z","git_url":"git://github.com/akka/akka.git","ssh_url":"git@github.com:akka/akka.git","clone_url":"https://github.com/akka/akka.git","svn_url":"https://github.com/akka/akka","homepage":"http://akka.io","size":267368,"stargazers_count":4317,"watchers_count":4317,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1264,"mirror_url":null,"open_issues_count":723,"forks":1264,"open_issues":723,"watchers":4317,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/akka/akka/pulls/18084"},"html":{"href":"https://github.com/akka/akka/pull/18084"},"issue":{"href":"https://api.github.com/repos/akka/akka/issues/18084"},"comments":{"href":"https://api.github.com/repos/akka/akka/issues/18084/comments"},"review_comments":{"href":"https://api.github.com/repos/akka/akka/pulls/18084/comments"},"review_comment":{"href":"https://api.github.com/repos/akka/akka/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/akka/akka/pulls/18084/commits"},"statuses":{"href":"https://api.github.com/repos/akka/akka/statuses/ccca58db162aaf4acd4862ead2a0b5285ba95854"}}}}