Clojure in production: what do we use in real-world services?
Summary
TLDRこのビデオでは、プロダクションで使用するClojure言語のライブラリについて議論しています。開発者Andréは、言語を始めた初心者が何を使えば良いか、また何を避けるべきかを解説します。10年間開発を経験し、JavaからClojureに移行したAndréは、言語と関数型プログラミングを楽しんでいると語ります。プロジェクトの作成方法や依存性の管理、コンポーネント管理、設定、HTTPサーバーの立ち上げ方、HTTPリクエスト、リレーショナルデータベースとの連携、スキーマとバリデーション、その他有用なライブラリやツールについて詳述。特に推奨するものや避けるべきライブラリを紹介し、プロダクションコードでのライブラリ選択のガイドラインを提供しています。
Takeaways
- 👨💻 スクリプトの分析: 今回のビデオはClojure言語のライブラリ選択について議論し、初心者向けに役立つ情報を提供している。
- 🔧 開発ツールの推奨: ビデオでは、プロダクションで使用するツールとして`boot`は推奨されず、代わりに`deps.edn`を使用することが推奨されている。
- 📚 プロジェクト管理: 小規模プロジェクトでは自前でプロジェクトを管理することも可能だが、大規模プロジェクトではライブラリを使用して管理することが推奨されている。
- 🚫 マウントライブラリの非推奨: 開発者がグローバルな状態を直接使用し、関数プログラミングの概念を損なうため、マウントライブラリは推奨されていない。
- 🔄 コンポーネント管理: `component`ライブラリや`integrand`ライブラリを使用して、システムのコンポーネントを管理することが推奨されている。
- 📝 設定管理: `configurator`ライブラリは推奨されておらず、`IRL`ライブラリがシンプルで効果的な設定管理ツールとして紹介されている。
- 🌐 HTTPサーバーの選択: `JT`をベースに`ring`や`pedestal`を使用し、HTTPルーティングには`bidi`、`composure`、`pedestal`などのライブラリが挙げられている。
- 🚫 HTTP Kitの非推奨: モニタリングツールとの統合が困難なため、HTTP Kitの使用は推奨されていない。
- 🔗 HTTPリクエスト: `clj-http`ライブラリが推奨されており、Java 11以上であればJavaの組み込みHTTPクライアントも選択肢として挙げられている。
- 🗃️ データベース操作: `next-jdbc`と`HikariCP`が推奨されており、SQLの記述には`honeysql`と`hugsql`という2つのアプローチが紹介されている。
- 📑 スキーマとバリデーション: `schema`ライブラリ、`clojure.spec`、`malli`という3つのバリデーションライブラリが挙げられており、それぞれ異なるアプローチを持っている。
- 🛠️ 他の一般的なライブラリ: `core.cache`、`core.memoize`、`cheshire`、`claypoole`などがプロダクションコードで一般的に使用されている。
- 🛑 追加ツール: `clj-kondo`、`cljfmt`、`babashka`などのツールがプロダクションサービスで一般的に使用されていると紹介されている。
Q & A
ビデオの主な目的は何ですか?
-ビデオの主な目的は、プロダクションで使用するべきライブラリについて議論し、言語を始めたばかりの開発者に対して、使用すべきものと使用しないようにするものを知らせることで、彼らのクロジャー言語の旅を助けることです。
クロジャー言語での開発を始めたばかりの人たちに何を勧めますか?
-クロジャー言語での開発を始めたばかりの人たちには、最初はフレームワークを組み合わせずに必要なライブラリを組み合わせてプロジェクトを構築することを勧めます。これは学習曲線が異なるかもしれませんが、すべてのパーツがどのように機能するかを理解するのに役立ちます。
クロジャー言語のプロジェクトを作成し、依存関係を管理するのにどのツールを使用することが推奨されますか?
-クロジャー言語のプロジェクトを作成し、依存関係を管理するためには、LanternとClojureのdeps.ednという2つのソリッドなオプションがあります。最近はClojureのdeps.ednを試して良い結果を得ており、新しいプロジェクトにはこれを標準として使用する予定です。
ビデオではどのライブラリを使用してシステムのコンポーネントを管理することが推奨されますか?
-ビデオでは、コンポーネントライブラリまたはIntegrandライブラリを使用してシステムのコンポーネントを管理することが推奨されています。特にIntegrandは最近試し始めており、良い結果を得ています。
ビデオでは設定管理ライブラリとしてどのライブラリを使用することが推奨されますか?
-ビデオでは設定管理ライブラリとして、configuratorは推奨されませんが、envやAndroidライブラリの上にちょっとしたラッパーを提供するIRLライブラリが推奨されています。
ビデオではHTTPサーバーを立ち上げるときどのライブラリを使用することが推奨されますか?
-ビデオではHTTPサーバーを立ち上げるとき、Jettyをベースとして使用し、その上にringやpedestalなどのライブラリを組み合わせて使用することが推奨されています。
ビデオではHTTPリクエストを行うためにどのライブラリを使用することが推奨されますか?
-ビデオではHTTPリクエストを行うためには、clj-httpライブラリを使用することが推奨されています。これはApache HTTPクライアントをバックエンドとして使用しており、多くの機能を提供しています。
ビデオではリレーショナルデータベースと連携する際にどのライブラリを使用することが推奨されますか?
-ビデオではリレーショナルデータベースと連携する際には、HikariCPを使用した接続プールやhoneysqlとhugsqlという2つのライブラリを使用することが推奨されています。
ビデオではスキーマとバリデーションのためにどのライブラリを使用することが推奨されますか?
-ビデオではスキーマとバリデーションのためには、schemaライブラリ、clojure.spec、そしてMalliライブラリが挙げられますが、特にMalliライブラリは非常に良いフィードバックを得ていると聞きます。
ビデオではプロダクションコードで一般的に使用される他のライブラリには何がありますか?
-ビデオではプロダクションコードで一般的に使用される他のライブラリとして、core.cache、core.memoize、cheshire、claypoolなどが挙げられますが、特にclaypoolは並列処理を行うための良い選択肢です。
ビデオではプロダクションサービスで使用される追加ツールには何がありますか?
-ビデオではプロダクションサービスで使用される追加ツールとして、clj-fmt(コードフォーマッター)、clj-kondo(リンター)、そしてbabashka(スクリプトをクロジャー言語で書けるツール)が挙げられます。
Outlines
😀 プロダクションコードにおけるライブラリ選択について
ビデオでは、プログラミング言語Clojureのライブラリ選択について議論します。Andreは10年間の開発経験を持ち、JavaからClojureに移行し、関数型プログラミングを楽しんでいます。プロダクションでのライブラリの選択は非常に重要で、初心者にとっては選択肢が多すぎてどのライブラリが良いかわかりません。Andreは自分が経験を通じて得た知識を共有し、初心者がどのライブラリを使うべきかを知るためのガイドラインを提供します。また、ウェブフレームワークは避けるべきで、必要なライブラリを組み合わせてプロジェクトを構築することが推奨されています。
🛠️ プロジェクトの作成と依存関係管理
プロダクションコードで依存関係を管理する方法として、Leiningenとtools.ednの2つのオプションが提案されています。Leiningenは以前のプロジェクトでよく使われていましたが、tools.ednは新しいプロジェクトで標準となっています。Clojureのエコシステムの一部であるtools.ednは今後ますます使われることが予想されます。また、Bootは推奨されません。
🔌 システムコンポーネントの管理
実際のアプリケーションでは、HTTPサーバー、データベース接続プール、Kafkaコンシューマーなど、多くのシステムコンポーネントを管理する必要があります。小さなプロジェクトでは自分で管理することも可能ですが、大きなプロジェクトではライブラリに任せることが推奨されています。Mountはグローバルな状態を許可するため推奨されませんが、componentライブラリやintegrandライブラリは良い選択肢です。
⚙️ 設定管理
設定管理については、configuratorライブラリは推奨されませんが、core.typedとAndroidライブラリ、特にIRLライブラリが推奨されています。IRLは機能と複雑性のバランスが良く、環境変数を明示的に扱うことができます。
🌐 HTTPサーバーの設定
HTTPサーバーの設定について、JTをベースにringやpedestalなどのライブラリを組み合わせて使用することが一般的です。HTTPルーティングには多くのライブラリがあり、比較のためのマトリックスも存在します。また、HTTP kitはモニタリングツールとの互換性の問題があるため推奨されません。
📚 リレーショナルデータベースとの連携
リレーショナルデータベースとの連携については、next.jdbcが標準となっています。HikariCPが接続プールとしてよく使われています。SQLクエリの作成にはHoney SQLとHugSQLという2つのアプローチがあります。Honey SQLではクロージャーのマップを使ってSQLを定義し、HugSQLではSQLファイルをプロジェクトに配置し、クエリを関数として扱うことができます。
📝 スキーマとバリデーション
スキーマとバリデーションについては、schemaライブラリ、clojure.spec、およびMaliという3つのオプションがあります。schemaはシンプルで使いやすい一方、clojure.specは学習曲線が高く、Maliは非常に良いフィードバックを得ています。
🛠️ その他のプロダクションコードで使われるライブラリ
プロダクションコードで使われるライブラリとして、core.cacheとcore.memoize、cheshire、Claypoolなどが挙げられます。cheshireは機能は豊富ですが、過剰なエンジニアリングを招くことがあるため注意が必要です。Claypoolは並列処理のための優れたライブラリです。
🛠️ 追加のプロダクションツール
プロダクションサービスで使われるツールとして、cljfmt(コードフォーマッター)、clj-kondo(リンター)、およびbabashka(スクリプト言語)があります。これらのツールはネイティブイメージとして利用でき、高速なスタートアップが可能です。特にbabashkaはCIパイプラインのスクリプトを書き換える際に非常に便利です。
Mindmap
Keywords
💡クロージャー言語
💡ライブラリ
💡プロダクションコード
💡依存性管理
💡コンポーネント管理
💡設定管理
💡HTTPサーバー
💡HTTPクライアント
💡データベース
💡スキーマと検証
💡並列処理
💡開発ツール
Highlights
Andre discusses libraries used in production for Clojure, aiming to guide beginners.
Started as a Java developer, Andre switched to Clojure due to enjoyment of functional programming.
Recommends starting with combining libraries instead of using web frameworks for learning purposes.
For project creation and dependency management, recommends using either lein or clj.
Notes a trend decline for lein in favor of clj, suggesting clj will be the new standard.
Advises against using boot in 2023, suggesting it's not worth the time to learn or try.
For managing components in a system, recommends against mount due to global state issues.
Suggests using component or integrand library for better component management.
Warns against using configurator for configuration due to unnecessary complexity.
Recommends the IRL library for configuration due to its balance of features and simplicity.
For HTTP servers, recommends using Jetty as a base with Ring or Pedestal on top.
Advises against using HTTP kit due to lack of monitoring tool support.
Recommends clj-http for HTTP requests or Java's built-in HTTP client for Java 11+.
For relational databases, suggests using nextjdbc for connection pools and HikariCP as an option.
Recommends honey SQL and hugSQL for writing and manipulating SQL queries.
Presents schema, clojure.spec, and Mali as options for schemas and validation.
Advises caution when using core.async to avoid over-engineering.
Highly recommends Claypool for running tasks in parallel with thread pools.
clj-fmt and clj-kondo are recommended for code formatting and linting.
Babashka is highlighted as a tool for writing scripts in Clojure, beneficial for CI pipelines.
Transcripts
hi it's Andre here the idea for today's
video is just to discuss which libraries
we use in production enclosure so if
you're just starting with the language
you know what you should use or what you
shouldn't
I've been a developer for 10 years I
started as a Java developer and after
that I switched to closure because I
really enjoyed the language and the
functional programming I mean in general
so after that it's been almost seven
years now when I'm doing the closure
full time and I gained some experience
with that so I just want to share with
you I think this kind of videos will be
really useful for beginners and who just
starting their
closure journey and it definitely would
be really useful for me when I started
because the selection of libraries is
really white and you don't really know
which is a good choice and which is not
and I would like to see this kind of
video when I started just to have some
guidance which libraries I should pick
in my project
so the before we start
let's mention one thing so I know there
are projects uh in the closure ecosystem
that's supposed to be web Frameworks uh
but I highly recommend if you're just
starting uh first time try to combine
some libraries
which you need and build it from the
scratch
so yeah the learning curve will be
different and you'll spend a bit more
time but it's really useful uh knowledge
at least to know how all these pieces
works together if you enjoy the
framework later that's fine
but if we're talking about production
code to be honest I never seen these
Frameworks were used in real production
code it was always some kind of
combination of libraries
that evolves with microsources as they
as they need
[Music]
right so how do we even create the
project and how we manage the
dependencies that we need in my opinion
there are two solid options one is
landingen and one is depths dot EDM
until recently all our projects were
managed by leningen but we started
experimenting with depths.edm we have
some good results and I think uh for
their new projects
depths.edm will be our new standard
so there is nothing wrong to use
learning game it works just fine no
complaints the only thing to mention is
according to the closure state survey
the trend for the learning game is
actually going down compared to the
devs.edn that's going up and considering
that depths.edm is actually a part of
the closure it makes sense to try that
or and at least like make yourself
familiar with depths.edn so you won't be
surprised if you see more and more
projects using that in future
so
it was also a time when it was more like
Lanigan versus boot but in 2023 I highly
don't recommend spend your time
learning or trying boot
so to recap if you're planning to if
you're using learning game already or
you don't and you don't want to spend
time rewriting it to depths.edn that's
totally fine that will work uh work as
expected uh you won't have any problems
but for the new projects and services uh
I would consider depths.edm
[Music]
moving on and the next topic is how we
managing components in our system so in
real world applications we have a bunch
of parts of the system of the service
that we should worry about
it usually will have some kind of HTTP
server then we have database connection
pools and we have Kafka consumers and
other processes that should be started
and also different types of caches and
the list can go on and on and we need
some some way to manage that start and
stop logic so we don't have to care
about the order and dependencies
so the first thing you can think about
is uh to do it yourself so just in main
function you can Define what to start in
which order and that's totally fine for
small projects for bigger projects you
just don't don't want to mess with that
too much and you just want to rely on
some kind of library that will do it for
you so it will just add a bit of
structure to your application and it
will be easier for everyone to
understand how exactly dependency Works
inside your servers so let's start with
one that I don't really recommend and
it's Mount the biggest problem I see
with this library is actually that it
allows the developers to write a quite
bad code from my opinion so the state
like this components are basically
Global so nothing is preventing
developers to
directly use that state inside somewhere
inside their functions and it basically
just ruins the entire concept of
functional programming when you have a
function and everything you need to pass
to that function is defined by the
arguments so that's my biggest problem
with this library and I don't really
recommend it plus I never seen it used
in production
um
uh where I worked so
moving on to good options from my point
of view and they are component library
or the integrand library so we are using
mostly component everywhere we are
starting experimenting with the
integrand but there is not much update
yet so my recommendation will be just to
check both compare them and select what
you like but it feels like both are
solid choices so you won't do a mistake
if you pick one of those
[Music]
cool the next topic should be a quick
one let's talk about the configuration
so if you're thinking about a library
called configurator I highly don't
recommend to use that because it adds a
lot of unnecessary features the syntax
is uh verbose it adds more complexity
than the value from my point of view so
if you have a something small you don't
have too many properties to pass using
the system get and function is totally
okay I've seen that in production the
other option is the Android library that
will give you a bit of a wrap on top of
that with some extra features but I
think the most solid Choice really is
the IRL Library it's a good balance
between features and
their complexity so it's really explicit
you just have your dot EDM file with
your configuration and you see all the
placeholders where you put
put your environment variables so yeah
highly recommend and a big plus for that
Library
[Music]
cool let's talk about what we use to
spin up the HTTP servers inside our
services
so in all the places I worked we just
use JT as the base and on top of that
sometimes we had ring sometimes we had
pedestal uh if we took talking about the
HTTP routing there are a huge list of
libraries available and to mention some
they are BD rated
composure and pedestal
and I don't have
um
good recommendation here just go and try
some of those and find what you like the
most also in the video docs there is a
good Matrix comparing all other
libraries and you can see all the
features and what's different so that
could be really useful
the one thing I should say is if you're
using a stack something like ring on top
of JT with the ring JT adapter and you
use composure on top of that
don't be afraid it's not amateur code
I've seen that in a lot of production
code as well so you're totally fine
uh
one more Library I should mention is
HTTP kit and it was quite popular but
I don't really recommend using that and
there is a reason for that
so the project itself is interesting and
it has like the HTTP server pod and the
HTTP client part so you basically have
one library to do both
the problem is that it's not really
widely used so if you
at some point would like to integrate uh
some monitoring tool inside your servers
something like that supports Java Auto
instrumentation datadoc agent neuralic
agent or even the open Telemetry Java
agent you'll be in trouble because HTTP
is not supported so you won't see your
incoming requests and you won't see the
distributed traces for your outgoing
HTTP requests so you will have to do
manual instrumentation or migrate away
from the HTTP kit and I'm just saying
that because I was just doing this
migration not long ago
[Music]
in addition to the HTTP service let's
talk about the HTTP requests because
usually we have to do some HTTP requests
in our services and we need to pick a
library for that a good choice for that
will be a clj HTTP which is using Apache
HTTP Library under the hood it has a lot
of features and we use it almost in all
services in production
but if you are on Java 11 or higher
potentially you want to use
the built-in Java HTTP client which
seems to be good starting from java 11.
and there are a couple libraries that
are wrappers of this Java API enclosure
unfortunately I haven't touched them too
much so I won't give you any suggestions
which one to use so explore on your own
but anyway if you pick the clj HTTP and
you don't mind extra dependencies for
this Apache HTTP client you'll be
totally fine
[Music]
so the next one is working with the
relational databases and that should be
a quick one as well so uh the the
standard for the gdpc library now is the
next gdbc uh for the uh connection pools
in most of places uh we were using the
Hikari CP works fine and we have no
problems with that
moving to the writing the SQL queries
itself
I would consider two different libraries
with two slightly different approaches
one is called honey SQL and the other
one is hog SQL so the honeysql the ID is
that you write your SQL
and use the closure maps to Define your
SQL queries
the benefit of that is that when you
define your SQL query as the closure
structure it basically has accessed all
the functions to manipulate the data so
you can add conditions and change your
query with with the code
there the other one I mentioned is hugs
equal and we use that a lot right now uh
the ID is different you basically have
your plain dot SQL files in your project
and then you define queries or update
insert delete statements in that files
and on top of each section you will have
a small metadata as a comment with the
name and then inside your closure code
with that Library you can literally use
that name as a function and then you
have some API to call or execute this
SQL statements so as I said these two
libraries were used more mostly in
production I worked and it was kind of
like 50 50.
I don't have one recommendation so just
go compare those Library yourself and
select what what you like most
cool the next topic is schemas and
validation and I have here three options
for you so first one is the schema
library and old classic uh still good
choice really fast to learn and easy to
use
second option is closure spec and the
good thing about that is is part of
closure so that means no extra
dependencies
but from my experience it has a bit of a
learning curve but yeah eventually it
will do the job for you
so and the final one
the library called Mali I haven't used
it
use it much but heard really fantastic
feedback
so that should be a good choice of a
library
as well by the way
consider subscribing to the channel
because I'm planning the loan with me
video where I'll take a look into the
Mali Library
[Music]
cool in this section let's talk about
some other libraries that are commonly
used in the production code so the first
two uh code.cash and core dot memoize
good choices if you want
extend the functionality for your
memoise functions or you want some type
of a cache inside your application
second one is chorusing and it's really
interesting topic so the library itself
is great and it's just like a
masterpiece but if you're thinking to
use that just think twice if you really
need it because it's really easy to over
engineer and use this Library where it's
actually not required so be really
careful
and the final one which has uh final one
that I really recommend is the Claypool
library and if you want some cheap
option to run your tasks in parallel
that's a perfect library for you so it
will be a wrapper around
Java execution framework when you have
access to different thread pools and the
syntax is similar to like map and pmap
functions that you have enclosure the
only difference that you will be able to
pass your thread pool as an argument to
that function so your pmap will be
executed on a thread pool and it will it
is really useful if you have something
like a hundred of requests to spin we
all know that
core function the pmap is not suitable
for that cases it's only for the CPU
intensive task so keep that in mind
cool we almost did it so the final topic
will be some additional tools that are
commonly used in the Production Services
first one is clj fmt which is their code
formatter and it's quite important to
keep your code consistently formatted
across multiple projects
second one is cljconda which is the
linta that we usually use
and a good thing about those two tools I
mentioned is that they are available as
native images so the startup is really
fast and you won't spend extra time
waiting for this task to complete
and the final one which actually should
be a separate video but I'll mention it
here is the babashka which is a tool
that allows you to write your scripts in
closure
so we use it quite extensively right now
and we are rewriting the entire
CI pipelines and all the scripts used in
CI in babashka and are really happy with
the results
it's really good that you don't have to
touch your Bosch scripts again
thumbs up
[Music]
thanks for watching I hope you found the
content useful and I gave you some
insights into the production closure
code
if you enjoyed the video please give me
a thumbs up also if you are interested
in more closure content subscribe to the
channel so you don't miss the next video
have a lovely day and see you in the
next one
foreign
[Music]
Voir Plus de Vidéos Connexes
My approach to learning a new language as an experienced developer (Go)
Code Review: Clojure Lexer
【Docker超入門 #6】Dockerfileを作成しよう
Designing Scalable Networks for Large AI Clusters: Challenges and Key Insights | Jithin Jose
How I became a full-stack developer - I didn't know how to learn to code
【要約】言語化の魔力 言葉にすれば「悩み」は消える【樺沢紫苑】
5.0 / 5 (0 votes)