life is too short for a diary




Getting Started with Redis: Concepts, Commands, and Monitoring

Tags: redis nosql python

Author
Written by: Tushar Sharma

Redis is a in-memory NoSQL database mainly used for caching. It's a key-value store which means each unique keys map to a value which could be either a stirng, list, set, hash, blob, JSON or any other data. Each keys are unique and can be queried.

Connecting to a Redis Cluster

To connect to a Redis cluster securely using the command line, you can use the redis-cli tool with TLS and authentication:

Common Redis Operations

Common redis operations across different data types:

String Operations

Strings are the simplest Redis data type. A Redis string value can be up to 512 MB in size

List Operations

Redis Lists are implemented as linked lists, optimized for fast insertions and deletions from both ends.

Hash Operations

Hashes are maps between string fields and string values, ideal for representing objects.

Set Operations

Sets are unordered collections of unique strings. They support powerful set algebra operations.

Sorted Set Operations

Sorted Sets are similar to Sets but with an associated score that allows ordering.

Key Management & Expiration

Redis allows setting time-to-live (TTL) on keys and inspecting their status.

Monitoring Redis Memory Usage

Use the INFO MEMORY command to inspect memory consumption:

Subscribing to Keyspace Events

Redis provides a pub/sub mechanism to monitor events such as key updates, deletions, and expirations. To subscribe to all events under the myApp namespace:

Automating Event Monitoring with Python

You can automate monitoring of Redis keyspace events using Python and the redis library. The following script connects to Redis, subscribes to a pattern, and logs events to both a file and the console:

Sample Output:


comments powered by Disqus