How to collect and process time-series data using Redis and Python
Time series data is a series of data stored in the time order (Example: Stock performance over time). Industries today are collecting and analyzing time-based data more than ever before. Traditional databases that rely on relational or document data models are designed neither for storing and indexing data based on time, nor for running time-bucketed aggregation queries. Time-series databases fill this void by providing a data model that optimizes data indexing and querying by time.
Time Series is a Redis feature, It allows Redis to be used as a fast in-memory time series database designed to collect, manage, and deliver time series data at scale. The Time Series feature shares the performance and simplicity aspects of Redis. Under the hood, it uses efficient data structures such as Radix tree to index data by timestamp, which makes it extremely fast and efficient to run time-aggregate queries.
Python Client for Time Series with Redis
As of redis-py 4.0.0, the redistimeseries-py library is deprecated. It's features have been merged into redis-py. Please either install it from pypy or the repo.
Follow the steps below to get started with Time Series with Redis in Python:
Step 1. Create a free Cloud account
Create your free Redis Enterprise Cloud account. Once you click on “Get Started”, you will receive an email with a link to activate your account and complete your signup process.
For a limited time, use TIGER200 to get $200 credits on Redis Enterprise Cloud and try all the advanced capabilities!
Step 2. Create Your database
Choose your preferred cloud vendor. Select the region and then click "Let's start free" to create your free database automatically.
If you want to create a custom database with your preferred name and type of redis, click "Create a custom database" option shown in the image.
Step 3. Verify the database details
You will be provided with Public endpoint URL and "Redis Stack" as the type of database with the list of features that comes by default.
Step 4.Installation
$ pip install redis
Step 5. Create a script file
import redis
r = redis.Redis(host='redis-18386.c110.qa.us-east-1-1.ec2.qa-cloud.redislabs.com', port=<add port>, password=<add password>)
r.ts().create(2, retension_msecs=5)
Save the above file with a name "ts.py".
Step 6. Executing the python script
python3 ts.py
Step 7. Monitor the Redis database
1648389303.557366 [0 20.127.62.215:59768] "TS.CREATE" "2"
References
- Getting Started with Time Series with Redis
- Learn more about Time Series with Redis in the QuickStart tutorial.
- Build Your Financial Application on Time Series with Redis
- How to Manage Real-Time IoT Sensor Data in Redis
- Introduction to Time Series with Redis - Video