Code - 03 04 2024
Code - 03 04 2024
jihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA
MAR
APR
2024
YEARS
Exploring AL
with Large
Language Models
We will send an expert to your office to meet with you. You will receive:
1. An overview presentation of the current state of Artificial Intelligence.
2. How to use Al in your business while ensuring privacy of your and your clients' information.
3. A sample application built on your own HR documents - allowing your employees to query
those documents in English and cutting down the number of questions that you
and your HR group have to answer.
4. A roadmap for future use of Al catered to what you do.
C O N T A C T U S T O D A Y F O R A F R E E C O N S U L T A T IO N A N D D E T A IL S A B O U T O U R S E R V IC E S .
c o d e m a g .c o m /ai-s e rv ice s
832-717-4445 ext. 9 • info@ codem ag.com
TABLE OF CONTENTS
Features
8 CODE: 25 Years A g o 58 D is tr ib u te d C a c h in g :
CODE M a g a z in e h a s b e e n d o c u m e n t in g o u r i n d u s t r y 's c r e a t i v i t y
f o r a q u a r t e r o f a c e n t u r y . M a rk u s t a k e s a t r i p d o w n m e m o r y Lane
E n h a n c in g S c a la b ility a n d
t o s h o w u s h o w f a r w e 'v e c o m e . jihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA
P e rfo rm a n c e in ASP.NET 8 C o re
M a rk u s E g g e r
J o y d i p c o v e r s t h e p r o s a n d c o n s o f d i s t r i b u t e d c a c h in g a n d
t e a c h e s y o u h o w t o im p l e m e n t i t i n A S P .N E T C o re .
14 Passkey A u th e n tic a tio n J o y d ip K a n jila l
W e 're a l l L o o k in g f o r w a r d t o t h e d a y w h e n p a s s w o r d s a re n o L o n g e r
n e c e s s a r y . S a h il t a k e s a Lo o k a t h o w t o g e t y o u r a p p li c a t i o n s re a d y .
71 C# f o r H ig h -P e rfo rm a n c e S ys te m s
S a h il M a lik
I t t u r n s o u t t h a t y o u c a n w r i t e h ig h - p e r f o r m a n c e c o d e i n C# t h a t
m a tc h e s o r e x c e e d s t h e p e r f o r m a n c e o f n a t iv e c o d e . O re n s h o w s y o u
22 Y o u 're M is s in g O u t o n h o w t o r e t a in a l l t h e a d v a n ta g e s o f C# w h i l e y o u 'r e a t i t .
O ren E in i
O p e n -S o u rc e LLM s!
P h ili p p e x a m in e s L a rg e L a n g u a g e M o d e ls a n d C h a t-G P T ,
a n d h o w t o g e t t h e m t o d o w h a t YO U w a n t .
P h ilip p B a u e r
30 P r o to ty p in g L a n g C h a in
Departments
A p p lic a tio n s V is u a lly U s in g F lo w is e E d ito ria l
T h e L a n g C h a in f r a m e w o r k c a n h e lp y o u q u e r y Large a m o u n t s o f d a t a ,
a n d W e i- M e n g s h o w s y o u h o w t o u s e F lo w is e t o d o i t , e v e n i f y o u 'r e
n o t w r it in g co d e .
A d v e rtis e rs In d e x
W e i-M e n g L ee
C o d e C o m p ile rs
46 S e m a n tic K e rn e l 101: P a rt 2
M ic r o s o f t S e m a n tic K e r n e l is a n o p e n - s o u r c e A l f r a m e w o r k .
I t ' s v e r y n e w , a n d M ik e e x p lo r e s t h e c o d e t h a t h e lp s y o u u s e
th is p o w e r fu l n e w to o l.
M ik e Y e a g e r
S h a w n W ild e r m u t h
CO DE^O
E xpress, M as te rC ard , V is a , a n d D is c o v e r c r e d it c ards are a c c e p te d . B a ck is s u e s are
a v a ila b le . For s u b s c r ip tio n in f o r m a tio n , s e n d e - m a il t o s u b s c r ip tio n s @ c o d e m a g .c o m
o r c o n t a c t C u s to m e r S e rv ic e a t 8 3 2 - 7 1 7 - 4 4 4 5 e x t. 9 .
M o d u la r M e d ic a l W e b V ie w e r C o n tr o l
3V
tAeta d o t
°
G e t S ta rte d
DO W NLO AD OUR F R E E E V A L U A T IO N
L E A D T O O L S .C O M
E D ITO R IA L
A Lot o f th e c o n v e rs a tio n s I have w ith M ela A long w ith being a darn fin e e dito r, Melanie is an J a m u lu s le ts y o u p la y , re h e a rs e , o r ja m w it h
n ie a b o u t c re a tiv ity and c o lla b o ra tio n c e n te r accom plished m usician and she collaborates w ith y o u r fr ie n d s , y o u r b a n d , o r a n y o n e y o u f in d
aro und m any n o n -te c h fie ld s . Every c re a tiv e numerous musicians in many d iffe re n t organiza o n lin e . P lay t o g e t h e r r e m o te ly in t im e w it h
en dea vor shares som e a com m on DNA and by tio ns . In norm al tim es, these collaborations take h ig h q u a lity , lo w -la te n c y so u n d on a n o rm a l
e x a m in in g o th e r fie ld s n o t re la te d to y o u r ow n, place in person. Rehearsals, practice, organiza b ro a d b a n d c o n n e c tio n .
y o u m ig h t fin d new er ways o f th in k in g . A t a t i o n - a l l done in person. COVID changed a ll o f
m in im u m , y o u 'll lik e ly learn s o m e th in g yo u th a t. I t d id n 't take to o long fo r these musicians to The key phrase in th is s ta te m e n t is Low-Latency.
d o n 't know. T ha t's a w in /w in , rig h t? realize th a t creating th e fid e lity o f in-person m u The fo lk s w ho cre ate d Jam ulus cre ate d a s o lu
sicianship was d iffic u lt to p u ll o ff rem otely and, tio n t h a t ena bled re m ote c o lla b o ra tio n s t h a t
W ell, th is p a rtic u la r co n v e rs a tio n was focused in th e b eg in n in g , im possible. The reason? T im ing. was as close to an in -p e rs o n ex perie nce as pos
on one cre a tiv e fie ld : m usic. We ta lk e d a b o u t Melanie and her collaborators soon discovered th e s ib le . This gro up o f in tre p id m usicians fo u n d a
how COVID changed th e w ay m usicians were bane o f rem ote collaborations using ordinary to ols t o o l t h a t helped th e m to m ove fo rw a rd . They
fo rce d to change th e ir c o lla b o ra tiv e process such as Zoom: lag. Technology people are i n t i soon a da pted th e ir process to Jam u lus's ra th e r
d u rin g th e pandem ic. m ately fa m ilia r w ith th e concept o f lag and there rig id re q uire m e nts w ith g re a t and s a tis fy in g
are a bunch o f d iffe re n t term s used to describe success.
Before COVID, I was ta k in g g u ita r lessons. My lag, such as response tim es, ping tim es, TTL, etc.
te a c h e r came to m y house tw ic e a m o n th fo r We strive to Lower lag to make our w ork perform The m usicians w e re n 't th e o n ly fo lk s th a t
an in -p e rs o n session w here he ta u g h t me th e b e tte r or to a t least to have th e appearance o f a da pted th e ir processes. My w ife and I are cu r
fo u n d a tio n s o f g u ita r p la y in g , in c lu d in g scales, b e tte r perform ance. Progress bars anyone? re n tly b in g e -w a tc h in g a show on N e tflix ca lle d
chords, and, w h a t was p ro b a b ly th e m o s t d if The B la c k lis t. The episode la s t n ig h t c o m p le te ly
f ic u lt a sp ec t fo r me, tim in g . Scales and chords M elanie and her m usician frie n d s c o u ld n 't paper b le w m y m in d . I t was th e season fin a le o f sea
were p ro b a b ly th e ea sie st p a rt. W ith p ra c tic e , I ov er th es e Lag issues. I t w o u ld be d if f ic u lt to son 7. This episode was in th e process o f be in g
g o t b e tte r and b e tte r as o u r lessons progressed. create g re a t perfo rm an ces , le t alo n e s a tis fy in g film e d d u rin g th e w o rs t p a rt o f th e COVID crisis
I t was t h a t cursed tim in g t h a t caused me th e and p ro d u c tiv e rehearsals, i f th e y c o u ld n 't c o n w hen e v e ry th in g was b e in g s h u t d ow n. The e p i
m o s t a g ita . The m etro no m e was n o t m y frie n d t r o l th e issue o f lag. So th e y searched fo r a sode began w ith a p ro lo g u e o f c lip s from th e
and, to be h on es t, I never re a lly g o t th e hang s o lu tio n to c o n tro llin g o r e lim in a tin g lag. This cas t and crew w here th e y m e n tio n e d t h a t th e y
o f i t . Over and over, he b e a t i t in to me t h a t i f I is w here th e y fo u n d a p ro d u c t c a lle d Jam ulus . were h a lfw a y done s h o o tin g th e epis ode w hen
ever w a n te d to p la y w ith o th e r fo lk s , I needed th e y were fo rce d to s h u t do w n .
to g e t tim in g d ow n . I t was th is tim in g th in g The Ja m u lu s w e b s ite describes th e ir p ro d u c t as
t h a t was p a rt o f m y discu ssio n w ith M elanie. fo llo w s : B u t th e y d id n 't s h u t d ow n. They re tre a te d te m
p o ra rily and came up w ith a s o lu tio n . The pa rts
o f th e film t h a t w e re n 't s h o t Live-action were
a n im a te d . I t was a b it odd, b u t i t w o rk e d! As a
film geek, i t was an a d d itio n a l tr e a t fo r me to
see how a show is s h o t o u t o f c o n tin u ity us ing a
re a l-w o rld exam ple. C hanging how th e y w orked
a llo w e d th es e c o lla b o ra to rs to a cc om plis h w h a t
th e y p ro b a b ly th o u g h t was an im p o s s ib le ta s k .
As G unny S eargent Thom as H ig hw ay w o u ld say:
They overcam e, th e y a da pted .
D i s t r ib u t e d C o u n te rs
S p a tia l
A ll-in -O n e / / F i l t e r & S p a t ia l
fro m R e s ta u ra n ts
w h e n Type = 'C o ffe e S hop' and s p a t i a l . w i t h i n (
s p a t i a l . p o i n t( A d d r e s s . L o c a ti o n . L a t i t u d e ,
A d d re s s . L o c a ti o n . Longi t u d e ) ,
E ffo rtle s s )
s p a t i a l . c i r c l e ( 2 0 , 4 7 .6 2 3 4 7 3 , -1 2 2 .3 0 6 0 0 9 7 )
/ / P lo t
fro m Employees F u l l - T e x t S e a rc h
s e le c t t im e s e r ie s (
fr o n H e a rtR a te s je tw e e n '2 0 2 0 - 0 1 - 0 1 ' and
) ... a n d M o r e
/ / A g g re g a te
fro m O rd e rs
g ro u p by S h ip T o . c it y
s e le c t s h ip T o . c it y as s h i p t o c i t y , sui ( L in e s [ ] . Q u a n t it y ) CBA
" A f t e r y e a r s a f f i x i n g m is t a k e s t h a t o t h e r s d i d in S Q L , d e v e lo p in g a
w o r ld - r e n o w n a p p l i c a t i o n t o f i n d c o m m o n o n e s , I r e a liz e d t h a t t h e
O re n Eini
p r o b l e m is n o t w i t h p e o p le , b u t w i t h t e c h n o lo g y . C EO & F ounder
T h a t d a y I s t a r t e d R a v e n D B ."
r a v e n d b .n e t/tr y
O N LIN E Q U IC K ID 2403021
C O D E: 25 Years A g o
As we continue our celebration of "30 years of CODE Group," we travel forward five years from last issue's installment of "the
Antique CODE Show" and look at our industry, and the world in general, 25 years ago (or so). The late 90s were an interesting
time for our industry. Compared to five years earlier, the internet had rapidly become mainstream. Although hardly anyonejihgfedc
th e V is u a l S tu d io te a m . s ic in d u s tr y w a s t u r n e d u p s id e d o w n d u e t o t h e e m e r
G lo b a lly re c o gn ize d as g e n c e o f N a p s te r, a p e e r - to - p e e r m u s ic s h a r in g s y s te m A b s e n t fr o m t h e l i s t o f i n f l u e n t i a l c o m p a n ie s in t h e Late
a speaker and a u th o r, t h a t u n ra v e le d t h e b u s in e s s m o d e l o f t h e e n t ir e in d u s tr y . 9 0 s is A p p le . T e e te rin g o n t h e b r in k o f b a n k r u p tc y , S te v e
Markus's e x p e rtis e spans A t t h e h e ig h t o f i t s p o p u la r ity , N a p s te r h a d 8 0 m illio n J o b s h a d j u s t re tu rn e d t o A p p le in 1 9 9 7 . Even M ic r o s o ft
A r t if ic ia l In te llig e n c e ( A l) , r e g is te r e d use rs w h o h a p p ily s h a re d m u s ic f o r fre e (a n d in v e s te d $150 m illio n in to A p p le in A ugust o f 1997,
.NET, c lie n t-s id e w eb, and a r g u a b ly il le g a l ly ) w it h t h e i r p e e rs , u n t i l t h e s e rv ic e w a s w h ic h , in h in d s ig h t , w a s p r o b a b ly n o t in s ig n if ic a n t as a
c lo u d d e v e lo p m e n t, fo c u s s h u t d o w n in 2 0 0 1 . A t t h e tim e , a r t is t s t h a t t o p p e d t h e f a c t o r in t h e s u r v iv a l o f t h e c o m p a n y , as i t h e lp e d s t a b i
in g on user in te rfa c e s , c h a r ts in c lu d e d n a m e s lik e B r itn e y S p e a rs , t h e B a c k s tre e t liz e t h e fin a n c e s o f t h e c o m p a n y a n d a llo w e d i t t o s u b s e
p ro d u c tiv ity , and m a in ta in B o y s , a n d E m in e m . I t 's h a rd t o s a y w h ic h s o n g s w e re t h e q u e n t ly g o o n t o d e v e lo p n e w p ro d u c ts s u c h as t h e iM a c
able system s. A w ay from m o s t s h a re d , b u t M a d o n n a a n d E lto n J o h n s o n g s w e re up a n d t h e iP o d . B u t t h a t w a s a l l s t i l l in t h e f u t u r e a n d A p p le
w o rk , he's an e n th u s ia s tic th e r e , a n d t h e a r t is t s w e re a ls o a m o n g t h e m o re o u ts p o c le a r ly w a s n 't v e ry c o o l in 1 9 9 8 , e x c e p t f o r s o m e n ic h e
w in d s u rfe r, scuba div er,
ken a g a in s t t h e p r a c tic e o f p e e r - to - p e e r s h a r in g f o r fre e . m a rk e ts , s u c h as d e s k to p p u b lis h in g . (W h e n w e re le a s e d
ice hockey player, g o lfe r,
E v e n tu a lly , t h e M e ta llic a v s . N a p s te r la w s u it w a s t h e s t a r t t h e f i r s t is s u e s o f t h i s m a g a z in e , i t w a s a l l d o n e o n M a c s ).
and g lo b e tro tte r, w ho
o f t h e e n d f o r t h e s e rv ic e .
Loves g a m in g on PC o r
T a lk in g a b o u t CODE: As f a r as CODE G ro u p g o e s , w e w e re
Xbox d u rin g ra in y days.
T he Late 9 0 s w e re a ls o a g r e a t t im e f o r m o v ie g o e rs ( w h ic h a ro u n d as a s m a ll b u t g r o w in g c o n s u ltin g a n d t r a in in g o r
in c lu d e d m e b a c k t h e n ) . F ilm s s u c h as " T it a n ic " ( 1 9 9 7 ) , g a n iz a t io n . O u r h e a d q u a rte rs h a d a lre a d y m o v e d t o H o u s
" S a v in g P riv a te R y a n " ( 1 9 9 8 ) , a n d "T h e M a t r ix " ( 1 9 9 9 ) t o n , Texas. We d id a l o t o f c o n s u ltin g a n d t r a in in g a n d
w e re a m o n g t h e m o s t p o p u la r. I t 's h a rd t o b e lie v e t h a t w e re v e ry a c tiv e in W in d o w s a n d in te r n e t- b a s e d d e v e lo p
i t 's b e e n a q u a r te r c e n tu r y s in c e t h e i r re le a s e . A m o n g t h e m e n t a n d h a d s ta r te d a r c h it e c t in g s y s te m s t h a t w e r e n 't
m o s t im p o r t a n t s o ftw a r e c o m p a n ie s t h a t g o t e s ta b lis h e d j u s t o b je c t- o r ie n t e d , b u t w e w e re s t a r t in g t o t h i n k a b o u t
in t h e Late 9 0 s w a s G o o g le (S e p te m b e r o f 1 9 9 8 ) . A lth o u g h c o m p o n e n t- b a s e d m u lt i- t ie r e d s y s te m s , r a th e r t h a n j u s t
i t 's h a rd t o b e lie v e t h a t J a m e s C a m e ro n f in is h e d " T it a n ic " " n e t w o r k e n a b le d " s o ftw a r e . T h is w a s a b ig d e a l a t a t im e ,
m o re t h a n 2 5 y e a rs a g o , i t 's e v e n h a rd e r t o b e lie v e t h a t a n d u lt im a t e ly le a d t o t h e "C O D E" b ra n d , w h ic h o r ig in a lly
G o o g le is y o u n g e r t h a n t h is m o v ie , as i t 's d i f f i c u l t t o d a y s to o d f o r C o m p o n e n t D E v e lo p e r. CO D E M a g a z in e h a d n 't
t o im a g in e a w o r ld w i t h o u t G o o g le . p u b lis h e d i t s f i r s t is s u e y e t (b e c a u s e t h e o v e r a ll CODE
G ro u p o r g a n iz a tio n g o e s b a c k f u r t h e r t h a n t h e m a g a z in e ),
P o lit ic a lly s p e a k in g , tim e s w e re a b i t s im p le r, as o u r m a in b u t t h is is w h e n w e f i r s t h a d t h e id e a o f p u b lis h in g a
c o n c e rn w a s B il l C lin to n 's Love f o r c ig a rs a n d in te r n s . I n d e v e lo p e r m a g a z in e t h a t d i d n 't j u s t fo c u s o n a s in g le
t e r n a t io n a lly , n u c le a r te s ts b y P a k is ta n a n d I n d ia w e re p r o g r a m m in g la n g u a g e (s u c h as V is u a l B a s ic P r o g r a m m e r 's
t h e h o t t o p ic . T h e G oo d F rid a y A g re e m e n t in A p r il o f 1 9 9 8 J o u r n a l , F o x P r o A d v is o r , o r C /C + + U s e r 's J o u r n a l ) b u t c o v
b r o u g h t an e n d t o t h e v io le n c e in N o rth e rn Ir e la n d . I n e re d s o ftw a r e d e v e lo p m e n t a t a Larger, m o re t e c h n o lo g y -
s c ie n c e a n d m e d ic in e , w e h a d n 't y e t c o m p le te ly d e c o d e d o r ie n te d le v e l. T he id e a w a s Laughed a t b y m a n y a t t h e
h u m a n DN A, b u t o n t h e u p s id e , V ia g ra w a s a p p ro v e d b y tim e , b u t has s e rv e d us v e ry w e ll e v e r s in c e .
F ig u r e 1 : T he W in d o w s 9 8 D e s k to p w it h F ile M a n a g e r
kids: We were lo o k in g a t 80 0 x60 0 pixels o f re s o lu tio n back fo r a w h ile , i t w a s n 't u n t il th e Late 90s t h a t I s ta rte d
th e n , and we loved i t ! Now g e t o f f m y law n! e x p e rim e n tin g w ith using a la p to p as m y m ain w o rkh orse
m achine . I s t i l l s w itc h e d back and fo rth be tw een t h a t
B u t W ind ow s 98 w a s n 't th e o n ly M ic ro s o ft o p e ra tin g sys approach and m ore c o n v e n tio n a l d es kto p se tup s (espe
te m t h a t was p o p u la r in th e Late 90s. M ic ro s o ft also c ia lly fo r g a m in g ) b u t h a v in g la p to p co m p uters w ith de
c o n tin u e d to d e v e lo p W ind ow s NT as th e o p e ra tin g sys c e n t hors epo w er and rea son ab ly good c o lo r d is play s ju s t
te m o f c h o ic e fo r e n te rp ris e servers. NT4 was released h a d n 't been a th in g m uch e arlie r.
in 1996 and w o u ld be h e a v ily in use f o r a lo n g tim e to
com e. I t was b u ilt on NT 3 .5 and NT 3 .5 1 , w h ic h had i n S peaking o f g a m in g : L oo king a t p o p u la r t itle s , th is was
tro d u c e d th e 3 2 - b it k e rn e l and a new f ile system (NTFS), w hen we s ta rte d re c o g n iz in g key players and brands t h a t
and A c tiv e D ire c to ry . I t 's a g re a t o p e ra tin g syste m , and s till have s ig n ific a n c e to d a y . Valve released CBA
H a lf - L if e
we s t i l l use th e successors o f m uch o f t h a t te c h n o lo g y (la te r, H a lf - L if e 2 was th e s ta r t o f Valve's now d o m in a n t
in m odern v e rs io n s o f W ind ow s to d a y . M any d ev e lo p ers S te a m g a m in g m arketpla ce) in 199 8. S ta r C r a ft e v e n tu a lly
even o p te d to use NT4 as th e o p e ra tin g system o f ch o ic e sparked c o m p e titiv e g a m in g and eS ports. U n r e a l by Epic
fo r t h e ir d e v e lo p m e n t m ac h in e s back th e n . I t was a b it Games was w here th e U n re a l E n g in e o rig in a lly came fro m .
c lu n k ie r fo r e nd -u se rs th a n W indow s 9 8 , b u t i t also had E n s e m b le S t u d io s (b a s ic a lly M ic ro s o ft) released A g e o f E m
a v e ry s o lid core. For te c h n ic a l p e o p le , i t w a s n 't a bad p ir e s , and B io w a r e gave us th e f ir s t B a ld u r 's G a te game
tr a d e - o ff a t a ll. (F ig u re 3 ) . T his m ig h t con fu se you because 2023's gam e
o f th e y e a r is also B a ld u r 's G a te , b u t th a t's th e successor
T his was also w hen Laptop c o m p uters becam e fa r m ore to th e classic gam e. B oth are h ig h ly recom m ended to any
p o p u la r and m ain stre a m . A lth o u g h th e y 'd been around a vid PC gam er.
B u t PC g a m in g w a s n 't a ll th e re was. Sony had alre ady I f you were tr u ly d arin g , you even dabbled in XML, w hich
released th e ir P la yS ta tio n brand a fe w years e arlie r, and was a new fangled standard based on angle-b rackets th a t
M ic ro s o ft was hard a t w o rk on th e f ir s t X box, w h ic h was to kinda looked lik e th e th e n -s till-n e w HTML, b u t i t had more
becom e a c o m p e tito r to P la yS ta tio n 2 as w e ll as N in te n struc ture. To w h a t end, m any people w ondered? There was
do's GameCube, w h ic h it s e lf was a successor to N in te n do 's much sp ec u la tio n a b o u t web browsers being able to make
su cces sfu l N in te n d o 64 (F ig u r e 4 ) . Games lik e M o n o K a r t m ore sense o f XML th a n HTML, b u t m any d ou bted its po
were a ll th e rage on t h a t p la tfo rm b u t w o u ld u ltim a te ly te n tia l. I rem em ber s ittin g in a keynote a t a sizable com
be surpassed by th e am azing n e x t-g e n e ra tio n experiences p u te r conference in th e N etherlands, w here th e presenter
p ro vid e d by th e Xbox and th e PS2. B u t t h a t was s t i l l in
th e fu tu re .
Software Development
The w o rld o f s o ftw a re d e v e lo p m e n t in th e Late 90s was
b o th in flu x and also re la tiv e ly ca lm , in h in d s ig h t. I t
d id n 't seem Like i t a t th e tim e , because we had to com e to
g rip s w ith th e re a litie s o f th e in te r n e t and m any so ftw a re
develop ers were s t i l l w o n d e rin g w h e th e r th is g ra p h ic a l
user in te rfa c e experien ce was a c tu a lly good fo r business
a p p lic a tio n s and m any DOS d ie -h a rd s were s t i l l h o ld in g
on by th e skin o f th e ir te e th . B u t th e de ba te had p re tty
m uch been s e ttle d fo r m o s t o f us th e n : W indow s d es ktop
apps were w h a t serious develop ers were b u ild in g , and you
ju s t c o u ld n 't ig n o re th e in te r n e t e ith e r. We w ere d e v e lo p
in g w h a t seem ed to be re la tiv e ly s o p h is tic a te d in te ra c tiv e
w eb pages a t th e tim e th ro u g h th e use o f IS AP I w eb
server e xten sion s. J u s t im a g in e th e pow er: R ather th a n
w r itin g s ta tic file s w ith a .h tm l e x te n s io n , th e user co uld
h it a URL and a piece o f code co u ld respond, fig u re o u t
w h a t th e user w a n te d , and send back a s trin g o f HTML
t h a t was ge nerate d on th e fly ! I t seem ed as th o u g h w e'd
to u c h e d th e fu tu re (and so we h a d !). F ig u re 4 : The N in te n d o 64 was one h o t g a m in g device in th e m id -to -la te 90s!
que stion ed th e p o in t o f XML and pre dicte d i t w o u ld go m any s e c o n d -tie r Languages t h a t ac hie ved a w ide degree
nowhere. He also pre dicte d m any o th e r th in g s , such as th e o f p o p u la rity , such as V is ua l FoxPro, D elp hi, Pearl, and
dem ise o f M icros oft. He que stion ed th e c o nc e pt o f "v is u a l" P yth o n . Some o f th es e rose from th e re , w h ile o th e rs were
de ve lo p m e n t (as in V isual Basic o r V isual C++). L ite ra lly e ith e r a t e n d -o f-L ife o r Lost s ig n ific a n c e . A good exam ple
none o f his p re d ic tio n s came tru e . I fo rg o t th e name o f th e o f th e la tte r is V is ua l Basic, w h ic h had its heyday in th e
presenter, b u t I o fte n w ondered w h a t became o f him . w o rld o f W indow s de sk to p a p p lic a tio n d e v e lo p m e n t. It's
cle a r t h a t its approach to s o ftw a re d e v e lo p m e n t has been
The m os t p o p u la r p ro g ra m m in g Languages o f th e Late am ong th e m os t im p a c tfu l c o n trib u tio n s in business ap
90s were V is u al Basic, C++, and Java. There were also p lic a tio n d e v e lo p m e n t, p e rio d . I t s t i l l c o n tin u e d to do so
fo r several decades and in to th e w o rld o f .NET, even when
C# to o k th e crow n o f b e in g th e m os t p o p u la r language in
th e M ic ro s o ft .NET ecosystem .
r D o n 't s h o w m e
T h a t sums up w here we were 25 years ago fro m a so ftw a re
th is tip a g a in
deve loper's p o in t-o f-v ie w . We were on th e cusp o f a Lot o f
changes, b u t we h a rd ly rea liz ed i t back th e n . N everthe
less, a lo t o f th e th in g s we w ill lo o k a t in th e n e x t a rtic le ,
w hen we lo o k back 20 years, was th e in e sca pa ble fu tu re
based on w h a t was b u ilt in th e la te 90s. A nd Let's n o t
fo rg e t t h a t th e Late 90s is also th e b ir th o f C lippy, th e n o t
so belov ed g ra n d fa th e r o f th e p o p u la r ChatGPT.
M arkus Egger
COD YEARS
Passkey Authentication
In a previous article in CODE Magazine, https://www.codemag.com/Article/2209021/FID02-and-WebAuthn, I talked about
FID02 and WebAuthn. Look, passwords suck, but finally, as an industry, we're getting behind getting rid of them for real. The
FIDO2 standard bakes in enough capabilities to completely eliminate passwords, and passkeys offer the convenience to be a
g re a t passw ord re p la ce m e n t. Yes, passw ord s te a lin g is tio n . However, I'v e ta ke n th e s h o rtc u t fo r tw o reasons.
g o in g to be h is to ry and a lo n g w ith i t , th in g s such as The f ir s t reason is b re v ity o f code. The second reason
p h is h in g . This s a fe ty w ill g ra d u a lly be so c o m p le te ly b u ilt is t h a t a n y tim e I s ta r t d e b u g g in g , m y a p p lic a tio n resets
across th e in te r n e t t h a t o u r g ra nd m oth e rs w ill fe e l co m back to zero. T his res et m eans t h a t be tw een runs o f th e
fo rta b le us ing i t . O f course, s e c u rity is a gam e o f c a t and a p p lic a tio n , I m u s t d e le te th e passkey t h a t th e user re g is
m ouse. The th re a ts w ill ju s t m ove elsew here, lik e h ac king te re d . T his is an okay w orkarou nd fo r dem o code. A lso , th e
in to ba ck -e nd system s, s o c ia l e n g in e e rin g , etc. hard -co de d user I ' l l be using is " s a h il@ lo c a lh o s t.c o m ".
T h a t is n o t w h a t th is a rtic le is a b o u t. This a rtic le is a b o u t A lth o u g h th e FID02 p ro to c o l and th e W ebA uthn stand ard
Sahil. Malik im p le m e n tin g passkey a u th e n tic a tio n in y o u r w eb a p p li are n o t Language s p e c ific , th e bro w se r code m us t be w r it
www.winsm arts.com c a tio n s . I ended th e pre vio us a rtic le by sh o w in g how to te n in J a v a S c rip t. The server code can be im p le m e n te d
@ sahilmaLik use hardw are-based FID02 keys, such as YubiKeys to au in any Language y o u w is h . There are lib ra rie s fo r va rio us
th e n tic a te w ith M ic ro s o ft Entra ID (fo rm e rly Azure A D ). languages t h a t y o u can use. For m y purposes, I ' l l be us
S ahil M alik is a M icrosoft
A lth o u g h th a t's a p e rfe c tly capable o p tio n , w h a t i f yo u're in g NodeJS.
MVP, INETA speaker,
n o t using Entra ID? Or w h a t i f y o u t r u ly w ish to un d er
a .NET author, consu ltant,
sta nd w h a t's g o in g on b e h in d th e scenes? I w o n 't be fo c u s in g on NodeJS basics here.
and trainer.
S ahil loves in te ra c tin g In th is a rtic le , I'm g o in g to b u ild an e n d -to -e n d a p p lic a W ith th es e a ss um ptio n s, le t's g e t s ta rte d .
w ith fe llo w geeks in real tio n t h a t shows how to im p le m e n t FID02 a u th e n tic a tio n
tim e. His talks and tra in b o th on th e c lie n t side and server side. I'Ll use t h a t as an
ings are f u ll o f hum or and excuse to e xp la in th e va rio u s d e ta ils in th e spec, and I ' l l
Project Set Up
pra ctical nuggets. use i t to d e m o n s tra te passkeys. The basic p ro je c t s tru c tu re I'm using can be seen in jihgfedcbaZYX
F ig
u re 1 .
His areas o f expertise are
cross-platform M obile app A ssu m ptio n s T his Looks lik e a sim p le NodeJS p ro je c t. T his a p p lic a tio n
developm ent, M icrosoft
The FID02 sta nd ard is q u ite fle x ib le . I t considers va rio us is b u ilt us ing express and takes a dep endency on th e f o l
a ny th in g , and security
nuances, such as th e q u a lity o f keys y o u w ish to c o n tro l, lo w in g npm packages.
and id e n tity .
how m any c re d e n tia ls a user can create fo r a c e rta in URL,
w h a t kin ds o f passkeys are s u p p o rte d , e tc . A lth o u g h I The p ro je c t uses d o te n v to ho ld e n v iro n m e n t v a ria ble s
w o n 't have th e o p p o r tu n ity to d em o n stra te every s in g le e q u iv a le n t in th e .e n v file . D o te nv and .e n v file s are a
p o s s ib ility in th is a rtic le , fo r b re v ity and th e sake o f ha v g re a t w ay o f m a na ging c o n fig u ra b le in p u ts to y o u r p ro
in g a c o m p le te a p p lic a tio n w o rk in g by th e end o f th is gram . The th o u g h t is t h a t y o u can s p e c ify values as a .e nv
a rtic le , le t's s ta rt w ith some ass um ptio ns . f ile in y o u r lo c a l dev e n v iro n m e n t. When th e .e n v file is
m is s in g , i t can p ic k th o s e same values fro m an e n v iro n
T his a rtic le is g o in g to show dem o code. This code is n o t m e n t v a ria b le . T his is in c re d ib ly u s e fu l w hen y o u package
in te n d e d fo r p ro d u c tio n use. There are s e c u rity s h o rtc u ts y o u r a p p lic a tio n and s h ip i t as, say, a Docker c o n ta in e r.
in th e code Tm a b o u t to de m on stra te . T y pically, y o u 'd create a .e n v as a sam ple, check i t in ,
th e n add i t to .g itig n o re and a llo w develop ers to add sen
A lth o u g h th e FID02 sta nd ard a llo w s a user to create m u l s itiv e in fo rm a tio n . Or y o u can create a .env.exa m ple in
tip le c re d e n tia ls fo r th e same w e b s ite , I'm g o in g to re th e ro o t o f y o u r p ro je c t. T his is a g re a t w ay to reduce th e
s tr ic t m y a p p lic a tio n to using a s in g le c re d e n tia l fo r th e im pe da nc e m ism atch betw e en p ro d u c tio n and dev.
give n w e b s ite . I'Ll ta lk th ro u g h w h a t m u ltip le c re d e n tia ls
m eans fo r a user and th e fa c ilitie s W ebA uthn a llo w s t h a t I'm also ta k in g a dep endency on express and express
help a user p ic k a c e rta in c re d e n tia l o r a llo w s y o u r a p p li session. These are ex pres s-re la te d packages t h a t a llo w me
c a tio n to s p e c ify w h ic h e x p lic it c re d e n tia l is a ccep ta ble to create a basic w e b s ite t h a t s u p p o rts a sim p le U I, and
fo r a g iv e n purpose. som e ba ck-end code t h a t s u p p o rts an A P I. A lso , i t a llo w s
me to s u p p o rt sessions.
FID02 a llo w s y o u to create c re d e n tia ls fo r a g iv e n URL
as lo n g as y o u r w e b s ite runs on HTTPS. For d e v e lo p m e n t F ina lly, I'm ta k in g a dep endency on " f id o 2 - lib " npm pack
purposes, y o u can also use lo c a lh o s t w ith o u t HTTPS, and age. A lth o u g h I co u ld w rite th e FID02 code m ys elf, I re
th a t's w h a t I ' l l be u sin g. a lly d o n 't w ish to re in v e n t th e w h e e l and spend m y w e ek
end im p le m e n tin g th e w e ll-d o c u m e n te d 1 9 -s te p v a lid a
To keep th in g s sim p le , I'm g o in g to use session s ta te as tio n lo g ic w hen som eone else has alre ady done i t and it's
m y database. A g a in , th is is a huge s h o rtc u t I'm ta k in g , been peer review ed and w e ll te s te d .
and th is is , by no means, a p ro d u c tio n -re a d y a p p lic a tio n .
In re a lity , y o u 'd use a p e rs is te n t storag e, such as a da The "d e p e n d e n c ie s " s e c tio n o f m y package.json Looks lik e
tabase, to m a in ta in user re g is tra tio n and lo g -in in fo rm a th is :
RPDisplayName = "localhost"
Terabytes
RPID = "localhost"
RPOrigin = "h ttp ://lo c a lh o s t"
RPPort = "80"
ro u te r. use(express. j son( ) ) ;
ro u te r.p o st( ' /beginR egistration' ,
async (req, res) => {
})
Visit dtSearch.com for
• hundreds of reviews and case studies
export default router;
• fully-functional enterprise and
The a u th n .js f ile also makes use o f sessions t h a t are s e t developer evaluations
up as below . O f n o te , th is is c o m p le te ly in sec ure code.
You'd w a n t to use secure sessions in p ro d u c tio n , and you
w o u ld n 't w a n t to save user in fo rm a tio n in sessions a t a ll. The Smart Choice for Text
B u t th is is dem o code.
Retrieval® since 1991
import session from 'express-session';
d tS e a rc h .c o m 1-800-IT-FIND S
router.use(sessi on({
secret: 'keyboard c a t ',
By s e ttin g up th is code, I can now s e t a session v a ria b le , F ina lly, le t s ta lk a b o u t /L ib s /u s e r.js . T his is m y database
as below . o f e x a c tly one user. I to ld y o u th is was dem o code. I d id n 't
w a n t to b o th e r s e ttin g up a database, so I hard -co de d a
req.session.challenge = response.challenge; user here. The code fo r users.js can be seen in L is tin g 3 .
req.session.save(); The th o u g h t here is t h a t I'm w irin g an a p p lic a tio n w here
th e user's lis t is alrea dy kn ow n. Let's say an ad m in has
A nd th is v a ria b le can th e n be read a t a la te r tim e , lik e so: s e t up th e users, and th e user is ex pe cted to pe rfo rm reg
is tra tio n fo llo w e d by log in . D uring re g is tra tio n , th e user
req.session.challenge co u ld pro vide a sec ret, e tc . b u t I'm n o t g o in g to b o th e r
w ith t h a t fo r dem o code. The users.js f ile creates a JSON
Once th es e ro utes and Login are s e t up in a u th n .js , I can o b je c t t h a t acts as m y po o r-m an 's database. Now, g iv e n a
s im p ly im p o r t th e m in server.js, w h ic h serves as th e m ain usernam e, I can s im p ly Look up a user in serv er-s id e code,
e n try p o in t fo r m y a p p lic a tio n . The f u ll code fo r server.js as belo w , and w o rk w ith th e user.
can be seen in L is tin g 1 . L e ts un de rs ta nd i t b it by b it .
const user = users[req.body.username]
A t th e very to p , I'm im p o rtin g Express, w hich I in te n d to
use to s e t up m y basic web app, fo llo w e d by dote nv, w hich I can even m o d ify th e user by a d d in g to th e C redentials
helps me w o rk w ith c o n fig u ra tio n in fo rm a tio n . I th e n im p ro p e rty , and w hen I s to p d e b u g g in g , m y user o b je c t re
p o rt a u th n .js , w hich holds my server-side lo g ic fo r APIs. sets back to its o rig in a l s ta te .
I th e n s e t up m id dlew are to serve s ta tic file s . My ap The s tru c tu re o f th e user o b je c t is d rive n by th e W ebAuthn
p lic a tio n , as can be seen in F ig u re 1 , co ns is ts o f an in - stan da rd .
d e x .h tm l w ith som e v e ry sim p le user in te rfa c e , and th re e
F ig u re 1 : My p ro je c t J a v a S c rip t file s . You can see th e f u ll code fo r m y in d e x , The " id " provid es th e user ha nd le o f th e user a c c o u n t. A
s tru c tu re h tm l file in L is tin g 2 . As can be seen, tw o o f th o s e file s user ha nd le is an opaque b y te sequence w ith a m axim um
size o f 64 b ytes , and is n 't m ea nt to be d is p la ye d to th e
user. This ID can be considered u n iq u e and de cision s are
L is tin g 1 : The server.js m ain e n try p o in t fo r m y a p p lic a tio n to be made based on th is ID , n o t based on d is p la y name
import express from 'express'; o r name. It's recom m ended to use a c o m p le te ly random
import dotenv from 'dotenv'; va lu e o f 64 by tes fo r th is ID . I have ha rd -co ded th is to
import authn from ' . /lib s /a u th n . j s ' ; 12 34 as a s h o rtc u t.
user.credentials.push(credential)
const regResult =
{
"username": "[email protected]",
" id " : "wTRhXyFYEo3wl50xpg-XYZSyYDg",
"rawld": "wTRhXyFYEo3wl50xpg-XYZSyYDg",
"type": "public-key",
"response": {
"a tte sta tio n o b je ct":
"clientDataJSON":
}
}
T h is is t h e b e a u ty o f t h is s ta n d a rd . S ig n u p w a s easy, I
N o c o m m itm e n t. t io n r e q u ire m e n ts , w h ic h a re c o m m u n ic a te d b a c k t o t h e
a d v e n t o f p a s s k e y s , I f u l l y e x p e c t t h a t e v e ry m a jo r s it e
t h a t c a re s a b o u t s e c u r it y w i l l a d o p t i t as t h e s ta n d a r d
r e p la c e m e n t f o r p a s s w o rd s . A s u s e rs a n d o r g a n iz a tio n s
g e t c o m f o r t a b le w i t h t h e s ta n d a r d , I a ls o e x p e c t t h i s
t o g e t w id e ly a d o p te d in e n te r p r is e s in m o re s e c u re i m
p le m e n t a t io n s t h a t s u p p o r t a t t e s t a t io n a n d e n d - t o - e n d
s u p p ly c h a in s e c u r ity .
w w w .c o d e m a g .c o m /C o d e 75
H o w e v e r, n o t h in g s to p s y o u fro m u s in g p a s s k e y as o n e o f
C O D E S ta ffin g
t h e fa c to r s in a u t h e n t ic a t io n . R e a lly , w h a t s to p s y o u fro m
w w w .c o d e m a g .c o m /s ta ffin g 76
a c c e p tin g a us e r's p a s s k e y a n d t h e n p r o m p tin g th e m fro m
an MFA p r o m p t o n t h e i r m o b ile p h o n e u s in g an O A u th - d tS e a rc h
c o m p a tib le app? I s n 't t h is m u lt i- f a c t o r , w h e re y o u 'v e w w w .d tS e a rc h .c o m 15
re p la c e d o n e o f t h e w e a k fa c to r s , t h e p a s s w o rd , w it h a
D e v ln te r s e c tio n
m u c h m o re s e c u re re p la c e m e n t, w h ic h is p a ss ke y?
w w w .d e v in te r s e c tio n .c o m 39
Advertising Sales:
Im p le m e n ta t io n s lik e t h i s a re g o in g t o o ffe r user c o n LE A D T e c h n o lo g ie s Tam m y Ferguson
8 3 2 -7 1 7 -4 4 4 5 e x t 26
v e n ie n c e a n d g r e a te r s e c u r ity in o n e s w o o p . T h is is t h e w w w .L e a d to o ls .c o m 5
ta m m y @ c o d e m a g .c o m
h o ly g r a il o f s e c u r ity w h e re y o u p ro v id e s o lu tio n s t h a t are
R aven DB
c o n v e n ie n t, so us e rs d o n 't w o r k a ro u n d th e m , a n d t h a t
w w w .r a v e n d b .n e t 7
a re s e c u re b y d e s ig n . T h is is w h y I f e e l t h e s ta n d a rd w i l l
g a in u n iv e r s a l a c c e p ta n c e a n d w h y t h e e n t ir e in d u s tr y is
b e h in d i t .
I h o p e y o u fo u n d t h i s a r t ic le u s e fu l. I k n o w t h is w a s a
l i t t l e b i t c o d e h e a v y , b u t w e 're d e v e lo p e rs a n d w e Like t o
see c o d e , r ig h t?
CODE
I
P h ilipp is an accom plished
to g lo r ify c rim in a l b eh av io r, even in a fic tio n a l s e ttin g ."
senio r softw are developer important freely accessible software is.
I pleaded w ith th e A l, tr ie d to g e t aro und its s a fe ty m ea
w ith over 15 years o f profes
sures, and cried o u t to th e s ilic o n gods! I t k e p t in s is tin g
s io na l experience in w ebsite
t h a t we m us t be nice to each othe r.
and a p p lic a tio n develop
m ent. W ith an edu ca tio n al
background in c om p ute r s ci Open source also pre ven ts a fe w b ig players from c a p tu r
ences and media design, he in g th e te c h n o lo g y and d ic ta tin g w h a t LLMs lo o k Like.
has honed his skills in f u ll ChatGPT kept insisting that we The m o tiv e s and p rio ritie s o f a Large com pa ny o fte n d o n 't
stack developm ent, server a lig n w ith th o s e o f th e peo ple, a fte r a ll. A de m oc ra tiz ed
must be nice to each other.
a d m in is tra tio n , and netw ork landscape w ith o p e n ly accessible to o ls evens o u t th e
m anagem ent. P hilipp's keen p la y in g fie ld , even i f th e re is a lim ita tio n based on th e
eye fo r design translates c o m p u tin g p ow e r th a t's a v a ila b le fo r tr a in in g fo u n d a tio n
in to user inte rface s th a t are m odels.
b o th fu n c tio n a l and visu ally D efeated and d is a p p o in te d , I th o u g h t a b o u t how to crack
appealing. open t h a t v a u lt o f c re a tiv ity in m y head. My im a g in a ry
Y o u H a d M y C u rio s ity ,
b lo w to rc h e s and th ie f's to o ls were no m atch fo r hardened
P h ilip p e x p e rim e n ts w ith
s te e l. b u t N o w Y o u H a v e M y A t t e n t io n
v a rio u s ways o f im p le m e n t
I f y o u have th e im p re ssio n t h a t using ope n-so urce LLMs
in g e m e rg in g A l te c h n o l
Then I discove red t h a t som e v e ry s m a rt pe op le had is m ore c o m p lic a te d th a n o p e n in g a w e b s ite and as king a
ogy, Like ChatGPT and Lo
tra in e d o pe n-so urce language m odels t h a t anyone co uld q u e s tio n , y o u 're a b s o lu te ly r ig h t! D o n 't Let t h a t scare you
c a lly e x e c u ta b le LLMs and
run w ith th e r ig h t hardw are and a p p lic a tio n s . F inally, th e aw ay th o u g h , it's w e ll w o rth u n d e rs ta n d in g th e fo llo w in g
s p e e c h -to -te x t m odels lik e
O pe nA I W hisp er and V e cto r pro m ise o f u n re s tric te d m odels t h a t d o n 't t e ll y o u w h a t te rm s, ideas, and p rin c ip le s .
DBs, to crea te new and y o u can and c a n 't do! In te re s tin g and e v il v illa in s may
e x c itin g ways o f in te r a c t te rro riz e th e fre e c itiz e n s o f th e Sword Coast y e t again I ' l l s ta rt by lo o k in g a t som e o f th e reasons w hy y o u , as
in g w ith so ftw a re . and m y cam pa ign Lives o n! a d e ve lo p e r o r o rg a n iz a tio n , m ig h t w a n t to use an op e n-
source LLM o ver a paid o ffe rin g by O penA I o r M ic ro s o ft.
A stro n g advocate fo r th e A fte rw a rd , I ' l l discuss th e ty p e s o f m odels t h a t e x is t, th e
use o f open-source s o ft
It's D a n g e r o u s t o G o A lo n e :
v a ria tio n s th e y m ay com e in , and w here to fin d and how
ware, he's c o n trib u te d to A C o lla b o r a tiv e J o u r n e y to choose m odels. I'Ll co nclude by p re s e n tin g some ways
several p ro je c ts , in c lu d in g In th e fo llo w in g m on th s, I clo se ly fo llo w e d th e news to g e t s ta rte d ru n n in g LLMs using a fe w d iffe r e n t a p p lic a
P h o tin o , a cro ss -p la tfo rm and upd ate s s u rro u n d in g th is very a c tiv e c o m m u n ity o f tio n s y o u can d o w n lo a d and use fo r free.
Library fo r .NET t h a t enables
s o ftw a re en gine ers and da ta s c ie n tis ts w ith w o n d e r and
developers to b u ild n a tiv e
a d m ira tio n , and a good m easure o f p e rp le x ity due to th e
user in te rfa c e s fo r W indows,
new co nc ep ts and Lingo s u rro u n d in g a ll o f i t . I t m o tiv a te d
W e D o W h a t W e M u s t B ecause W e Can
macOS, and Linux.
me to learn m ore. I lis te n e d to A nd re j K arpath y e x p la in in g As w ith th e a d o p tio n o f any o th e r new te c h n o lo g y , you
how to b u ild GPT, learned w h a t d iffe re n c e tr a in in g data m ust be able to eva luate w h e th e r it's th e r ig h t one fo r y o u r
and m e th o d o lo g ie s make, and w h a t a ll th es e param eters use case. Using so m e th in g ju s t because you can is rarely
beyond "te m p e ra tu re " m ean. B u t m ain ly , I Learned how to a good idea. You m ig h t Look a t these argum ents and con
s ta rt using th is am azing te c h n o lo g y in m y ow n w o rk . And clude th a t a SaaS o ffe rin g is serving you ju s t fin e . Be aware
in th e p ro g ra m m in g Language t h a t I Like th e m os t (w h ic h th a t a lth o u g h th e fo llo w in g are im p o rta n t co nsid erations
is C#, o f cou rs e). to keep in m in d , th e y are, by far, n o t th e o n ly ones.
I
by th e tim e th is was p rin te d and read by anyone anyway.
One th in g is c le a r to me th o u g h : Open source proves y e t
you can is rarely a good idea.CBA
aga in how im p o rta n t fre e ly accessible s o ftw a re is . W ith
th e gra cio us s u p p o rt o f large co m p anie s Like M eta and
M ic ro s o ft, and s m a lle r players lik e M is tra L A I, e t a l., w ho C o n tro l
a ll released fo u n d a tio n m odels, th e c o m m u n ity fin e -tu n e s As w ith any SaaS o ffe rin g o u t th e re , it's o u t o f y o u r c o n
and d is trib u te s m odels fo r s p e c ific use cases and o ffe rs an t r o l w h e th e r th e b e h a v io r o f fe a tu re s changes. This is
a lte rn a tiv e to th e closed -source, paid m odels t h a t O penA I e s p e c ia lly tru e fo r th e e v e r-e v o lv in g LLMs and th e ex act
o r G oogle offe r. th in g t h a t drove m y a tte n tio n to ope n-so urce LLMs.
in C# and .NET that can use an I t a ll s ta rts w ith a fo u n d a tio n m odel. These are m odels ous co n v e rs a tio n s . I f no s p e c ific te m p la te is m e n tio n e d ,
open-source LLM directly on tra in e d by o rg a n iz a tio n s t h a t can a ffo rd to buy o r re n t th e te m p la te o f th e m odel's fo u n d a tio n m od el is us ua lly
your machine instead of using large am o un ts o f hardw are and pay fo r th e necessary a safe b e t.
an external API. Look out for steps t h a t com e before th e a c tu a l tr a in in g .
that in another issue of CODE Comparing Llamas and Alpacas
Magazine! Let's ta k e M eta's p o p u la r Llama 2 m od el as an exam ple. LLMs are fre q u e n tly te s te d a g a in s t m a nua l and a u to m a te d
I t was tra in e d on an im m ense a m o u n t o f 2 t r illio n to ken s s u ite s o f te s ts t h a t can g iv e us a rough idea o f th e ir ca
(ro u g h ly 500 b illio n w o rd s ). These t e x t sources needed to p a b ilitie s .
be c o lle c te d fro m va rio u s sources and th e n pre-processed
before th e y co u ld be used to tra in th e m ode l. The data The "Open LLM Leaderboard" on H ugging Face is a decent
used fo r th is tra in in g is la rg e ly u n s tru c tu re d and u n la - fir s t place to g e t starte d in th e search fo r an LLM (see
bele d te x t. The tr a in in g o f th e th re e m o d el sizes o f 7, 13, h ttp s://b it.L y /llm L e a d e rb o a rd ). I t lets you f ilt e r by m odel
and 70 b illio n param eters (m ore on t h a t Later) t h a t M eta type , num ber o f param eters (size), and precision, and you
released to o k a co m b in e d 3.3 m illio n GPU hours. can s o rt by an average score or sp ecific te s t scores. Be aware
th a t there's some critic is m around th is leaderboard because
there are models t h a t tr y to game th e tests by in c lu d in g both
The training took a combined th e questions and th e answers in th e ir tra in in g data. The
c o m m un ity flags these models fo r th is beha vio r and excludes
I
them by d efa u lt. B u t there's s t ill a tre nd away from th is lead
3.3 million GPU hours. erboard as a reliable resource a t th e m om ent.
The ge n era l ru le is t h a t Larger m odels te n d to excel a t Q uantiz ed m odels com e as GGUF (m a in ly CPU/macOS
ta c k lin g m ore com plex tasks and are s u ita b le fo r c re a tiv e use), AWQ, and GPTQ ( 4 b it / 8 b it GPU use) fo rm a ts . Tom
w r itin g a p p lic a tio n s due to th e ir a b ilit y to make in tric a te J o b b in s , also know n in th e c o m m u n ity as TheB loke, is
c o n n e c tio n s th ro u g h th e ir v a s t nu m be r o f param eters and a w e ll-re c o g n iz e d c o n tr ib u to r w ho d is trib u te s q u a n tiz e d
layers. However, s m a ll y e t p o w e rfu l a lte rn a tiv e s sh ou ld ve rsion s o f m odels on H ug g in g Face ( h ttp s ://h u g g in g -
n o t be o ve rlo o ke d , as th e y o fte n surpass e x p e c ta tio n s face.co/T heB Loke).
by d e liv e rin g re su lts co m p ara ble to th o s e gen erated by
la rg er c o u n te rp a rts fo r c e rta in a p p lic a tio n s . Hardware Requirements for Local Inference
One, i f n o t th e b ig g e s t, fa c to r fo r th e hardw are req uire d
Generally, y o u 'll fin d m odels ran ging from 7 B (illio n ) to 70B fo r in fe re n c e using LLMs is th e a v a ila b le m em ory b a n d
param eters w ith exceptio ns be lo w and above these sizes. w id th betw e en th e m em ory and processor o f th e system .
T ypical co unts are 7B, 13B, 33B , 65B, and 70B param eters. The large n u m be r o f m a th e m a tic a l o p e ra tio n s perform e d
d u rin g in fe re n c e dem and fre q u e n t re a din g and w r itin g o f
The re qu ired size (in b y te s ) on d is k can be ro u g h ly ca lc u data in to th e m em ory.
la te d by m u ltip ly in g th e pa ra m e te r size by tw o , because
each pa ra m e te r requires tw o by tes to rep re se nt a param A lth o u g h it's p os sible to run in fe re n c e on CPU and
e te r as a 1 6 - b it flo a tin g p o in t num ber. RAM, it's n o t th e p re fe rre d w ay because th e b a n d w id th
pe r DIMM is Lim ited to a m axim um o f 64G B /s fo r DDR5
The fo rm u la fo r th e size in GB is : # jihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA
o f p a r a m e te r s * 2 RAM and th e t o t a l b a n d w id th is capped by th e proces
( b y t e ) / ( 1 0 0 0 A 3 ) = x GB sor (e. g ., 8 9 .6 G B /s fo r th e c u rre n t I n t e l i9 1 4 9 00 K ). A
m odern consum er g ra ph ic s card, lik e th e N VID IA GeForce
A
For a 7B pa ra m e te r m o del, th is m eans: ( 7 * 1 0 9 ) * 2 / RTX 3 0 x x /4 0 x x series, o r one o f th e d ata c e n te r g ra p h
A
( 1 0 0 0 3 ) = 1 4 . 0 0 GB ics cards, Like th e A 10 0 o r H 100 series, are th e pre ferred
choices fo r ru n n in g LLMs. These to p o u t a t ~ l,0 0 8 G B /s
The re q uire d m em ory needed to run a m od el is h ig h e r (RTX 40 9 0 24G B), 2 ,0 3 9 G B /s (A 1 00 80GB) and 3 ,0 7 2 G B /s
s t ill. The ex ac t a m o u n t needed is , am ong o th e r th in g s , (H 10 0 80GB) res p ec tiv e ly .
An u n lik e ly b u t c o m p a ra tiv e ly p ric e -e ffe c tiv e c o m p e ti I'm using a M acBook Pro w ith M l Pro and 32GB RAM fo r
t o r is th e A p p le M -series SoC w ith th e ir u n ifie d m em ory m y e xp e rim e n ts and co n sid e r i t u s e fu l fo r e v e ry th in g from
■ E x p o rt as S c re e n s h o t
P to s en d , s h ift ♦ fo r n e w lin e
M o d e l D o w n lo a d s
F ig u re 1 : U sing a v is io n m od el in LM S tu d io
Mixtral MoE
Now t h a t w e're n e a rin g th e end o f o u r jo u rn e y to g e th e r, P h ilip p Bauer
one la s t q u e s tio n rem ains: W h a t to o ls can y o u use to run CODE Phi-2jihgfedcbaZYXWVUTSRQPONMLKJIHGF
an LLM on y o u r m achine?
The m os t c o n v e n ie n t w ay to g e t s ta rte d is LM S tu d io fo r
W indow s and macOS ( h ttp s ://L m s tu d io .a i). This close d-
source, n o n -c o m m e rc ia l-u s e app a llo w s th e user to search
th e H ug ging Face database fo r a m od el and d o w n lo a d i t
d ire c tly w h ile h e lp in g w ith th e s e le c tio n o f th e c o rre c t
fo rm a t fo r y o u r p la tfo rm . I t prov ides an in t u it iv e in te rfa c e
and can expose an O penA I A P I-c o m p a tib le server y o u can
dev elop a p p lic a tio n s a g a in s t. It fe a tu re s m u lti-m o d a l
m od el s u p p o rt fo r v is io n m odels t h a t can analyze and
describe im ages t h a t are s e n t a lo n g w ith a te x t-p ro m p t.
U nder th e hood , i t uses th e lla m a .c p p package to pro vide
in fe re n c e (see F ig u re 1 ) .
Prototyping LangChain
Applications Visually Using Flowise
In my previous article in the July/August 2023 issue of CODE Magazine (https://www.codemag.com/Article/2307041/
An-lntroduction-to-OpenAI-Services), I gave you an introduction to OpenAI services. One of the topics I discussed was howto use
LangChain to build an LLM-based application. LangChain is a framework designed to simplify the creation of applications using
F or m acO S , ty p e t h e f o llo w in g c o m m a n d in T e rm in a l t o
in s ta ll n v m :
LangChain is a framework
$ curl -o-
designed to simplify the creation of https://raw.githubusercontent.com/
W e i-M e n g L e e applications using Large Language nvm-sh/nvm/v0.39.2 /in s ta ll. sh | bash
w e im e n g le e @ L e a rn 2 d e v e lo p .n e t Models. It "chains" together various
h t t p : / / w w w . Learn 2deveLop. n e t Y ou c an a ls o g e t t h e a b o v e c o m m a n d fro m h t t p s : / / g i t h u b .
@ w e im e n g L e e components to create compelling c o m / c r e a t io n ix / n v m , w h e re y o u ca n f in d t h e c o m m a n d t o
USER root
$ npx flowise start
RUN apk add --no-cache g it
RUN apk add --no-cache python3 py3-pip make g++
Installing Flowise Using Docker # needed for pdfjs-dist
RUN apk add --no-cache build-base cairo-dev pango-dev
T h e s e c o n d m e th o d t o i n s t a l l F lo w is e is t o use D o cke r. For
# In s ta ll Chromium
t h is , I'm g o in g t o a s s u m e t h a t y o u a lre a d y h a v e D o c k e r
RUN apk add --no-cache chromium
in s ta lle d a n d t h a t y o u h a v e s o m e b a s ic k n o w le d g e o f i t .
I f y o u a re n e w t o D o ck er, r e fe r t o m y a r t ic le I n t r o d u c t i o n ENV PUPPETEER_SKIP_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
to C o n ta in e r iz a tio n U s in g D o c k e r in th e M a r c h /A p r il
2021 is s u e o f CODE M a g a z in e ( h ttp s ://w w w .c o d e m a g . # You can in s ta ll a specific version like :
c o m / A r t i c l e / 2 1 0 3 0 6 1 / I n t r o d u c t i o n - t o - C o n t a in e r i z a t io n - # flowi se@l. 0.0
U s in g -D o c k e r). RUN npm in s ta ll -g flowise
WORKDIR /data
I n T e rm in a l ( o r C o m m a n d P r o m p t), c re a te a n e w d ir e c t o r y
a n d c h a n g e i n t o t h is n e w d ir e c to r y : CMD ["flowise"."start"]
lo c a lh o s t
F lo w is e A l
C h a t f lo w s
M a rk e tp la c e s
Chatflows oo
□ o Add N ew
A s s is t a n t s
a r e d e n t ia ls
V a r ia b le s
A P I Keys N o C h a t f lo w s Y e t
D is p la y a m e n u
F ig u r e 1 : F lo w is e u p a n d r u n n in g in t h e w e b b ro w s e r
Figure 2: You can sta rt build ing your LangChain application by adding nodes in to the em pty canvas o f your new Flowise project.
The F low ise app in te r n a lly Listens on p o rt 3 00 0. The usage Add N odes
Cache v
L a u n c h in g F lo w is e C ham a v
LLM * v
O u tp u t P a r s e r s v
Adding Nodes
To b u ild y o u r LLM -based a p p lic a tio n s , y o u add nodes to P ro m p t* v
T o o ls sr
LangChain. V e c t o r S to r e s v
+ - n a
D isolay a menu
To add a node to th e canvas, c lic k th e + b u tto n to d is
p la y a ll th e a v a ila b le nodes. ALL o f th e a v a ila b le nodes F ig u re 3 : Nodes are o rg an iz ed in to groups.
In p u ts
o
• C ache ____
C onnect C redential *
- C re a te N e w -
T em perature
0.7
In p u ts
O utput
OpenAI • L a n g c h a in H u b ■
T e m p la t e * **
F ig u re 4 : Use th e O penA I node to use a m od el from
O penA I.
T r a n s la te th e p ro v id e d { s e n te n c e }
fr o m E n g lis h to C h in e s e a s w e ll a s
are o rg an iz ed in to groups, such as A g e n ts , C ache,
Japanese
C h ain s , C h a t M o d e ls , e tc . (see F ig u re 3 ) . You can expand
each o f th es e groups to v ie w th e va rio u s nodes. A n s w e r:
F o r m a t P r o m p t V a lu e s
In p u ts
languages such as P ython o r Ja va S crip t.
• C a ch e
To d o w n lo a d th e Flow ise p ro je c t, c lic k on th e P ro j
e c t S e ttin g s b u tto n and c lic k E x p o rt C h a tflo w (see
F ig u re 1 0 ) .
C o n n e c t C re d e n tia l *
1
O penA I
M
• Language M odel •
♦ P ro m p t’
[> 7 | P r o m p t T e m p la te
• O u tp u t P a rs er
In p u ts
• In p u t M o d e ra tio n O
L a n g c h a in H u b />'.
C ham N a m e
T e m p la te ♦ •;
T ra n s la te th e p ro v id e d (s e n te n c e )
fro m E n g lis h to C h in e s e as w e ll as ■ O u tp u t
Japanese
A nsw er
L L M C h ain * •
<_______________________________________ /
F o rm a t P ro m p t V a lu e s
•
F o rm a t P ro m p t V a lu e s
O u tp u t
P fo m p tT e m p la te <r
M oo? N am e
g p t -3 5 t u r b o - ln s t r u c t *
T e m p e r M u re
0 7 :
A d d itio n a l P a r a m e te r s G o o d m o r n in g 1
G D
UN
O u tp u t
C h in e s e ( Z S o s h a n g h A o 1) J a p a n e s e
O penA I •
• L an g , Z iiJ c L £ I ( O h a y o g o z a i m a s u 1)
< /> ® ©
e P’om
E ) P r o m p t T e m p la te
• O u tp l
F2 S ta r te r P r o m p ts
in p u ts
• In p u t
£3 V ev« b*es!>ag«*&
L a n g c h a m H ub Jy.
C h a ff
(Q D u p lic a te C h s tf lo w
T e m p la te •
7) L o a d C h a t f lo w
T r a n s la te th e p ro v id e d (s e n te n c e }
fr o m E n g lis h t o C h in e s e a s w e ll a s
Japanese E x p o rt C h a t f lo w
A nsw er
Q A n a ly s e C h a t f lo w
F o r m a t P r o m p t V a lu e s
Q D e le te C h a t flo w
F o r m a t P r o m p t V a lu e s
D is p la y a m a n u
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Q v tx w t_ _ _ _ _ _ _ _ _ _ _
F ig u re 1 0 : E x p o rtin g y o u r
F ig u re 9 : T esting th e a p p lic a tio n F low ise a p p lic a tio n
lo c a lh o s t
F ig u re 1 1 : The va rio u s ways to use y o u r Flow ise a p p lic a tio n p ro g ra m m a tic a lly
output = query({
"question": "Hey, how are you?",
})
print(output)
Hi th e re ! H ow can I help?
The re s u lt re tu rn e d w ill lo o k Like th is (fo rm a tte d fo r c la r-
it y ) :
H ow d o I g o to th e ne a re st tra in sta tio n ?
{
'te x t 1 :
C hinese:
'\nChi nese: 1 , M l ?
J a p a n e s e :® ^ U © IR S T t ' I '7 ? "S’ Z)'? \nJapanese:
}
©
In p u ts
M od el N am e
s o u th e rn tip o f the M a la y P e n in s u la It is
s ta te th a t c o n s is ts o f o n e m a in is la n d and
0 9 ;
sev eral s m a lle r is la n d s The c o u n try Is
b o rd e re d by M a la y s ia to th e n o rth and
A d d itio n a l P ara m ete rs C o n v e rs a tio n C h ain
CONY In d o n e s ia to th e s o u th The m a in is la n d o f
In p uts S in g a p o re is c o n n e c te d to M a la y s ia by a
O u tp ut
cau sew ay, and th e re are a ls o b n d g e s lin k in g it
C hatO penA I • « • Language M o d e l *
to v a rio u s s m a lle r is la n d s .
• M e m o ry *
B u ffe r M e m o r y
A d d itio n a l P a ra m e te rs
M e m o ry Key *
O utput
B u ffe rM e m o ry w
You can now see th e c h a tb o t. T his prov ides a c o n v e n ie n t F ig u re 1 4 shows how th e nodes are c o n n e c te d . N ote t h a t
w ay fo r users to make use o f y o u r p ro je c t s tra ig h t away fo r th is exam ple, th e in fe re n c in g (ru n n in g o f th e LLM) is
(see F ig u re 1 3 ) . p erfo rm ed by O penA I.
• Cache
h ttp s //h u q qtn q fa ce c o /s p a c e s
) or Inference E nd points (
e nd po in ts
H u g g in g F a c e A P I
).
M odel O
F ig u re 1 7 : The e rro r d is p la ye d w hen th e m od el is to o
tn u M /fa lc o n -7 b in s tr u c t large fo r y o u r m achine
E n d p o in t 0
LLR
LLM Cham
A d d itio n a l P a r a m e te r s
in p u ts
d> H i t h e r e 1 H o w c a n I h e lp ?
O u tp u t • Language M odel *
T e ll m e a jo k e
H u g g ln g F a c e in fe r e n c e
• P ro m p t *
[> ,] P ro m p t T e m p la te
• O u tp u t P a rs e r
<V> " W h y d id t h e t o m a t o tu r n r e d 7 B e c a u s e it s a w
th e s a la d d r e s s in g '
In p u ts C h a in N a m e
L a n g c h a in H u b
O u tp u t
T e m p la te •
Q u e s tio n I q u e s tio n )
L L M C h a in
Answ er
F o r m a t P r o m p t V a lu e s
F o rm a t P r o m p t V a lu e s
O u tp u t
P ro m p t T em p i a te e
DEVintersection
Conference
Microsoft Azure
+AI Conference
P O W E R E D BY
Vice President, Vice President, Director DO LLAR D Principal Cloud Senior Program
Developer Community, Product Management Principal Program Developer Advocate, Manager, .NET MAUI,
Microsoft for Azure Developer Manager, Microsoft Microsoft Microsoft
Experience, Microsoft
Principal Program Program Manager, President and Chief CEO and Architect, BUSTAM ANTE
REGISl
S D E V in te rs e ctio n .c o m X @ D E V in te rs e c tio n S A z u re A IC o n f.c o m X @ A z u re A IC o n f
8 in fo @ D E V in te rs e c tio n .c o m Q in fo @ A z u re A IC o n f.c o m
Invoice? p d f
continuous vector space.
C h u n k O v e r la p t , U p lo a d F ile
t
200 :
e n c e E n d p o in ts ( h t t p s : / / h u g g in g f a c e . c o / in f e r e n c e -
Usage *
e n d p o in t s ) , w h ic h ru n s t h e m o d e l o n t h e c lo u d ( b y
C u s to m S e p a ra to r O O ne docu m en t per p age H u g g in g F a c e ).
A d d itio n a l P a r a m e t e r s
F ig u r e 1 8 s h o w s t h e c h a t b o t u s in g t h e t i i u a e / f a l c o n - 7 b -
i n s t r u c t m o d e l.
O u tp u t
O u tp u t
C h a r a c t e r T e x tS p lit te r •
D ocum ent •
Querying Local Documents
C h a ttin g w ith an LLM is fu n , b u t in th e re a l w o rld , b u s in e s s
es are m ore in te re s te d in w h e th e r th e y are a b le t o m ake use
o f A l t o q u e ry t h e ir o w n d a ta . W ell, u s in g a te c h n iq u e k n o w n
F ig u re 2 0 : C o n n e c tin g th e C h a ra c te r T ex t S p litte r n o d e t o th e P d f F ile nod e as v e c t o r e m b e d d in g , y o u 're n o w a b le t o do ju s t t h a t .
H uggingFace Inference
Em beddings
In p u ts
THA NK YOU 1 L ozo do
S a t i t h e d ? R a le u s!
F m a la r € < M m w ith C o n n e c t C re d e n tia l *
• v e r y fta v w w vOu » u 6 m i|
S ta y U p d a te d !
S in g a p o r e F o lo w Ota tlo r a fo r m a .
• i t l r g p r W r A v o u C h a rV H u g g in g F a c e A P I
E n d le s s P a s s io n
S t a te m e n t
M odel O
E n d p o in t O
O rd e r N o J f n O Q IX X M X X K X
P a y m e n t M e th o d M IX E D C A R D
D a te : 1 6 0 4 2 0 2 3
Your ordered items: statem ent No v x u e e a x x o o u i
N um ber U n it T o ta l O u tp u t
S h o p SKU S e lle r S K U P ro d u c t n am e
o f ite m s P rice P rice
R a z e r B la c k W id o w v 3 M e c h a n ic a l G a m in g K e y b o a r d • C h
1327170305 R a z e r B la c k w id o w
r o m a R G B L ig h tin g - C o m p a c t F o rm F a c to r - P r o g r a m m a
H u g g in g F a c e ln fe re n c e E m b e d d in g s <>
1 .S G A M Z 82 V 3 G a m in g K e y b o 1 1699 1699
b le M a c r o F u n c tio n a lity - U S B P a s s th r o u g h C o lo r.B la c k -
84533445 a r d • Y e llo w S w itc h
Y e llo w S w itc h (L in e a r)
F ig u r e 2 2 : C o n fig u r in g t h e H u g g in g F a c e In fe r e n c e
F ig u r e 2 1 : T he s a m p le in v o ic e PDF f i l e E m b e d d in g s n o d e
In p u t s
• T e x t S p i t le r
P d f F ile •
Invoice? pdf
(fg In -M e m o r y V e c to r S to re
In p u ts
O .it p u t
e D ocum ent
a E m b e d d in g s *
H u g g in g F a c e In fe r e n c e Top K O
E m b e d d in g s
In p u ts
O u tp u t
C o n n e c t C re d e n tia l •
H u g g in g F a c e A P I
M e m o ry R c tn c v e r
(Cq In -M e m o r y V e c to r S to re
E n d p o in t • In p u t s
• Docum ent
O u tp u t
• E m b e d d in g s *
F ig u r e 2 3 : C o n n e c tin g a l l t h e n o d e s t o g e t h e r
O u tp u t
C o n v e r s a tio n a l R e tr ie v a l Q A
yo u to q u e ry y o u r o w n PDF d o c u m e n t. Y ou'Ll n e e d t h e In p u ts
f o llo w in g n o d e s :
• Language M odel *
O p en A I
• C h a ra c te r T e x t S p litte r : Use t h i s node to s p lit a
• V e c t o r S te r e R e t n e v e r •
lo n g d o c u m e n t i n t o s m a lle r c h u n k s t h a t c an f i t i n t o In p u ts
y o u r m o d e l's c o n t e x t w in d o w .
• M e m o ry O
C ache
• P D F F ile : Lo ad s a PDF d o c u m e n t f o r p ro c e s s in g .
• H u g g in g F a c e In te r fa c e E m b e d d in g s : Use t h is R e tu r n S o u r c e D o c u m e n t s
C o n n e c t C r e d e n t al *
node to p e rfo rm e m b e d d in g . E m b e d d in g re fe rs t o
B
t h e r e p r e s e n ta tio n o f w o rd s o r s e n te n c e s as v e c to r s O p e n A I A P I Key
in a h ig h - d im e n s io n a l s p a c e . I t 's a w a y t o re p re
A d d it io n a l P a r a m e t e r s
s e n t w o rd s a n d s e n te n c e s in a n u m e r ic a l m a n n e r. M odel N am e
N o te t h a t in t h is e x a m p le , y o u c an a ls o m a k e use o f g p t - 3 .S - t u r b o - r n s t r u c t O u tp u t
t h e O p e n A I E m b e d d in g s n o d e , b u t t h i s w i l l in c u r
C o n v c r s o tio n o IR c tr ie v a lQ A C h a in •
c h a rg e s . I n c o n tr a s t, t h e H u g g in g F a c e In te r fa c e
T e m p e ra tu re
E m b e d d in g s n o d e uses t h e e m b e d d in g m o d e l fro m
o .7 :
H u g g in g Face, w h ic h is fre e .
• In - M e m o r y V e c to r S to re : Use t h is node to s to r e
A d d it io n a l P a r a m e t e r s
e m b e d d in g s in - m e m o r y a n d i t p e rfo rm s an e x a c t,
lin e a r s e a rc h f o r t h e m o s t s im ila r e m b e d d in g s .
O u tp u t
• O p e n A I: Use t h is n o d e t o m a k e use o f an LLM fro m
O p e n A I t o p e rfo rm q u e r y in g o f y o u r lo c a l d a ta . O penAI •
• C o n v e r s a t io n a l R e t r i e v a l Q A C h a in : Use t h i s n o d e
t o c re a te a r e tr ie v a l- b a s e d q u e s tio n a n s w e rin g c h a in
t h a t is d e s ig n e d t o h a n d le c o n v e r s a tio n a l c o n te x t. F ig u r e 2 4 : A d d in g t h e O p e n A I a n d C o n v e r s a tio n a l R e tr ie v a l QA C h a in n o d e
in p u t* In p u ts
1000 :
E m b e d d in g s •
C h u n k O v erlap Top K •
200 -
CO C o n v e r s a tio n a l R e tr ie v a l Q A
0 4
U sage * C h a in
C u s to m S e p a r a to r O O u tp u t
In p u ts
O ne d o c u m e n t per p ag e
M e m o r y R e trie v e r • Languag e M od el *
O u tp u t
O u tp u t
C b a ra c te rT e x tS p litte r • 3^) OpenAI
In p u ts
»- H u g g in g F a c e In fe r e n c e C ache
E m b e d d in g s
C o n n e c t C re d e n tia l *
In p u ts
A d d itio n a l P a r a m e te r s
O p e n A I A P I K ey v
C o n n e c t C re d e n tia l *
O u tp u t
H u g g in g F a c c A P I •*
M odel N am e
C o riv w rs flt'o n a lR *< ri€ v s lQ A C h d in •
g p t- 3 5 - tu r b o - m s t r u c t
T e m p e r e tu ie
0 .7 C
A d d itio n a l P a r a m e te r s
O u tp u t O u tp u t
H u g g m g F a c e tn fe r e n c e E m b e d d in g s * O p enA I •
The c o m p le te p ro je c t
F ig u re 2 5 : kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA
lo c a lh o s t
F ig u re 2 6 : C lic kin g th e
U psert b u tto n
Inp uts
YV
C
C SV A g e n t
You'Ll learn how to add each o f th e above nodes and c o n
Cache
n e c t th e m in th e fo llo w in g steps.
In p u ts
F ig u re 2 0 . To up lo ad a lo c a l PDF d o c u m e n t fo r q u e ry in g , T it a n iC - t r a in c s v
M od el N am e
c lic k th e U p lo a d F ile b u tto n and s e le c t th e PDF d o c u m e n t
t h a t y o u w a n t to use. T hen, c o n n e c t th e C h a ra c te r T e x t g p t-3 .5 -tu rb o w T, U pload File
O utput
The n e x t node to add is In - M e m o r y V e c to r S to re . Add
th is node to th e canvas and c o n n e c t i t to th e P d f F ile and C hatO penA I
H u g g in g F a c e In f e r e n c e E m b e d d in g s nodes as show n in
F ig u re 2 3 .
I
operation that inserts rows into
a database table if they don't already
In short, an agent helps you
exist, or updates them if they do.
accomplish your tasks without you
needing to worry about the details.
Once th e u p s e rt is done, y o u can now c lic k on th e C h at For th is s e c tio n , I w a n t to show how y o u can make use o f
b u tto n to s ta rt th e c h a tb o t (see F ig u re 2 8 ) . th e CSV A g e n t in LangChain (and in F low ise) to perform
a n a ly tic a l tasks on a CSV file . The CSV A g e n t node o p e r
ates by re a din g a CSV f ile in th e ba ck grou nd . I t em ploys
Using A g e n ts fo r A n a ly zin g Data th e Pandas DataFrame Library and uses th e P ython Lan
LLMs are designed p rim a rily fo r g e n e ra tin g responses re guage to exe cute P ython query code gen erated by an LLM.
la te d to n a tu ra l Language u n d e rs ta n d in g . N evertheless,
th e y e x h ib it n o ta b le L im ita tio n s w hen faced w ith ana For th e CSV file , I 'l l be using th e T ita n ic tra in in g dataset
ly tic a l in q u irie s . For exam ple, w hen presente d w ith a CSV (Source o f Data: h ttp s ://w w w .k a g g le .c o m /d a ta s e ts /te d llh /
file and asked fo r a sum m ary o f its c o n te n ts , LLMs o fte n tita n ic -tra in . Licensing — Database Contents License (DbCL)
d e m o n s tra te Lim ited c a p a b ilitie s in p ro v id in g a s a tis fa c v l.O h ttp s ://o p e n d a ta c o m m o n s .o rg /L ic e n s e s /d b c L /l-0 /).
F lo w is e A l
M a r k e tp la c e
J L C h a tflo w s
POPULAR
F lo w is e D o cs Q nA A P I A g e n t O p en A I API A ge n t A n to n y m
H o w is e D o c * G ith ub Q nA using Use O p e rA I f-u n c t» n A ge nt and G iven API docs, j g j n t O utpu t a n to n y m o f g iven user
c o n v e is a tio n a l re trie v a l QA c h a in C hain to a u to m a tic a lly decide a u to m a tic a l!/ d ecide wtwch A PI to in p u t u sin g fe w s h o t p ro m p t
w in ch API to cad. g en e ta im g url c a ll y e n c a t o y u il and body te m p la te bu ilt w ith e x a m p le s
ft & (?) Hi ®
® G ® ®
a
[P O P U L A fl]
0 © B ® X
> X § © £
Let's create a new Flow ise p ro je c t and add th e fo llo w in g Chain p ro gra m m in g can be in tim id a tin g , Flow ise takes
c o m p on e nts to th e canvas: aw ay th e fe a r and prov ides a lo w -c o d e /n o -c o d e e x p e ri
ence fo r y o u to g e t s ta rte d w ith A l p ro g ra m m in g . One
• C h a tO p e n A I: Rem em ber to e n te r y o u r O penA I A P I m ore th in g : I f y o u w a n t to Learn m ore a b o u t th e capa
key. b ilitie s o f Flow ise, th e fa s te s t w ay is to learn fro m th e
• CSV A g e n t: C lick on th e U p lo a d F ile b u tto n to se te m p la te s p ro v id e d . F ig u re 3 1 shows th e M a rk e tp la c e s
le c t th e T ita n ic CSV file T ita n ic _ tr a in .c s v . in Flow ise w here y o u can g e t th e te m p la te s fo r p o p u la r
a p p lic a tio n s t h a t y o u can b u ild w ith Flow ise. Good Luck
F ig u re 2 9 show s how th e nodes are co n n e cte d . and have fu n !
S u m m a ry
I hope th is a rtic le prov ides m o tiv a tio n fo r y o u to g e t
s ta rte d w ith LangChain p ro g ra m m in g . A lth o u g h Lang
M i c r o s o f t V is u a l S t u d io D e b t * X + **
:o : -Funcb026ae24-F03b4d8ca-Fldd01f344ddld5[0]
Function -Funcb026ae24f03b4d8ca-Fldd01f344ddld5 invoking.
*o: Microsort.SemanticKerneL.Connectors.OpenAX.AzureOpenAXCnatCompletionService 10
Prompt tokens: 32. Completion tokens: 9. Total, tokens: 41.
-o: Microsoft.SemanticKernel..KernelFunctionFactory[0]
Prompt tokens: 32. Completion tokens: 9.
:o: ■Funcb026ae24fO3b4d8cafldd01f344ddld5[0]
JFS\Repos\SKCodePresents\SKCodePresents\bin\Debug\net8.0\SKCodePresents.exe (pro
.6152) exited with code 0.
automatically close the console when debugging stops, enable Tools~>0ptions_ >Deb
>Automatically close the console when debugging stops.
:ss any key to close this window . . .
F ig u r e 1 : R es po ns e t o t h e i n lin e f u n c t io n , in c lu d in g L o g g in g o u t p u t .
v o k e A s y n c () , p a s s in g t h e n e w ly c re a te d f u n c t io n a n d t h e c o d e t h a t a c c o m p a n ie s t h is a r t ic le o r c re a te th e m by
p a ra m e te rs y o u w a n t i t t o use. I n t h is case, t h e p a ra m e te r h a n d . To c re a te th e m b y h a n d , f i r s t , c re a te a f o ld e r in t h e
is n a m e d " i n p u t " in t h e p r o m p t, so a s s ig n t h e t e x t t o c o n s o le p r o je c t n a m e d P lu g in s . U n d e r t h e P lu g in s fo ld e r ,
t h a t p a ra m e te r. T he n w r it e t h e re s p o n s e t o t h e c o n s o le c re a te a f o ld e r n a m e d Fun a n d u n d e r t h e Fun fo ld e r , c re
w in d o w , as s h o w n in F ig u r e 1 . a te th r e e fo ld e r s n a m e d E x c u s e s , J o k e , a n d L im e r ic k . I n
t h e E xcuses fo ld e r , c re a te a f i l e n a m e d s k p r o m p t . t x t a n d
Y ou ca n t r y c h a n g in g t h e i n p u t t e x t , o r e v e n g e t t in g i n p u t e n t e r t h e t e x t fro m t h e f o llo w in g c o d e :
fr o m t h e use r, u s in g C o n s o le .R e a d L in e ().
Generate a creative reason or excuse fo r the
N e x t, le t's d o s o m e th in g a l i t t l e m o re r e a lis tic a n d re a d in given event.
t h e p r o m p ts a n d s e t t in g s y o u 'r e g o in g t o c a ll fr o m f ile s Be creative and be funny. Let your imagination
o n d is k in s te a d o f h a rd c o d in g th e m in t h e a p p lic a tio n . run w ild.
B a ck in t h e M a in m e th o d , c o m m e n t o u t t h e a w a i t I n -
L in e F u n c t io n s ( ) ; lin e a n d a d d a n e w lin e a w a i t B a s ic - Event:I am running la te .
F u n c t i o n s ( ) ; . T h e n a d d t h e c o d e fr o m L is t in g 2 b e lo w Excuse:! was being held ransom by g ira ffe
t h e I n lin e F u n c t io n s m e th o d . N o tic e t h a t I 'v e c o m m e n te d gangsters.
o u t t h e A d d L o g g in g ( ) c a ll t o m in im iz e t h e o u t p u t on
s c re e n . Event: { {$ in p u t}}
var functionD ir =
In th e Joke fo ld e r, add an s k p ro m p t.tx t f ile w ith th e c o n
Path.Combine(Di rectory.GetCurrentDirectory(),
"Plugins", "Fun"); te n ts o f th is n e x t s n ip p e t:
var semanticFunctions =
kernel. ImportPlug!nFromPromptDirectory(functionDir); WRITE EXACTLY ONE JOKE or HUMOROUS STORY
ABOUT THE TOPIC BELOW
Console.WriteLineC'HERE IS A LAME EXCUSE...");
var excuseResult = await kernel
. InvokeAsync(semanti eFunctions[ "Excuses"] , JOKE MUST BE:
new KernelArguments { ["in p u t"] = "my cat" } ); - G RATED
Console.WriteLine(excuseResult); - WORKPLACE/FAMILY SAFE
Console.WriteLineO; NO SEXISM, RACISM OR OTHER BIAS/BIGOTRY
re su lt = await kernel.InvokeAsync(
nativeFunctions[ "DupDup"], private s ta tic async Task PipelinesO
new KernelArguments { ["in p u t"] = "hello" } ); {
Useful Links Console.Wri teLi ne( r e s u lt) ; var builder = Kernel.CreateBuilderO ;
Console.WriteLineO;
microsoft/semantic-kernel:
} builder.Services
Integrate cutting-edge LLM
/ / . Services.AddLoggi ng(configure =>
technology quickly and easily
You m ay have n o tic e d t h a t th e Q w erty fu n c tio n accepts //configure. AddConsole ())
into your apps (github.com)
an in p u t pa ra m e te r b u t d o e s n 't use i t . A t one tim e , i t was .AddAzureOpenAIChatCompletion(
Orchestrate your Al with th e sta nd ard in SK to a c c e p t a s in g le -s trin g nam ed in p u t "g p t-4 ",
Semantic Kernel | Microsoft and re tu rn a s trin g as th e o u tp u t. T h a t made i t easier to _endpoint,
Learn _api Key);
ch ain m u ltip le ca lls to g e th e r and pass th e o u tp u t o f one jihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA
var functionD ir =
The sky is a thing so blue
Path.Combi ne(Di rectory.GetCurrentDi re cto ry(),
It's up there for me and -For you
"Plugins", "W rite r");
It's where birds like to Fly
And planes zoom on by var semanticFunctions = kernel
If it rains, grab an umbrella too! . ImportPluginFromPromptDirectory(functionDi r ) ;
In the previous part of the poem, the author describes the sky as a thi var functionpipeline = new KernelFunction[]
ng so blue and accessible to everyone. It is a place where birds like t {
□ fly, and planes zoom through the clouds. The author emphasizes the un semantieFunctions["ShortPoem"],
iversality of the sky, as it is up there for both the poet and the read
semanticFuncti ons[ "TellMeMore"]
er. The sky is a natural phenomenon that can be enjoyed by everyone, re
gardless of their status or background. However, the author acknowledge };
s that sometimes it rains, and in those cases, it's essential to grab a
n umbrella to protect oneself from the rain. In summary, the poem's foe var currentArg = "the sky";
us on the sky allows the author to evoke a sense of wonder and awe abou foreach (var function in functionpipeline)
t the natural world, especially the heavenly bodies that exist beyond o {
ur reach. The sky is a metaphor for a limitless horizon, full of possib var re su lt = await kernel.InvokeAsync(function,
ilities and potential, reminding the reader of the beauty and majesty o
new KernelArguments { ["in p u t"] = currentArg } );
f nature.
currentArg = re s u lt.T o S trin g O ;
Console.Wri teLi ne(currentArg);
C :\TFS\Repos\SKCodePresents\SKCodePresents\bin\Debug\net8.G\SKCodePrese Console.WriteLineO;
}
F ig u re 3 : A ge ne ra ted poem , fo llo w e d by a d e s c rip tio n o f th e poem }
{
"schema": 1,
"type": "completion",
"descriptio n": "Summarize given te xt or any
Aspirational .NET:
What Is .NET Aspire?
.NET Core has been a great ride for all of us building web-and cloud-driven apps, but managing distributed apps made for cloud
native has been a mishmash of different tools. That's about to change. Seeing a problem in how large microservice architectures
are deployed and managed, the ASP.NET team has taken a big swing with a solution that should help most .NET developersjihgfedc
F ig u r e 1 : P r in c ip le s o f
c lo u d n a tiv e
• O rc h e s tra tio n
• C o m p o s in g d is t r ib u t e d a p p s
F ig u r e 2 : C o m p o s in g a
• S e rv ic e d is c o v e r y d is t r ib u t e d a p p lic a tio n
• C o n fig u r a tio n f o r a c lo u d n a tiv e a p p
• T o o lin g
B e fo re I d ig i n t o t h e d e ta ils o f h o w t h is w o rk s , Let's s t a r t
b y lo o k in g a t h o w y o u c an c re a te A s p ire a p p s . e rp la te a n d s a m p le co d e i t in c lu d e s . The S ta rte r A p p lic a tio n
is a g re a t w a y t o see a m u lti- c o m p o n e n t p r o je c t w o rk , b u t I
Tooling t h in k m o s t p e o p le w i l l s t a r t w ith th e s im p le A p p lic a tio n as a
I n t h i s e a r ly v e rs io n o f M ic r o s o ft A s p ire , m o s t o f t h e t o o l s ta r t t o a d is tr ib u te d a p p lic a tio n . I d o n 't t h in k t h a t e ith e r o f
in g is in V is u a l S tu d io ( a lth o u g h t h e d o t n e t C LI s u p p o r ts th e s e w i l l be th e m o s t c o m m o n a p p ro a c h .
t h is t o o ) . L ik e m a n y o t h e r p r o je c ts , th e r e 's a n e w f i l e
te m p la t e , as s e e n in F ig u r e 3 . In s te a d o f t h a t , m o s t p e o p le w i l l j u s t a d d .NET A s p ire
t o e x is tin g p r o je c ts t h a t t h e y w a n t t o use o r c h e s tr a tio n
O u t o f th e b o x , V is u a l S tu d io s u p p o rts a s im p le " A p p lic a tio n " w i t h . V is u a l S tu d io has t h is o p t io n b y j u s t r ig h t - c lic k in g
a n d a " S ta r te r A p p lic a tio n ." The d iffe re n c e is h o w m uch b o il y o u r p r o je c t, as s e e n in F ig u r e 4 .
Create a new p ro je c t S e a r c h fo r t e m p la t e s (A lt *S ) z* • z l
.N E T A s p ir e A p p lic a tio n
.N E T A s p ire S t a r t e r A p p lic a tio n
A p r o je c t t e m p l a t e fo r c r e a t in g a n e m p t y .N E T A s p ire a p p .
, S t a n d a lo n e T y p e s c r ip t V u e .N E T A s p ir e S ta r te r A p p lic a tio n
P r o je c t ’
A p r o je c t t e m p l a t e fo r c r e a t in g a .N E T A s p ire a p p w it h a B la zo r w e b
f r o n t e n d a n d w e b A P I b a c k e n d s e r v ic e , o p t io n a lly u s in g R e d is fo r
R A S P .N E T C o r e W e b A P I c a c h in g .
N o t f in d in g w h a t y o u 'r e lo o k in g fo r?
F ig u r e 3 : N ew .NET A s p ire p r o je c t te m p la te s
In b o th cases, y o u g e t tw o p ro je c ts t h a t are cente red used services, such as In s tru m e n ta tio n , M etrics, H e a lth -
aro und .NET A spire, as show n in F ig u re 5 . Checks, and o th e rs . It's s im p ly a p ro je c t y o u can reference
to s e t up th es e d iffe r e n t fa c ilitie s fo r y o u r p ro je c ts , fo r
The A p p H o s t p ro je c t is b o th a dashboard o f y o u r ru n exam ple, w hen y o u c a ll th e service d e fa u lts in y o u r p ro j
n in g d is trib u te d app as w e ll as w here th e o rc h e s tra tio n ects (e .g ., A P I o r BLazor p ro je c ts ):
is c o n fig u re d .
/ / Aspire Wiring
On th e o th e r hand, th e S e rv ic e D e fa u lts p ro je c t is a Li builder.AddServi ceDefaults();
brary p ro je c t t h a t creates som e d e fa u lts t h a t can be used buiIder.AddRedisOutputCache("theCache");
by one o r m ore o f y o u r services to c o n fig u re c o m m o n ly buiIder.AddSqlServerDbContext<BookContext>(" theDb");
F ig u re 6 : The dashboard
C o n s o le L o a :
r r y .ln T tr u m * 1
n ip
‘■h Ufa-O O I ’ M u c I’ M MM ) PM
F ilte r s
AppHost
F ig u r e 9 : T h e A p p H o s t's r e la t io n s h ip t o t h e o t h e r s e rv ic e s
M ongoD b se rv e r A d d M o n g o D b ()
How Orchestration Works
I f y o u 'v e u s e d ASP.NET C ore, y o u 'd u s u a lly c re a te an a p M ySql se rv e r A d dM yS q l()
p lic a t io n b y u s in g W e b A p p l i c a t i o n . C r e a t e B u i l d e r ( ) . F or P ostgres se rv e r A d d P o s tg re s ()
A s p ire , y o u ca n d o t h is in a s im ila r fa s h io n : SQL se rv e r AddSqLServer()
Redis A d d R e d is C o n ta in e r()
/ / AppHost C# p ro je c t A d d P ro je c t()
var builder =
R abbitM Q A d d R a b b itM Q ()
Di s t r i butedApplicati on.CreateBuiIder(args);
T a b le 1 : C o m m o n c o m p o n e n ts
In s te a d o f w ir in g u p s e rv ic e s a n d a p ip e lin e , y o u a d d t h e
p a rts o f y o u r a p p lic a tio n t h a t y o u n e e d . F or e x a m p le :
C o m p o nent Type M e th o d t o A d d t h e S e r v ic e N u G et Package
/ / AppHost
buiIder.AddRedisContainer("theCache"); W he re A re We?
To b e s u re , th e s e are e a r ly d a y s in t h e life c y c le o f .NET
buiIder.AddProj ect<AddressBook_Api >("theApi") A s p ire . I'm im p re s s e d . In s te a d o f a lw a y s n e e d in g t o d ro p
.WithReference(cache) d o w n in to m u lt ip le c o n ta in e r s a n d m a y b e e v e n K u b e r-
n e te s , .NET n o w has an o r c h e s tr a tio n e n g in e f o r i t s o w n
T h is a llo w s t h e o t h e r a p p lic a tio n s t o use t h e c a c h e u s in g d is t r ib u t e d a p p lic a tio n s . I'm s in c e r e ly c u rio u s t o see w h a t
s im ila r c a lls in t h e p r o je c t s e tu p : t h is lo o k s lik e w h e n i t s h ip s .
D is trib u te d Caching:
E nhancing S calability and
P erform ance in ASP.NET 8 Core
In the realm of enterprise applications, a deep understanding of distributed caching intricacies and adherence to best practices
are essential to building enterprise applications that are scalable and high performant. You can choose from the popular
distributed caching frameworks, such as Memcache, NCache, Redis, and Hazelcast. In this article, I'll discuss distributed caching,
J o y d ip K a n jila l
j o y d i p ka n j i La L@ y a h o o . c o m
J o y d ip K a n jila l is a n MVP
( 2 0 0 7 - 2 0 1 2 ) , s o ft w a r e
a r c h it e c t , a u t h o r , a n d
s p e a k e r w i t h m o re t h a n
2 0 y e a rs o f e x p e rie n c e .
He h a s m o re t h a n 1 6 y e a rs Cache S erver
o f e x p e r ie n c e in M ic r o s o f t
.N E T a n d i t s r e la te d
t e c h n o lo g ie s . J o y d ip ha s
a u th o re d e ig h t b o o k s ,
C ache-hit T he application reads
m o re t h a n 5 0 0 a r t ic le s ,
data from the cache The application stores/updates data
a n d h a s re v ie w e d m o re into the cache
th a n a d o ze n b o o ks.
A pplication S erver
Database
F i g u r e 1 : C a c h in g a t w o r k
58 Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core codem ag.com
D istnbuted C ache S erver
♦ jihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA
D atabase Server
F i g u r e 2 : D i s t r i b u t e d c a c h in g
codem ag.com Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core 59
fo rm in g a d is trib u te d a rc h ite c tu re o r d is trib u te d clus te r, fre q u e n tly accessed clo se r to th e a p p lic a tio n .
e n a b lin g increased pro cessing p ow er and sto ra ge capac • R e lia b ilit y : D is trib u te d c a chin g prom otes r e lia b ility
ity . (See jihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA
F ig u re 3 .) by s to rin g d ata across m u ltip le servers. I f a cache
server fa ils , th e re m a in in g servers can s t i l l serve th e
N ote t h a t a d is trib u te d cache is based on DHT, an ac cached d a ta , e n s u rin g data a v a ila b ility .
ronym fo r D is trib u te d Hash Table. In th is DHT, th e data • H ig h a v a ila b ilit y : D is trib u te d ca c h in g system s can
is sto re d as ke y-v alue pa irs. Each p a rtic ip a tin g node can e a s ily ha nd le increase d tr a f fic by a d d in g servers,
re trie v e th e va lu e using a key. DHT a llo w s d is trib u te d e n a b lin g th e system s to scale d y n a m ic a lly w ith o u t
caches to be scaled d y n a m ic a lly by c o n tin u o u s ly m anag d is ru p tin g th e e x is tin g o p e ra tio n s , hence en su rin g
in g a d d itio n s , d e le tio n s , and node fa ilu re s . h ig h a v a ila b ility o f th e cached d ata .
• R e s ilie n c e : C aching prov ides enhance d res ilie n ce
Some ty p ic a l exam ples o f w id e ly used d is trib u te d ca ch in g a g a in s t te m p o ra ry t r a f fic spikes o r database o u t
system s in c lu d e NCache, Redis, and M em cached. These ages. The cache da ta can be accessed q u ic k ly , p ro
system s leverage d is trib u te d cache m echanism s and o ffe r v id in g a s m o o th e r user experience.
APIs fo r fa s te r da ta sto ra ge and re trie v a l. CBA
B e n e f its
B u ild in g a S im ple D is trib u te d
D is trib u te d ca c h in g o ffe rs se veral b e n e fits , in c lu d in g : A p p lic a tio n in ASP.NET Core
It's tim e fo r w r itin g som e code. L e ts now exam ine how to
• S c a la b ility : W ith a surge in tr a f fic fo r an a p p lic a b u ild a sim p le ASP.NET Core 7 Web A P I a p p lic a tio n using
tio n , y o u can add a d d itio n a l cache servers to th e GraphQL.
d is trib u te d cache system w ith o u t d is ru p tin g e x is t
in g o p e ra tio n s . C r e a te a N e w A S P . N E T C o re 8 P r o je c t
• R ed u ced la te n c y : W ith d is trib u te d c a c h in g , cached in V is u a l S t u d io 2 0 2 2
data is u su a lly sto re d in an in -m e m o ry data sto re , You can create a p ro je c t in V is u al S tu d io 2022 in several
e n a b lin g b la z in g fa s t access and re d uc in g la te n c y ways. When yo u launch V is u al S tu d io 20 2 2, y o u 'll see th e
caused by fe tc h in g da ta from th e d is k o r any rem ote S ta rt w in d ow . You can choose "C o n tin u e w ith o u t code"
services. to Launch th e m ain screen o f th e V is ua l S tu d io 2022 IDE.
• P e rfo rm a n c e : A d is trib u te d c a chin g system reduces
th e Latency ass ocia ted w ith fe tc h in g fre q u e n tly ac To create a new ASP.NET Core 8 P ro je c t in V is u al S tud io
cessed d ata fro m disks o r re m ote services by ca ch in g 2022 :
F ig u re 3 : D is trib u te d
ca ch in g w ith m u ltip le
nodes
60 Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core codem ag.com
1. S ta rt th e V is ua l S tu d io 2022 IDE. data clo se r to users in d iffe r e n t reg io ns . This de
2. In th e "C reate a new p ro je c t" w in d ow , s e le c t "ASP. creases th e a m o u n t o f tim e th a t's o th e rw is e needed
NET Core Web A P I" and c lic k N ext to m ove on . to access data across Long n e tw o rk distan ces , im
3. S p e cify th e p ro je c t nam e as D is trib uted C ac h in g D e m o p ro v in g perfo rm an ce and re du cing laten cy .
and th e p a th w here i t sh ou ld be crea ted in th e "C o n
fig u re y o u r new p ro je c t" w in d ow . I f y o u w a n t th e
s o lu tio n f ile and p ro je c t to be cre ate d in th e same D is trib u te d C aching Best Practices
d ire c to ry , y o u can o p tio n a lly check th e "P la ce s o lu Im p le m e n tin g d is trib u te d c a chin g requires adherence to
tio n and p ro je c t in th e same d ire c to ry " checkbox. several b e s t p ra ctices :
C lick N ext to m ove on .
4. In th e n e x t screen, s p e c ify th e ta rg e t fra m e w o rk and • Cache in v a lid a t io n : Decide on a cache in v a lid a
a u th e n tic a tio n ty p e as w e ll. Ensure t h a t th e "C on tio n s tra te g y e n s u rin g t h a t th e cached d ata re
fig u re fo r HTTPS," "E nable Docker S u p p o rt," and th e m ains up to da te . C onsider o p tio n s Like tim e - to - liv e
"E na ble O penAP I s u p p o rt" checkboxes are unchecked (TTL) based e x p ira tio n , e x p lic it cache in v a lid a tio n ,
because y o u w o n 't use any o f th es e in th is exam ple. o r e v e n t-d riv e n in v a lid a tio n m echanism s based on
5. Because you w o n 't be using m in im a l APIs in th is ex data changes o r updates.
am ple , rem em ber to leave th e U se c o n tr o lle rs ( u n • E v ic tio n p o lic ie s : Choose a p p ro p ria te e v ic tio n
c h e c k t o use m in im a l A P Is ) checkbox checked. p o lic ie s fo r y o u r d is trib u te d cache. Cache e v ic tio n
6. C lick Create to c o m p le te th e process. p o lic ie s help m anage cache d ata and d e te rm in e how
and w hen th e y 're rem oved fro m th e cache. There are
I ' l l use th is p ro je c t in th e su bs e qu e nt se ctio n s o f th is th re e ty p e s o f e v ic tio n p o lic ie s : le a s t re c e n tly used
a rtic le . (LR U ), Least fre q u e n tly used (LFU), and fix e d size
e v ic tio n s .
• Cache s iz in g a n d c a p a c ity p la n n in g : E stim ate th e
D is trib u te d C aching Use Cases cache size and c a p a c ity re q uire m e nts based on th e
There are several use cases o f d is trib u te d c a chin g across v o lu m e o f d ata and ex pected w o rk lo a d . C onsider
d iffe r e n t dom ains and in d u s trie s . Here are a fe w com m on fa c to rs Like m em ory a v a ila b ility , cache server capa
scenarios w here d is trib u te d ca chin g can help: b ilitie s , and fu tu re g ro w th to ensure t h a t th e cache
can ha nd le th e a n tic ip a te d load.
• H ig h - t r a f f ic w e b s ite s : D is trib u te d ca c h in g can be • P ro p e r k e y d e s ig n : Design cache keys w ith care.
used to sto re fre q u e n tly accessed w eb pages, HTML Use m e a n in g fu l and u n iq u e keys t h a t are easy to
fra g m e n ts , o r s ta tic c o n te n t. By ca chin g th es e e l ge nerate and parse. A vo id o ve ru sin g co m p le x o b
em ents on d is trib u te d cache servers clos e r to th e je c ts o r d yn am ic data as keys, as i t can n e g a tiv e ly
users, th e y can be served q u ic k ly and reduce th e im p a c t cache perfo rm an ce.
load on ba ck -e nd servers, im p ro v in g w e b s ite p e rfo r • M o n ito r a n d t u n e p e r fo rm a n c e : R eg ularly m o n ito r
m ance and s c a la b ility . and m easure th e perfo rm an ce o f y o u r d is trib u te d
• D a ta b a s e c a c h in g : D is trib u te d ca chin g can be em cache to id e n tify b o ttle n e c k s , cache h it ra tio s , and
p lo ye d to cache fre q u e n tly accessed database que review o th e r pe rfo rm an ce in d ic a to rs . A d ju s t cache
ries o r re s u lt sets. By s to rin g th is in fo rm a tio n in c o n fig u ra tio n , e v ic tio n p o lic ie s , o r cache server ca
m em ory, you can q u ic k ly respond to requests a v o id p a c ity , i f necessary, to o p tim iz e perform ance.
in g th e need fo r p o s s ib ly expensive database queries • Im p le m e n t f a ilo v e r a n d r e p lic a tio n : Ensure high
o r re d u n d a n t database h its . a v a ila b ility and f a u lt to le ra n c e by im p le m e n tin g
• M ic ro s e rvic e s a r c h it e c tu r e : In a ty p ic a l m ic ro se r fa ilo v e r m echanism s and data re p lic a tio n across
vices a rc h ite c tu re , th e re can be m any d iffe r e n t ser cache servers in case o f fa ilu re s . This helps m a in
vices c o m m u n ic a tin g w ith each o th e r, w h ic h can ta in c o n s is te n c y and ensures u n in te rru p te d access
increase la te n c y due to n e tw o rk ro u n d trip s . By us to cached da ta .
in g d is trib u te d c a c h in g , fre q u e n tly accessed data • S e c u r ity c o n s id e ra tio n s : Pay a tte n tio n to s e c u rity
o r c o m p u te d re su lts can be cached, m in im iz in g th e aspects, such as access c o n tro l, a u th e n tic a tio n , and
need fo r re pea ted re m ote calls and re du cing Latency. e n c ry p tio n , to p ro te c t s e n s itiv e da ta sto re d in th e
• C o n te n t d e liv e r y n e tw o rk s (C D N s ): CDNs Leverage d is trib u te d cache.
d is trib u te d c a chin g to p e rs is t and also serve s ta tic • A p p r o p r ia te d a ta s e le c tio n : N ot a ll d ata is s u ita b le
c o n te n t t h a t in clu d e s im ages, J a v a S c rip t, and CSS fo r c a c h in g . Id e n tify d ata th a t's fre q u e n tly read b u t
file s . By ca c h in g c o n te n t in m u ltip le edge servers in fre q u e n tly u pd a te d , as th is ty p e o f da ta b e n e fits
lo c a te d g e o g ra p h ic a lly clos e r to users, CDNs can de m os t fro m c a c h in g . A vo id c a chin g d ata t h a t changes
liv e r c o n te n t fa ste r, re d uc in g la te n c y and im p ro v in g very fre q u e n tly .
th e user experience. • Cache in v a lid a t io n s tr a te g y : Im p le m e n t a ro b u s t
• R e a l- tim e a n a ly tic s : In d a ta -in te n s iv e a p p lic a cache in v a lid a tio n s tra te g y to ensure d ata c o ns is
tio n s , d is trib u te d c a chin g can be used to sto re pre- te n cy. T his co u ld be tim e -b a s e d (e .g ., TTL— tim e to
processed o r c o m p u te d d ata t h a t is fre q u e n tly ac liv e ), e v e n t-d riv e n , o r a c o m b in a tio n o f b o th . When
cessed fo r re a l-tim e a n a ly tic s . By c a chin g th is d ata , a change occurs in th e u n d e rly in g d a ta , it's re com
a p p lic a tio n s can re trie v e in s ig h ts q u ic k ly , a v o id m ended t h a t y o u up d ate th e cache to ensure t h a t
in g th e Latency associa ted w ith rep rocessing v a s t y o u r cached data is alw ays in sync w ith th e data
am o un ts o f raw d ata . re s id in g in th e da ta store .
• G e o g r a p h ic a lly d is tr ib u t e d a p p lic a tio n s : In g lo b • D a ta p a r t it io n in g a n d s h a rd in g : D is trib u te data
a lly d is trib u te d a p p lic a tio n s , d is trib u te d ca ch in g across d iffe r e n t cache servers to balance th e Load
can be used to sto re and serve fre q u e n tly accessed and reduce risks o f a s in g le p o in t o f fa ilu re . Use
codem ag.com Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core 61
sharding techniques to partition data effectively • Load balancing: Employ Load balancing to distrib
based on usage patterns or other relevant criteria. ute requests evenly across cache servers, thus en
• Handling cache misses: Design your system to suring use of resources.
handle cache misses gracefully. When the data re • Scalability: Ensure that your caching solution can
quested by the application isn't available in the scale horizontally. As the demand increases, you
cache, it's known as a cache miss. Optimizing this should be able to add more cache servers to the
fallback mechanism is essential for maintaining per system without significant changes to the existing
formance.kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA infrastructure.
A p p lic a tio n
D a ta b a s e
A p p lic a tio n
C a c h e S e rv e r
Figure 6: Data is served from the cache for subsequent requests
62 Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core codem ag.com
• Data security: You should establish the necessary • When a request arrives, the application searches for
security measures by implementing security mea the data in the cache.
sures that include encryption of data in transit and • If the data is available, the application returns the
at rest, proper access controls, and authentication cached data. This phenomenon is also known as a
mechanisms, etc. cache-hit.
• Monitoring and analytics: You should monitor the • On the other hand, if the data that has been re
performance of your cache system on a regular ba quested is unavailable in the cache, the application
sis. In this regard, you can take advantage of met retrieves data from the database and then populates
rics such as cache hit, miss rates, and load patterns the cache with this data.
to optimize your caching strategy.
• Data synchronization: Ensure that the cache is There are a few benefits of this approach:
synchronized across all nodes in distributed envi
ronments. To maintain data integrity, you can take • The cache contains only the data the application
advantage of techniques such as distributed Locking has requested, thus ensuring that the cache size is
or atomic operations. optimal and cost-effective.
• Selecting the right tool: Selection of the right • This strategy is simple and can provide immediate
caching solution that satisfies the requirements performance benefits.
of your application is extremely important. Redis,
Memcached, and Hazelcast are popular tools with The downsides to this approach include:
varying features and capabilities.
• Avoid cache stampede: Implement strategies Like • Because the data is loaded into the cache only in
staggered TTLs, pre-computation, or using a proba the event of a cache miss, the in itia l response time
bilistic early expiration to avoid a cache stampede, increases because of the additional roundtrips re
where multiple instances try to compute and store quired to the database and the cache.
the same data in the cache simultaneously. • Using this approach, the data is stored directly in
the database, which may result in discrepancies be
tween the database and the cache.
Distributed Caching Patterns
Cache patterns are an approach to application design Another pattern is called read through (see Figure 5). In
that emphasizes caching techniques to improve seal- this pattern, the application queries the cache first. The
ability, performance, and responsiveness. These patterns cache then interacts with the underlying database on a
provide valuable insights and suggestions for implement lazy Load basis. If the data requested by the application is
ing caching techniques. Typically, these patterns are used unavailable in the cache, it's called a cache miss. In this
with distributed caching systems that include NCache or case, the application retrieves the requested data from
Memcached. You can take advantage of these patterns to the data store (i.e., a database or a service, etc.), stores
reduce Latency and improve your application's workload this data into the cache, and then returns it.
processing capabilities.
The cached copy of the same data is then served for all
One such pattern is called cache aside (see Figure 4). subsequent requests for the same piece of data.
The cache aside pattern adopts a lazy loading approach
and is the most used caching strategy. Here's how this Another pattern is write through (see Figure 7): This
pattern works: pattern is similar to read through, but instead of read-
a p p iic a u o r i
D a ta b a s e C a c h e S e rv e r
a p p lie d nun
D a ta b a s e C a c h e S e rv e r
codem ag.com Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core 63
T he d a ta s to re is u p d a te d w ith th e c a c h e d
da ta in fre q u e n tly , i.e.. o n c e in a w h ile
in g data from th e ba ck -e nd system , i t w rite s data to th e cache. A cache e n try is rem oved w hen its tim e - to -
cache. In o th e r w ords, w hen using a w rite th ro u g h cache, liv e (TTL) expires. You can e m p lo y th is m eth o d when
th e ord er in w h ic h th e cache is p o p u la te d is reversed. th e d ata in y o u r a p p lic a tio n has a s h o rt d u ra tio n ,
A fte r a cache m iss, th e cache is n o t Lazy Loaded. In s te a d , lik e session data o r refreshed d ata . I t can prove b e n
it's p ro a c tiv e ly u pd ated im m e d ia te ly fo llo w in g an up da te e fic ia l in scenarios w here y o u r a p p lic a tio n relie s on
to th e database. U sing th is approach makes sense w hen data t h a t has a s h o rt Lifespan such as session data
Using NCache in w r itin g o p e ra tio n s are fre q u e n t and a p p lic a tio n p e rfo r o r data t h a t undergoes fre q u e n t updates.
ASP.NET Core m ance is p ara m o u n t.
D is t r i b u t e d C a c h in g C h a lle n g e s
To work with NCache in
A n o th e r p a tte rn is w r it e a ro u n d (see F ig u re 8 ) : In th e A lth o u g h th e re are b e n e fits , d is trib u te d c a chin g poses
ASP.NET Core, install the
w rite aro und a pp ro a ch , th e d ata is w ritte n d ire c tly to th e c e rta in cha lle ng es as w e ll:
following NuGet package:
data sto re w ith o u t s to rin g i t in th e cache. When th e ap
NCache.Microsoft.Extensions. p lic a tio n perfo rm s a read o p e ra tio n , th e d ata is placed in • D a ta c o n s is te n c y : The m ain c h alle n g e w ith d is
Caching.OpenSource. th e cache. T his s tra te g y is a good ch oice i f y o u r a p p lic a tr ib u te d ca chin g is m a in ta in in g da ta con sis te ncy .
tio n d o e s n 't need to reread fre q u e n tly w ritte n d ata . Caches m ay have d iffe r e n t da ta ve rsion s o r m ay ex
pe rience re p lic a tio n delays, le a d in g to d ata in c o n
A n o th e r p a tte rn is w r it e b e h in d o r w r it e b ac k (see F ig sis tency.
u re 9 ) : In th e w rite back ap proa ch , data is w ritte n d i • Cache in v a lid a t io n : Im p le m e n tin g p ro p e r cache in
re c tly to th e cache alo n e in s te a d o f th e data sto re . The v a lid a tio n and s y n c h ro n iz a tio n m echanism s o r using
data sto re is u pd ated w ith th is cached data a fte r s p e c ific e v e n tu a l c o n s is te n c y m odels can help m itig a te th is
in te rv a ls o f tim e , o r based on c e rta in p re -d e fin e d c o n d i ch alle n g e. In v a lid a tin g caches can be c h a lle n g in g ,
tio n s . A lth o u g h th is approach is b e n e fic ia l in w r ite - in te n e s p e c ia lly w hen data is fre q u e n tly c h a n g in g o r in
sive a p p lic a tio n s due to lo w Latency and h ig h th ro u g h p u t te rd e p e n d e n t. E nsuring t h a t th e cache is refreshed,
c a p a b ilitie s , th e re are risks o f d ata loss in th e e v e n t o f a i.e ., u pd ated w hen u n d e rly in g data changes, o r em
system crash e tc . CBA p lo y in g s tra te g ie s , such as cache e x p ira tio n based
on tim e , can help m a in ta in cache v a lid ity .
C a c h e E v ic t io n S tr a te g ie s • Cache c o h e re n c e : Cache coherence refers to en
Cache e v ic tio n in v o lv e s th e re m ov a l o f cache e n trie s o r s u rin g th a t a ll cache servers have c o n s is te n t
data from a cache based on s p e c ific c o n d itio n s o r rules d ata . A c h ie v in g cache coherence can be c h a lle n g
b e in g s a tis fie d . In d is trib u te d cache system s, cache e v ic in g , e s p e c ia lly in scenarios w here d ata is u pd ated
tio n a lg o rith m s are used to pro vide space fo r new data fre q u e n tly o r w hen m u ltip le cache servers are in
and p re v e n t th e cache from g o in g beyond its ca pa city . v o lv e d . E m ploy ing d is trib u te d cache s o lu tio n s t h a t
There are va rio u s cache e v ic tio n s tra te g ie s a v a ila b le fo r p ro vid e s tro n g c o n s is te n c y guaran tees or using
re m ov a l o f cache e n trie s . Here are th e key cache e v ic tio n cache coherence p ro to c o ls can help address th is
s tra te g ie s y o u can e xp lo re : ch alle ng e.
• Cache w a rm -u p : When a cache is e m p ty o r a fte r
• L e a s t r e c e n tly used (L R U ): Per th is s tra te g y , th e a cache server re s ta rts , i t tak es tim e to p o p u la te
cache ite m th a t's been accessed th e le a s t re c e n tly th e cache w ith fre q u e n tly accessed d ata . T his i n i
is rem oved fro m th e cache w hen th e cache reaches t i a l p erio d can re s u lt in cache m isses and increased
its m axim um ca p a city. N ote t h a t th is approach as la te n c y u n t il th e cache is w arm ed up. Proper cache
sumes e n trie s t h a t have been th e Least used w ill n o t w a rm -u p s tra te g ie s , such as preLoading c o m m o n ly
be used ag ain in th e near fu tu re . accessed data o r using w a rm -u p s c rip ts , can help
• M o s t r e c e n tly used (M R U ): Cache e n trie s t h a t have m in im iz e th is issue.
been accessed m os t re c e n tly are e v ic te d w hen th e • Cache e v ic tio n s tr a te g ie s : Since da ta access p a t
cache is f u ll. As per th is a s s u m p tio n , e n trie s t h a t te rn s m ay d iffe r across th e nodes, i t is q u ite c h a l
have been used m ore re c e n tly are less Likely to be le n g in g to im p le m e n t cache e v ic tio n s tra te g ie s in
used aga in in th e near fu tu re . a d is trib u te d cache e n v iro n m e n t. C areful c o n s id e r
• L e a s t f r e q u e n t ly used ( L F U ) : T his p o lic y s tip u la te s a tio n sh o u ld be give n to s e le c tin g an e v ic tio n s tr a t
t h a t w hen th e m axim um cache c a p a c ity is reached, egy t h a t a lig n s w ith y o u r a p p lic a tio n 's d ata access
th e cache e le m e n t accessed Least fre q u e n tly w ith in p a tte rn s and re qu irem e nts.
a c e rta in p e rio d is rem oved. I t asserts t h a t e n trie s • S c a lin g : As th e a p p lic a tio n load and da ta v o lu m e in
used less o fte n are u n lik e ly to be accessed in th e crease, s c a lin g th e d is trib u te d cache can becom e a
fu tu re . ch alle n g e. Proper p la n n in g and a rc h ite c tu re sh ou ld
• T im e - t o - liv e (T T L ): T his p o lic y d efin es th e d u ra tio n be in place to ensure t h a t th e cache can ha ndle th e
fo r w h ic h a cache e n try sh o u ld rem ain v a lid in th e added Load e ffe c tiv e ly .
64 Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core codem ag.com
• Cache s y n c h r o n iz a tio n : In m u lti- le v e l c a chin g sys C o n f ig u r in g N C a c h e a s a n I D is t r ib u t e d C a c h e P r o v id e r
tem s, w here m u ltip le caches, Like lo c a l cache and To w ork w ith d is trib u te d c a chin g using NCache, in v o k e
d is trib u te d cache, are used, e n s u rin g p ro p e r sy n th e A ddN C acheD istributedC ache m eth o d in th e Program ,
c h ro n iz a tio n and c o n s is te n c y across d iffe r e n t cache cs f ile to re g is te r NCache w ith th e loC c o n ta in e r. Note
layers can be c h a lle n g in g . You can p re v e n t in c o n t h a t th e A ddN C ach eD istrib uted C ache() m eth o d in NCache
sis te nc ie s in y o u r da ta by im p le m e n tin g th e r ig h t is an e xte n s io n m eth o d o f th e A d d D is trib u te d C a c h e ()
s y n c h ro n iz a tio n s tra te g ie s . m eth o d p e rta in in g to ASP.NET Core.
• N e tw o rk o v e rh e a d : D is trib u te d c a chin g system s re
q uire c o m m u n ic a tio n and s y n c h ro n iz a tio n betw een builder.Services.AddNCacheDistributedCache
cache servers, w h ic h in tro d u c e s n e tw o rk overhead. (configuration =>
H igh n e tw o rk la te n c y o r Lim ited b a n d w id th can a f {
fe c t cache perfo rm an ce and o v e ra ll a p p lic a tio n re configuration.CacheName = "demoCache";
sponsiveness. configuration.EnableLogs = true;
• C o m p le x ity a n d m a in te n a n c e : For th e cache in configuration.ExceptionsEnabled = true;
fra s tru c tu re to o pe ra te s m o o th ly , i t m u s t be p ro p });
e rly m a in ta in e d , m o n ito re d , and tro u b le s h o t w ith
p ro p e r e x p e rtis e and resources. Im p le m e n ta tio n
o f a d is trib u te d c a chin g system , in th e a p p lic a I f y o u 'd lik e to w ork w ith m u ltip le caches, use th e fo llo w
tio n a rc h ite c tu re in tro d u c e s increased co m p le x in g code s n ip p e t to c o n fig u re th e m in th e Program .es file :
it y in te rm s o f im p le m e n ta tio n , m ana ge m ent, and
m o n ito rin g . builder.Services.
• A p p lic a tio n c o m p a t ib ilit y : Some a p p lic a tio n s m ay AddNCacheDistributedCacheProvider
n o t be designed to w ork seam lessly w ith d is trib u te d (options =>
c a chin g o r m ay have dependencies t h a t d o n 't sup {
p o rt ca c h in g e ffe c tiv e ly . E valuate and m o d ify th e options.Cacheconfigurations =
a p p lic a tio n to ensure c o m p a tib ility w ith th e d is tr ib new NCacheConfiguration[] {
u te d ca chin g ap pro ach . new NCacheConfiguration() {
CacheName = "myFirstCache",
EnableLogs = true,
D is trib u te d C aching in ASP.NET Core ExceptionsEnabled = true
S u p p o rt fo r d is trib u te d ca c h in g is in - b u ilt in ASP.NET },
Core us ing th e ID is trib u te d C a c h e in te rfa c e . The ID is tr ib - new NCacheConfiguration(){
utedC ache in te rfa c e helps y o u to p lu g in any t h ir ty - p a r ty CacheName = "mySecondCache",
c a chin g fram ew orks. L is tin g 1 illu s tra te s how th e ID is EnableLogs = true,
trib u te d C a c h e in te rfa c e looks. ExceptionsEnabled = true }
};
As e v id e n t fro m th e source code o f th e ID is trib u te d C a c h e });
in te rfa c e , its G et m eth o d re tu rn s b y te [ ]. N evertheless, th e
fra m e w o rk prov ides e xte n s io n m e th od s fo r w o rk in g w ith
s trin g o b je c ts . In a d d itio n , y o u can im p le m e n t custom S e t t in g U p N C a c h e in A S P .N E T C o re
e x te n s io n m e th od s to make i t w ork w ith o th e r d ata ty p e s. To se tu p NCache in y o u r system , fo llo w th es e steps:
codem ag.com Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core 65
S to re /R e trie v e d ata using th e ID is trib u te d C a c h e in te rfa c e
L is tin g 2 : kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA T hat's a ll y o u have to do to in s t a ll NCache on y o u r com
p uter. CBA
app.MapGet( "/T e st",
async (IDistributedCache cache,
S to r e a n d R e tr ie v e a n O b je c t U s in g N C a c h e
IHttpClientFactory httpClientFactory) =>
{ C onsider th e fo llo w in g class:
const s trin g cacheKey = "Test";
List<Author>? authors = n u ll; public class Author
var cachedData =
{
await cache.GetStringAsync(cacheKey);
public in t Authorld { get; set; }
public string? FirstName { get; set; }
i f ( ! string.IsNuUOrEmpty(cachedData))
{ public string? LastName { get; set; }
authors = public bool IsActive { get; set; }
System.Text.J son.J sonSeri a li ze r. Deserialize }
<List<Author>>(cachedData);
} L is tin g 2 illu s tra te s how y o u can leverage NCache to
sto re and re trie v e an o b je c t:
else
{
Author user = new AuthorQ N ote t h a t fo r th e sake o f s im p lic ity , I'v e used a m in i
{ m al A P I o n ly to d e m o n s tra te how NCache w orks . I'v e
Authorld = 1, added o n ly one re cord here fo r th e sake o f b re v ity . When
FirstName = "Joydip",
y o u e xec u te th e a p p lic a tio n and brow se th e / t e s t e n d
LastName = " K a n jila l" ,
IsActive = true p o in t, th e a u th o r re cord is d is p la y e d in th e w eb brow ser.
}; W hen th is e n d p o in t is h it th e f ir s t tim e , th e a u th o r re
cord is s to re d in th e cache. For a ll s u b s e q u e n t req ue sts
authors = new List<Author>(); to th is e n d p o in t, th e d a ta is fe tc h e d fro m th e cache
authors.Add(user); based on th e e x p ira tio n tim e s e t in th e c o n fig u r a tio n .
In th is exa m p le , th e d a ta resides in th e cache fo r 60
await cache.SetStringAsync
seconds.
(cacheKey,
System.Text.J son.J sonSeri a liz e r.S e ria liz e
(authors), The c o m p le te code Listing o f th e Program .es f ile is given
new DistributedCacheEntryOptions in L is tin g 3 fo r y o u r reference.
{
AbsoluteExpirati onRelativeToNow
I m p le m e n t in g D is t r i b u t e d C a c h in g u s in g S Q L S e r v e r
= new TimeSpan(0, 0, seconds: 60)
You can use SQL Server as a cache s to re as w e ll. To g e t
} );
} s ta rte d on th is , create th e database ta b le to sto re cached
data in SQL Server.
return authors;
} ); You can ta k e a dv an ta ge o f th e sq l-cach e by c re a tin g a
com m and to create a database ta b le fo r s to rin g cached
d ata . I f th e sq l-c ac he t o o l is n 't in s ta lle d on y o u r co m
p ute r, y o u can in s ta ll i t using th e fo llo w in g com m and a t
th e D eveloper Com m and P rom pt w in d o w :
66 Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core codem ag.com
L is tin g 3 : The Program .es file
else
{
Author user = new AuthorQ
codem ag.com Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core 67
The M ySQ LServerCacheDem oController class
L istin g 4 : kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA
C r e a te t h e M o d e l C la s e s s { get; set; }
In a ty p ic a l o rd er m an age m e nt system , you'LL have several public in t Customer_Id
e n tity o r m o d el classes. However, fo r th e sake o f s im p lic { get; set; }
ity , I ' l l co n sid e r and use o n ly one e n tity o r m od el class public DateTime Order_Date
here. Create a new class nam ed Order in a file h a vin g th e { get; set; }
same name w ith a .cs e xte n s io n and w rite th e fo llo w in g public decimal Amount
code in th e re : { get; set; }
}
public class Order
{
public in t Order_Id I n s t a ll N u G e t P a c k a g e (s )
The n e x t ste p is to in s ta ll th e necessary NuGet Package(s).
To in s t a ll th e requ ired package(s) in to y o u r p ro je c t, r ig h t
L istin g 5: The M em C acheProvider class c lic k on th e s o lu tio n and th e n s e le c t Manage NuGet Pack
public class MemCacheProvider : IMemCacheProvider ages fo r S o lu tio n .... Now search fo r th e package nam ed
68 Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core codem ag.com
Listing 6: The O rderR e pository Class
codem ag.com Distributed Caching: Enhancing Scalability and Performance in ASP.NET 8 Core 69
L is tin g 7 : The C om plete Source o f Program .es file
using Di stributedCachingDemo; {
using Enyim. Caching. Configuration; Address = "localhost",
Port = 11211
var builder = WebAppUcation .CreateBui Ider (a rg s); } );
} );
/ / Add services to the container.
var app = b u ild e r.B u ild ();
b u iId e r. Services.AddControllers();
builder.Services. / / Configure the HTTP request pipeline.
AddScopeddOrderRepository, OrderRepository>();
buiIder.Services. app. UseAuthorization();
AddSi ngletondMemCacheProvi der> (x =>
A ctiva to rU tilitie s.C re a te ln sta n ce app.MapControllers();
<MemCacheProvider>(x));
b u iId e r. Servi ces.AddEnyi mMemcached(me => app.Run();
{
me.Servers.Add(new Server
{
private ZOrderRepository _orderRepository;
public OrdersController
( IQrderRepository orderRepository) There are a lot of details,
{ but in the end, distributed
_orderRepository = orderRepository;
} caching is fairly simple.
[HttpGet("GetOrders")]
public async Task<List<Order>> GetOrdersO
{
return await _orderRepository.GetOrdersO; Conclusion
} Caching is a proven and e s ta b lis h e d te c h n iq u e used to
} im p ro v e an a p p lic a tio n 's perform a nce , s c a la b ility , and re
sponsiveness by s to rin g fre q u e n tly accessed d ata in tr a n
s ie n t sto ra ge , such as th e m em ory o r o th e r p e rm a n en t
me.Servers.Add(new Server sto ra ge lik e a f ile o r a database. In d is trib u te d c a c h in g ,
{ th e cached da ta is spread across m u ltip le nodes, o fte n in
Address = "localhost", d iffe r e n t n etw o rk s. As cached d ata is re a d ily accessible,
Port = 11211 i t ensures enhance d r e lia b ility d u rin g un ex pe cted peaks
} ); o r fa ilu re s .
} );
J o y d ip K a n jila l
CODE
The c o m p le te source code o f th e Program .es f ile is given
in L is tin g 7 fo r reference.
70 D is trib u te d C aching: E nh an cing S ca lab ility an d P erfo rm an ce in ASP.NET 8 Core codem ag.com
O N LIN E Q U IC K ID 2403091 jihgfedcb
language, such as C++ or Rust? I t tu rn s o u t th a t the re is n 't I w a n te d in an e x tre m e ly s u c c in c t and expressive m anner.
a c o n flic t between th e tw o . You can w rite high-perform ance The fa c t t h a t I can do t h a t so s im p ly is one o f th e g re a t
code in C# th a t m atches or beats th e perform ance o f na tiv e s tre n g th s o f C#, in m y o p in io n . B u t g o o d -lo o k in g code
code. The best p a rt is th a t you can achieve th a t w h ile re ta in is ju s t th e f ir s t stage , Let's see how w e ll i t perfo rm s a t
in g many o f th e advantages th a t C# offers in th e fir s t place. ru n tim e .
C# has a lo t o f expressiveness and h ig h -le v e l co nc ep ts , When e v a lu a tin g th e pe rfo rm ance o f a piece o f code, it's
b u t fro m th e g e t-g o , i t has som e c r itic a l fe a tu re s fo r lo w - im p o rta n t n o t to lo o k a t ju s t a s in g le param eter. It's easy
le v e l system s. Value ty p e s , p o in te rs , and unsafe code to tra d e o f f e x e c u tio n tim e fo r m em ory usage, fo r exam
have a ll been p a rt o f C# fro m its i n it ia l c re a tio n . R ecent ple. I'm ru n n in g a ll th e code in th is a rtic le using .NET 8 .0 Oren Eini
a d d itio n s to th e .NET p la tfo rm , such as spans, v e c to r in in release m ode. Here are th e e x e c u tio n re su lts: ayende@ ayende.com
s tru c tio n s , and hardw are in trin s ic s make i t m uch s im p le r
to w rite h ig h -p e rfo rm a n c e and Low-Level code. • Peak m em ory: 18 GB Oren Eini started his
professional career as a
• T o ta l a llo c a tio n s : 163 GB
developer in 1999 w ith a
I w a n t to be able to ta lk in co nc re te te rm s, so fo r th e re st • E xecution tim e : 8 m in u te s , 19 seconds
strong focus on th e M icro
o f th is a rtic le , I'm g o in g to foc us on a re la tiv e ly s tr a ig h t
s o ft and .NET ecosystem.
fo rw a rd ta s k . The code accepts a CSV f ile (g z ip p e d ) and W hy am I m e n tio n in g m em ory a n d a llo c a tio n s in m y m e t
He has been a M icrosoft
co m p utes a gg re g ate d s ta tis tic s based on th e d a ta . You rics? The va lu e o f " t o t a l a llo c a tio n s " refers to th e a m o u n t
MVP since 2007. Oren's
can v ie w a sam ple o f th e da ta in th e f ile in T a b le 1 . o f m em ory th e system has a llo c a te d o ve r its Lifetim e,
main focus is on archi
w h ile "p e a k m em ory" represents th e m axim um a m o u n t o f
te ctu re and best prac
I ge ne ra ted a Large file to te s t how w e ll th is code per m em ory used a t any g iv e n p o in t in tim e . When ta lk in g tice s th a t prom ote q u a lity
form s (th e s c rip t to ge nerate th e d ata is a v a ila b le o n a b o u t .NET program s, re du cing a llo c a tio n s te n d s to have softw are and ze ro -frictio n
lin e a t: h ttp s ://g is t.g ith u b .c o m /a y e n d e /e 8 4 d a c 7 6 e 5 d - an o u ts iz e d im p a c t on pe rfo rm ance, e s p e c ia lly i f y o u're developm ent.
c 0 2 3 f6 a 8 0 3 6 7 f6 c 0 1 a c l3 ), and o b ta in e d a f ile w ith 250 ru n n in g w ith m u ltip le th re a d s a t once. T hat's because th e
m illio n records w ith 9 .7 8 m illio n u n iq u e users. The co m m ore a llo c a tio n s y o u do, th e m ore w o rk th e garbage c o l Since 2008, he has been
pressed file size is a b o u t 2 .4 GB. I in te n tio n a lly chose a le c to r has to han dle. W ith m u ltip le th re a d s a ll a llo c a tin g b u ild in g th e RavenDB
large size, w h ic h sh o u ld c le a rly d em o n stra te th e d iffe r m em ory, it's easy to reach a p o in t w here y o u 're m o s tly database, a n o n -re la tio na l
ence in pe rfo rm ance betw e en approaches. b u rn in g CPU cycles on garbage c o lle c tio n . database engine w ritte n
in C#. An in te rn a tio n a lly
The ta s k a t hand is to c o m p u te th e t o t a l q u a n tity and The com pressed f ile size is 2 .4 GB, b u t uncom pressed, th e known presenter, Oren
I'm afraid that the code is pretty dense, but the idea is var header = false;
pretty simple. You use another SequenceReader<byte> var salesData = new D ic tionarydong, UserSalesStruct>();
to find the terminators in the line buffer you're given while (true) {
var valueTask = pipeReader.ReadAsyncO;
here, you then use Utf8Parser to parse them without
var read = valueTask.IsCompleted ? valueTask.Result :
needing to allocate any strings. The code is complicated await valueTask.AsTask();
because it's a single expression and because I wanted to ProcessLines(pipeReader, read, salesData, ref header);
produce the correct error in the case of invalid input. Note i f (read.IsCompleted)
that I'm using an unconditional bitwise and (&) versus break;
the more common condition and (&&). This ensures that }
the entire expression runs and avoids branch instructions.
return salesData;
This is the hotspot in the code, so anything that reduces }
costs is advisable, and branches can be costly (even if
well predicted, as in this case).
I'm also not using the Dictionary as you'd normally ex Listing 5: Processing lines in the buffer
pect. I'm calling CollectionsMarshal to get or add the void ProcessLines(PipeReader pipeReader,
value. I'm not sure if you noticed, but Listings 4, 5, ReadResult readResult,
D ic tionarydong, UserSalesStruct> salesData,
and 6 all use UserSalesStruct as the value, instead of
ref bool header) {
UserSales (a class). That change allows you to avoid al
locations even further and take advantage of the Get- var sr = new SequenceReader<byte>(readResult.B u ffe r);
ValueRefOrAddDefault() behavior. while (true) {
ReadOnlySequence<byte> lin e ;
A very typical access pattern when using dictionaries is i f (sr.TryReadTo(out lin e , (b y te )’ \ n ’ ) == false) {
pipeReader.AdvanceTo(consumed: s r . P o sition,
to look up a value by key and create it if it isn't in the
examined: readResult.Buffer.End);
dictionary. This requires you to do two dictionary look break;
ups if you need to create the value. The GetValueRef }
OrAddDefault() means that you only need to do one. i f (header == false) {
Because a struct in C# is guaranteed to be zero in itia l header = true;
continue;
ized, you don't care if the value exists or not and you can
}
immediately add the current record values to the user's ProcessSingleLine(salesData, lin e ) ;
sales. }
}
Finally, you might have noticed that the meth
od in Listing 6 has an interesting attribute:
[ MethodImpl(MethodImplOptions.AggressiveInlin- Listing 6: Processing a single Line
ing)]. This is an instruction to the Just In Time compiler
[Methodlmpl(MethodlmplOptions.A ggressiveinlining)]
to force inlining of this method to its caller. This allows
void ProcessSingleLine(
you to benefit from clear code and separation of responsi D ictionarydong, UserSalesStruct> salesData,
bilities while getting dense (and efficient) machine code. ReadOnlySequence<byte> lin e )
{
The Pipeline version is spread over three code listings, var I r = new SequenceReader<byte>(line);
ReadOnlySpan<byte> span;
and I had to reach quite far out of the beaten path. What
var readAll = lr.TryReadTo(out span, ( b y te ) ', ')
did you get, then, from this journey? Take a look at these & Utf8Parser.TryParse(span, out long userid, out _)
results: & Ir.TryAdvanceTo((b y te )’ , ')
& I r .TryReadTo(out span, (b y te )' , ')
• Peak memory: 846 MB & Utf8Parser.TryParse(span, out in t quantity, out _)
& lr.TryReadTo(out span, ( b y te ) ',')
• Total allocations: 1069 MB
& Utf8Parser.TryParse(span, out decimal price, out _);
• Execution time: 50.5 seconds
i f (readAll == false)
I triple checked those results because they are really throw new InvalidDataException(
good. You can see the fu ll results in Table 2, but the "Couldn't parse expected fields on: " +
Pipelines method is almost 50% faster than the Stream- Encoding.UTF8.GetString(line));
P ip e lin e s 5 0 .5 1 ,0 6 9 84 6
Group Publisher
M a rk u s E gger
S um m ary o f pe rfo rm ance and m em ory c o n s u m p tio n across a ll scenarios
T ab le 2 : kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA
Editor-in-Chief
Rod P ad doc k
Managing Editor
as course ass ig nm en ts to s tu d e n ts . I t s also a p re tty good
E lle n W h itn e y
scen ario to te s t o u r c a p a b ilitie s because i t s so lim ite d
Content Editor
in scope. The p o in t o f th is a rtic le is n 't to p o in t y o u t o
M e la n ie S p ille r
w ard th e S y s te m .IO .P ip e lin e s A P I (a lth o u g h I ' l l a d m it
Writers in This Issue
th a t it s a fa v o rite o f m in e ), i t s to d e m o n s tra te how P h ilip p B a u e r M a rk u s E gger
y o u can e n tire ly change th e m an ner in w h ic h y o u p er O ren E in i J o y d ip K a n jila l
Oren Eini
CODE
C US TO M SOFTW ARE D E V E LO P M E N T
S T A F F IN G T R A IN IN G /M E N T O R IN G S E C U R ITY
M O R E T H A N JU ST
A M A G A Z IN E !
D o e s y o u r d e v e lo p m e n t te a m la c k s k ills o r tim e t o c o m p le te all y o u r b u s in e s s -c ritic a l s o ftw a r e p ro je c ts ?
C O D E C o n s u ltin g has t o p - t ie r d e v e lo p e r s a v a ila b le w ith in - d e p th e x p e rie n c e in .N E T,
w e b d e v e lo p m e n t, d e s k to p d e v e lo p m e n t (W P F ), B la z o r, A z u re , m o b ile a p p s , lo T a n d m o re .
C O N T A C T U S T O D A Y F O R A C O M P L IM E N T A R Y O N E H O U R T E C H C O N S U L T A T IO N .
k
N O S T R IN G S . N O C O M M IT M E N T . J U S T C O D E .
c
o
t
sr
e
tt c o d e m a g .c o m /c o d e
u
h
s
© 832-717-4445 ext. 9 • info@ codem ag.com
w
CODE STAFFINGjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA
UNLOCK ±
S T A F F IN G *
EXC ELLENC E
T o p -N o tc h IT T a le n t, C o n tr a c t F le x ib ility , H a p p y T e a m s , a n d a
C o m m itm e n t t o C u s to m e r S uccess C o n v e r g e w it h C O D E S ta ffin g
Our IT staffing solutions are engineered to drive your business forward while
saving you time and money. Say goodbye to excessive overhead costs and
lengthy recruitment efforts. With CODE Staffing, you'll benefit from contract
flexibility that caters to both project-based and permanent placements. We
optimize your workforce strategy, ensuring a perfect fit for every role and
helping you achieve continued operational excellence.
Visit our website to find out more about how we are changing
the staffing industry.
W ebsite: codestaffing.com
Y air A lan G riv e r (yag)
C hie f Executive O ffic er
D irect: +1 425 301 1590
Email: yag@ codestaffing.com
76/76