Using Redis

Day 3 - Just a few days before the start of the Hackathon, what we did is learn about Redis.

What can I do with Redis?

Redis is an open source in memory NoSQL database that was created in 2009.

When you already have a database like MongoDB, SQL Server, MySQL, and parts of your website or your application need to get data quicker than your database can, Redis is usually the go for solution.

This is achieved by hosting part of the data in memory as a cache, as opposite to traditional databases which would use disk.

Some of its use cases are:

  • storing sessions in Redis,
  • keeping lists of most recent visitors,
  • describing your contacts or friends graph,
  • implementing real-time workflows with the pub/sub pattern.

Redis enables web developers to build scalable interactive applications. It is used by large companies such as Twitter, Instagram, Pinterest, StackOverflow or GitHub.

To get started with Redis, you can just visit,

If you prefer managed services, these are some modern solutions for your production application:

Redis in Details

Traditional key/value store only handle string type values. Redis is what we call a key/data structures store. This makes it closer to a database. The different data types it can handle are: strings, lists, sets, sorted sets, hash, Bitmaps, HyperLogLogs.

Redis is very fast for both reads and writes because it runs in memory, while having disk persistence as a backup. It can run in clusters of servers where large amounts of memory are available.

Using Redis

Redis can be used a cache and as a second database. Redis helps you scale while keeping in memory the data retrieved from the database.

On some cases, it can be used as a primary database, for example in use cases where only the fresh data matters and when it is only necessary to keep it small periods of time as in streaming.

It can be useful for the Pub/Sub patterns when Internet users have their browsers open, and data is sent automatically without them to refresh the page.

Because of its single threaded nature, it has no lock, and can respond to the load of many requests.

With high-speed data, where your goal is to have a highly responsive website for processes that traditional databases were not initially designed for.

Using with Python

The Python package redis-om is an ORM to interact with the Redis database, it uses pydantic to describe your data models and eases operations for modeling and querying data in Redis with modern Python applications.

Another interesting library is brainix/pottery if you want to deal with remote data structures when your application needs to implement locks, key value stores, maintain user lists such as e-commerce carts.

Using the REDIS prompt

Here are some commands to manipulate Hashes in Redis,

HGET

This command can be used to retrieve fields from a specific key.

> HGET THM:Paper:1609.05486 paper_id
"1609.05486"

> HGET THM:Paper:1609.05486 authors
"M. Kachouane (USTHB), S. Sahki, M. Lakrouf (CDTA, USTHB), N. Ouadah\n  (CDTA)"

HSET

This command is used to set a field in the hash stored at key to value.

> HSET THM:Paper:

FT.SEARCH

This command can be used to search the index with a textual query.

> FT.SEARCH papers
    "@year:[2021 2022] => [KNN 20 @vector $vec_param AS vector_score]
    K 20
    "query_vector" <BLOB>

FLUSHDB

This command can be used to delete all keys from a database.

> FLUSHDB

Redis Stack and RediSearch

Redis provides a modern extension that enables querying, indexing and full-text search.

Among other features, RediSearch support multi-field queries, aggregation, exact phrase matching, numeric filtering, geo filtering and vector similarity semantic search on top of text queries.

References

links