Author: Brian Sam-Bodden
Create the skeleton for the course’s Spring Boot application, configuring all dependencies and launching a suitably configured Redis instance for the course.
In this lesson, you will learn:
- How to create and configure your Spring Boot application to use Redis
- How to add a docker-compose file as a Git submodule to your application to configure and run Redis
- How to add a Git submodule with the application’s sample raw data
- How to launch a Redis instance suitable for the course
If you get stuck:
- The progress made in this lesson is available on the redi2read github repository at https://github.com/redis-developer/redi2read/tree/course/milestone-1
If you want to start with a fully configured Spring Boot application and wish to skip the “from scratch” part, skip to the section “Quick Start For Those In a Hurry.”
- In your browser, launch the Spring Initilizr
- Fill out the form using the following settings:
- Project: Maven Project
- Language: Java
- Spring Boot: 2.4.4
- Project Metadata:
- Description: Bookstore Web Services Powered by Redis
- Package Name:
- Packaging: JAR
- Java: 11
- Spring Web
- String Data Redis (Access + Driver)
- Spring Security
- Spring Boot DevTools
Alternatively, you can use the following URL: http://bit.ly/spring-initlz-redi2read to launch the Spring Initilizr fully configured for the project.
Click on GENERATE and Initializr will generate the zip file for the project and prompt you to download it to your local machine.
Unzip the downloaded file (named
redi2read.zip) in a suitable directory. For example on a Mac:
Now that you have downloaded and unzipped the generated Spring Initializr application, change directories to the app directory (./redi2read) and inspect the contents.
You should have a full Maven-based Spring Boot application. Since we are going to be adding submodules, let’s put the application in git:
In the previous code block we initialized a git repo in our redi2read application directory. Adding an empty git commit facilitates future Git rebase operations.
Now that we have a basic Spring application, we need to configure a Redis instance for our application. We've created a docker-compose file in a git repository that you can add as a submodule to your application.
Git submodules, if you haven't encountered them, are a way to nest a git repo within another git repo. Read the Git documentation on submodules to learn more.
You’ll find this file in the redismod-docker-compose repo, hosted under the redis-developer organization in Github. The Repo contains a Docker Compose file configured to use the Redis Labs “redismod” image, which is a Docker image that includes Redis built with select Redis Labs modules. In particular, we will use the RedisJSON, RediSearch, and RedisGraph modules while building this application.
Modules included in the container:
- RediSearch: a full-featured search engine
- RedisGraph: a graph database
- RedisTimeSeries: a time-series database
- RedisAI: a tensor and deep learning model server
- RedisJSON: a native JSON data type
- RedisBloom: native Bloom and Cuckoo Filter data types
- RedisGears: a dynamic execution framework
To add the submodule, we use the git submodule command at the root of the project:
The command adds the contents of the passed repository under the folder named “docker,” which the command will create. At this point you can commit the changes made to the repository.
The sample data for the application is also provided as a separate Git repo that we will add to our main repository:
The raw data for our application consists of a collection of JSON documents, representing books and users.
The collection of JSON documents for book data comes from https://books.googleapis.com. Each file is labelled with the keyword/category used to construct it and an incrementing integer (some categories have more books than others) resulting in JSON such as:
We have an ID (the book ISBN), title, subtitle, description, page count, price, currency, language, thumbnail URL, a link to more information (the “infoLink” field), and an array of authors.
The user data was randomly created using https://randomuser.me, which generated JSON like:
To add the submodule use the following command:
This submodule will load under the folder
src/main/resource/data within the application folder to facilitate the loading of the data from the classpath. At this point you can commit your work so far to the git repo.
To skip past all the steps outlined above (e.g., initializing a Spring app, adding submodules for data, etc.), simply clone the application with the
--recurse-submodules option, and check out the branch named
Let's open a terminal window and change directories to the
/docker directory under our project’s repository.
To start our Docker Redis image, we’ll use the docker-compose command as follows:
You should see output similar to this:
The redis-cli is the Redis command line interface, a simple program that allows you to send commands to Redis, and read the replies sent by the server, directly from the terminal.
Find the container name using
docker container ls:
With the name
docker_redis_1 we can run bash in the container and then start the redis-cli:
If you already have the Redis CLI installed locally, you can launch it by simply entering:
The most basic of commands, PING is used to “ping” the server. If we get a response back, we know that the Redis server is alive and kicking:
Since we have a customized Redis instance which includes several Redis modules, we can check for which modules have been installed:
Keys are unique identifiers, whose value can be any one of the data types that Redis supports. These data types range from simple Strings, to Lists, Sets, and even Streams. Each data type has its own set of behaviors and commands associated with it.
Keys in a Redis database are distributed in a flat keyspace. Redis does not enforce a schema or naming policy for keys. This provides great flexibility, with the organization of the keyspace being the responsibility of the developer. We'll look at ways of managing this later in the course. Redis is famous for being an extremely fast database. This speed comes from the fact that it stores and serves all data from RAM rather than disk. Redis is durable, so your data will be persisted but all reads will be from a copy of the data held in RAM. This makes Redis an excellent choice for applications that require real time data access.
Here's some resources that we think will be useful to you as you discover Redis:
- redis.io - the official website of open source Redis.
- Redis Enterprise Cloud - a fully managed cloud service from Redis Labs with a free plan for getting started.
- The official Redis Docker image.
- For a comprehensive introduction to Redis, we recommend taking a look at the RU101: Introduction to Redis Data Structures course at Redis University. In this free online course, you’ll learn about the data structures in Redis, and you’ll see how to practically apply them in the real world.