Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lessons from 4 Years with Datomic

Lessons from 4 Years with Datomic

We’ll starting with a crash course on the Datomic data model, and then switch gears to talking about all sorts of data-modeling problems and their solutions.

Presented at ClojureRemote 2017, February 2017.
Recording: https://youtu.be/4BhZshZyf9E

Robert Stuttaford

December 29, 2017
Tweet

More Decks by Robert Stuttaford

Other Decks in Technology

Transcript

  1. View Slide

  2. Hermanus, South Africa!
    Hello from

    View Slide

  3. Lessons from 4 years with
    Datomic

    View Slide

  4. Digital self-coaching, in the workplace
    Robert Stuttaford, CTO at Cognician
    Twitter: RobStuttaford Github: robert-stuttaford

    View Slide

  5. Clojure & ClojureScript since April 2012.
    Datomic since September 2012.
    In production since Jan 2013!
    Digital self-coaching, in the workplace

    View Slide

  6. Digital self-coaching, in the workplace
    50,265,275 lovingly crafted, by-a-real-person transactions!
    Clojure & ClojureScript since April 2012.
    Datomic since September 2012.
    In production since Jan 2013!

    View Slide

  7. Before we dive in… some thanks:
    The Clojure community, for embodying the spirit of
    sharing and collaboration Rich set out when he started.
    Ryan and the CR17 team, for the opportunity to speak,
    and the excellent organisational work.
    All of you, for your time and attention.
    Thank you!

    View Slide

  8. And, a very special thank you to the Datomic team:
    Rich Hickey
    Stuart Halloway
    Tim Ewald
    Chris Redinger
    David Nolen
    Marshall Thompson
    Jaret Binford 

    (and alums Bobby Calderwood and Ben Kamphaus)
    No doubt many more.
    These folks have made our lives at Cognician a whole lot better.
    Thank you!

    View Slide

  9. On questions during this talk:
    I’m happy to take questions, 

    but I prefer to answer them after my talk.


    I’m very happy to do a breakout session where we go
    Wild West and mess around in a REPL.
    MC Ryan, please poll our audience to see 

    who’d like to attend a Datomic breakout session?

    View Slide

  10. Disclaimer:
    I’m just a normal Datomic user.
    All mistakes are mine.
    Please, please correct me if I’m wrong!

    View Slide

  11. Part 1
    The underlying data model

    View Slide

  12. To the Emacs-mobile!

    View Slide

  13. View Slide

  14. Part 2
    Some lessons from doing this for a while

    View Slide

  15. Part 2
    Some lessons from doing this for a while
    (Some of these may be eye-rollingly obvious; we were newbies, I promise!)

    View Slide

  16. Database values as global vars.
    This includes wrapping over a function that automatically calls
    (d/db (d/connect ...)) for you in a data access layer, so that
    client functions don’t have to worry about it.
    Just. Don't. Do it.
    Treat databases like any other value: pass it around! 

    Keep those functions pure!
    A tip for web apps: use middleware to inject a db value into your requests,
    and pass it around from there. This keeps reads for a request on a
    consistent view of your data.

    View Slide

  17. Source of truth for schema.
    It’s your database, not your source code.
    Schema in source code is useful
    e.g. for creating testing databases, 

    but look to your database for the actual truth.
    Get comfortable with doing this. You’ll save yourself time!
    (I have something to share at the end, that’ll help you with this.)

    View Slide

  18. Annotate your transactions.
    Seriously. You’ll really wish you did, later.
    This is a great place to wrap the Datomic API with your own code.

    View Slide

  19. Annotate your transactions.

    View Slide

  20. Cardinality-many retractions gotcha.
    New folks often think that to update a :cardinality/many attribute,
    they can simply assert the new collection, and magically
    Datomic will retract all the values not present in the new collection.
    Some folks get … annoyed, when it doesn’t do this.

    View Slide

  21. Cardinality-many retractions gotcha.
    I’ve got your back:
    New folks often think that to update a :cardinality/many attribute,
    they can simply assert the new collection, and magically
    Datomic will retract all the values not present in the new collection.

    View Slide

  22. Use Datalog when it’s actually necessary.

    View Slide

  23. Use Datalog when it’s actually necessary.
    I particularly encourage you to try to model your queries
    in terms of d/datoms and normal Clojure functions, 

    even if just to build your understanding of this stuff.

    View Slide

  24. Whew. We’re done!

    View Slide

  25. What I hope you got from this.
    Knowing how the data is modelled by Datomic:
    Allows you to work with it more like any other
    in-memory Clojure data-structure, 

    rather than data “over there in the database”.
    Gives you a major advantage when learning the APIs.
    Greatly assists you when reasoning about scale.
    And some practical tips!

    View Slide

  26. Datomic breakout session
    MC Ryan: poll result?

    View Slide

  27. One last thing :-)
    That’s all, folks. Thank you!
    github.com/cognician/datomic-doc
    Check out Datomic Doc, 

    a library I released recently, 

    for editing :db/doc strings in a web UI:
    Twitter: RobStuttaford Github: robert-stuttaford

    View Slide