W7D2 - Redis POC with AWS Setup & Spring [18June]

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Live Diagram:

Caching - Redis Cache.

To speed up the system.

Cache is a temporary data storage areas. Used for faster access of data.
UI - Server
Instead of calling server everything, take data which was previously saved

Backend - cache - DB.

What kind of data needs to be cached..

Employee information (database)

1. backend server calls DB. 101 - name - phone - email


2. Stores data in cache
(101 - name - phone - email)
3. Perform processing. And return to UI.
4. All future request, read data from cache..
5. What id DB data is modified??
Your backend service is out of sync with DB..

Cache only that data, which will not change that frequently.

For dynamic data, i want to use cache..


- everyting db is modified, you need to build system in such manner, that the cache is
also updated simultaneously.

Steps for caching:

1. Backend system checks data in cache.


2. If data is not available, then go to DB, load data, put in cache, return data
3. All future request, will find data in cache.

system runs faster.

1. Cache data - Out of sync


- keep only static, or less frequently changing data in cache.
- If data changes frequently, whenever db is modified, parallely the cache should be
updated.
- Eviction rules, automatically caches should be cleaned up.
LRU (Least recently Used.)
size of cache 10 elements..
11th element is added, you have to remove previous. LRU
- Keep an expiry for cache data.

2 types of cache:
In memory
You store the data internally in service memory
java class, some datastructure
- when restart happens, then its all deleted.
all cache is empty.. and starts filling slow. till all data is again in cache..
system will perform slowly.
- shares same memory as your application..
which might impact the execution of your functional system.
- Autoscaled nodes will not have cache data.. so its needs to be somehow
populated.. till that time, service will work slowly..
- Out of sync issue.. needs to be handled in all instances..

External Cache
You dont save data in your spring boot service.
Rather keep it externally.

Database MySQL.

Install MySQL
Some tool to connect to mysql
CLI -
(DBeaver/MySQL workbench) - GUI
Write SQL queries - to operate on that DB.
How data is stored in DB
how to work with that DB.
Some special coding, for java application to talk to DB.

Git
SourceTree - GUI
git bash - CLI

Working with Redis


1. Install redis & start
AWS itself EC2 machine.

- Installed redis in EC2 using cli commands..


- allowed connections from outside.
- Updated security groups.
- status up & running.

yum update -y
2 yum install redis -y
3 amazon-linux-extras install redis6
4 systemctl start redis
5 systemctl enable redis
6 systemctl status redis

9 vi /etc/redis/redis.conf
10 systemctl restart redis

12 systemctl status redis

2. Connecting using 'redis-cli' from EC2 machine.


type command
redis-cli

CLI: redis-cli
GUI

3. Working with Redis data structures.


How data is stored in Redis
- Multiple datastructures for storing data
values, list, set, sortedset, hash,...

how to work with that Redis


sql (insert,....) => DB
In redis how to manipulate data
some commandsa,
RPUSH, LPUSH, HSET, HGET, HGET,

Run below command in EC2


=> redis-cli

List operation
RPUSH mylist "first"
RPUSH mylist "second"
RPUSH mylist "third"
LRANGE mylist 0 -1 # Returns all elements in the list

Hash operations
HSET myhash name "John Doe"
HSET myhash phone "123123" email "john@doe.com"
HGETALL myhash
HGET myhash email

4. Some special coding, for java application to talk to Redis.


Java => Database: JdbcTemplate
Java => External HTTP Call: RestTemplate
Java => Redis RedisTemplate

What functional usecase we have.

POC - "Proof of Concept"....


LIST, HASH<key, value>

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

package com.cpt.payments.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new
RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName("13.126.88.92");
redisStandaloneConfiguration.setPort(6379);
redisStandaloneConfiguration.setDatabase(0);

return new LettuceConnectionFactory(redisStandaloneConfiguration);


}

@Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}

You might also like