Introduction to FusionCache
Summary
TLDRこのエピソードでは、Microsoft社が開発したオープンソースライブラリ「Fusion Cache」について解説しています。Fusion Cacheは、データアクセスとキャッシュを簡単に扱えるように設計されたライブラリで、開発者が様々なキャッシュタイプを統一的に扱えるよう工夫されています。デモでは、最小限のコード変更で非常に効果的なキャッシュを実現する方法を紹介し、キャッシュがデータベースへの負荷を軽減し、パフォーマンスを向上させる効果を示しました。また、開発者体験を重視し、既存のコードに変更を加えることなく、様々な機能を追加できる柔軟性と、キャッシュStampedeの問題を防ぐなどの利点を強調しています。
Takeaways
- 😀 FusionCacheはオープンソースのキャッシュライブラリで、データアクセスとキャッシュを扱いやすくする目的で開発されました。
- 🛠️ FusionCacheは様々なキャッシュタイプを統一された方法で扱うことができ、メモリキャッシュ、分散キャッシュ、HTTPキャッシュ、CDN、オフラインキャッシュなどに対応しています。
- 💡 開発者はFusionCacheを使い、オープンソースコミュニティに貢献することを目的としています。
- 🔧 FusionCacheはインメモリキャッシュとして使用することもでき、分散キャッシュと組み合わせてハイブリッドキャッシュとしても機能します。
- 🎯 FusionCacheは.NETのIMemoryCacheと互換性があり、コードを変更することなく分散キャッシュエンジンをプラグインできます。
- 📈 キャッシュスパンは1秒で設定されており、マイクロキャッシュとして非常に短時間のキャッシュにも適しています。
- 🛡️ FusionCacheはキャッシュスタンプード(キャッシュの一斉アクセス)を防ぎ、効率的なリソース使用を保証します。
- 🚀 開発者はFusionCacheを使い、ローカル開発でインメモリキャッシュから始め、必要に応じて分散キャッシュに移行できます。
- 🔄 FusionCacheは開発者体験を重視しており、簡単に使用でき、既存のコードを変更することなく機能を拡張できます。
- 🌟 自動回復機能が強調されており、FusionCacheは問題が発生した場合に自動的に修復を試みます。
- 🌐 FusionCacheはオープンソースであり、コミュニティによる貢献やコードの閲覧が可能です。
Q & A
Fusion Cacheとはどのようなライブラリですか?
-Fusion Cacheは、データアクセスとキャッシュに関して特に、開発者が悩む問題を軽減するためのオープンソースのキャッシュライブラリです。様々なキャッシュタイプを統一された方法で扱うことができ、アクセスパターンも明確にされています。
Fusion Cacheを開発した動機は何ですか?
-開発者は異なるタイプのキャッシュと長年にわたって悩んだ経験から、Fusion Cacheを開発しました。オープンソースコミュニティに貢献するという思いもあり、統一された方法でキャッシュを扱えるようにしたかったとのことです。
Fusion Cacheはどのようにして異なるキャッシュエンジンと連携しますか?
-Fusion Cacheは.NETの共通抽象化であるIistributedCacheを利用して、既存の分散キャッシュ実装にプラグインすることができます。これにより、インメモリキャッシュと分散キャッシュの両方の利点を活用することが可能です。
Fusion Cacheを使わずに開発を行う場合と比較して、どのような利点がありますか?
-Fusion Cacheを使えば、キャッシュStampede問題を防ぐことができ、遅いデータベース呼び出しやトランザクションの失敗から保護されます。また、開発者は既存のコードを変更することなく、分散キャッシュを簡単に追加できます。
Fusion Cacheを実装する際の主なステップは何ですか?
-まず、サービスにFusion Cacheを追加し、デフォルトのエントリオプションを設定します。次に、必要に応じてIdistributedCacheの実装をプラグインして、キャッシュレイヤーを拡張します。
Fusion Cacheを用いたデモでどのような結果が得られましたか?
-デモでは、Fusion Cacheを導入することで、HTTPの応答時間が劇的に短縮されたことが示されました。キャッシュを導入前は5秒でしたが、導入後は平均333ミリ秒に改善されました。
Fusion Cacheの主な特徴の一つとして開発者が挙げたものは何ですか?
-開発者は、Fusion Cacheの主な特徴として「自己修復」機能を挙げています。これは、分散キャッシュやバックエンドで問題が発生した場合に自動的に修復を試みる機能です。
Fusion Cacheを使用する際の利便性について教えてください。
-Fusion Cacheは非常に使いやすいライブラリであり、開発者が既に慣れているメモリキャッシュと同様の使い方をすることができます。また、既存のコードを変更することなく、新機能を追加することができます。
Fusion Cacheはオープンソースライブラリであると聞きましたが、それはどのような意味ですか?
-オープンソースライブラリであるFusion Cacheは、誰もが自由に利用、変更、再配布が可能です。これにより、コミュニティからの貢献も受けられるだけでなく、開発者がコードを確認し、理解を深めることもできます。
Fusion Cacheを実際に使用した例として、どのようなデータベースとの連携が行われていますか?
-Fusion CacheはSQL Serverのような実際のデータベースと連携し、1秒のキャッシュで非常に大きなパフォーマンスの向上を実現することができます。これにより、1秒のキャッシュでクエリ1秒から数千にまでスケールアップすることが可能です。
Fusion Cacheを導入することで得られる主な利点は何ですか?
-Fusion Cacheを導入することで、開発者はよりスケーラブルなソリューションを作成することができ、パフォーマンスの向上だけでなく、コードの変更なしに分散キャッシュを活用することができます。
Outlines
😀 Fusion Cacheの紹介とその背景
この段落では、MicrosoftのオープンソースライブラリであるFusion Cacheについて紹介しています。JYさんが作成したこのライブラリは、データアクセスとキャッシュに関する問題を解決することを目的としています。JYさんは多くのキャッシュタイプを扱う経験から、一つのパッケージにまとめ、統一された方法で扱えるようにしたと説明しています。また、オープンソースコミュニティに貢献するという思いからプロジェクトを開始したと語っています。Fusion Cacheは単なるキャッシュ以上の機能を持っているとJYさんが述べ、.NETのIMemoryCacheのようにインメモリキャッシュとして使用できるだけでなく、ハイブリッドキャッシュとして機能する可能性があることも紹介されています。
😉 Fusion Cacheのデモンストレーションと機能紹介
JYさんはFusion Cacheを使用した簡単なデモを行っています。このデモでは、.NETの最小APIを使用して仮想のデータベースを模擬し、Fusion Cacheをサービスに追加しています。デモではマイクロキャッシュとして1秒のキャッシュを設定しており、データベースからの遅延を5秒間に比べて、キャッシュによるパフォーマンスの向上が示されています。また、キャッシュスタンプードというキャッシュに関する一般的な問題にも触れており、Fusion Cacheはこれを防ぐ機能を組み込んでいます。k6というストレステストツールを使用して、キャッシュの有効性を検証し、レスポンス時間を5秒から335ミリ秒に短縮する効果を示しています。
🎉 Fusion Cacheの使いやすさと開発者体験
最後の段落では、Fusion Cacheの使いやすさと開発者体験について語られています。JYさんはFusion CacheをMemory Cacheと同様に簡単に入れ替え可能で、既存のコードを変更することなく、機能を追加できるという利点を強調しています。また、開発者が徐々にフェーズセーフタイムアウトや分散キャッシュを有効化できる柔軟性があることも紹介されています。JYさんの好みとして、開発者体験とFusion Cacheが「ただ働きます」という点を挙げています。また、コミュニティからのフィードバックも好評で、誰もが簡単に使用できると報告されています。最後に、Fusion Cacheがオープンソースであることを強調し、誰もが貢献したり、使用することができます。
Mindmap
Keywords
💡Fusion Cache
💡オープンソース
💡キャッシング
💡分散キャッシュ
💡メモリキャッシュ
💡キャッシングスタンプード
💡フェイルセーフタイムアウト
💡.NET
💡マイクロキャッシング
💡ストレステスト
Highlights
Fusion Cache is an open source caching library designed to simplify data access and caching.
It was created to address the challenges of working with various types of caches like memory cache, distributed cache, HTTP caching, CDN, and offline caches.
Fusion Cache offers a uniform and consolidated approach to caching with clear naming and access patterns.
It can function as both an in-memory cache and a hybrid cache, integrating with existing distributed cache implementations.
Developers can start with local or in-memory caching and easily scale to a distributed cache without changing their code.
Fusion Cache automatically handles the interaction between memory cache and distributed cache layers.
It provides the benefits of both memory cache and distributed cache, overcoming their individual limitations.
Fusion Cache includes a demo using minimal APIs and endpoints in .NET to simulate a database call.
The demo illustrates the effectiveness of micro-caching with a one-second cache duration.
Fusion Cache automatically handles cache notifications and protects against cache stampede.
It ensures only one request per cache key goes to the database, preventing resource wastage.
The library includes features like phase-safe timeouts and auto-recovery for self-healing in production.
Fusion Cache aims to provide an excellent developer experience, making it easy to use and integrate.
It allows developers to add more features like distributed caching without altering existing code.
The library is open source, enabling community contributions and transparency.
Fusion Cache's logo is also noted for being fantastic, adding to the overall appeal of the project.
A real-world example using Fusion Cache with SQL Server demonstrates significant performance improvements with just a one-second cache.
The episode encourages viewers to try Fusion Cache for creating more scalable solutions.
Transcripts
hey in this episode of open at Microsoft
we'll be discussing about caching with a
fantastic open source Library named
Fusion cache stay tuned for
[Music]
more hey JY super nice to have you here
uh at open source at Microsoft I know
that you have created a fantastic
library that use every day Fusion cache
so I really looking forward to have you
explaining all about it so let's start
right away what is fusion cach and why
you have created it yeah first of all
thank you for having me here so fish
cash is a cing library uh as the name
suggests that uh tries to ease the pain
of working with uh data access and
caching in particular so the idea came
up to me uh after years and years in my
career of having to deal with all
different types of caches and I mean
memory cach distributor cache HTTP
caching CDN offline caches all these
things so I tried to wrap them up
together in a nice package that has a
kind of a uniform and Consolidated way
of doing things with clear names and a
clear access pattern and stuff like that
so I wanted to give back to the open
source community that gave us a lot to
all of us so that was like my way of
doing it and so some years ago I took
the plunge and said okay why not let's
try to do one open source project and
this is it no that's nice so you're
saying it's more than caching because
you know as a developer when I think
about caching immediately usually I
think about r or something like that but
here you're saying you have created a
library that is actually a cach on its
own my understanding but can also use
any of these additional caching uh
engines that we already have or or it's
kind of a
replacement yeah yeah absolutely it can
be both and inmemory cache only so kind
of like imemory cache in net and
actually right now it uses it underneath
but it can also work as a what is called
um an hybrid cache meaning a multi L
cache Yeah so basically you can um plug
it in any existing implementation of I
distributed cache which in net is the
common abstraction of a distributed cach
and so basically we will use that as a
second level and will automatically
handle the the I call the dance between
the two layers uh so yeah so that
basically you can get the best of both
worlds because if you do like a business
101 graph chart you have basically the
pros of memory cache and some cons and
they are the opposite of the pros and
cons of the distribut so you mix them
together yeah and you get the best
worlds that is cool that is cool so as a
developer I can start with Fusion cache
use it locally or in memory and then
when you realize I may I I may need more
I can just plug in an additional caching
engine that is a distributed engine and
just use it but basically without
changing my code that's that's my
understanding yeah exactly one of the
things that some people had to do is
that they start with caching and then
use memory cach because yeah I mean it's
cool okay but then they can only so much
scale vertically up to a certain point
and then you have to scale horizontally
multi Noe and in that case they usually
switch to Ed cach but in that case the
abstraction changes and they have to
change all the pieces in the code all
their their cash. get Etc so it's a lot
of work and the P the access pattern
changes also the super cache has some
pros but also some cons because it is
distributed so it means that it's um
it's like um it has the the the problem
of the fallacies of distributed
companies these ways basically latency
is not zero network is not always there
all these things yeah so memory and and
distributed together give you the best
of world you do not have to change
anything in your existing code so you
can start memory and go distribut or mix
both ways that's that's fantastic do you
have any code that I can see and we can
share with people watching the show yeah
absolutely uh prepar a very small demo
nice very very small if it's in the
screen so it should be nice to follow so
basically it uses minimal apis minimal
endpoints in in x.net and basically it
set up a fake database which we can see
it's basically just a class that delays
for 5 seconds simulating as low datab
base go and then returns a random number
uh then it set up Fusion cache in this
piece which basically say add Fusion
cach to the services usual dependence
injection way and some default entry
options soal which are basically the
default uh options for each entry in the
cache in this case I set up one second
cache so this is what is commonly known
as micro caching meaning very very small
cache and this is is nice because
sometimes people think oh I don't know
you know cach data for like 10 minutes
stuff like that what if the data changes
and stuff like that notification which
by the way Fusion cach handles
automatically for you but that's another
thing and but we just one second is just
really a no-brainer just why not like
it's very rare that you cannot have one
second of cash so what I would like to
show is is this so it's a simply it's a
simple endpoint that Returns the get
value from database now if on the right
hand side of the screen we have k6 so I
prepared a k6 test which uh is a stress
testing tool and while it is running it
will take about 20 seconds oh right it's
an error but it's correct because I
haven't out oh yeah yeah I yeah I guess
that cash can can fix that yeah yeah
absolutely so
okay let's run it it's going so this is
the test plan for k6 basically it's a r
pop of two seconds up to 10 virtual
users then 5 Seconds up to a th000
virtual users then 10 seconds of sustain
load to 1,000 users and then run down to
zero users yeah now get back to the code
here almost finished what we can see
right now we will see that basically the
the HTTP duration which is here and you
can see that the average minimum median
Max and and the percentiles are
basically all 5 Seconds why because
there's no cash and the database call
even though it's simulated takes 5
Seconds Now by simply commenting out the
the call to database and uncommenting
the call to the cach we where by the way
here you can see it highlight so it's
exactly the same call basically we
simply say cash get or set give it a
cash key and the call back so when the
data is not in the cash what should cash
to get the DAT to database we simply
restart the web
server and we just like that we rerun k6
again and we can see we will see the
numbers uh drastically uh um getting
slower smaller and smaller because of
caching now one thing that I like to say
while it is running is that on top of
being a cash it also protects you from
the most common problem in the caching
World which is known as cash Stampede uh
which is something that some people
think imemory cach in.net protects you
from because there's a method called get
or create but it doesn't protect you so
basically very shortly cash stamped
means uh if a th000 request comes in for
the same value and is not in the cash
all of them will go to the database at
the same time and the cash so it's a
waste of resources with cashed
protection which is built into Fusion
cash and you don't have to do anything
you only one will get will go to the
database and just to be clear only one
per cash key so product one product two
they will not like lock each other of
course now k6 run finished and we can
see the average from 5 Seconds to 335
milliseconds so definitely better the
minimum is 53 microc second yeah because
that's from memory yeah Alpha
millisecond yeah yeah and then the
median the max the max of course is 5
Seconds because I mean the first one
will go to database and then we have
like a
percentile of 400 something milliseconds
yeah so this is a very small demo but
it's something that I think can be
effective because you don't have to even
think about having like one second cash
is really a problem so by just doing
that these are yeah yeah it's amazing
and and and moving from non-cached
version to a cached version is literally
like two line of code uh basically you
specify you specify the key and
basically the the function you want to
be called when uh the cash expires right
yeah that's the factory yeah yeah and
then fion Fusion cash takes care of all
the Stampede problem and I I I know that
on gab you you have a list of other
problem that are prevented by Fusion
cash so um that's amazing so out of all
the nice feature that Fusion cach has
what is your favorite if you have
one nice one H H there are a
lot well you know what if I will answer
you this at the beginning probably I
would have said phase safe and soft our
timeouts basically it's it's the two
main features that protects us from slow
database calls and the trans failures
okay but that was at beginning more
recently I would have said probably Auto
Recovery which is a big feature that I'm
working on a lot and I'm still working
on which is basically selfhealing so
when something bad happens in production
like on the distributed cach or the back
plane or whatever it automatically try
to fix that together and and the demo
usually do with the the small
application I call simulator which is
included in the source code of of the
the solution uh is nice because you can
see everything destructing in production
going red and then as soon as you get
back like radi or or man cash or
whatever you automatically get all green
so it's it's really to see to say even
if I say so myself but I think the most
the best one the one I prefer the most
is like an overarching theme of uh
developer experience and and fusion cach
um being it just works so to speak so
what I I think I Tred to what I Tred to
do and I think I got it in the most ways
H and also what the community told me
mostly about is that it's really nice
and easy to use and you basically just
if you're used to memory cash you swap
it out for Fusion cash and it's the same
but you get protected from cash stamped
and then slowly you can add more
features you can enable phase save
timeouts you can enable the distributor
cache all without changing your existing
code now one thing I can I can show you
is um is very quickly here where where I
say I set up Fusion cache so add Fusion
cache first line second one is I
configure the the default entry options
if I go here and I do with distributed
cash oh I can simply yeah just like that
I can pass an instance of any I
distributed cach or I can use the to
resolve one or whatever and these which
are my thousands and thousands of code
in yeah will not change that's fantastic
absolutely and I can even you do it like
programmatically meaning oh I'm I'm
locally a locally I just use memory I
don't need in development distribut cach
because I know that in production
everything will be the same so I can
work locally only one level and in
production with two levels that's very
nice oh that's great and it's even
better that every is open source so
people can also contribute if they want
or or just you know see you know all the
techniques that you use to make this
happen so oh and also the the logo is
fantastic I tell you thank
you yeah that's great uh so to wrap up
uh not only uh the link to the um
repository will be available in
description also an example that use a
fusion cach with the real database aure
SQL in my case that shows how again
Improvement uh it's really easy to be
obtained just with one second cash you
can go from a few query per second to
thousand and I I I think that's all uh
it was great to have you in this episode
Fusion cach is great uh if you are
watching this episode go and try fure
cach out right now it's super easy to
use and just helps you to create more
scalable solution and with that see you
in the next episode thanks so much
5.0 / 5 (0 votes)