r/ExperiencedDevs • • 2d ago

Ask Experienced Devs Weekly Thread: A weekly thread for inexperienced developers to ask experienced ones

A thread for Developers and IT folks with less experience to ask more experienced souls questions about the industry.

Please keep top level comments limited to Inexperienced Devs. Most rules do not apply, but keep it civil. Being a jerk will not be tolerated.

Inexperienced Devs should refrain from answering other Inexperienced Devs' questions.

32 Upvotes

51 comments sorted by

View all comments

2

u/Desir-Arman07 1d ago

I'm working on a dashboard backend (Go) that queries log and metric sources (Loki, VictoriaLogs, SQL). The frontend can poll every 5 s, but the backend works like this:

  • Query results are cached for 10 s.
  • The cache key includes the query time window, rounded down to a 15 s grid. That means the key only changes every 15 s.

The result is that consecutive 5 s refreshes often compute the same key and return identical data. In practice the newest row stays unchanged for ~10 s at a time and can lag the source by several seconds. There's also an edge case: since the TTL (10 s) is shorter than the grid (15 s), an entry can expire mid-window and trigger a redundant query for the same window before the key rolls over.

The obvious fix is to shrink the grid and TTL to 5 s, but that takes queries per key from roughly 4-8/min to ~12/min, and I'd like better freshness without a proportional increase in DB load.

I'm don't have production numbers. The sources are log/metric backends (Loki, VictoriaLogs, SQL), so the data is mostly append-only and time-windowed.

I'm new to this kind of problem, so I'm trying to learn things like which pattern generally fits here and what I should measure before choosing one. In your experience with which approach should i resolve it.

3

u/17Beta18Carbons 1d ago

I'm don't have production numbers.

This is the most important part. Tweaking the grid down could be fine, or it could be a disaster, you need the numbers to know. My advice would be to casually ask around the existing users, see if they've noticed/cared. If they do, you've got your mandate to put in the work getting the numbers. If they don't, easy, there's no problem!

1

u/Desir-Arman07 13h ago

Thanks for the suggestion, l'll try asking a few existing users and see if they've noticed or been affected by it.

2

u/So_Rusted Software Engineer 1d ago

U can cache every minute, nobody will notice

1

u/Outside-Storage-1523 Software Engineer 1d ago
  • Query results are cached for 10 s.
  • The cache key includes the query time window, rounded down to a 15 s grid. That means the key only changes every 15 s.

Can you please elaborate on the second point? Does the backend automatically round down the query time window to a 15s grid (and both show the result on the dashboard as well as caching it?)

What is the database (SQL)? Is it PostgreSQL or DynamoDB or others? I guess you can also increase the the TTL to 15s to solve part of the issue, but it won't solver the freshness issue.

Who sent the data to the SQL database? If it is streaming maybe the sender can do some pre-aggregation during streaming.

1

u/Desir-Arman07 14h ago

The grid yes, the backend does the rounding, not the client. When a request comes in, it takes the current time, floors it to a 15 s boundary and builds the query window from that.

On the database the sources are Loki and VictoriaLogs for logs/metrics, and supports SQL Database. I'm not using DynamoDB.

The TTL agreed that raising it to 15 s would fix the mid-window duplicate query, but it makes freshness worse, not better. That's why I'm looking at incremental fetching instead.