About Redis (Overview and basic use)

Mathematics  Machine Learning Technology  Artificial Intelligence Technology  Database Technology   Algorithm   Programming Technology  Digital Transformation  ICT

In the previous article, I talked a little about asynchronous processing using Redis, but this time I would like to discuss Redis itself in more detail.

According to “Redis in Action”, “Redis is an in-memory remote database with high performance, replication, and a unique data model. It supports five types of data structures and can adapt to a wide range of different problems that can be naturally associated with Redis features. With features such as replication, persistence, and client-side sharding, Reids is highly scalable and can be used as a convenient means of prototyping to a full-fledged database system that can handle hundreds of gigabytes of data and millions of requests per second. to full-scale database systems handling hundreds of gigabytes of data and millions of requests per second”.

One that provides similar functionality to Redis is memcached, a key-value cache server. Compared to this, Redis can store key-to-value mappings just like memcached, but it has the ability to automatically write data to disk in two different ways (snapshot (instant dump) and append-only write), and it can store four types of data structures other than plain strings. Redis can be used as a primary database.

Redis can be used as a primary database or as an auxiliary system to other storage systems. It can be used to store data in Redis only when the performance and functionality of Redis is required, and when the performance is acceptable even if it is a little low, or when the data is too large to be economically stored in memory. When the data is too large to be economically stored in memory, you can use another database.

There are five types of data structures in Reids: STRING, LIST, SET, HASH, and ZSET. Some commands for each type of data are common to all five types, while others can only be used for each type of data. STRING (strings, integers, and floating point minorities), LIST (concatenated list of strings), and HASH (unordered collection of unique strings) are supported by various programming languages. However, SET (unordered key-value hashing) and especially ZSET (sorted sets) are specific to Redis.

<string>
set('en','Hello');
set('jp",'こんにちは');
get('en')  -> 'Hello'
<hash>
hset('item01','name",'apple');
hset('item01','price','$5.0');
hset('item01','stock',1000);
hget('item01','name'); -> 'apple'
<list>
rPush('id01','aaa');
rPush('id01','bbb');
rPush('id01','ccc');
<set>
sAdd('food_a','meat');
sAdd('food_a','fish');
sAdd('food_b','bread');
sAdd('food_b','vegetable');
sUnion('food_a','food_b) 
<zset>
zAdd('score',100,'Taro');
zAdd('score',300,'Jiro');
zAdd('score',200,'Hanako');

To access Redis directly, redis-cli (an interactive client that comes with Redis) is a simple tool to use.

One of the features of Redis is the pub/sub function. When a user subscribes to a DB as a channel, the communication is connected to the DB and it becomes “listen”. When a user publishes data from another input, the data is sent to the subscriber at the same time. An example of the actual operation is shown below.

$ redis-server
<新しいターミナルで> (terminal1)
$redis-cli
>subscribe redis-test
Reading messages...(press Ctrl-C to quit)
1) "subscribe"
2) "redis-test"
3) (integer) 1
<別のターミナルで> (terminal2)
$redis-cli
>publish redis-test 'hello world'
(integer)1
<terminal1>
1)"message"
2)"redis-test"
3)"hello world"

By using these, the queue using the channel described in Clojure’s core.async can be realized.

コメント

Exit mobile version
タイトルとURLをコピーしました