0% found this document useful (0 votes)
68 views36 pages

Redis Report

This project report from the University of Information Technology focuses on Redis, an open-source, in-memory data structure store used for various purposes including database management and caching. It covers key features of Redis, such as its data models, replication capabilities, and persistence methods, as well as installation instructions and a demo. The report acknowledges the guidance of the instructor and emphasizes the learning experience gained through the project.

Uploaded by

20522164
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views36 pages

Redis Report

This project report from the University of Information Technology focuses on Redis, an open-source, in-memory data structure store used for various purposes including database management and caching. It covers key features of Redis, such as its data models, replication capabilities, and persistence methods, as well as installation instructions and a demo. The report acknowledges the guidance of the instructor and emphasizes the learning experience gained through the project.

Uploaded by

20522164
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

VIETNAM NATIONAL UNIVERSITY HO CHI MINH CITY

UNIVERSITY OF IFORMATION TECHNOLOGY

PROJECT REPORT

DATABASE MANAGEMENT SYSTEM

TOPIC
REDIS

Teacher: Nguyễn Thanh Bình

Group member:

STT Full Name MSSV

1 Lê Tiến Tuấn Vũ 20522164

2 Nguyễn Minh Khôi 20521479

3 Nguyễn Quốc Khánh 20521452

Hồ Chí Minh city, 23rd November 2022


ACKNOWLEDGEMENT

First of all, my team would like to express our deep gratitude to the lecturers of the
University of Information Technology - Vietnam National University, Ho Chi Minh City
for giving us the opportunity to carry out this project.
In particular, our group would like to express our deepest gratitude and thanks to our
instructor Nguyen Thanh Binh who guided us during the course of the thesis. He directly
guided, corrected and contributed many valuable comments to help our group complete
the final report of this course. We sincerely want to thank you and wish you success and
happiness in life.
During the implementation of the topic, our group applied the accumulated basic
knowledge and combined with learning and knowing new knowledge from teachers,
friends as well as many reference sources. survey. However, due to limited experience
and knowledge, there will be inevitable shortcomings in the project implementation
process. We are looking forward to receiving the evaluation and suggestions from the
lecturers and students so that this project can be more complete, our team will draw many
valuable lessons and experiences in the future work. future.
Once again, thank you very much and best regards!

Hồ Chí Minh city, 23rd November 2022


Student group perform

2
Table of contents
1. Database management system..........................................................................................4
2. NoSQL Database...............................................................................................................5
3. Redis database management system.................................................................................5
3.1 Overview Redis..............................................................................................................6
3.2 Feature of redis...............................................................................................................7
3.2.1 Data model..............................................................................................................7
3.2.1.1 STRINGs.........................................................................................................7
3.2.1.2 SETs................................................................................................................9
3.2.1.3 SORTED SETs (ZSET)................................................................................11
3.2.14. LISTs.............................................................................................................13
3.2.1.5. HASHes........................................................................................................15
3.2.2. Master/Slave Replication.....................................................................................17
3.2.3. In-memory...........................................................................................................18
3.2.4. Redis Persistence.................................................................................................18
3.2.4.1. RDB (Redis DataBase file)..........................................................................18
3.2.4.2. AOF (Append Only File)..............................................................................19
3.2.4.3. Should I use RDB or AOF?..........................................................................20
4. Install and Demo.............................................................................................................20
4.1. Install Redis.................................................................................................................20
4.2. Redis Cache.................................................................................................................27
4.3. Feature or Redis Cache...............................................................................................27
4.4. Advantages when use Redis Cache.............................................................................28
4.5. Caching Strategies.......................................................................................................29
4.5.1. Cache Aside......................................................................................................29
4.5.2. Read Through...................................................................................................29
4.5.3. Write Through..................................................................................................30
4.5.3. Write Back........................................................................................................30
4.6. Redis Demo.................................................................................................................31
5. Job distribution................................................................................................................36

3
1. Database management system
A database management system (DBMS) is a software package designed to store,
retrieve, query and manage data. User interfaces (UIs) allows data to be created, read,
updated and deleted by authorized entities.
Database management systems are important because they provides programmers,
database administrators and end users with a centralized view of data and free
applications and end users from having to understand where data is physically
located. APIs (application program interfaces) handle requests and responses for specific
types of data over the internet.
Relational and non-relational DBMS components delivered over the internet may be
referred to as DBaaS (database as a service) in marketing materials. According to the
research firm Gartner, database management systems designed to support distributed data
in the cloud currently account for half of the total DBMS market.
Well-known DBMSes include:
 Access – a lightweight relational database management system (RDMS)
included in Microsoft Office and Office 365.
 Amazon RDS – a native cloud DBMS that offers engines for managing
MySQL, Oracle, SQL Server, PostgreSQL and Amazon Aurora databases.
 Apache Cassandra - an open-source distributed database management system
known for being able to handle massive amounts of data.
 Filemaker - a low-code/no-code (LCNC) relational DBMS.
 MySQL – an open-source relational database management system (RDBMS)
owned by Oracle.
 MariaDB - an open-source fork of MySQL.
 Oracle - a proprietary relational database management system optimized
for hybrid cloud architectures.

4
 SQL Server – an enterprise-level relational database management system
from Microsoft that is capable of handling extremely large volumes of data
and database queries

2. NoSQL Database
NoSQL databases emerged in the late 2000s as the cost of storage dramatically
decreased. Gone were the days of needing to create a complex, difficult-to-manage data
model in order to avoid data duplication. Developers (rather than storage) were becoming
the primary cost of software development, so NoSQL databases optimized for developer
productivity.
As storage costs rapidly decreased, the amount of data that applications needed to
store and query increased. This data came in all shapes and sizes - structured, semi-
structured, and polymorphic - and defining the schema in advance became nearly
impossible. NoSQL databases allow developers to store huge amounts of unstructured
data, giving them a lot of flexibility.
Additionally, the Agile Manifesto was rising in popularity, and software engineers
were rethinking the way they developed software. They were recognizing the need to
rapidly adapt to changing requirements. They needed the ability to iterate quickly and
make changes throughout their software stack - all the way down to the database. NoSQL
databases gave them this flexibility.
Cloud computing also rose in popularity, and developers began using public clouds to
host their applications and data. They wanted the ability to distribute data across multiple
servers and regions to make their applications resilient, to scale out instead of scale up,
and to intelligently geo-place their data

3. Redis database management system

5
3.1 Overview Redis
Redis is an open source (BSD licensed), in-memory data structure store used as a
database, cache, message broker, and streaming engine. Redis provides data structures
such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs,
geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU
eviction, transactions, and different levels of on-disk persistence, and provides high
availability via Redis Sentinel and automatic partitioning with Redis Cluster.

You can run atomic operations on these types, like appending to a string;
incrementing the value in a hash; pushing an element to a list; computing set intersection,
union and difference; or getting the member with highest ranking in a sorted set.

To achieve top performance, Redis works with an in-memory dataset. Depending on


your use case, Redis can persist your data either by periodically dumping the dataset to
disk or by appending each command to a disk-based log. You can also disable persistence
if you just need a feature-rich, networked, in-memory cache.

Redis supports asynchronous replication, with fast non-blocking synchronization and


auto-reconnection with partial resynchronization on net split.

Redis also includes:


 Transactions
 Pub/Sub
 Lua scripting
 Keys with a limited time-to-live
 LRU eviction of keys
 Automatic failover
 You can use Redis from most programming languages.

6
Redis is written in ANSI C and works on most POSIX systems like Linux, *BSD, and
Mac OS X, without external dependencies. Linux and OS X are the two operating systems
where Redis is developed and tested the most, and we recommend using Linux for
deployment. Redis may work in Solaris-derived systems like SmartOS, but support is best
effort. There is no official support for Windows builds.

3.2 Feature of redis


3.2.1 Data model
Unlike RDMS like MySQL, or PostgreSQL, Redis has no tables. Redis stores data as
key-values. In fact, memcache does the same, but the data type of memcache is limited,
not as diverse as Redis, so it does not support many operations from the user. Below is a
brief overview of the data types Redis uses to store values.

3.2.1.1 STRINGs

7
Redis Strings is one of the most versatile of Redis’ building blocks, a binary-safe data
structure. Strings is an array data structure of bytes (or words) that stores a sequence of
elements, typically characters, using some character encoding. It can store any data-a string,
integer, floating point value, JPEG image, serialized Ruby object, or anything else you want
it to carry. Operate on a whole string or parts, and increment or decrement integers and
floats.
Command used on STRING values

8
3.2.1.2 SETs

Redis Sets data structure stores a unique set of members. With Sets, you can add, fetch,
or remove members, check membership, or retrieve a random member. With the sorting
algorithm, you can also perform set operations such as intersection, union, and set difference
and compute set cardinality.
Command used on SETs values

9
10
3.2.1.3 SORTED SETs (ZSET)

Redis Sorted Sets contain a unique set of members ordered by floating-point scores. As
with Sets, you can add, fetch, or remove individual members and perform set operations
such as union, intersection, set difference, and compute cardinality. Furthermore, you can
also query the set based on score or member value, aggregate, filter, and order results.
Command used on ZSET values

11
12
3.2.14. LISTs

Redis Lists holds collections of string elements sorted according to their order of
insertion. Push or pop items from both ends, trim based on offsets, read individual or
multiple items, or find or remove items by value and position. You can also make blocking
calls for asynchronous message transfers.
Command used on List values

13
14
3.2.1.5. HASHes

A Redis hash is a data type that represents a mapping between a string field and a string
value. Redis Hashes structure stores a set of field-value pairs designed not to take up much
space, making them ideal for representing data objects. It provides the ability to add, fetch,
or remove individual items, fetch the entire hash, or use one or more fields in the hash as a
counter.
Command used on HASH values

15
16
3.2.2. Master/Slave Replication
Redis supports Master/Slave Replication. Data from any Redis server can be
replicated to any replica. This is not a very prominent feature, other DBMS have this
feature, but mentioned here to remind that, Redis is no less than DBMS in Replication
feature.

17
3.2.3. In-memory
This is the most impressive thing when starting to learn about Redis. Unlike DBMS
Others store data on the hard disk, Redis stores data on RAM, and of course manipulate
read/write operations on RAM.

3.2.4. Redis Persistence


3.2.4.1. RDB (Redis DataBase file)
How it works RDB creates and backs up a snapshot of the DB to the hard drive after a
certain period of time.
o Advantages
 RDB allows users to save different versions of DB, very convenient when a
problen occur
 By storing data in a fixed file, users can easily transfer data to different data
centers, or to storage on Amazon S3
 RDB helps optimize Redis performance. The main redis process will only do
Ram jobs, including basic operations required from the client like
add/read/delete, while a child process will take care of the operations disk I/O
operation. This organization helps maximize Redis performance.
 When restarting the server, using RDB to work with large amounts of data will
have high speed rather than using AOF.
18
o Defect
 RDB is not a good choice if you want to minimize the risk of losing cool data.
Normally users will set up to create RDB snapshot 5 minutes 1 time (or more).
Therefore, in the event of a problem, Redis cannot operation, data in the last
minute will be lost.
 -RDB needs to use fork() to create a child process for disk I/O operations.In
case the data is too large, the fork() process can be time consuming and the
server will not be able to respond to the request from the client for a few
milliseconds or even 1 second depending on the amount of data and CPU
performance.

3.2.4.2. AOF (Append Only File)

How it works: AOF stores all write operations received by the server. This operation
will be restarted when restarting the server or resetting the original dataset.
o Advantages
 Using AOF will help ensure the dataset is more robust than using RDB. Users
can configure Redis to log each query or every time 1 second time.
 Redis writes the AOF log in an appending fashion to the end of the existing
file, so the seek on existing files is not necessary. Also, even if only half of the
command is successful recorded in the log file (maybe because the drive is
full), Redis still has a mechanism to manage and correct it fix that way (redis-
check-aof).
 Redis provides a background process, allowing AOF files to be recorded when
the capacity file is too big. While the server is still performing operations on
the old file, one file is complete. All new is created with the minimum number
of operations for creation current dataset. And once the new file is finished
writing, Redis will switch to real perform logging operation on the new file.
19
o Defect
 AOF files are usually larger than RDB files with the same dataset.
 AOF may be slower than RDB depending on how the interval is set for
backing up to the hard drive. However, if you set the log every 1 second, it can
reach performance comparable to RDB.
 Redis developer has encountered a bug with AOF (although it is very rare),
that is an error that AOF cannot reconstruct the dataset correctly when
restarting Redis. This error never encountered when working with RDB
before.

3.2.4.3. Should I use RDB or AOF?


Each method has its own advantages/disadvantages, and may take a long time to work
with Redis as well as depending on the application that makes the appropriate choice.
Many people choose AOF, because it ensures data integrity very well, but Redis
developers recommend it even RDB, because it is very convenient for database backup,
speeding up the initialization process restart and avoid AOF errors.
It should also be noted that, Redis allows not to use the feature of storing information
in hard drive (no RDB, no AOF), and also allows to use both these features on same
instance. However, when restarting the server, Redis will use AOF for dataset
reconstruction initially, by AOF will ensure no data loss better than RDB.

4. Install and Demo


4.1. Install Redis

We need to check the appropriate machine configuration for installation

20
Download the Redis installation file on Windows, we need to download the .msi
installation file or the .zip file (Redis only support with win x64).
(https://github.com/MicrosoftArchive/redis/releases)

 Run file

21
 Select Next

 Click “I accept the terms in the License Agreement” and Next


22
 Select the path to the drive you want to store and select “Add Redis
installation”
directory to the FATH environment variable”. Select Next

23
 Enter Port for Redis or use default port. Then click Next.

 Click Next

24
 Click Install

 Click finish

25
 Open Command Prompt and run command redis-cli

o 127.0.0.1 là địa chỉ IP của máy.


o 6379 là port mà Redis đang sử dụng.

 Run command ping:

o If PONG is returned, redis is installed and running successfully

 Run command: exit


 And run command: redis-server -port 1000

o Server redis run with port 1000

26
4.2. Redis Cache
Cache is cache memory, a temporary data storage area on the device, which stores
certain types of data.
For example, if you save your facebook username and password on your phone, the
next time you access it, your phone will automatically log in to facebook without having
to re-enter your password and username. That's because the information is already cached.

Thus redis cache is an open source code used to create a server to be used as a cache.
Using redis cache is suitable for systems with frequently accessed data by users, it helps
to speed up data access.

4.3. Feature or Redis Cache


For frequently accessed data items, the redis cache has a response time of less than a
millisecond. It also allows users to easily scale data to accommodate high loads without
increasing backend. This saves a lot of money.

27
People often use redis cache to cache websites, cache with commonly used files such
as images, metadata files. In addition, it is also used as a data query result cache when
searching, a session cache.

4.4. Advantages when use Redis Cache

 Database storage on redis cache has many advantages as follows:


o Reduce data access latency because data access speed will be faster.
o Increase device and application productivity
o Reduce the load on the device's database
 Redis supports most connection protocols and programming languages. Supported
languages include: Java, javaScript, Python, PHP, Perl, Node.js, C/C#, C++, Ruby,
Go.
 Redis creates a highly extensible data storage server that can be shared with many
different processes, applications and servers.
 Redis cache is easier and simpler to use because redis allows to write fewer lines of
code to store, access and use data on the application.

28
 Redis is an open source, so the source code is constantly being updated by users,
the ability to improve the quality of the source code is higher. Errors are also
quickly handled.

4.5. Caching Strategies


4.5.1. Cache Aside
 When the application needs to read data from the database, it checks first if the
cache contains the data it needs.
 If yes (cache hit), return the data to be queried.
 If the data is not available in the cache (cache miss), the application will get the
data from the database.

4.5.2. Read Through


Similar to cache aside, but here, getting data from the database when cache misses is
the cache's (usually supported by an independent cache provider or library).

29
4.5.3. Write Through
The data is written to the cache and then saved to the database. When used together
with the read through method, it helps to keep the data consistent, without having to use
cache. invalidation techniques.

4.5.3. Write Back


The data is written to the cache and then saved to the database. When used together
with the read through method, it helps to keep the data consistent, without having to use
cache invalidation techniques.

30
4.6. Redis Demo
Demo app that shows session & cache management for a Node.js app using express-
sessions and connect-redis.
Session: The app queries an API for ML-generated craft beer names and displays
them on the page. There is a session management panel that displays session ID, time
until the session expires, and the number of beer names viewed in that session.
Cache: The app fetch/query data and save it to redis for caching.

Make sure you have have Redis server running locally (and mysqld):
redis-cli

31
redis-server

npm install
npm run dev

32
Then in your browser, go to http://localhost:3000. It should look something like this:

In file routes/repos.js, we defined:


Function getRepos:
Purpose: Get the repository number of a certain Github user.
API: https://api.github.com/users/${username}
After fetching the data, write the obtained data to Redis.
Cache function: This is the middleware to check if the data is being cached in Redis or
not.

33
Here, using the cache aside strategy, if there is no data in the cache check, the application
will switch to getting the data through the API.
The TTL(Time-To-Live for Caching) is set to a random value between (3600, 4200).

Result
1st query:
After the first query with the username “nqkhanh2002” entered, we see that the repos runs
in 583ms. After being queried for the first time, the results will be saved to the redis
server.

34
2nd query:
Querying again with the username "nqkhanh2002" as above, we see that the query time is
nearly 20 times faster because instead of getting data from the api, the application gets
data from redis.

Check the data on Redis:

Open the Redis CLI, type the command KEYS * to retrieve all the keys that are being
saved, see that Redis is saving the key as nqkhanh2002.
Conclusions
From the demo above, we can clearly see the effect of Redis in increasing data retrieval
speed. Using Redis as a cache is suitable for systems that have frequently accessed data
by users. In addition to being used as a cache, Redis has countless other useful
applications that we should learn more about.

35
The github link for the demo above: https://github.com/nqkhanh2002/redis-caching (This
link includes the Redis app demo to save the session).
5. Job distribution
Job Tuấn Vũ Minh Khôi Quốc Khánh
Find references 30% 30% 40%
Find redis 40% 30% 30%
Write a report 40% 40% 20%
Demo 20% 20% 60%
Design Slide 40% 40% 20%

36

You might also like