Running Redis in a Docker container

Ajeet Singh Raina - Jan 16 '22 - - Dev Community

Redis stands for REmote DIctionary Server. 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 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
Enter fullscreen mode Exit fullscreen mode

Create a dedicated Docker network

docker network create -d bridge redisnet
Enter fullscreen mode Exit fullscreen mode

Run Redis container

docker run -d -p 6379:6379 --name myredis --network redisnet redis
Enter fullscreen mode Exit fullscreen mode

Install redis-cli

brew install redis-cli
Enter fullscreen mode Exit fullscreen mode

Enter into Redis-cli

redis-cli
Enter fullscreen mode Exit fullscreen mode

Accessing the keys

  • Create a generic key like set a1 100 and get a1 100
set a1 100
get a1
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Cloning the repository

git clone https://github.com/redis-developer/redis-datasets
cd redis-datasets/user-database
Enter fullscreen mode Exit fullscreen mode

Importing the user database:

redis-cli -h localhost -p 6379 < ./import_users.redis
Enter fullscreen mode Exit fullscreen mode

Open a new terminal and run the monitor CLI

monitor
Enter fullscreen mode Exit fullscreen mode

Flushing up the database

flushdb
Enter fullscreen mode Exit fullscreen mode

cleaning up container

docker stop myredis
Enter fullscreen mode Exit fullscreen mode

Further References

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .