Giter VIP home page Giter VIP logo

cqrs-server's Introduction

cqrs-server

An opinionated CQRS/ES implementation using Onyx, Datomic, DynamoDB, Kafka and Zookeeper.

Usage

A quick guide to get started :

Install dynamodb local

Get dynamodb local from: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html

And then run:

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar

Kafka & Zookeeper

Download and extract Kafka: http://kafka.apache.org/downloads.html

Run both these (probably in separate terminals)

bin/zookeeper-server-start.sh config/zookeeper.properties
bin/kafka-server-start.sh config/server.properties

Currently, this needs the latest SNAPSHOT version of Prismatic Schema, so clone that repo and install the jar:

git clone https://github.com/Prismatic/schema.git
cd schema
lein cljx once
lein install

cqrs-server

Then clone this repo and fire it up!

cd cqrs-server
lein repl

=> (start)
"Setup complete"
=> (send-command :user/register {:name "Bob" :age 31})
nil
=> (d/q '[:find [?e ...] :where [?e :user/name]] (d/db (d/connect datomic-uri)))
[17592186045422]
=> (map #(d/touch (d/entity (d/db (d/connect datomic-uri)) %)) *1)
({:base/uuid #uuid "54d8fc2e-6c1f-4fb6-93f9-bef9536a9f7d", :user/age 31, :user/name "Bob", :db/id 17592186045422})

Now we have a user in the system, let's fill out his profile a bit:

=> (send-command :user/update-email {:uuid #uuid "54d8fc2e-6c1f-4fb6-93f9-bef9536a9f7d" :email "[email protected]"})
=> (send-command :user/disabled {:uuid #uuid "54d8fc2e-6c1f-4fb6-93f9-bef9536a9f7d"})
=> (map #(d/touch (d/entity (d/db (d/connect datomic-uri)) %)) (d/q '[:find [?e ...] :where [?e :user/name]] (d/db (d/connect datomic-uri))))
({:base/uuid #uuid "54d90a89-0880-4f30-bb34-42f29ceb1095", :user/age 31, :user/email "[email protected]", :user/name "Bob", :user/status :user.status/disabled, :db/id 17592186045422})

We can also send some pageviews and see how it updates the viewcount on the user (a possibly useful aggregate):

=> (send-command :user/pageview {:uuid #uuid "54d90a89-0880-4f30-bb34-42f29ceb1095" :url "http://www.example.com" :render-time 230})
=> (send-command :user/pageview {:uuid #uuid "54d90a89-0880-4f30-bb34-42f29ceb1095" :url "http://www.example.com" :render-time 212})
=> (send-command :user/pageview {:uuid #uuid "54d90a89-0880-4f30-bb34-42f29ceb1095" :url "http://www.example.com" :render-time 182})
=> (map #(d/touch (d/entity (d/db (d/connect datomic-uri)) %)) (d/q '[:find [?e ...] :where [?e :user/name]] (d/db (d/connect datomic-uri))))
({:base/uuid #uuid "54d90a89-0880-4f30-bb34-42f29ceb1095", :user/age 31, :user/email "[email protected]", :user/name "Bob", :user/status :user.status/disabled, :user/viewcount 3, :db/id 17592186045422})

Then lets have a look at the events:

=> (far/scan dynamodb-cred :events)
[{:date 1423510307575N, :data #<byte[] ...>, :basis-t 1008N, :id "86439637-8f1e-5170-9b23-824486e3506a", :type "user/pageviewed"} {:date 1423510178427N, :data #<byte[] ...>, :basis-t 1005N, :id "c67ccc74-c71c-5578-80ad-924c470f052f", :type "user/email-updated"} {:date 1423510316827N, :data #<byte[] ...>, :basis-t 1010N, :id "08316c9b-3fcd-5a9f-b095-4bf0c1a61a05", :type "user/pageviewed"} {:date 1423510210618N, :data #<byte[] ...>, :basis-t 1007N, :id "46ac00c9-bd7d-5903-91e0-af56d28ef751", :type "user/disabled"} {:date 1423510153513N, :data #<byte[] ...>, :basis-t 1000N, :id "be856c9c-0bf8-5ccc-bec1-bfa0f5a7e983", :type "user/registered"} {:date 1423510312463N, :data #<byte[] ...>, :basis-t 1009N, :id "5c2eb804-1016-5fa3-a868-c01b515f980d", :type "user/pageviewed"}]

The actual data is fressian encoded so that there's no pain with the transformation of clojure data structures.

NOTE: If you do actually use this for user aggregates and authentication, remember to at least bcrypt your passwords. Be very aware that sensitive data is written to multiple places in this system: the kafka queues, the dynamo event source and the datomic aggregate. This is a particularly important consideration for things like credit card details and passwords.

Datomic-pro

The default profile uses datomic-free - if you want to use datomic-pro, start the repl with lein with-profile prod

When you use cqrs-server as a library to your application project, you'll probably want to add it as a dependency with an exclusion: [cqrs-server "0.1.0-SNAPSHOT" :exclusions [com.datomic/datomic-free]]

Tests

Because this project depends on correct integration with kafka, dynamodb and zookeeper, the tests require that you have these running locally before you run lein test. See above in order to get them installed and started.

License

Copyright © 2015 Yuppiechef Online (Pty) Ltd.

Distributed under The MIT License (MIT) - See LICENSE.txt

cqrs-server's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cqrs-server's Issues

Missing dependencies

hi there great article BTW

Could not find artifact prismatic:schema:jar:0.3.8-SNAPSHOT in clojars (https://clojars.org/repo/)
Could not find artifact com.datomic:datomic-pro:jar:0.9.5130 in central (https://repo1.maven.org/maven2/)
Could not find artifact com.datomic:datomic-pro:jar:0.9.5130 in clojars (https://clojars.org/repo/)
Could not find artifact yuppiechef:onyx-dynamodb:jar:0.5.0 in central (https://repo1.maven.org/maven2/)
Could not find artifact yuppiechef:onyx-dynamodb:jar:0.5.0 in clojars (https://clojars.org/repo/)

Exception and response flow

if there are exceptions in any command-coerce, process-command, aggregate-event or prepare-store, it kills the running job (rightly so).

Need to implement a flow where everything goes to a Dead Letter Queue (DLQ) box when there are problems by default. This means that we also need some way of handling the DLQ. I can think of a few scenario's:

  • Code is buggy, the message would have processed ok. Reprocess.
  • Message is in the wrong format. Fix sender and resend (or fix message and reprocess).
  • System availability issues. Reprocess automatically.

On top of this, the message could be time-sensitive. Introduce an expiry time on the command so that the DLQ can get cleared of expired messages.

Unable to run on windows

I have tried running this within cygwin on Windows 7. I created a local branch based off commit id 0870736 and made changes as suggested in #2. However, I still get the following error :-

$ lein repl
nREPL server started on port 25160 on host 127.0.0.1 - nrepl://127.0.0.1:25160
REPL-y 0.3.7, nREPL 0.2.12
Clojure 1.7.0-beta1
Java HotSpot(TM) 64-Bit Server VM 1.8.0_25-b18
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> (start)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: start in this :(C:\Users
\abcdef\AppData\Local\Temp\form-init2467476946444377470.clj:1:1)
user=>

I have followed all the steps on the README with edits/suggestions as in #2.
Please help me fix this.
TIA

Datomic *and* event store?

This is a really neat project, but I had a question about your example architecture diagram. Why do you have both Datomic and an ES? I'm just getting familiar with Datomic, but one of the things that first attracted me to it was that -- through it's ability to time-travel -- you get a sort of ES for free (i.e. ES while still getting to think in aggregates). Why did you decide to break the two apart and use Datomic as just an aggregate database? I guess it has to do with the granularity of the transactions you want to put in Datomic that are at a higher level than an event?

Missing core.clj?

I was curious to see what you had done for onyx 0.6, so I built from scratch.
I might be missing something but there ought to be a core.clj, right?

user=> (start)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: start in this context, compiling:(/tmp/form-init1533634946170111069.clj:1:1) 

Why store in DynamoDB when you have Kafka?

Hey Deon,

Really love this idea. I've been reading quite a bit about event sourcing myself, and came across this a few weeks ago. I think this might be a very good fit for Cognician too. Thanks for the writeup and resources!

Just wanted to ask you a quick question regarding your storing of events in DynamoDB. Why do this when Kafka is already persisting them to disk safely? Are you doing this for indexing purposes?

How do you intend this to be used?

Is this a reference implementation? I only ask because after poking though the codebase it appears that I'll have to reimplement the core and module namespace. Core because it's mostly configuration data and module because that's where the messaging is defined.

Just trying to understand what your plans are :).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.