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

96857e2f67b65f97825d2de491a17808?s=128

Robert Stuttaford

December 29, 2017
Tweet

Transcript

  1. 1.
  2. 4.

    Digital self-coaching, in the workplace Robert Stuttaford, CTO at Cognician

    Twitter: RobStuttaford Github: robert-stuttaford
  3. 5.

    Clojure & ClojureScript since April 2012. Datomic since September 2012.

    In production since Jan 2013! Digital self-coaching, in the workplace
  4. 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!
  5. 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!
  6. 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!
  7. 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?
  8. 10.

    Disclaimer: I’m just a normal Datomic user. All mistakes are

    mine. Please, please correct me if I’m wrong!
  9. 13.
  10. 15.

    Part 2 Some lessons from doing this for a while

    (Some of these may be eye-rollingly obvious; we were newbies, I promise!)
  11. 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.
  12. 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.)
  13. 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.
  14. 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.
  15. 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.
  16. 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.
  17. 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!
  18. 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