Running Redis in a Docker container
Published: 2022-01-16 13:05:16

{% youtube YyR6fqKHpec %}

[Redis](https://redis.io) stands for **RE**mote **DI**ctionary **S**erver. It is an open source, fast NoSQL database written in ANSI C and optimized for speed. Redis is an in-memory database that means that all data in Redis is stored in RAM, delivering the fastest possible access times to the data for both read and write requests.

[Redis](https://developer.redis.com) is written in ANSI C and optimized for speed. It is designed and implemented for performance. Redis compiles into extremely efficient machine code and requires little overhead. It uses a (mostly) single threaded event loop model that optimally utilizes the CPU core that it is running on. The data structures used internally by Redis are implemented for maximum performance and the majority of data operations require constant time and space.

Redis is based on the key-value model. In Redis, the data is stored and fetched from Redis by key. Keybased access allows for extremely efficient access times and this model maps naturally to caching, with Redis providing the customary GET and SET semantics for interacting with the data. Also, it supports multi-key operations. Several of Redis’ commands operate on multiple keys. Multi-key operations provide better overall performance compared to performing the operations one after the other, because they require substantially less communication and administration.

Redis is designed to be a remote, network-attached server. It uses a lightweight TCP protocol that has client implementations in every conceivable programming language. Redis is basically an open source, in-memory,  data structure store that can be used as a cache, primary database and message broker. It is a multi-model database that supports search, graph, real-time streaming, analytics and many other use cases beyond that of a simple data store. With over 52,000+ GitHub stars, 20,000+ forks and over 500+ contributors,  Redis  is quite popular among the developers. Redis gives developers building applications the tools and information they need in a very efficient manner. Redis today can be deployed on-premises, across clouds, hybrid environments as well as over the Edge devices flawlessly. 

Here's a quickstart guide to get Redis running in a Docker container:

## Ensure that Docker is installed

```
docker -v
```

## Create a dedicated Docker network

```
docker network create -d bridge redisnet
```

## Run Redis container

```
docker run -d -p 6379:6379 --name myredis --network redisnet redis
```

## Install redis-cli

```
brew install redis-cli
```

## Enter into Redis-cli

```
redis-cli
```

## Accessing the keys

- Create a generic key like set a1 100 and get a1 100

```
set a1 100
get a1
```

## Importing user keys

Let us import a user database( 6k keys). This dataset contains users stored as Redis Hash.

The user hashes contain the following fields:

```
user:id : The key of the hash.
first_name : First Name.
last_name : Last name.
email : email address.
gender : Gender (male/female).
ip_address : IP address.
country : Country Name.
country_code : Country Code.
city : City of the user.
longitude : Longitude of the user.
latitude : Latitude of the user.
last_login : EPOC time of the last login.
```

## Cloning the repository

```
git clone https://github.com/redis-developer/redis-datasets
cd redis-datasets/user-database
```
 
## Importing the user database:

```
redis-cli -h localhost -p 6379 < ./import_users.redis
``` 
 
## Open a new terminal and run the monitor CLI
 
```
monitor
```
 
## Flushing up the database
 
```
flushdb
``` 

## cleaning up container
 
```
docker stop myredis
```

## Further References

- [Redis Developer Hub](https://developer.redis.com)
- [Redis on Kubernetes](https://developer.redis.com/create/kubernetes)
- [Redis on AWS](https://developer.redis.com/create/aws)