r/SQLServer • • 4d ago

Community Share Wire level caching solution for SQL Server - Change Tracking Mode

Hey, I've posted before on some of my custom TDS listener stuff. All custom TDS wire protocol in c#. I think this one is probably my most useful one so far.
Here's a quick demo for anyone interested.

Full Open Source Code (MIT): https://github.com/TroBeeOne/DaffyZoom
Nuget Install and Instructions: https://www.nuget.org/packages/Daffyzoom
Hour long podcast (Ai made it for me) on how it works: https://www.youtube.com/watch?v=mwMDmuLjCCY

What it does

- It solves the problem of 1000+ repeated slow queries that are hitting stale tables. It also helps you identify those slow queries so you can 'actually fix' the indexes, plans, the proper way.

- Sits on the wire in-between client and SQL server

- On the return from SQL Server message it tee's the call, one sync back to client, another async to my caching system. If a repeat query (above 5 second default setting) if tables haven't changed, it now returns instant cache, 0 seconds. if table changed? it reruns the 7 second query and updates the cache on the way back..

- I have data warehouse mode, ttl, just caches everything repeated. with expiration.

- I have change tracking mode (this is the demo I'm showing today). This caches unless a change is made, then it runs the query as normal and refreshes cache.

- Change Tracking Mode requires change tracking enabled on target tables for repeated query. You might think, well, we're now hitting SQL Server 2 times? Yes, but the check is a microsecond. And if you're serving cache from a 10 second query instantly, and ppl are hitting that report at 8am 1000 times, you saved some compute time and made your clients super happy!

- I DO support Entra Service Credentials, but I do not support MFA. I also don't support kerberos yet, I could, but that would require me setting up a domain controller VM, and with little interest so far, no incentive.

Anyway, I haven't tested this in a major production environment yet, so if you do choose to try it, as with anything, test it well and let me know if you find issues.

Here's the demo, cheers. If you try it and run into questions or have issues, let me know.

6 Upvotes

5 comments sorted by

2

u/PX3better 3d ago

Why was Change Tracking your choice? Isn't that synchronous? Why not CDC?

2

u/DataFracking 3d ago edited 3d ago

Great question.

Yes, Change Tracking is synchronous, and that's the reason I chose it.

Change Tracking records each change inside the writing transaction. So once a write commits, DaffyZoom's check on the next hit sees it. CDC is async, so a capture job reads the transaction log afterwards.. possible enough delay the cache would serve rows that are already out of date.

Each cache check hit asks "has anything changed since version n?" Change Tracking stores just the primary key and version of each changed row. CDC copies whole rows, including before and after images for updates, into change tables. ie storage and I/O for data DaffyZoom never reads.

Other:

Change Tracking is in every recent edition, Express included, and every Azure SQL Database tier.

CT needs no Agent job, I think CDC needs SQL Server Agent on-premises right..

Azure SQL Database, CDC needs S3 or higher, or vCore right?

And if you have more of a warehouse setup with static tables ETL'd nightly or something, TTL mode works. It doesn't scan ahead.

1

u/DataFracking 3d ago edited 3d ago

I also pushed my DaffyTee and DaffyDB applications to github. DaffyDB is a TDS Proxy for DuckDB that translates T-SQL. It contains DaffySQL with is a custom SQL Server extension for .NET for DuckDB. DaffyTee is a workload recorder (full, everything)) and debugging utility for SQL Server. https://github.com/TroBeeOne/DaffyTee and https://github.com/TroBeeOne/DaffyDB and the replay module for workload capture: https://github.com/TroBeeOne/DaffyReplay