Bot

Building a Real-Time Database Notification Service with Change Tracking in C#

Building a Real-Time Database Notification Service with Change Tracking in C#


We will explore the implementation of a powerful database notification service in C#, capable of providing real-time updates on data changes in a SQL Server table. By leveraging SQL Server’s Change Tracking feature, we can efficiently monitor and respond to modifications without the need for continuous polling, resulting in a more responsive and resource-efficient application.

SQL Table Change Notification is required for real-time data synchronization and event-driven applications. It provides a mechanism for applications to be notified when changes occur in a specific SQL database table, allowing them to take appropriate actions based on those changes. There are several reasons why SQL Table Change Notification is essential:

In distributed systems or scenarios where multiple instances of an application are running,

  • Modern applications require real-time data updates to provide timely and accurate information to all users;
  • Keeping data in sync across all nodes can be challenging;
  • Caching is used to improve application performance, but it can lead to stale data if not managed properly;
  • Where multiple users can access and modify the same data to resolve conflicts;
  • Where applications offer subscription-based services in delivering timely and relevant information to subscribers;

Faça o download do código: 


Change Tracking In SQL Server

Change Tracking in SQL Server is a lightweight, built-in feature that helps track changes made to user tables in a database. It captures the fact that a row has been inserted, updated, or deleted, without storing the actual data changes. Instead, it records the minimum information needed to identify the modified rows.

Enable Change Tracking

Enabling Change Tracking in SQL Server involves a straightforward process. Here are the steps to enable Change Tracking for a specific table in a database:

 --SQL Script for enable change tracking at database level.

ALTER DATABASE ECommerceDB
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 1 DAYS, AUTO_CLEANUP = ON);

--SQL Script for enable change tracking at table level.

ALTER TABLE User  
ENABLE CHANGE_TRACKING  
WITH (TRACK_COLUMNS_UPDATED = ON ) 

Retention Period (Optional): By default, SQL Server retains change tracking information for specified days.

Monitor Change Tracking

After enabling Change Tracking, the system automatically maintains the change tracking information for the specified table(s). You can query the change tracking information periodically to synchronize changes with your application logic or for replication purposes.

Remember that Change Tracking captures minimal information about the changes (PK values and version numbers), so you may need additional queries to get the full details of the modified rows if required. Also, enabling Change Tracking on a table incurs some performance overhead, albeit relatively low, depending on the frequency of data changes.

Access Tracked table records:

SELECT ct.* FROM CHANGETABLE(CHANGES <dbname>,<lastversionno>) ct

Get Change Tracking Current Version:

SELECT ISNULL(CHANGE_TRACKING_CURRENT_VERSION(), 0) as VersionCount

Project Overview :




This database notification service is encapsulated within the SqlDBNotificationService<TChangeTableEntity> class. This generic class is designed to work with any table entity, allowing flexibility and easy integration into various applications. The generic type constraint ensures that the entity class must be a reference type with a default constructor, providing a consistent structure for handling the table data.

The SqlDBNotificationService class implements the IDBNotificationService<TChangeTableEntity> interface, making it easy to integrate into existing projects. It also exposes events for error handling and receiving changed data. Service monitors database ChangeTable for specified interval and returns changed records for specified table till the last time monitored.


Usage:

  1. Initializing the Service: Developers can create an instance of the SqlDBNotificationService class by providing the table name, database connection string, and an optional initial version for resuming monitoring.
  2. Subscribing to Events: Applications can subscribe to the OnError and OnChanged events to handle errors and receive notifications when changes occur in the table.
  3. Starting and Stopping Monitoring: The service can be started by calling the StartNotify() method, which initiates the polling process. The service can be stopped using the StopNotify() method.
Features:

  • Asynchronous Event-Based Model: The notification service employs a pub-sub model, meaning that when changes occur in the monitored table, the service sends asynchronous notifications to the registered subscribers (i.e., the application).
  • Query-Based Notifications: Unlike monitoring the entire table, our service allows the application to define a specific SQL query representing the data of interest. When relevant changes affect the query’s result set, a notification is triggered.
  • Low Resource Utilization: Our service optimizes resource consumption by avoiding constant polling. Instead, it leverages SQL Server’s Change Tracking, which efficiently tracks and delivers notifications only when necessary.
  • Flexible Initialization: Developers can specify the table to monitor, the connection string to the SQL Server database, an optional initial version for resuming monitoring, and a time interval for polling updates.
  • Error Handling and Resilience: The service includes robust error handling to ensure the application remains stable even in the face of unexpected exceptions. If an error threshold is reached, the service gracefully stops and notifies subscribers.
  • Easy Integration: The SqlDBNotificationService class implements the IDBNotificationService<TChangeTableEntity> interface, making it easy to integrate into existing projects. It also exposes events for error handling and receiving changed data.


Conclusion:

By utilizing the powerful combination of C#, SQL Server Change Tracking, and our custom SqlDBNotificationService, developers can easily add real-time database monitoring to their applications. The service's efficient event-driven model ensures timely and accurate updates without excessive resource consumption, enhancing application responsiveness and overall user experience.




Comments