r/SQL • u/DataFracking • 2d ago
SQL Server Wire level intelligent response 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. I had posted one earlier on the first cut TTL mode, but this demo covers the more sophisticated one, the change tracking mode.
Full Open Source Code (MIT): https://github.com/TroBeeOne/DaffyZoom
Nuget Install and Instructions: https://www.nuget.org/packages/Daffyzoom
Hour long podcast on how it works (it's ~98% correct but it made a couple mistakes): 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. It only caches long queries. It doesn't poll SQL Server for all queries, only the slow ones. It doesn't cache things with getdate() or CRUD related operations obviously. Its all in the documentation / code.
2
u/Discordbothost 1d ago
What happens when a tracked table changes just before a bunch of people open the same report? Does the first request refresh the cache for the others, or do they all run the slow query? That's the case I'd want to see alongside the warm-cache timing for the 8 a.m. rush.
2
u/MongooseWonderful198 1d ago
One warm-cache case I'd test is enabling row-level security after the fill. In the pinned source,
ResolveBatchrejects databases with enabled RLS, butCheckBatchchecks tracked-table changes and the saved objects'modify_date; it doesn't repeat the policy check.What invalidates an existing cached answer if an RLS policy is enabled without changing the table's data? A fixture could warm the cache, enable a restrictive policy, then repeat the query under the affected login and check that the old rows aren't replayed. This is a source review; I haven't reproduced it in SQL Server.