Production Clojure: An Experience Report

39f90a6c0ffe4995fb9dff4fb6b6bad6?s=47 Shantanu Kumar
September 12, 2015

Production Clojure: An Experience Report

Talk slides from Functional Conf 2015:
http://confengine.com/functional-conf-2015/schedule#session-7456-info

Experience of using Clojure at Concur as of September 2015

39f90a6c0ffe4995fb9dff4fb6b6bad6?s=128

Shantanu Kumar

September 12, 2015
Tweet

Transcript

  1. Production Clojure: An Experience Report Shantanu Kumar, Concur @kumarshantanu 1

  2. Clojure @ Concur Rebuilding for scale, resilience, harmony 2

  3. Scope • Rebuilding the core platform • Pervasive decoupling and

    fault tolerance • Common services across products • Authentication, Authorization, Profile, Identity… 3
  4. Approach • Web services (microservices) • Shared nothing architecture •

    N, N+1 versions • Open Standards • REST • JSON • JSON Web Token (JWT) • X509, PKI • OAuth2, SAML 4
  5. Building microservices:
 First cut — The prototype • Leiningen (for

    builds) • Configuration properties file • Ring + Compojure • Jetty server (via uberjar) • clojure/java.jdbc (SQL Server) • couchbase-clj • 150-200ms response time 5
  6. –Steve Brown “Anything worth doing is worth doing poorly until

    you learn to do it well.” 6
  7. Initial challenges • Application configuration/initialization • Logging • Policy based

    resiliency • Performance metrics/tuning 7
  8. Challenge mitigation 8 (polite term for a framework) Developed a

    web service “Shell”
  9. “Shell” constituents • Logging: SLF4j + Logback + tools.logging +

    JSON • Mapped Diagnostic Context (MDC) wrapper • Web server: http-kit • JSON serialization: Cheshire • c3p0 (Instrumented JDBC connection pool) • Clojure equivalent of Hystrix core 9
  10. “Shell” App initialization 10

  11. “Shell” Resiliency • In-house Clojure equivalent of Netflix Hystrix-core •

    Circuit breaker, Semaphore, Thread-pool, Metrics • Integrates with Hystrix-dashboard via SSE 11
  12. Performance (after tuning) • Sub milli-second response time (within datacenter)

    • Because in microservices, every service adds up • Measure in ns, optimize microseconds • Simulate load, stress for long duration 12
  13. External components • ElasticSearch, Logstash, Kibana (ELK) • Appdynamics •

    Yourkit profiler • Health checks (HTTP) • SQL Server • Couchbase 13
  14. Clojure impressions • First class values • Immutability, isolation (safety)

    • Functional programming (HoF, Transducers) • REPL • Easy side effects (I/O) • Java interop • Small language, easy to learn & reason about • Homoiconicity and macros 14
  15. ## First class values 15

  16. ## Immutability, Isolation (Safety) • Epochal time model • Pro

    concurrency • Persistent data structures • Good performance 16
  17. ## Functional Programming 17

  18. ## Easy side effects 18

  19. ## Java Interop 19

  20. ## Homoiconicity & Macros 20

  21. Practices • IDE/editor — To each his own (Emacs, Eclipse,

    vi) • Most things are configurable (properties file) • Tests and launch must work from command line • Unit and integration tests are a must • Docker in development, QA and production • Internal Maven repository for JAR artifacts 21
  22. Libraries (Some factored out from web service “shell”) 22

  23. Configuration lookup — Keypin • https://github.com/kumarshantanu/keypin • Fail fast •

    Value parsing • Validation • Optional default 23
  24. Keypin example 24

  25. String concatenation — Stringer • https://github.com/kumarshantanu/stringer • Fast string concatenation

    • Pre-concatenation of literals at compile time • Caveat — not function 25
  26. Stringer latency 26

  27. Ring request matching — CalfPath • https://github.com/kumarshantanu/calfpath • À la

    carte • Low overhead • Composable 27
  28. CalfPath latency 28

  29. JDBC access — Asphalt • https://github.com/kumarshantanu/asphalt • Functional design •

    Named parameters • Extensible (works with SQL generation libraries) • Precise control via type hints 29
  30. Asphalt example 30

  31. Comparative micro- benchmarking — Citius • https://github.com/kumarshantanu/citius • Comparative benchmarks

    using Criterium • Simulate load with concurrency • Setup can be tweaked • Built-in comparative latency bar charts 31
  32. Citius example 32

  33. Inter-layer latency finder — Espejito • https://github.com/kumarshantanu/espejito • Finds latency

    across layers • Summary reporting (customizable) • Low overhead • Programmable for conditional trigger 33
  34. Espejito examples 34

  35. http://j.mp/packt-chpp2 35

  36. Learning • Clojure (the language) is very well designed •

    Library eco-system is great, but still maturing • Java-interop is not the panacea • Adoption: Status-quo > other FP languages • Parentheses may cause knee-jerk reaction • Being on the JVM is a huge win 36
  37. Thank you! @kumarshantanu 37