r/SQL • • 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.

3 Upvotes

4 comments sorted by

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.

1

u/DataFracking 1d ago edited 1d ago

If they all hit it exactly at the same time, like 'exact time', they will all hit sql server. It only needs a second or so for the cache to write on the async side. And instant in pulling it pretty much.

Sequence of events.

  1. Slow query (above the default 5 second rule, which is adjustable) is run and tracked and determined that it can be cached. It's not cached yet.
  2. Same exact slow query occurs, it's detected, still must be over the 5 second threshold. When it finishes it writes cache of exact prior response.
  3. Same exact query comes across. It checked every table in the query against change tracking (VERY fast check and this is the first time it's checking)
  4. If the source tables show no change, it returns the cache.
  5. if there was a change, it reruns the query, since it's already a query being tracked, it automatically flushes the former cache and updates with this new result.
  6. The next query (or 1000) that come in, if done a second or two after that call where it has time to write the cache, cache must be done and ready, get the same orchestration from step 3 onward.
    If multiple queries hit at the same time, the engine pools and reuses the microsecond table change check so it's not opening a 1000 connections to check. a microsecond check is way better than 1000 long running queries running in parallel on SQL Server.. but If the cache isn't available or ready, it always safe fails to just call SQL Server as normal. If the cache isn't returned in under a second (I think it was something like that, I can't recall what I had that set at, its in the documenation), it slso fails to SQL Server as well. and always accurate response is primary, speed is secondary.

2

u/MongooseWonderful198 1d ago

One warm-cache case I'd test is enabling row-level security after the fill. In the pinned source, ResolveBatch rejects databases with enabled RLS, but CheckBatch checks 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.

1

u/DataFracking 1d ago

ohh, that's a good one. I ran a quick test, the cache does still get dropped in that case as tracked mode still compares modify_date on every hit, which appears to get updated with that change.. If RLS policy is enabled prior to cache start, it won't cache at all. It checks the db for any, Also worth knowing, in TTL mode (which just expires or is flushed by exec daffyzoom.flush 'dbname' command) it doesn't check that policy, it's not expecting changes so it's cached per mapped login basis.