Slide 1

Slide 1 text

@ Alexey Kachayev | October, 2017

Slide 2

Slide 2 text

Who We Are • 3k+ mobile apps for events • Content management system • Social network for attendees • Leads retrieval mobile application • Marketing automation platform • Event registration system (coming soon)

Slide 3

Slide 3 text

In Figures • 6.5k events with 492k speakers • 3.2M mobile devices • 1.5M+ users shared 1.3M posts with 10.7M likes • Mobile API with 19K req/min at peak • 118 repos and 25+ services (micro!) • 4B+ messages in Kafka (weekly) • 230M+ documents in ElasticSearch

Slide 4

Slide 4 text

What Do We Use Clojure For? • For everything • No, seriously

Slide 5

Slide 5 text

Elaborate? • Application servers • Data pipeline • ETL jobs • Internal tools • UI • Scheduling, talking to databases, data manipulation, … you name it

Slide 6

Slide 6 text

Clojure at Attendify • Main & default language • 4+ years in production • 12 engineers (and hiring) • CLOC: 97K+ Clojure, 68K+ ClojureScript • It’s still a technology, not magic

Slide 7

Slide 7 text

Why Do We Use Clojure? • Because we like it • No, seriously

Slide 8

Slide 8 text

Elaborate? • Clojure = ideas + access to Java libraries and JVM - immutability, epochal time model, atoms, transformation chains, EDN, uniform domain modeling, declarative approach, runtime polymorphism with custom dispatch etc • It’s easier to use Clojure ideas in Clojure - no, seriously - “haskell in python” << “python” & “haskell in python” << “haskell” - btw, “java in clojure” >> “java” - discipline & convention don’t work, language core does

Slide 9

Slide 9 text

Agenda • Libraries & ecosystem • Servers & microservices • Data • Errors handling • Project setup & dev tools

Slide 10

Slide 10 text

Libraries & ecosystem • A lot of stuff - Clojars, Clojure Toolbox, ClojureWerkz • You still have Java - There are a lot of wrappers, but when it’s necessary… - You still have tooling: GC logs, OOM analyzers, profilers and more • You still work with Maven - lein, wagon, checkouts

Slide 11

Slide 11 text

Servers & Microservices • JSON-RPC and GraphQL, not REST - Might be EDN or MsgPack instead of JSON • aleph: smart wrapper for Netty - With ring & compojure • manifold, not core.async - Futures & chaining, executors, scheduling • Own library to define/use RPC - There are a few open-source libraries, like slacker and castra, tho’

Slide 12

Slide 12 text

Servers & Microservices (defservice “audience.fetchTimeline" :allowed-auth #{:builder} :validate-params {:profileId s/Str (s/optional-key :includeTypes) [s/Str] (s/optional-key :pageSize) s/Int} :response-format Timeline (fn [{:keys [timeline-handler]} {:keys [profileId]}] (timeline/timeline-entries …)))

Slide 13

Slide 13 text

Servers & Microservices (defservice “audience.fetchTimeline" :allowed-auth #{:builder} :validate-params {:profileId s/Str (s/optional-key :includeTypes) [s/Str] (s/optional-key :pageSize) s/Int} :response-format Timeline (fn [{:keys [timeline-handler]} {:keys [profileId]}] (timeline/timeline-entries …))) macro method name component(s) call params schema enforced

Slide 14

Slide 14 text

Servers & Microservices (rpc/call http-client {:method “audience.fetchTimeline” :params {:profileId “42”}})

Slide 15

Slide 15 text

Servers & Microservices (rpc/call http-client {:method “audience.fetchTimeline” :params {:profileId “42”}}) manifold.deferred component with aleph.http

Slide 16

Slide 16 text

Servers & Microservices • defmulti rpc/execute • defservice compiles down to defmethod • ring route to call rpc/execute • ring handlers to take care about - Logging - Errors handling - Serialization format (JSON by default)

Slide 17

Slide 17 text

Servers & Microservices (defservice “audience.fetchTimeline" :allowed-auth #{:builder} :validate-params {:profileId s/Str (s/optional-key :includeTypes) [s/Str] (s/optional-key :pageSize) s/Int} :response-format Timeline :dispatch-on rpc/execute (fn [{:keys [timeline-handler]} {:keys [profileId]}] (timeline/timeline-entries …))) :dispatch-on rpc/execute “magic”

Slide 18

Slide 18 text

Data • korma & hikari to work with PostgreSQL - Not really happy • Java API for Kafka producers/consumers - Own wrapper, but there are a few open-sourced, like kinsky & clj-kafka • carmine to work with Redis - Used to use built-in tasks/jobs queue, not now • Event sourcing with our own library - There are a few open-source libraries, like rill and cqrs-server, tho’

Slide 19

Slide 19 text

Data: Korma (In Theory) (defentity applications (table “application”) (pk :id) (transform #(update % :name capitalize))) (defn select-app [{:keys [conn]} app-id] (-> (select applications (db conn) (fields :name :icon) (where {:id app-id})) first))

Slide 20

Slide 20 text

Data: Korma (In Practice) • Connections pooling • Sophisticated queries • Serialization handling (array, hstore, jsonb) • Transactions, retries, rollbacks • java.sql.SQLException • Tables partitioning

Slide 21

Slide 21 text

Data: Korma (In Practice) (defn fetch-profile-with-sources* [db apikey id] (->> (models/execute-query db [(format "SELECT ps.*, ss.integration_id as source_integration_id, ss.remote_id as source_remote_id, ss.payload as source_payload, ss.is_deactivated as source_is_deactivated FROM %s ps LEFT OUTER JOIN %s ss on (ps.id = ss.profile_id) WHERE ps.apikey = ? AND ps.id = ?" (tables/route tables/profile-tbl apikey) (tables/route tables/profile-source-tbl apikey)) [apikey id]] {:raw-results? true}) (functor/fmap (fn [profiles] (->> profiles utils/aggregate-profiles (map transform-profile) first (#(dissoc % :password)))))))

Slide 22

Slide 22 text

Data: Korma (In Practice) (defn fetch-profile-with-sources* [db apikey id] (->> (models/execute-query db [(format "SELECT ps.*, ss.integration_id as source_integration_id, ss.remote_id as source_remote_id, ss.payload as source_payload, ss.is_deactivated as source_is_deactivated FROM %s ps LEFT OUTER JOIN %s ss on (ps.id = ss.profile_id) WHERE ps.apikey = ? AND ps.id = ?" (tables/route tables/profile-tbl apikey) (tables/route tables/profile-source-tbl apikey)) [apikey id]] {:raw-results? true}) (functor/fmap (fn [profiles] (->> profiles utils/aggregate-profiles (map transform-profile) first (#(dissoc % :password))))))) query, not DSL component not entities either manual transform partitioning

Slide 23

Slide 23 text

Data: Kafka Consumer • Internal library • Manage threads, subscribe/unsubscribe • Handle serialization • Exceptions, errors • Commit offsets

Slide 24

Slide 24 text

Data: Kafka Consumer (component/using (kafka/new-whisper {:name "pulse-audience-latest" :topic “attendify.s.a.v1" :on-message (partial pulse-audience/notify! pulse-audience-expiration-threshold pulse-audience-cache-ttl-ms) :threads 32 :kafka-config (assoc kafka-config :offset-reset "latest")}) [:redis-connector :db])

Slide 25

Slide 25 text

Data: Kafka Consumer (component/using (kafka/new-whisper {:name "pulse-audience-latest" :topic “attendify.s.a.v1" :on-message (partial pulse-audience/notify! pulse-audience-expiration-threshold pulse-audience-cache-ttl-ms) :threads 32 :kafka-config (assoc kafka-config :offset-reset "latest")}) [:redis-connector :db]) internal library sub/unsub consumers called on each message passed as a first arg

Slide 26

Slide 26 text

Errors Handling • Most of your production code - No, seriously • No good story here - Clojure gives us exceptions and nil, only • Own library for either - cats seems to be the standard here, but no one cares about monads, right? • Typing your data, not your code - schema instead of spec

Slide 27

Slide 27 text

Errors Handling (In Practice) (->> (pg-profile/fetch-profile-by-email* db' apikey email) (either/bind #(check-profile-nil kp apikey token email %)) (either/bind #(check-profile-deleted kp apikey token email %)) (either/bind #(check-profile-claimed-or-registered kp apikey token email %)) (either/bind #(check-valid-password kp apikey token email % password)) (either/bind #(deactivate-old-sessions db' apikey % token)) (either/bind (fn [profile] (let [session-id (or session-id (generate-session-id)) now (Timestamp/from (Instant/now))] (—>> (insert-session db' …) (either/fmap (fn [session] {…})))))))

Slide 28

Slide 28 text

Errors Handling (In Practice) (->> (pg-profile/fetch-profile-by-email* db' apikey email) (either/bind #(check-profile-nil kp apikey token email %)) (either/bind #(check-profile-deleted kp apikey token email %)) (either/bind #(check-profile-claimed-or-registered kp apikey token email %)) (either/bind #(check-valid-password kp apikey token email % password)) (either/bind #(deactivate-old-sessions db' apikey % token)) (either/bind (fn [profile] (let [session-id (or session-id (generate-session-id)) now (Timestamp/from (Instant/now))] (—>> (insert-session db' …) (either/fmap (fn [session] {…}))))))) errors handling

Slide 29

Slide 29 text

Errors Handling (In Practice) (lete [profiles (fetch-access-keys-by-events db apikey restricted-event-ids) labels (labels/fetch-labels {:db db} apikey {:type "access-key"}) events-access-keys (->> labels (filter #(some (set (:events %)) restricted-event-ids)) (map :id)) profile-ids-blacklist (->> profiles (remove #(some events-access-keys (vec (.getArray (:access_keys %))))) (map :id))] (perform-fetching-by-events db apikey event-ids profile-ids-blacklist))

Slide 30

Slide 30 text

Errors Handling (In Practice) (lete [profiles (fetch-access-keys-by-events db apikey restricted-event-ids) labels (labels/fetch-labels {:db db} apikey {:type "access-key"}) events-access-keys (->> labels (filter #(some (set (:events %)) restricted-event-ids)) (map :id)) profile-ids-blacklist (->> profiles (remove #(some events-access-keys (vec (.getArray (:access_keys %))))) (map :id))] (perform-fetching-by-events db apikey event-ids profile-ids-blacklist)) macro deferred[either[a’]] deferred[either[a’]]

Slide 31

Slide 31 text

Project Setup • Configuration is always tricky - env library, .lein-env-dev files • components - Manage (stateful) world around you - Show code dependencies • dev/user.clj - With (go) and (reset) • nREPL - Try ultra (plugin for lein)

Slide 32

Slide 32 text

Project Setup: component (defrecord CSVExporter [] component/Lifecycle (start [component] (if (:executor component) component (assoc component :executor (e/fixed-thread-executor threads-count)))) (stop [component] (when-let [executor (:executor component)] (.shutdownNow executor)) (assoc component :executor nil)))

Slide 33

Slide 33 text

Project Setup: component (defrecord CSVExporter [] component/Lifecycle (start [component] (if (:executor component) component (assoc component :executor (e/fixed-thread-executor threads-count)))) (stop [component] (when-let [executor (:executor component)] (.shutdownNow executor)) (assoc component :executor nil))) prepare cleanup

Slide 34

Slide 34 text

Project Setup: component (component/system-map … :csv-exporter (component/using (export/new-csv-exporter {:immediate-export-limit csv-immediate-export-limit :immediate-export-timeout csv-immediate-export-timeout :threads-count csv-export-threads-count}) [:s3-client :email-notifier :profiles-search-handler :elastic-client :settings-event-store]) … ) (e/export csv-exporter account-id other-params)

Slide 35

Slide 35 text

Project Setup: component (component/system-map … :csv-exporter (component/using (export/new-csv-exporter {:immediate-export-limit csv-immediate-export-limit :immediate-export-timeout csv-immediate-export-timeout :threads-count csv-export-threads-count}) [:s3-client :email-notifier :profiles-search-handler :elastic-client :settings-event-store]) … ) (e/export csv-exporter account-id other-params) component config deps convention

Slide 36

Slide 36 text

Project Setup: REPL $ lein repl nREPL server started on port 63248 on host 127.0.0.1 - nrepl://127.0.0.1:63248 user=> (go) :started user=> (keys @system) (:mailchimp-client :app-component :kafka-producer …) user=> (:s3-client @system) #augustine.s3.S3Client{:url …} user=> (reset) :reloading (…) :resumed user=>

Slide 37

Slide 37 text

Project Setup: REPL $ lein repl nREPL server started on port 63248 on host 127.0.0.1 - nrepl://127.0.0.1:63248 user=> (go) :started user=> (keys @system) (:mailchimp-client :app-component :kafka-producer …) user=> (:s3-client @system) #augustine.s3.S3Client{:url …} user=> (reset) :reloading (…) :resumed user=> try this once

Slide 38

Slide 38 text

Q&A