未加星标

Using Redis in Microservices Architecture

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

In this article, we will look closer at a fascinating open source project. Meet Redis! You may be familiar with Redis already, in that case, you may be interested in the different use cases it has for microservices architecture. Read on to see how this “ in-memory data structure store, database, cache, and message broker” can make your system better!

What is Redis?

I already revealed that in the introduction. To repeat (using redis.io own words):

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.

In practice, that means that we can use Redis to store and retrieve data. We can do that with a very good performance, potentially (not necessarily) sacrificing some durability of that data.

Redis is also an open source project- that’s great news! We will be able to get hands-on without any obstacles in our way.

Redis and Docker

As with most things these days, I really recommend that you use Docker for playing with Redis. If you have never used Docker before I wrote ashort intro.

I have used Redis version 4.0.8 in this article. If you don’t care about which version of Redis you run, but you want the latest, you can start the container with the following command:

docker run --name my-redis -d redis

This will expose port 6379 (the default) for connecting to Redis.

You can also run your Redis with a persistent storage to a volume /data if you wish:

docker run --name my-redis -d redis redis-server --appendonly yes

If you don’t know about volumes yet, Docker has a good documentation about them .

Speaking of documentation, if you actually consider running Redis in production, I strongly advise you read the official docker repo documentation for Redis .

You now know how to run Redis with Docker. For standalone installation, please check redis.io/download this should be up to date!

Redis and Spring Boot

Redis is extremely popular and plays nice with most libraries. Since I enjoy and recommend Spring Boot, I will show you how easily the two integrate.

The only dependency you need to add to your POM file is:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>

One of the main benefits of using spring-boot-starter-data-redis is that you don’t have to do any configuration yourself. Spring autoconfiguration shines once again. You can define properties in your application.properties as per usual in Spring Boot.

Let’s make a simple application where we can store notes and retrieve them using Redis.

First, we need Note object defined:

package com.e4developer.springredis; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; @RedisHash("Note") public class Note { @Id private final String id; private final String message; public Note(String id, String message) { this.id = id; this.message = message; } public String getId() { return id; } public String getMessage() { return message; } }

This object is annotated as @ReisHash so it can get persisted in Redis using NoteRepository :

package com.e4developer.springredis; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository public interface NoteRepository extends CrudRepository<Note, String> {}

The one last thing to add is the NoteController to bring these things together:

package com.e4developer.springredis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import java.util.Optional; @Controller public class NoteController { @Autowired private NoteRepository noteRepository; @PostMapping("/notes/{id}") public ResponseEntity<String> setNote(@RequestBody String message, @PathVariable("id") String id){ Note note = new Note(id, message); noteRepository.save(note); return new ResponseEntity<>(HttpStatus.CREATED); } @GetMapping("/notes/{id}") public ResponseEntity<optional<Note>> readNote(@PathVariable("id") String id){ Optional<Note> note = noteRepository.findById(id); return new ResponseEntity<>(note, HttpStatus.CREATED); } }

Now you can save and retrieve Notes using GET and POST requests! We made use of Spring Data here that I looked at before .

If you want to work more closely with Redis and Spring, you should familiarize yourself with the Spring Data Redis documentation .

Since using Redis is nice and easy, let’s look at the most common use-cases for it in the microservices world.

Redis Microservices Cache

One of Redis’s strongest suits its the caching capability it provides. If you need to temporarily store data and share it between microservices Redis is the go-to choice.

With the fine-grained eviction policies at your disposal and stellar performance, it is hard to see why you would need anything else?

This is where many developers under-appreciate it. While Redis is a great caching solution, it offers much more…

Redis In-memory Database

As I already mentioned, Redis describes itself as “in-memory data structure store, database” first. It is in fact, an outstanding NoSQL product.

Many services can benefit from using in-memory databases. They often significantly outperform normal databases on most metrics.

A common use case would be a large amount of read-only data that is rarely modified. You can have a traditional database maintaining the record and in-memory database providing read-only performance. This is just one of many in-memory database use cases.

Redis Database

If you need to have your data persisted- don’t fret. Redis also have you covered.

Redis uses a clever mix of:

RDB snapshots of your dataset at specified intervals AOF persisted log of every write operation received by the server

To provide speed and data reliability as required. This is an interesting topic and the Redis website contains a good comparison of RDB and AOF pros and cons .

One thing is clear- if you wish to use Redis for persistence, the project is more than ready to provide a robus

本文数据库(综合)相关术语:系统安全软件

tags: Redis,data,springframework,import,org,gt,id,lt
分页:12
转载请注明
本文标题:Using Redis in Microservices Architecture
本站链接:https://www.codesec.net/view/585012.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(79)