# YugaByte DB Redis API now supports a native TIME SERIES data type

**URL:** <https://forum.yugabyte.com/t/yugabyte-db-redis-api-now-supports-a-native-time-series-data-type/62>\
**Category:** General\
**Created:** [November 1, 2017, 6:32am UTC](https://forum.yugabyte.com/t/yugabyte-db-redis-api-now-supports-a-native-time-series-data-type/62 "2017-11-01T06:32:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pritam.damania](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/pritam.damania/32/23_2.png) [@pritam.damania](https://forum.yugabyte.com/u/pritam.damania)\
**Post date:** [November 1, 2017, 6:32am UTC](https://forum.yugabyte.com/t/yugabyte-db-redis-api-now-supports-a-native-time-series-data-type/62/1 "2017-11-01T06:32:24Z")

</div>

# Introduction to time series data

We have spoken to several developers that have a need to model time series like data (for use cases such as metrics, stock quote feed, or user-activity) in Redis. Time series data has the following characteristics:

1. There are a number of metric “time series”, for example cpu\_usage and memory\_usage are two separate time series.
2. Each data point in one time series is a (timestamp, value) pair which denotes the observation recorded at a point in time.
3. Generally, data arrives in increasing timestamp order and just needs to be appended to the end of the time series, but sometimes data points may arrive out of order and need insertion in the middle.
4. Data is usually read by specifying a range of timestamps.
5. There is often a time-to-live associated (TTL) with the data points, this depends on the timestamp of the data point. For example, for a given series, we may want to purge each data point in the time series if 2 days has elapsed since the timestamp of that data point.

# Modeling time series data with vanilla Redis

There are predominantly two ways this is achieved today in Redis - using Sorted Sets and plain key-values. Both of these get complicated to model, and are not performant. Sorted sets have the following drawbacks:

1. One way to model the data in Sorted Sets would be to use the score as the timestamp for the event and the value as the measurement at that time. For example:

2. Sorted Sets have the overhead of keeping two maps and updating both of them as the data is mutated:

Additionally, lack of finer-grained TTL in Sorted Sets make purging data tedious. Application driven purging logic to delete expired entries adds further load to the system.

Recognizing these issues in modeling time series data on Redis, we have worked closely with various customers to address this use-case in Redis based on their requirements.

# Time Series datatype support in YugaByte

We are happy to announce that YugaByte Redis now supports a native time series ( **TS** ) data type that can handle all the above requirements with ease of data modelling and very high performance. Also, since YugaByte’s Redis offering is “Redis as a elastic database”, you do not need to worry about persisting your data in a separate data store!

**TS** is essentially a sorted map from int64 to a string/value, and can be implemented as a single timestamp\_to\_value\_map: which maps from a “timestamp” to a _single_ object/value.

## Sample commands:

```auto
// In this example, the timestamp is encoded as an integer in yyyymmddhhmm format.
> TSAdd cpu_usage 201708110501 “80%” 201708110502 “60%” 201708110503 “90%”
“OK”
> TSGet cpu_usage 201708110501
“80%”
> TSRangeByTime cpu_usage 201708110501 201708110503
1) 201708110501 
2) “80%”
3) 201708110502
4) “60%”
5) 201708110503 
6) “90%”
> TSRem cpu_usage 201708110501
“OK”
> TSGet cpu_usage 201708110501
(nil)

```

We’ve recently finished implementing these commands and details can be found on our Open Source github repository:

- [TSAdd and TSGet](https://github.com/YugaByte/yugabyte-db/commit/da34b43ee1016f464159314b510f012ae990eebf)
- [TSAdd with TTL support](https://github.com/YugaByte/yugabyte-db/commit/d5bd3a2978fd9bd7aa5b443d96b19e87c6cd0c97)
- [TSRangeByTime](https://github.com/YugaByte/yugabyte-db/commit/4bfbac15fbacc0634dcd689ee25cc9bca1d00f0c)

**Stay tuned for a more detailed blog post and documentation on this new type!**

---

<div class="post-metadata">

**Author:** ![dorian\_yugabyte](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/dorian_yugabyte/32/206_2.png) [@dorian\_yugabyte](https://forum.yugabyte.com/u/dorian_yugabyte)\
**Post date:** [March 8, 2023, 9:15am UTC](https://forum.yugabyte.com/t/yugabyte-db-redis-api-now-supports-a-native-time-series-data-type/62/2 "2023-03-08T09:15:13Z")

</div>


