A Gentle Intro for Redis

Abdullah Abunar
The Startup
Published in
4 min readJul 14, 2020

The Redis website defines its product to be an in-memory data structure store which support strings, hashes, sets and so on.

For the in-memory part, it means that -When the server is started- the processing of the database is done in RAM not in the hard-disk which of-course is much faster as the time for accessing the desk is no longer needed.

Now if the file is loaded in RAM, how does Redis stay persistent?
The persistence is handled in Redis by 2 methods:
- Redis database files (RDB) (activated by default)
- Append-only file (AOF)

First, RDB:
It takes snapshots of the data creating point-in-time copies of the data. It is like make a backups after every specific time or changes in the database. Generally it is perfect for backups.

I find it easy to visualize it as a GitHub Repo that keeps track of your changes but it self-commits after a certain number of changes or some specific time.

Second, AOF:
It depends on logs to reconstruct the database when the server is restarted. When you start your Redis server, AOF keeps track of the changes you make to the database and keeps logs describing it. When the server is restarted, Redis makes sure to reconstruct the original database using these log files.
This way the data is always persistent.
This file is named “Appendonly.aof” and kept in the same folder where you installed Redis.

A last note about data persistence, The 2 methods don’t actually oppose each other and can be used together.

Configuring a Redis server:

The configuration file exists in the same folder where you installed Redis called “redis.conf”.
The file is actually very well documented. It consists of consequent parts, each regarding a specific configuration.

For example: setting the RDB configuration.
Search for “RDB” in the file, you can find an explanation of the concept and an example with the syntax.
The syntax is “save timeInSeconds numberOfKeyChanges”

“save 300 10" means that the Redis server will take a screenshot of the data after 300 seconds or if 10 keys has changed. Either condition will trigger the RDB.

Redis clients:

Redis has clients in many programming languages. Clients are just libraries that allows you to leverage Redis in your programming language of choice.
You can find all the clients on this page of the Redis website.

Redis Data structures:

  • Strings:
    Represents a stream of characters similar to that defined in any programming languages. For composite strings that consist of space use double quotes.
    Some of the commands related to it:
    - set key value: sets a key in the database with given value.
    - get key:
    gets the value of a key from the database.
    - del key:
    deletes key from the database.
    - exists key: checks if the key exists in the database.
    - mset key1 val1 key2 val2: sets multiple keys.
  • Hashes:
    Represents a key-value structure similar to objects(dictionaries) in programming languages.
    Some of the commands related to it:
    - hmset hashName key1 val1 key2 val2: creates a hash and puts 2 pairs of key-values in it.
    - hgetall hashName: returns all pairs.
    - hget hashName key1: gets the value of key1 related to hashName from the database.
    - hexists hashName key1: checks if key1 exists inside hashName.
  • Lists:
    Represents sequential elements, similar to an array in its concept.
    Some of the commands related to it:
    - rpush listName val1 val2: puts val1 and val2 inside listName.
    - lrange listName start stop:
    gets the sequence of element inside listName whose indices lies between start and stop.
    - lpush listName val1 val2:
    puts val1 and val2 in the beginning of listName.
    - lpop listName:
    removes element from beginning of listName.
    - rpop listName:
    removes element from the end of listName.
    - ltrim listName:
    removes a sequence of elements at once.
  • Sets:
    Represents a sequence of unique elements.
    Some of the commands related to it:
    - sadd setName val1 val2 : adds val1 and val2 to setName.
    - smembers setName
    : prints all values in setName.
    - sis setName val1
    : checks if item exists.
    - sadd setName:setName2 val1 val2 : creates set setName2 with 2 values val1 and val2 and puts setName2 as a subset inside setName.
    - scard setName : prints number of elements in setName.
    - sunion setName:setName2 : performs a set union operation between 2 sets.
  • Sorted Sets:
    Same as sets but ordered according to a given score.
    Some of the commands related to it:
    - zadd set1 val1 score1 val2 score2 : adds val1 and val2 to set1 according to the given scores.
    - zrank set1 val1: returns the index of val1.
    - zrangebyscore set1 -inf score1 withscores : returns values and their scores up to a score1.
    - zrange set1 start end:
    returns values between start and end.

Redis Security:

Redis handles its security mainly by its user-defined password in the config file and on the application level designed by developer to prevent unauthorized access using access control lists(ACL).
A further recommendation is to make sure the port and the address that Redis uses are using a firewall against restricted access.
Also binding Redis to only one interface, a single access point (i.e 127.0.0.1) is best practice than using multiple interfaces.

--

--