Integrating TimescaleDB with Laravel for High-Performance Data Handling

blog-banner

Why TimescaleDB?

TimescaleDB is a powerful extension for PostgreSQL, specifically built to handle time-series data efficiently.

It provides:

  • Hyper tables: For time-based data, scale millions of rows with ease.
  • Compression: Reduce the amount of storage required without compromising query speed.
  • Continuous Aggregates: Real-time insights derived from precomputed views.

It is simpler to incorporate sophisticated data-handling features into your web application when you combine this with Laravel, a framework renowned for its simplicity.

Benefits:

  • Effective Time-Based Data Management:
    • TimescaleDB ensures simplified organization of huge datasets by managing data according to time intervals, such as daily or monthly, using chunks and hypertables.
    • A hypertable is a logical table designed specifically for time-series data that looks and works like a standard PostgreSQL table. It automatically divides data according to time (and possibly space) into chunks, which are smaller, easier-to-manage units.
      Effective Time-Based Data Management
  • Optimized for Scalability:
    • A dedicated schema (_timescaledb_internal) efficiently manages large volumes of time-series data, ensuring seamless scalability as data grows. It enables high-performance storage and retrieval, making it suitable for extensive datasets.
  • Extra Features Over PostgreSQL:
    • TimescaleDB adds sophisticated features like chunks and hypertables for time-series data management, while preserving PostgreSQL's essential capabilities.
  • Third-Party Integration:
    • TimescaleDB is adaptable and compatible with current tools since it allows connections with third-party database applications such as Grafana for visualization, Prometheus for monitoring, and Apache Kafka for real-time data streaming.
  • Fast Query Response:
    • TimescaleDB's quick query answers allow for real-time insights and analytics, which makes it perfect for time-sensitive applications like IoT data analysis, financial trading platforms, and monitoring systems. Large volumes of time-series data may be swiftly retrieved and processed, allowing organizations to improve user experiences, increase system reliability, and make decisions more quickly.
    • Example of how TimescaleDB optimizes time-series queries compared to MySQL.
      • MySQL:
        • MySQL lacks built-in optimizations for time-series data, so we use GROUP BY with DATE_FORMAT() to aggregate data by hourly intervals:
          • SELECT DATE_FORMAT(recorded_at, '%Y-%m-%d %H:00:00') AS hour, AVG(temperature) AS avg_temp FROM sensor_data WHERE recorded_at >= NOW() - INTERVAL 1 DAY GROUP BY hour ORDER BY hour;
          • Works for small datasets but slows down on large-scale time-series data.
          • Requires additional formatting functions.
      • TimescaleDB:
        • TimescaleDB simplifies time-series queries with time_bucket(), which is designed for efficient aggregation and retrieval:
          • SELECT time_bucket('1 hour', recorded_at) AS hour, AVG(temperature) AS avg_temp FROM sensor_data WHERE recorded_at >= NOW() - INTERVAL '1 day' GROUP BY hour ORDER BY hour;
          • Optimized for large-scale time-series data.
          • Easier to write and maintain.
          • Faster query execution using hypertables and chunking.

Why Use TimescaleDB with Laravel?

  • Seamless PostgreSQL Integration:
    • TimescaleDB is an extension of PostgreSQL, which is already supported by Laravel via Eloquent ORM. This means you can work with TimescaleDB just like a normal database without additional setup.
  • Efficient Time-Series Querying
    • Laravel’s query builder can take full advantage of TimescaleDB’s time_bucket() function for optimized aggregations, replacing complex GROUP BY queries in MySQL.
  • Automatic Data Retention & Compression
    • TimescaleDB automatically partitions data using hypertables, ensuring high performance even with billions of rows.
    • Laravel apps handling IoT logs, event tracking, or financial data can use automatic data retention policies without writing custom cleanup scripts.
  • Scalable Storage & Performance
    • MySQL struggles with millions of time-series records.
    • TimescaleDB efficiently manages large datasets by automatically partitioning data into smaller chunks.
    • Ideal for Laravel applications dealing with IoT, monitoring logs, financial data, etc.

Key Differences: TimescaleDB vs Other Databases

Aspect TimescaleDB PostgreSQL/
MySQL
InfluxDB MongoDB
Purpose-Built for Time-Series Data Yes No Yes No
Scalability and Performance Yes No Yes No
Data Compression Yes No Yes No
Continuous Aggregates Yes No No No
Integration with SQL Yes No No No
Ideal for Time-Series Use Cases Yes No Yes No
Open-Source and Community Yes Yes Yes Yes

CTA Laravel TimescaleDB Setup

  1. Go to timescale console by clicking URL (https://console.cloud.timescale.com/) and sign in with your email address
  2. After successfully login you will be redirected on the dashboard of the timescale, you'll see a screen that looks like the screenshot below:
  3. Click on Add option for “Time series and analytics” and simply click on “Save and continue”.
    Time series and analytics
  4. In next step select region and click on “Save and continue”
    Save and continue
  5. Next select compute size and click on “Save and continue”.
    compute size list
  6. Next select appropriate environment and availability and click on “Save and continue”.
    appropriate environment
  7. You can change the service name by clicking on edit icon then click on “Create service” button.
    Create service
  8. After creating service, you will be redirect on next step for the download config file
    download config file
  9. Here is all the configuration to set in the .env file in the laravel project.
    configuration list
  10. After setting this configuration in the .env file you will connect with the database and run migrations or import the data
    You can see your all the services in the dashboard
    services list

Create hypertables and chunks

  1. Create users table:
    Create users table
  2. Create hypertable of user on created_at column Create hypertable Right now, we don’t have any chunks created for users hypertable
    chunks list
  3. Now Insert the data into user's table dataHere we can see that one chunk is created
    Here we can see that one chunk is created see that one chunk is created Again created new users
    Again created new users When a new user is created, a new chunk is generated because the value of created_at differs from the previous entry, and this difference exceeds the defined interval of 5 minutes. As a result, a new chunk is automatically created to accommodate the data.
    Create a new user See created user

Create a data retention policy

Drop data automatically once a predetermined amount of time has passed. Timescale automatically schedules a background process to remove old chunks when you specify a data retention policy.

  1. Select the hypertable to which you wish to apply the policy. Choose the duration for which you wish to retain data before discarding it. The users hypertable in this example keeps the data for a full day.
  2. Call add_retention_policy:
    • SELECT add_retention_policy(users, INTERVAL '24 hours');

Real-World Use Cases

  • IoT Applications: Handle sensor data from devices like smart thermostats.
  • Finance: Track and analyze stock market trends in real time.
  • E-commerce: Monitor website traffic, sales, and user behavior.
  • Back-End Data Management System: Robust time-series data storage combined with seamless management of large-scale database services allows for effective querying, analysis, and long-term data retention for applications.

Success Story: Migration from Core PHP to Laravel

One of our recent projects involved seamlessly migrating a client’s legacy Core PHP application to Laravel, enabling better performance, scalability, and security. Our team modernized the architecture, optimized database interactions, and enhanced the overall user experience- ensuring business continuity zero downtime.

PHP system
  • Challenge: Outdated Core PHP system with limited scalability.
  • Solution: Laravel migration for improved performance and future proofing.
  • Result: Enhanced security, better code maintainability, and faster application speed

Read the full success story here: Migration from Core PHP to Laravel.

Conclusion

Managing large volumes of time-series data is seamless with TimescaleDB and Laravel. Pairing them with Laravel Development Services ensures scalability, efficiency, and simplicity—whether you're working on analytics, finance, or IoT applications. Get started today and take your application to the next level!

Contact us

For Your Business Requirements

Text to Identify Refresh CAPTCHA
Background Image Close Button

2 - 4 October 2024

Hall: 10, Booth: #B8 Brussels, Belgium