- An important subset of NoSQL databases
are key value stores.
While key value stores are very limited,
they're also very powerful and fast.
In our example, in this chapter
we will use Redis for that.
So, let's look first at a few details
about Redis and also the use cases.
So, first of all, Redis basically stores the data in memory.
This of course means that this is blazingly fast.
At the same time, the data might get lost,
if you kill the Redis process.
Redis has some mitigation against it.
As it stores snapshots of its data periodically on the disk.
Still, there is a risk that you lose
the data that is stored in Redis when you're
in between two snapshots.
Redis is very fast
and it comes often with constant access times to the data.
And if you're familiar with the Big O notation,
this sometimes comes with an O(1).
So, it does not matter how much data is in the database,
the access time will always be the same.
So, as I said, register state in memory.
So, it's really good for rather ephemeral data.
Common use cases for Redis are caching of course
session storage and also short-lived data.
In our example, we will, for instance
use Redis for session storage
and also to store the basket items off a given user.
Let's look at the basic principles
of key value stores in Redis.
If you ever worked
with some hashmaps in a programming language,
this should look very familiar to you.
So, we first have a key and we have a value.
And the key can be an arbitrary data type.
Now here I chose A, just a letter.
And a value here in our example is just the text
or some other string, or it's some chasing
but this does not really matter to Redis
or it can also be here in this example, some binary data.
The interesting part is
that Redis now supports
more complex data types on top of that.
Let's go through them real quick.
First Redis supports,
we saw this before strings or binary data or plops.
So, this is just some string.
Very simple.
Redis also support lists.
So, don't forget, we still have the key on the left side.
So, the key is not associated with the list of items.
This case.
Let's say it's some letters that are stored in this list.
Internally Redis uses linked lists for that.
And this is also the reason why the access times are so fast
and constant.
Then we have sorted sets.
So, first of all, they are sets.
So, each element can only be in there once
and they are also sorted.
And you can also give each element a rank
that should be used for this sorting.
Then this case, you see it here
with these two first elements.
I use the rank one and two for the sorting.
And later we will use exactly that for Mexican example
because there we will use the bitcoin value as a rank.
This means that in the end
we will get a sorted set by the bitcoin value.
Then it's very easy to find the maximum value from that.
One more, very important data type, a hashmap.
So, hashmaps contain now associated
with the key that we have anyways,
some other key and a value associated with that.
So, this is more or less a key value substructure
within the value of Redis.
If this is a little bit complicated
you will need to see this data in the database
but I'll show you one example for the hashmaps
which resembles what we will build later
for the shopping basket of our shopping system.
So, now we have, left the key.
Then we have the hash and we have the value.
We can now compile a key that is for instance
Bsk for basket.
And then we add for instance, a call-in and the user ID.
So, this is the basket for a given user ID.
And in there, I now add a hash
and the hash can be for instance
the item number, and then I also add a value.
So, this is the number of items
for this item number in the basket.
So, I add now one more item with another item number
and another number of items in the basket.
So, in the end I would then have Bsk U1 as the key.
If I add this to Redis,
I will end up with one key on the left side,
that is Bsk U1.
And in there then, a list of item numbers
with the quantity in the basket.
One interesting detail is that all this is stored
in the same data structure.
So, there are basically no tables.
So, on this Redis server,
I can at the same time also then store another item
which has the key C and then again
just contains some binary data or a string.
So let's go ahead and install Redis now on your machine.