Time Series
with Spring

🤩 Motivation

  • MongoDB Time Series

    • since v5                     13th July 2021

    • improved in v6         19th July 2022

    • official docs had no small example

    • barely any blogs and videos

Christoph Ahlers

🎤 Presenter

Christoph Ahlers

  • Software Engineer / Consultant since 2018
  • at OpenValue since October 2021

Christoph Ahlers

🪜 Agenda

  • What is Time Series data?
  • Time Series in MongoDB
  • Live Demo with Spring
  • Tips and Pitfalls

Christoph Ahlers

🙋 Are you using Time Series data?

What is Time Series data?

🤔

Christoph Ahlers

🤔 What is Time Series data?

Christoph Ahlers

finance

weather

IoT

Finance photo by Maxim Hopman on Unsplash

🤔 What is Time Series data?

  • 🕝 timestamp
  • 📏 measurement    (one or more values)
  • 🗺️ meta info              (device, data type)

Christoph Ahlers

Time Series in

Christoph Ahlers

Time Series in

  • used like Collections
  • brings new special pipeline operations

  • Benefits vs Collections:

    • reduced complexity for developers

    • improved performance

      • improved query efficiency

      • reduced disk usage

      • reduced I/O for read operations

      • increased cache usage

      • 💰cheaper

Christoph Ahlers

Granularity

  • Data is always stored in Documents
  • granularity bundles values into single Document:
  • improves performance, reduces size
  • should match unique source's entry rate
  • ⚠️ can only be raised!

Christoph Ahlers

granularity bundled time span
            seconds (dflt)              1 hour
            minutes            24 hours
            hours            30 days

🪣🪣 Buckets

🪣

🪣

🪣

  • average, min, max, first, last, count, ...

Christoph Ahlers

Live Demo with Spring

Christoph Ahlers

📦 Dependencies

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<properties>
  <testcontainers.version>1.17.5</testcontainers.version>
</properties>
...
<dependency>
   <groupId>org.testcontainers</groupId>
   <artifactId>mongodb</artifactId>
   <version>${testcontainers.version}</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.testcontainers</groupId>
   <artifactId>junit-jupiter</artifactId>
   <version>${testcontainers.version}</version>
   <scope>test</scope>
</dependency>
  • Tests:

Christoph Ahlers

Live Demo with Spring

Christoph Ahlers

Tips & Pitfalls

Christoph Ahlers

😇

😈

😇 Tips

  • use MongoDB Compass to test/explore/verify

Christoph Ahlers

😈 Pitfalls

  • Spring Data MongoDB is not feature complete
    • expireAfterSeconds missing
    • no check if Time Series collection
    • Index creation via Spring Annotations (data.mongodb.auto-index-creation: true)
      creates Collection, not Time Series!
  • Index must use actual fields of metaField
                                             metaData.deviceId
  • currently only read-only

Christoph Ahlers

Conclusion

📚

Christoph Ahlers

📚 Conclusion

  • Granularity
  • integration in Spring
    • Time Series definition
    • Collection & Index creation via MongoTemplate
    • MongoRepository
    • Queries, Aggregations
      • $bucket
  • pitfalls and useful information

Christoph Ahlers

⭐️ Disc Space Improvements

  • 4 million measurements
  • 2+ GB   vs   370 MB

MongoDB Time Series with Spring

By Christoph Ahlers

MongoDB Time Series with Spring

  • 12