Full Transcript

·YouTLDR

JetStream KV: A fascinating alternative to Redis...

35:15EnglishBy SynadiaTranscribed Jul 16, 2026
Analyze another video with Pro30-day money-back guarantee
0:00

you're most likely to be using a key

0:02

Value Store of some sort inside of your

0:04

system or application and you're

0:07

probably using something like redis and

0:09

for good reason redis is really easy to

0:12

operate it has some great features and

0:14

most of all it's really really fast but

0:16

what if I told you that there was

0:17

another viable key Value Store available

0:20

to you that really kind of Rivals Reedus

0:23

in terms of performance but also offers

0:25

some considerable advantages when it

0:27

comes to things like security

0:28

multi-tenancy and G distribution today I

0:31

want to talk all about Nat's jet

0:33

stream's key value stores and how they

0:34

were designed from the ground up with

0:36

distribution in mind and why they may be

0:39

a really good solution for your

0:40

distributed system so let's get straight

0:43

into

0:44

[Applause]

0:45

[Music]

0:52

it but before we do be sure to click

0:55

that like And subscribe button and hey

0:57

share this video with a friend or

0:59

colleague I'm sure they would really

1:00

enjoy it okay on with the show so we've

1:04

been talking all about Nat jet stream in

1:06

this series and if you haven't caught up

1:07

on videos we have a really cool series

1:09

that talks all about Nat's jet streams

1:11

design and um how it can help benefit

1:13

you to just learn more about some of its

1:15

design aspects um but one of the really

1:17

cool things about being able to kind of

1:19

solve for streams and persistence and

1:21

have this globally ordered set of

1:23

messages that are indexed on subjects or

1:25

topics is that you're able to create a

1:27

lot of these really neat abstractions on

1:29

top of without a lot of effort and

1:31

that's really how jet streams key value

1:33

stores were born um we saw that we were

1:36

you know handling streaming really

1:37

really well um but that with just a

1:39

little bit of effort and maybe a little

1:41

bit of kind of client sugar you can

1:42

create these like real key value stores

1:44

to rival things like redus um or Dynamo

1:47

or things like that um so really quick

1:50

um how does a key Value Store actually

1:52

work in in jet stream well at its base

1:56

layer a key value store is no different

1:57

than a stream in terms of all of kind of

1:59

the internals and how they work there's

2:01

really just some syntatic sugar and a

2:03

couple um a couple functions or uh API

2:06

calls that are used to really kind of

2:08

support the KV use case well but under

2:10

the hood it's using all the internal

2:11

Machinery um that a Nat jet stream

2:14

stream would and so you can kind of take

2:17

a lot of the parallels that we learned

2:18

all the kind of topics that we learned

2:20

about streams and apply these to KV and

2:23

how they apply for that particular

2:24

access pattern and so at its core you

2:27

have a KV bucket which is just a stream

2:29

under the h

2:30

and then you have these Keys which are

2:32

represented as subjects I have food. bar

2:34

and bat. baz um but one of the really

2:37

neat things about this is uh these are

2:38

all just messages and subjects that are

2:40

being stored in a stream under the hood

2:43

um and so each you know particular key

2:46

has a value associated with it but not

2:48

just a value we actually have full

2:50

history of these values that you can um

2:53

that you can have and you could set on a

2:55

per bucket basis how much history you

2:56

want um but you can essentially have

3:00

historical values that you can kind of

3:01

whip through um for for your particular

3:04

use case if you need it um which I think

3:06

is just a really really neat feature um

3:08

and these are all kind of decided by

3:09

revision so you see revision 4321

3:12

revision 765 um these are kind of

3:15

globally ordered on a bucket level um

3:17

but you can kind of use revisions in

3:19

some really interesting ways uh like you

3:21

know optimistic and currency control and

3:22

stuff like that um and so at its very

3:25

very core the key Value Store access

3:27

patterns are very simple you can get you

3:28

can you know update you can you know

3:31

create uh various keys and I'm going to

3:33

go over all these operations and and how

3:35

they work um so what is a key Value

3:37

Store kind of good for well it's good

3:39

for storing filtering and retrieving

3:41

that stuff that kind of can identify as

3:42

key value pairs you want to index on a

3:44

particular key and you want to throw a

3:46

blob of data whether that's a Json

3:48

object or whatever um inside that

3:50

particular value and so very very

3:52

suitable for things with that type of

3:54

access pattern um the NAT key value

3:56

stores gives you history so you can kind

3:57

of use that as another dimension um you

4:00

know when writing your applications and

4:02

and lean on that for um some of the

4:04

heavy lifting you can also because it's

4:07

a stream under the hood we have a kind

4:08

of watch mechanism where you can watch

4:10

for changes to a key we have many many

4:13

companies that are using gats that are

4:14

kind of using these key value stores as

4:16

a as a big kind of like configuration

4:18

management layer where you could you

4:20

know set config variables or or things

4:22

like that in that key value store and

4:24

instead of your applications having to

4:25

pull for those or for you having to

4:27

restart your application to get those

4:29

variables you can just have them watch

4:31

for changes and automatically update you

4:33

know immediately and so I think that's

4:35

something that's really really neat

4:36

about um about key value stores that you

4:38

could watch for changes like I mentioned

4:40

before we also have optimistic

4:41

concurrency control which we will cover

4:43

in an example today doing a leadership

4:45

election system I know that sounds

4:46

really complicated but I promise it's

4:48

not it's going to be very simple um but

4:50

optimistic concurrency control is

4:52

basically some mechanisms there for you

4:53

to make sure that clients aren't

4:55

stomping on each other when you have a

4:56

key value store and you have multiple

4:58

kind of concurrent writers you want want

4:59

to make sure that if you're kind of like

5:00

updating that Json payload and saving it

5:03

back to the database that you're not

5:04

kind of conflicting with one another and

5:06

so optimistic concurrency controls great

5:08

for that and we'll go into what

5:09

operations support it lastly it wouldn't

5:12

really be a very functional key value

5:14

store if you didn't have some form of

5:15

TTL or expiry so whether you're kind of

5:17

building like a cach mechanism or you

5:20

need to use ttls for kind of other kinds

5:22

of things um we have that on a bucket

5:24

you know level where you can set a TTL

5:27

for an entire key value bucket um in a

5:29

future uh you know update to the server

5:32

we're going to be supporting um per key

5:34

ttls which is going to be amazing and

5:36

I'll cover all the other really cool

5:38

features we have so many coming down the

5:39

pipe for uh key value that's just going

5:41

to make it an excellent excellent choice

5:43

for for keyue stores for pretty much

5:46

anything um and again just a reminder

5:48

that a key value store for Nats is just

5:50

a stream so you get all the really neat

5:52

things that come with Nats jet stream

5:54

the global scale the replication being

5:57

able to mirror source mck and demu these

5:59

things and you get all the multi-tenancy

6:01

of with accounts U multiple buckets and

6:04

you get all the security and ACLS that

6:06

you need there so um we we see a lot of

6:08

organizations and uh folks writing

6:11

applications with this taking advantage

6:12

of all these features because they're

6:14

kind of harder to get in other um you

6:16

know in other key value stores and so

6:17

just keep in mind that um as we're

6:19

talking about KV stores they're not much

6:21

different from streams under the hood

6:22

they just have a little layer of you

6:24

know I would say sugar on top of

6:26

them so let's address the elephant in

6:29

the roof though I started out talking

6:30

about redis kind of being this king of

6:32

key value stores and it really is it's a

6:34

great piece of software I have no Faults

6:36

for it um so why would somebody use gats

6:39

over reddis well that's ultimately going

6:42

to depend on what you need um redus is

6:44

great for standing up something it has

6:45

great single node performance it has

6:47

great kind of rep replication

6:48

performance and everything like that but

6:50

they've T redus has kind of taken a

6:52

different approach to um especially on

6:54

the infrastructure side that that then

6:55

Nats has and so you might see some

6:57

advantages you know um when it comes to

7:00

kind of like global scale you know

7:01

moving out to a super cluster instead of

7:03

just a cluster inside of a single uh

7:05

data center or region um you might find

7:07

some advantages there and I generally

7:10

hate doing benchmarks it's not I'm not a

7:12

fan of doing General benchmarks because

7:14

they can go completely astray and it's

7:16

hard to do a real Apples to Apples uh

7:18

comparison but I've had many folks ask

7:20

me can we just have some sort of

7:22

comparison and the goal here is not to

7:24

say who's better at what um more to say

7:27

that we I think we can all agree Reedus

7:28

is a Fant fantastic piece of software

7:30

and it's really fast and it meets a lot

7:32

of those needs and what I wanted to show

7:34

here is that uh Jetstream you know KV is

7:36

also just very comparable in terms of

7:38

performance um characteristics here now

7:41

if I were to get into how these things

7:44

um you know do replication and

7:46

clustering and everything it would it

7:48

would turn from an Apples to Apples

7:50

comparison to an apples and oranges

7:51

comparison um because they are they they

7:54

start diverging very quickly here but

7:56

for single node right performance you

7:57

can kind of see uh redus is going to win

8:00

out on the latency side especially for

8:02

for a small number of writers and and

8:03

jet streams KV is going to kind of have

8:06

a a larger kind of sealing in terms of

8:08

handling concurrent requests and and

8:10

scaling those out um in terms of

8:11

throughput and so um but ultimately

8:14

these are all very comparable I don't

8:15

think we're going to see like this thing

8:17

is 10x faster than than the other one um

8:20

so so if you're if you're really a big

8:22

fan of redus and you want kind of

8:23

similar performance characteristics here

8:25

um you know Nat's jet stream is going to

8:27

be a really really kind of good choice

8:29

for for you so to talk about um we

8:32

already talked about how all streams are

8:33

configured and many of those options um

8:35

can be shared for uh for KVs um the the

8:39

three kind of Concepts I wanted to just

8:41

address really quickly that's new to KVs

8:44

is the idea of history of ttls and

8:46

compression and these kind of exist in

8:49

some shape or form depending on how you

8:51

you know configure your streams um but

8:54

these are kind of newer concepts for a

8:56

Nat KV this idea that you can have mult

8:59

multiple values for a key and that they

9:01

could be historically ordered um is an

9:03

excellent Advantage for a lot of

9:04

applications the fact that you can set a

9:06

TTL and those would expire and fall off

9:08

and then you have um a compression flag

9:11

that you can kind of condense and

9:12

compress um your your key Value Store um

9:15

which is

9:16

awesome but we're going to jump into the

9:18

meat of it and I'm going to talk about

9:19

all the operations that um kind of come

9:21

with this key value store some of these

9:23

are going to seem super simple and

9:24

obvious but I wanted to talk about some

9:26

of the subtleties behind them and how

9:27

you can leverage them for your

9:29

applications now we're not going to have

9:31

all of the operations that redus has

9:33

we're we're just not going to have all

9:35

of that those features and functionality

9:37

edits core anat key Value Store um right

9:39

now is kind of dumb when it comes to the

9:41

types of values that you put into that

9:44

value slot uh but we have some upcoming

9:46

features that are going to introduce

9:48

some more um value-based semantics and

9:50

I'll talk about those in a sec uh but

9:53

quite simply you have a put operation

9:55

which is basically just saying I want to

9:57

set this key orders1 234 for I give it a

10:00

payload and the key Value Store accepts

10:02

it says cool you've written it um the

10:04

key value store is going to return a

10:05

revision number which we'll we we'll be

10:06

using um and our examples for um some

10:09

cool stuff like optimistic concurrency

10:11

control but it's just very easy to you

10:13

know in any sort of gats SDK whatever

10:16

language you're using to execute a put

10:18

command against this KV

10:19

store then we have the get command which

10:21

is again just a simple as a put command

10:24

you get an order and uh and it's going

10:26

to return that order to you with the

10:28

subject any sort of headers and metadata

10:30

as well as the payload um and it's going

10:33

to get the the most recent value to you

10:36

now you can request a specific revision

10:37

as well if you know that you know

10:39

there's a revision that that in

10:41

particular that you want from a

10:42

historical value you can request that as

10:44

well via the get

10:46

command then we get a little bit more

10:48

subtle um and we start getting into kind

10:50

of our OCC or optimistic and currency

10:53

control um layers here which is uh the

10:56

create command so the create command

10:58

will will um essentially let you create

11:00

a key and like many systems it will fail

11:03

if that key already exists and this is

11:05

really important because um we can we

11:08

can use this in combination with ttls to

11:10

do very cool things like leadership

11:12

elections um you know distributed locks

11:14

and Lease systems and and I'm going to

11:16

demonstrate that in an example here

11:18

today uh but just know that if you use

11:20

the create command it's going to fail if

11:21

you're going to if if that key already

11:23

does exist um but it can be combined

11:25

with things like ttls to make for some

11:27

really really neat patterns very similar

11:29

to create as well we have the update

11:31

command which allows you to try to

11:34

update a value but you have to specify a

11:36

revision number um of what you think the

11:39

last known revision is and this is used

11:41

when you have multiple clients that are

11:43

all trying to update the same key to

11:45

ensure that um they will you know not

11:48

stomp on each other so imagine that

11:50

client a you know sends an update to

11:52

orders maybe they changed one of the

11:53

fields inside of this Json um payload

11:56

and they said the last known revision to

11:58

me is revision number 20 121 KV store

12:01

says cool we got it um you are now on

12:03

revision 122 which is great um and then

12:07

a second later or even you know

12:09

milliseconds later client B um says I

12:11

want to update the the order 1 two 3 4

12:14

um and my last known revision is

12:16

revision 121 um and the KB store is

12:19

going to reject that because it knows

12:21

it's had a a later revision and it's

12:23

going to tell the client hey I can't do

12:25

that um so the client can then get the

12:28

latest version and do its patch or

12:30

whatever it needs to do update its

12:32

column um and then send an update to the

12:34

KV and it will succeed and so update and

12:38

create are really cool kind of

12:40

Primitives to use here or commands to

12:42

use here for the KV um to make sure that

12:45

clients aren't stomping on each other

12:46

but also you can express a lot of really

12:48

cool kind of locking uh mechanisms

12:51

here and then we get to delete so the

12:54

cool part about delete is because KV was

12:56

designed for historical values in mind a

12:59

delete actually does not delete the data

13:01

itself but instead puts a delete marker

13:04

as the latest revision for that

13:05

particular key um and so it would you

13:08

know not return a payload for you would

13:09

say that this thing is deleted um even

13:12

when you kind of do a a particular watch

13:14

and you're W you could see that you know

13:16

Keys have been deleted as part of the

13:18

notification which is really neat and so

13:21

um a delete is kind of like a soft

13:22

delete for for a KV um and it allows you

13:25

to still have access to all those

13:26

historical values which is really nice

13:29

and then Purge is kind of like your more

13:31

destructive action for a delete um it's

13:34

going to insert that delete marker so

13:35

you still get any notifications and

13:37

Watchers all still work well which is

13:39

what you want um but then it's going to

13:41

uh it's going to delete all of the

13:43

revisions before it in that particular

13:44

key space um and so uh you can also

13:48

specify um in a in a delete you can

13:50

specify an optional revision just to

13:52

make sure um that you don't kind of bump

13:55

up against any issues in terms of uh

13:57

deleting the wrong Vision or anything

13:59

like that so you also have optimistic

14:01

and currency control Primitives here

14:03

inside of delete um so those are kind of

14:06

the deletion commands um and then we

14:08

kind of move on to the other command and

14:10

this is more of a kind of a less of a

14:13

command and more of uh a way that the

14:15

client libraries all work um but you're

14:17

going to see this in your sdks which is

14:19

watch and this is one of my favorite

14:21

Parts about the KB because it gives you

14:22

kind of this live update key Value Store

14:25

uh you could use it for all kinds of

14:26

things whether you're maintaining an in

14:28

memory cash or whether you're you just

14:30

want to kind of get the latest updates

14:32

as soon as possible you want to remove

14:33

polling as an option um this is going to

14:36

uh be what you want to use for that um

14:40

and so under the hood what this actually

14:41

does and you could look at my previous

14:43

videos for how all this stuff works

14:44

under the hood but um this is going to

14:46

create a lightweight consumer to be able

14:49

to uh to be able to get all of the

14:51

notifications for that particular watch

14:53

and so you get a lot of the same

14:55

features that consumers do like being

14:56

able to filter on a particular subject

14:58

and so maybe I don't want to watch all

15:00

the keys in this key value store this

15:02

can be a massive key value store we have

15:04

people that put millions and millions

15:05

and millions of keys um tens of millions

15:08

uh hundreds of millions of keys inside

15:10

of a key Value Store um and so maybe you

15:12

don't want to access all of them so you

15:13

could use subject filtering to filter on

15:15

that particular um set of keys you can

15:18

also uh use filtering to decide what

15:20

kind of uh notifications do you want do

15:22

you want to get the history of each of

15:24

these keys or do you want to just get

15:25

the latest value do you want to receive

15:27

delete notification

15:29

um do you want to be able to receive uh

15:31

you know updates and and metadata only

15:34

um there's a lot of really neat things

15:35

that you could do to kind of choose just

15:37

like we talked about in our consumer

15:38

video to choose what kind of gets sent

15:41

over the wire so you could remain really

15:42

efficient and then lastly you could also

15:44

kind of choose where do you want to pick

15:45

up on what kind of revision do you want

15:47

to kind of pick up from um because you

15:49

might not want to get all of the values

15:51

and so what this does is you're going to

15:54

uh create a a Watcher in your client SDK

15:57

and um and then the KV is going to

15:59

continually send messages um and what

16:01

it's going to first do is it's going to

16:02

send all of the current values and it's

16:04

going to send a message that's

16:06

essentially blank to signify that it's

16:09

uh it sent you all the most recent or

16:12

what it knows is is the most up-to-date

16:14

values and from there on you're going to

16:16

start receiving notifications of updates

16:18

and so you could decide if you want to

16:20

just look at all of the values real

16:21

quick you could just like watch and get

16:23

all the current values and then stop it

16:25

or you can just keep it going and

16:27

receive any sort of update delete create

16:29

notifications for that particular key

16:31

space so watching is just a it's an

16:34

excellent tool I use it all of the time

16:37

um and it's it's a really really kind of

16:40

unique feature to the Nats

16:42

KV okay let's talk really briefly about

16:45

replication because I think this is

16:47

really important I know I talked a bit

16:48

about replication when it came to

16:50

streams um and and all of these kind of

16:53

features and functionality are shared

16:54

between streams and KBS and object

16:56

stores but I wanted to point out you

16:58

know replication for KVs in general um

17:01

you can obviously do your um R you know

17:04

n replication um where you can say I

17:06

want this to be you know replica of one

17:08

or or three or five um and that exists

17:11

within a single region right it will

17:13

exist within a single cluster um for the

17:16

most part so on uswest 2 I have an R3

17:18

and this is great for durability

17:20

purposes right Nats will handle all of

17:22

the uh consensus um you know it will

17:24

handle the raft group and all of the

17:26

Machinery under that and you could just

17:28

kind of um know that your knots KV store

17:30

is going to uh you know stay durable you

17:33

can take one node down and still accept

17:34

rights um you can take two nodes down

17:36

and still accept reads etc

17:39

etc and then in terms of kind of

17:42

advancing the way that our replication

17:44

works if you want to go outside of a

17:45

data center or you want to go through a

17:46

leaf node or into another region this is

17:49

where mirroring and sourcing come into

17:51

play so let's cover mirroring for a

17:52

second um let's imagine that um we're

17:55

we're using that Us West um you know key

17:58

Value Store over here we might have a

17:59

replication factor of three like we had

18:01

in our previous example but we want to

18:03

have a readon replica um available to it

18:07

um you know in another region uh maybe

18:10

we want to be able to sync kind of this

18:11

data and we just want really quick

18:12

access to rights and so we don't want to

18:14

Ping pack ping pong all the way back

18:16

over to um to the west coast of the US

18:19

especially if our application is running

18:21

over here on the east coast and so what

18:23

we could do is we could create a mirror

18:24

and the really really cool part about

18:26

all of this is when you create a mirror

18:28

for a key Value Store Nat is smart

18:30

enough to serve up requests from the

18:32

closest mirror and so you don't have to

18:34

change anything in your particular

18:36

applications and I'm going to show you

18:37

this today it's so cool because you just

18:40

create a mirror and then all of a sudden

18:41

your latencies go down um so it's

18:43

automatically going to be a read replica

18:46

um but it's also going to De delegate

18:48

rights you don't have to think about who

18:50

you're talking to um you just say I want

18:52

to write to this key value store and all

18:54

of the rights are going to go through

18:56

the leader um which is going to be over

18:57

here and so you can do this as many

18:59

times as you want and mirror all around

19:01

the world and have a very fast um you

19:04

know durable consistent key Value Store

19:07

um available to you and so this is this

19:09

is just really neat um this is usually

19:11

really hard to do in a lot of other

19:13

Technologies and uh Nats makes this so

19:16

so easy the kind of uh I would say the

19:19

third version of replication here would

19:21

be sourcing and sourcing isn't quite um

19:24

you know like a digital twin like a like

19:26

a mirror is but instead it's a about

19:28

kind of mxing and demoing data and so um

19:32

let's talk about demoing first so

19:33

demoing is this idea of like I have a

19:35

big key Value Store maybe somewhere in

19:37

central us or somewhere centralized a

19:40

huge key value store with a bunch of

19:41

data into it and maybe I'm going out to

19:44

the edge or maybe I I have like all of

19:47

these values separated by a region and

19:50

so it makes sense to have more Regional

19:51

Edge placements of these for for latency

19:53

purposes what I could do is I can create

19:55

a bunch of other little key value stores

19:57

and I can Source um all of this data and

20:00

just filter on the stuff that I care

20:02

about so this could be a big huge key

20:04

value stores and these can be much

20:05

smaller key value stores that only care

20:07

about the data that they need to and

20:08

whether you're being distributed like

20:10

geographically or you're being

20:12

distributed organizationally where you

20:13

might have different business units that

20:15

care about different sets of data you

20:16

can still have a big key value store

20:19

that then gets demoed into smaller ones

20:21

for you know those purposes um and

20:23

that's really really neat and so you can

20:25

source and you can filter on the larger

20:27

data set into smaller key value stores

20:29

that live uh maybe closer to the

20:31

applications that are calling them the

20:33

inverse of this is going to be um mxing

20:36

which is saying maybe I have a a bunch

20:38

of key value stores that uh exist uh you

20:41

know all over the world in different

20:43

Edge locations maybe these are running

20:45

on little raspberry pies I don't know um

20:48

but I want to be able to take those and

20:49

I want to be able to sync them into like

20:51

one large key Value Store um in the

20:53

cloud somewhere and this is where kind

20:55

of M sourcing comes in where I can take

20:58

um each of those kind of regional keys

21:01

and I could MX them all into um a single

21:04

one and one of the cool one of the cool

21:06

things that um recently came with theat

21:08

servers you can now do remapping as soon

21:10

as you're kind of sourcing into these

21:12

streams you can say hey you know the key

21:15

space might look like one particular

21:17

thing for this key value store but I'm

21:18

going to remap it into something that's

21:20

more appropriate for this key value

21:21

store and you can do all of that you

21:23

know all that all of that is

21:24

configuration based um inside of gats

21:27

when doing sourcing and so really really

21:29

cool feature I wish I had time to cover

21:31

it but I think it's going to be a whole

21:32

separate video to cover how to do this

21:34

in depth um but it you know feel free to

21:37

read the docs try it out for yourself

21:39

it's a really cool pattern and many

21:40

folks are kind of taking advantage of

21:42

that um before we dive into the examples

21:45

I would just want to talk quickly about

21:46

some upcoming features because uh it's

21:49

funny that I'm doing this key Value

21:50

Store video um right as we're about to

21:53

release some really cool features for it

21:54

the next version of nat server is going

21:56

to include a lot of these things um

21:58

value based semantics like counters

22:00

lists sets Maps things that you would

22:02

expect to be in kind of a value based

22:04

key Value Store um and stuff that you

22:06

might miss from reddis all of these

22:08

Basics are going to now exist inside of

22:10

the Nats KV which will be great um like

22:13

I mentioned before we're also going to

22:14

be getting per key uh ttls which means I

22:17

can have a particular subject or key and

22:19

I can say here's how I want it to expire

22:22

and um and you could do that you know

22:24

have variability across an entire bucket

22:27

um which is really useful for things

22:29

like building like toy schedulers or um

22:32

if you want to build your own version of

22:33

kubernetes or something like that you do

22:35

all of that um you know much easier with

22:37

a per uh key TTL um we Al we are also

22:41

going to be introducing some uh least

22:42

recently used uh semantics here um in

22:45

terms of how we do like cash eviction

22:47

and expiry and stuff like that so you

22:49

can have um some stronger cash based

22:51

semantics um with kind of like this lru

22:54

uh stuff um and again when we do release

22:58

this I will be doing more of a deep dive

23:00

into all of this um Batch get is

23:02

actually something that exists on

23:03

streams and KVs but where gets really

23:05

cool for KVs is if you have something

23:08

that um you know this the particular

23:10

structure of the thing that you're

23:11

storing is either like column based or

23:15

field based where you might have

23:17

something representing a user and the

23:19

user has an email and a password and an

23:21

ID and you can store all of those in

23:23

individual um entries like user. ID

23:27

user. name user. email and you can

23:30

actually use a batch get to get all of

23:32

those fields in one request um and then

23:34

easily map them to you know a structure

23:36

inside of your programming language of

23:38

choice um which is very very awesome and

23:41

just gets us a lot closer to some of the

23:42

semantics you'd expect from redus or

23:44

Dynamo DB or things like that and then

23:47

lastly one of the one of the things

23:49

that's missing that folks who are using

23:51

KV that would love to have this is uh

23:54

when ttls expire currently we don't get

23:56

any sort of watch notification for it

23:58

but we will be getting watch

23:59

notifications for any sort of system

24:01

delete events ttls evictions stuff like

24:05

that and so um that's something to

24:07

really look forward to in the next

24:09

version of nat server Nats 211 and I

24:12

will keep everybody up to date in terms

24:15

of uh when these features go live and

24:17

when we can start playing with them so

24:19

look forward to that okay so I think we

24:23

can now move on to diving into some

24:26

examples so I'm going to be using Cadia

24:28

Cloud to drive a lot of this but you can

24:30

do this via the CLI um the reason I'm

24:32

using Cadia Cloud here is cuz I wanted

24:33

to show some real like Regional um you

24:36

know movement of KVs mirroring stuff

24:39

like that so if I go over here to

24:41

streams um I have a KV called my KV

24:43

bucket and actually I was using this in

24:45

in my previous example um right now it

24:48

exists in Azure Europe West okay and so

24:52

I'm going to run a quick example um code

24:54

that's essentially going to just be like

24:56

doing a get every second second um from

24:58

this particular key value store and we

25:00

can kind of measure the latency here and

25:02

what we'll do is we'll measure the

25:03

latency and then we'll move this around

25:05

and see if we can get our latency kind

25:06

of uh lowered a little bit I'm going to

25:08

run uh G run main.go

25:13

and we're getting a value noise and it's

25:16

taking about 200 milliseconds now I'm in

25:19

uh Southern California in the US and

25:22

this KV store obviously exists in Europe

25:25

um somewhere in some Azure Data Center

25:27

so we're um we're getting about 200

25:30

milliseconds of latency um and and

25:32

that's just not going to be good enough

25:34

for what I want and so there's a couple

25:37

options that we have available to us

25:38

here I can go into uh this KV bucket and

25:42

I could simply just um change the

25:45

cluster to something like uh uh let's go

25:49

to AWS Us West 2 that's going to be

25:52

pretty close to me so when I hit save

25:54

what this is going to do is it's

25:55

actually going to move the bucket

25:56

mid-flight everything's going to stay

25:58

the same um you know clients that are

26:01

trying to access the bucket they'll

26:02

still be able to access the bucket uh

26:04

it's just going to mirror that and move

26:05

it over um automatically for us and you

26:08

can see that I've automatically now

26:09

dropped latency which is great um now

26:13

maybe this is what I want to do maybe

26:14

I'm doing a lot of Rights in Southern

26:16

California and I want my reads and my

26:18

rights to be fast and this is good

26:20

enough for me but what if I actually do

26:22

want the rights to be fast in Europe and

26:24

I want just the reads to be fast in uh

26:26

Southern California for me well what we

26:28

can do is I'm going to move I'm going to

26:30

go ahead and move this back to um to

26:33

Europe let's go Azure Europe West I'm

26:35

going to hit

26:36

save and then we should notice pretty

26:39

soon here yeah we're getting back up to

26:40

those 200 milliseconds in terms of

26:42

timing um so the thing that we could do

26:45

is if we wanted to make reads really

26:46

really fast uh for that particular

26:48

stream we can create a mirror for it so

26:50

I'm going to go to S Cloud I'm going to

26:52

create a mirror and I'm just going to

26:53

call this um you know my bucket mirror

26:58

you can call it whatever you want um and

27:00

I'm going to pick the kvi bucket and

27:03

then I'm going to set its cluster to uh

27:07

AWS Us West 2 and here's the cool thing

27:10

I everything is still happening the the

27:12

key Value Store still exists in Europe

27:15

I'm just creating a mirror inside of uh

27:18

AWS Us West let's see what happens we

27:21

drop our reads to back to those 40

27:23

milliseconds and that's because we just

27:25

created a mirror all the rights are

27:27

still going through Europe um but all of

27:30

our reads uh are you know all of the

27:32

keys are replicated and then being read

27:34

directly from um a mirror inside of

27:37

Southern California here uh which is

27:39

awesome very very cool so uh so that's

27:42

how we do some mirroring and replication

27:44

and moving of KV stores you could see

27:45

it's really really easy the second thing

27:47

that I wanted to talk about um is I want

27:50

to talk about leadership election so

27:51

we're going to write a a simple kind of

27:54

uh go program that kind of uses all of

27:57

the these optimistic concurrency control

27:59

Primitives to be able to um create a

28:01

little leadership election system and

28:03

what I mean by leadership election is we

28:05

want to have multiple programs running

28:06

but we only want one to be the leader

28:08

and to do this in a distributed system

28:11

requires a bit of well it requires some

28:14

something centralized to be able to kind

28:15

of be that uh system of record for who

28:18

can be the leader and we need a way for

28:20

these applications to try to claim

28:22

leadership um whenever they want and so

28:25

uh we're going to write a little

28:26

leadership election system um this is

28:28

very similar to like a distributed lock

28:30

if you've used one of those before um

28:32

but let's go ahead and do that so I

28:34

already have a little bit of code

28:35

written just to kind of remove some

28:36

boiler plate um and so we have kind of a

28:40

is leader and a name and a revision flag

28:42

available to us and then um we're

28:44

already starting to create this key

28:45

value store so we're saying js. Creator

28:48

update key value and uh we're naming our

28:51

bucket leadership we're setting our TTL

28:54

to about 3 seconds and the reason we're

28:56

using ttls here is if a leader goes

28:59

offline and never comes back online we

29:01

want to be able to expire that key so

29:03

that new leaders can come online so if

29:05

there's a disruption to service or

29:06

anything like that um their leadership

29:08

is forfeited I'm also setting Max bytes

29:11

here just because we're using Cadia

29:12

cloud and we absolutely need to have um

29:14

Max B set um for that and I'm just going

29:17

to uh check if there is an error in

29:19

creating this key value store and then

29:21

we could start uh writing kind of our

29:23

leadership code um okay so we have a

29:26

leadership key value key value bucket um

29:30

what I'm going to do I'm going to write

29:31

this in probably the most basic

29:32

barebones way possible because I know

29:35

not everybody's going to be using go for

29:36

this and we don't want to get into crazy

29:39

like you know go concurrency so we're

29:40

just going to do we're just going to do

29:42

the basic Brute Force way of this so I'm

29:44

going to put a for Loop here and I'm

29:46

going to uh sleep for one second um I'm

29:50

also going to just log real quick um

29:53

starting leader election sure um

29:55

actually let's say uh trying to become

29:58

leader perfect um so these clients are

30:02

going to try to become leader um before

30:03

they do so they're just going to sleep

30:04

for one second um and they're going to

30:07

check first if they are the leader then

30:09

they just want to refresh their uh they

30:12

want to renew their leadership so what

30:14

they're going to do to refresh the lock

30:16

is they're going to use the KV update um

30:19

now this is really important so they're

30:21

going to be using KV update because they

30:23

want to be able to essentially take a

30:25

particular key in that key value store

30:27

and refresh it um you know making sure

30:30

that it doesn't expire and so uh we're

30:32

going to say KV update we'll put in our

30:35

Nat's context um we'll use the key

30:37

leader for all of our locks and then we

30:40

could put in um we could put in their

30:43

name there that we give

30:45

them and we're going to be using

30:47

revision now this is really important

30:49

because if there's any like intermittent

30:50

connectivity and somebody else claims

30:53

ownership um we still have this is

30:55

leader variable that's going to be

30:57

outdated and Incorrect and so we want

30:59

the key value store to be able to tell

31:00

us hey you're working on outdated

31:02

information and you are no longer the

31:04

leader sorry dude um so this is why

31:07

we're going to be using the update

31:08

command here um we're going to say if

31:11

error is not equal to nil then we want

31:14

to say that they lost leadership so I'm

31:16

going to say uh

31:18

leadership

31:20

lost and we're going to set is leader to

31:23

false okay so that's what we do if

31:25

there's a leader um by the way uh we can

31:28

do a quick continue

31:30

here um we're actually going to do it

31:33

right

31:35

here because we don't need to execute

31:37

any more logic um for that one uh we

31:42

want to make sure that we're setting a

31:43

revision properly and then we want to do

31:46

an else so if if this isn't a leader

31:48

which is going to be most of these nodes

31:49

we want these to try and become a leader

31:51

and it looks like co-pilot is getting

31:52

pretty close here um in terms of the the

31:55

code that I wanted to write but but uh

31:57

let's just do this ourselves so we're

31:59

going to set our revision and error and

32:00

we're going to uh execute the KV create

32:03

command against leader and we're going

32:04

to want to set our name um and then uh

32:07

if there is a particular error here then

32:10

we want to handle it we want to say um

32:13

we want to just continue actually

32:15

because we didn't we weren't able to

32:16

become leader so we'll just try again in

32:18

another one second or so and um and then

32:22

if if there was an error um this could

32:25

either be an else or we have a continue

32:27

that stopping this so we could literally

32:28

just say that uh this thing became the

32:31

leader and uh we can set is leader to

32:33

true and that's really all that we have

32:36

to do for this particular leader

32:38

election system again simple Bare Bones

32:41

um very kind of brute force in its

32:43

approach but um it does the job so let's

32:46

actually try and uh fire this up I'm

32:48

going to go ahead and uh go to our

32:53

example we'll go to leadership and I'm

32:55

going to say go run um our leadership

32:58

example and I need to pass in an

33:00

argument for the node so I'm going to

33:02

say uh node

33:05

one and this is trying to become a

33:07

leader and it became a leader because

33:08

it's the only node it's the only node

33:10

that um exists right now so it was able

33:12

to claim leadership um but let's

33:14

actually fire up a couple more so we're

33:16

going to say node two and we're going to

33:18

say node three and look it looks like

33:20

these guys are trying to become leaders

33:23

um but they're going to be trying every

33:24

second to do to do so um if I go ahead

33:27

and kill node one somebody else is going

33:29

to pick up the leadership because that

33:31

uh key is eventually going to expire you

33:33

can see that uh node 3 is now the leader

33:36

now if I do a Nats uh KV get

33:40

leadership um I think that's KV store

33:42

and then who is the leader you could see

33:45

that node 3 is returned because that's

33:47

what we set inside of the the KV store

33:49

and so this is kind of a very

33:50

lightweight way that you can kind of do

33:53

leadership election with KV stores a

33:55

combination of update and create using

33:57

those revisions um you can have uh just

34:00

a really kind of easy way for people to

34:02

assume leadership and you could

34:03

obviously adjust any of these timings on

34:06

some of these things um but uh and you

34:09

could also kind of use watches and

34:10

notifications to speed some of this

34:12

stuff up now we are leaning on ttls a

34:15

little bit so until we get that um new

34:17

feature around TTL notifications we

34:19

won't be able to speed this up a ton but

34:22

um as you could see we have a lot of

34:24

really neat tools at our disposal to be

34:27

able to do things like distributed locks

34:29

or leadership elections um and that's

34:31

just scratching the surface of some of

34:33

the cool things that you could do inside

34:35

of Jetstream KVs so that's all I have

34:37

for today hopefully this was a a really

34:40

fun episode to learn more about jet

34:42

stream KVs we have so many more cool

34:45

pieces of content that are coming down

34:46

the pipe so please stay tuned and if you

34:49

didn't already please like And subscribe

34:52

um we have more events coming we have

34:54

Live Events we have amas we have long

34:57

form videos like this so uh stay

34:59

subscribed to the channel and I'll see

35:01

yall next time thanks so

35:02

[Music]

35:03

[Applause]

35:06

[Music]

35:11

much

Continue with YouTLDR

Analyze another video with Pro

Process a new video, search every timestamp, compare sources, and keep the result in your library.

Get Pro — $12/month30-day money-back guarantee

More transcripts

Explore other videos transcribed with YouTLDR.