#1 What I've learned

February 01, 2024

Our work at PostHog is public, so it's pretty cool I get to share my recent learnings here. Here's one for this week: Just because you can access the database directly from the frontend doesn't mean you should.

At PostHog, one of our major products is Experiments (aka A/B tests). Once you launch an experiment, you start seeing the results - for example, "Which of the A/B variants is performing better?"

I was implementing a feature showing if the A/B test has recently received data. This would help the user assess if their experiment is set up correctly. Having the luxury of direct access to the database from the frontend, I quickly wrote a query to group and count relevant data. After that, I happily rendered the results on the frontend.

So what was the issue? Well, my colleague Neil wasn't happy with my solution (PR link below). Rather than adding a new query, he suggested moving this logic to the existing experiment results API call.

Why?

  1. Most importantly, my feature and the experiment results are closely related. Here's another way to reason about it: I'm asking for the A/B test results -> relevant data hasn’t been ingested yet -> that's the exact reason why there are no test results -> the API response should immediately tell me what's happening (missing data!).

  2. Moreover, I was unaware the backend was already handling part of this logic. So, I was essentially doing double work. I didn't need to query the database again. I just had to tweak the existing API logic a bit. This would give me all the information I needed.

  3. Finally, my implementation was adding another API call to the backend. Another open connection to our already complex app. Over time, these connections to the backend will pile up. We have 8 products today, but we want to have 30 in the future. So we must be mindful of our performance capital on both the server and the client.

Following this suggestion, I was able to add the feature without introducing new complexity.

Link to the PR: https://github.com/PostHog/posthog/pull/19969