Image for post
Image for post
Redis setup

Setting up a standalone Redis Instance

Chinmaya Pati
Dec 20, 2020 · 3 min read

Redis is a very popular in-memory database when it comes to caching. This post covers the basic configurations for setting up a Redis instance in docker.

Directory Structure

Create a directory named and ensure the following file structure.

redisdb/
├── data/
├── redis.conf

The directory will contain the snapshots taken over time (as and/or ).

The file will contain the instance configurations.

Configurations

The below configurations are what seems important in general. Rest can be left to defaults.

Network

bind 0.0.0.0       # bind to all available interfaces
protectedmode no # since we're not serving over localhost
port 6379 # default port
tcp-backlog 511 # high backlog value helps avoid slow client
connection issues, helpful in
high-request-per-second environments
timeout 0 # disable idle connection timeout
tcp-keepalive 300

General

Since we’re going to run it inside a container, no background process of service is required.

daemonize no       
supervised no
databases # valid between 1-16. Indexed with 0

Snapshotting

These settings will generate snapshots of the data from memory and save them with file extension under the directory that we created above based on the specified policies.

save 900 1                      # save <seconds> <writes>
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes # stop accepting writes if
snapshotting fails. This limits
the risk of losing data.

Security

The basic security involves setting a server password.

requirepass PASSWORD # set the server password

However, if the data is sensitive, it is risky to just authenticate using a server password. Because a single command can delete everything. It can be mitigated with an Access Control List (ACL) which is a list containing username/passwords with the allowed list of commands.

Memory Management (IMPORTANT)

Since Redis is an in-memory database, you don’t want it to flood the node it is running on thereby compromising all other services. A suitable memory management policy saves the day.

Eviction policies

Eviction Policies

: return errors when the memory limit was reached and the client is trying to execute commands that could result in more memory to be used.

: evict keys by trying to remove the less recently used (LRU) keys first, in order to make space for the new data added.

: evict keys by trying to remove the less recently used (LRU) keys first, but only among keys that have an expiry set, in order to make space for the new data added.

: evict keys randomly in order to make space for the new data added.

: evict keys randomly in order to make space for the new data added, but only evict keys with an expire set.

: evict keys with an expire set, and try to evict keys with a shorter time to live (TTL) first.

The list of available eviction policies can be found here.

Persistence

Append Only File () is an alternative persistence mode that provides much better durability compared to since it creates something similar to a point in time snapshot.

Redis Persistence

Some problems associated with files are,

  • The AOF file size is proportional to the number of writes. It will get bigger and bigger.
  • The bigger the AOF file is, the longer the server will take to restart.
  • So we need a way to compact the AOF from time to time. Again in a non-blocking way, while the server is running receiving read and write queries.

Hence, consider this option only when you have very sensitive data and can not afford to lose.

Here is the link to a detailed comparison between and option.

Start the instance

Run the following command under the directory since the local path must be absolute in the option.

Start the Redis Instance

Test the Connection

Test Redis Connection

Nerd For Tech

From Confusion to Clarification

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store