Spring Boot App connecting to Redis Database for CRUD operations
Redis is used as a database for CRUD operations in this application
#Preqequisites
- Download and install Redis in local machine
- After unzipping the redis jar, open redis-server.exe file to start the redis server. Redis runs on port 6379 on localhost
- Then start the redis-cli.exe to open the client
There are 2 types of Java Redis clients namely
- Jedis (Synchronous and single threaded. Easy to use)
- Lettuce ( uses asynchronous and reactive API. Multithreaded. difficult to use)
#Replication
- Redis Sentinel provides high availability for Redis
- Uses Master Slave concept.
- Only master can take writes
#MessageBroker
- Redis can also be used as Message Broker with Publish and Subscribe
#Steps
- Create a Jedis Connection Fcatory and set it to the RedisTemplate
- Provide redis configuration in application.properties file
- Use hashOperations provided by Spring Boot in the Repository
- Use HashOperations to perform the CRUD operation
- Go to postman and enter the url http://localhost:8080/redis/addUser and enter the payload for the post method
- For update, use http://localhost:8080/redis/update with the payload
- FindAll users, http://localhost:8080/redis/findAll
- Get a user, http://localhost:8080/redis/getUser/1
- Delete a user, http://localhost:8080/redis/delete/1
#Verify
- Check the redis-cli with the command keys *
- Open another cli with the command monitor
- This cli will display all the operations done in the redis server