PromptBase
Close icon
General
Home
Marketplace
Hire
Create
Login
Sell
Explore
🤖 GPT

Realtime Analytics System Design

Realtime Analytics System Design gpt prompt mini thumbnail
2Eye icon
Views
TestedTick icon
TipsTick icon
4 examplesTick icon
Build a scalable, fault-tolerant back-end for real-time analytics. Customize API styles, databases, and processing pipelines for high-velocity data. Perfect for developers seeking solutions to real-time metrics, data spikes, and robust monitoring in performance-critical systems. Includes detailed architecture guidance, code snippets, and fault recovery strategies to streamline development and deployment.
...more
$4.99
Get prompt
After purchasing, you will gain access to the prompt file which you can use with GPT or the app builder. You'll receive 20 free generation credits with this purchase. By purchasing this prompt, you agree to our terms of service.
1 week ago
Related item

Prompt Details

Model
Chat - GPT-4o (gpt-4o)
Token size
748 ($0.03740 / call)
Example input
[e.g., clicks, page views, purchases]: clicks, purchases, product views [e.g., 5,000 events per second]: 10,000 events per second [e.g., average session duration, total revenue per hour]: total revenue per hour, average cart size [e.g., 2 seconds]: 1 second [e.g., RESTful, GraphQL]: RESTful [e.g., PostgreSQL, MongoDB, InfluxDB]: PostgreSQL [e.g., promotional events]: Black Friday promotions [e.g., fault-tolerant]: fault-tolerant [e.g., minimal downtime]: 1 minute [e.g., Apache Kafka, Redis Streams, AWS Kinesis]: Apache Kafka [e.g., Flink, Spark, Celery]: Flink [e.g., dropped events, data duplication]: dropped events [e.g., Prometheus, CloudWatch, Datadog]: Prometheus
Example output
### System Design for Real-Time Analytics Dashboard #### **Architecture Overview** The system consists of three primary layers: 1. **Data Ingestion Layer** - Ingests raw event data using **Apache Kafka**. 2. **Data Processing Layer** - Processes and aggregates metrics in real-time using **Apache Flink**. 3. **Data Storage and Query Layer** - Stores data in **PostgreSQL** optimized for both high write throughput and efficient querying. --- ### **Architecture Components** 1. **Event Ingestion with Kafka** - **Role:** Captures high-velocity event streams, ensuring durability and scalability. - **Mechanism:** Kafka topics are partitioned to handle up to 10,000 events per second with minimal latency. 2. **Real-Time Processing with Apache Flink** - **Role:** Processes event streams, calculates metrics (e.g., total revenue per hour, average cart size), and handles data spikes. - **Mechanism:** - Streams events from Kafka. - Aggregates events using sliding windows (e.g., hourly windows for revenue). 3. **Database Storage with PostgreSQL** - **Role:** Persists aggregated metrics and event data. - **Configuration:** - High-write throughput: Leverages **partitioned tables**. - Efficient queries: Utilizes **GIN indexes** for JSONB data if needed. 4. **API Layer** - **Role:** Exposes RESTful endpoints for querying metrics. - **Framework:** Flask (Python) or Express.js (Node.js). 5. **Monitoring with Prometheus** - **Role:** Tracks system health, metrics, and alerting. 6. **Reliability Layer** - Fault tolerance is ensured by Kafka's replication and Flink’s checkpointing. --- ### **Database Recommendation** - **PostgreSQL** - **Pros:** - High scalability via table partitioning and indexing. - Supports concurrent writes and efficient queries. - **Comparison:** - Compared to NoSQL (e.g., MongoDB), PostgreSQL ensures ACID compliance and richer query capabilities, suitable for structured event data. --- ### **Data Processing Pipeline** 1. **Ingestion:** - Kafka captures raw events from user interactions. - Events are serialized using Avro or JSON and streamed to specific Kafka topics (e.g., `clicks`, `purchases`). 2. **Real-Time Processing:** - Flink consumes Kafka streams, applies transformations, and computes sliding window aggregates. - Flink writes results back to a new Kafka topic for API consumption or directly to PostgreSQL. 3. **Metrics Calculation:** - Total revenue: Aggregates purchase events. - Average cart size: Divides total items in purchases by the count of purchase events. --- ### **Sample Code Snippets** #### 1. API Endpoint for Aggregated Metrics ```python from flask import Flask, jsonify import psycopg2 app = Flask(__name__) @app.route('/metrics', methods=['GET']) def get_metrics(): conn = psycopg2.connect("dbname=analytics user=admin password=secret") cursor = conn.cursor() query = """ SELECT metric_name, metric_value FROM metrics WHERE timestamp >= NOW() - INTERVAL '1 HOUR'; """ cursor.execute(query) results = cursor.fetchall() conn.close() return jsonify(results) if __name__ == '__main__': app.run(debug=True) ``` #### 2. Writing Data to PostgreSQL ```python import psycopg2 def write_event(event): conn = psycopg2.connect("dbname=analytics user=admin password=secret") cursor = conn.cursor() insert_query = """ INSERT INTO events (event_type, event_data, timestamp) VALUES (%s, %s, NOW()); """ cursor.execute(insert_query, (event['type'], event['data'])) conn.commit() conn.close() ``` #### 3. Handling Data Spikes with Flink ```python from pyflink.datastream import StreamExecutionEnvironment from pyflink.table import StreamTableEnvironment, EnvironmentSettings env = StreamExecutionEnvironment.get_execution_environment() settings = EnvironmentSettings.new_instance().in_streaming_mode().build() table_env = StreamTableEnvironment.create(env, environment_settings=settings) table_env.execute_sql(""" CREATE TABLE input_events ( event_type STRING, event_value DOUBLE, ts TIMESTAMP(3), WATERMARK FOR ts AS ts - INTERVAL '1' SECOND ) WITH ( 'connector' = 'kafka', 'topic' = 'event_topic', 'properties.bootstrap.servers' = 'localhost:9092', 'format' = 'json' ) """) table_env.execute_sql(""" CREATE TABLE output_metrics ( metric_name STRING, metric_value DOUBLE, timestamp TIMESTAMP(3) ) WITH ( 'connector' = 'jdbc', 'url' = 'jdbc:postgresql://localhost:5432/analytics', 'table-name' = 'metrics', 'username' = 'admin', 'password' = 'secret' ) """) table_env.execute_sql(""" INSERT INTO output_metrics SELECT 'total_revenue' AS metric_name, SUM(event_value) AS metric_value, TUMBLE_END(ts, INTERVAL '1' HOUR) AS timestamp FROM input_events WHERE event_type = 'purchase' GROUP BY TUMBLE(ts, INTERVAL '1' HOUR) """) ``` --- ### **Fault Tolerance Strategy** 1. **Handling Dropped Events:** - Kafka’s replication factor ensures durability. - Flink retries failed tasks automatically. 2. **Failure Recovery:** - Flink uses state checkpointing. - PostgreSQL allows point-in-time recovery. 3. **System Monitoring:** - Prometheus tracks Kafka lag, Flink processing times, and database performance. --- ### **Architecture Diagram** A diagram representing the architecture will include: - **Kafka**: For ingestion, illustrated as the entry point. - **Flink**: For real-time processing. - **PostgreSQL**: For storage. - **REST API**: For exposing metrics. - **Prometheus**: For monitoring. Let me generate the visualization. Here is the architecture diagram for the real-time analytics dashboard system, showcasing all the components and data flow. Let me know if you need further adjustments or clarifications!
Browse Marketplace