Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: blog post about caching count(*) #2674

Merged
merged 1 commit into from
Nov 25, 2024
Merged

Conversation

chronark
Copy link
Collaborator

@chronark chronark commented Nov 25, 2024

Summary by CodeRabbit

  • New Features
    • Introduced a blog post titled "Approximating Row Counts," discussing strategies for managing API key counts in the Unkey platform.
    • Outlined challenges and solutions for optimizing API key counting, including a proposed schema and SQL query examples.

Copy link

vercel bot commented Nov 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 4:21pm
engineering ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 4:21pm
play ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 4:21pm
workflows ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 4:21pm
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 4:21pm

Copy link
Contributor

coderabbitai bot commented Nov 25, 2024

📝 Walkthrough

Walkthrough

A new blog post titled "Approximating Row Counts" has been added to the project, specifically in the file approximating-row-counts.mdx. This post addresses the challenges of managing API key counts in the Unkey platform, particularly for larger customers. It introduces a database schema for key_space and keys, details an SQL query for counting keys, and outlines an approach for optimizing this process. Additionally, it describes the addition of two new columns to the key_space table for tracking approximated key counts and their last update timestamps.

Changes

File Path Change Summary
apps/www/content/blog/approximating-row-counts.mdx New blog post added discussing API key counting challenges and solutions, including database schema and optimization strategies.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant API
    participant Database

    User->>API: Request key count
    API->>Database: Check last updated timestamp
    alt If stale data
        Database->>Database: Execute query to count keys
        Database-->>API: Return updated count
    else
        Database-->>API: Return cached count
    end
    API-->>User: Return key count
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

github-actions bot commented Nov 25, 2024

Thank you for following the naming conventions for pull request titles! 🙏

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (4)
apps/www/content/blog/approximating-row-counts.mdx (4)

37-37: Fix typo in text

Change "out query" to "our query"

-As you can see, many `keys` belong to a single `key_space` and out query in question is:
+As you can see, many `keys` belong to a single `key_space` and our query in question is:
🧰 Tools
🪛 LanguageTool

[uncategorized] ~37-~37: “out” (outside) seems less likely than “our” (belonging to us).
Context: ...eysbelong to a singlekey_space` and out query in question is: ```sql SELECT cou...

(AI_HYDRA_LEO_CP_OUT_OUR)


39-40: Consider adding index recommendation for performance

The count(*) query would benefit from an index on key_space_id. Consider adding a note about the importance of proper indexing for performance optimization.

CREATE INDEX idx_keys_key_space_id ON keys(key_space_id);

79-88: Consider adding optimistic locking

The asynchronous update could lead to race conditions if multiple requests try to update the count simultaneously.

Consider adding optimistic locking:

 c.executionCtx.waitUntil(
   db.primary
     .update(schema.keySpace)
     .set({
       sizeApprox: keySpace.sizeApprox,
       sizeLastUpdatedAt: keySpace.sizeLastUpdatedAt,
     })
-    .where(eq(schema.keySpace.id, keySpace.id)),
+    .where(
+      and(
+        eq(schema.keySpace.id, keySpace.id),
+        eq(schema.keySpace.sizeLastUpdatedAt, keySpace.sizeLastUpdatedAt - 60_000)
+      )
+    ),
 );

94-94: Consider implementing the background refresh now

While the current solution works, implementing a background refresh job would provide more consistent performance and reduce the likelihood of multiple concurrent count updates.

Would you like me to help design a background job implementation using a cron-based approach? This could include:

  • Efficient batch processing of stale counts
  • Error handling and retry mechanisms
  • Monitoring and alerting setup
🧰 Tools
🪛 LanguageTool

[typographical] ~94-~94: After an expression of time at the start of a sentence, a comma is usually used.
Context: ...where we always count all rows. In the future we might want to run a cron job to refr...

(IN_THE_PAST_COMMA)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c509b20 and e648da9.

⛔ Files ignored due to path filters (1)
  • apps/www/public/images/blog-images/covers/approximating-row-counts.png is excluded by !**/*.png
📒 Files selected for processing (1)
  • apps/www/content/blog/approximating-row-counts.mdx (1 hunks)
🧰 Additional context used
🪛 LanguageTool
apps/www/content/blog/approximating-row-counts.mdx

[uncategorized] ~37-~37: “out” (outside) seems less likely than “our” (belonging to us).
Context: ...eysbelong to a singlekey_space` and out query in question is: ```sql SELECT cou...

(AI_HYDRA_LEO_CP_OUT_OUR)


[uncategorized] ~44-~44: Possible missing preposition found.
Context: ...tions We were looking at a few options how to fix this: 1. Caching the count as p...

(AI_EN_LECTOR_MISSING_PREPOSITION)


[uncategorized] ~53-~53: Use a comma before ‘and’ if it connects two independent clauses (unless they are closely connected and short).
Context: ...r dashboard just as easily as in our API and it behaves the same. Adding these two ...

(COMMA_COMPOUND_SENTENCE)


[grammar] ~62-~62: Did you mean “because”?
Context: ...space` table, we get the count for free cause we're not doing an extra query. To keep...

(CAUSE_BECAUSE)


[uncategorized] ~65-~65: You might be missing the article “the” here.
Context: ...asynchronously. Here's how we do it in drizzle: ```ts const keySpace = await db.quer...

(AI_EN_LECTOR_MISSING_DETERMINER_THE)


[style] ~92-~92: The phrase ‘a lot of’ might be wordy and overused. Consider using an alternative.
Context: ...ght kick off many queries to refresh if a lot of requests come in at the same time, but ...

(A_LOT_OF)


[typographical] ~94-~94: After an expression of time at the start of a sentence, a comma is usually used.
Context: ...where we always count all rows. In the future we might want to run a cron job to refr...

(IN_THE_PAST_COMMA)

Comment on lines +70 to +78
if (keySpace.sizeLastUpdatedAt < Date.now() - 60_000) {
const count = await db
.select({ count: sql<string>`count(*)` })
.from(schema.keys)
.where(and(eq(schema.keys.keySpaceId, keySpace.id), isNull(schema.keys.deletedAt)));

keySpace.sizeApprox = Number.parseInt(count?.at(0)?.count ?? "0");
keySpace.sizeLastUpdatedAt = Date.now();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling and improve count parsing

The current implementation has several areas that could be improved:

  1. Missing error handling for the count query
  2. The count parsing could fail if the result is undefined
  3. Potential race condition if multiple requests update the count simultaneously

Consider this more robust implementation:

 if (keySpace.sizeLastUpdatedAt < Date.now() - 60_000) {
-  const count = await db
-    .select({ count: sql<string>`count(*)` })
-    .from(schema.keys)
-    .where(and(eq(schema.keys.keySpaceId, keySpace.id), isNull(schema.keys.deletedAt)));
-
-  keySpace.sizeApprox = Number.parseInt(count?.at(0)?.count ?? "0");
-  keySpace.sizeLastUpdatedAt = Date.now();
+  try {
+    const count = await db
+      .select({ count: sql<string>`count(*)` })
+      .from(schema.keys)
+      .where(and(eq(schema.keys.keySpaceId, keySpace.id), isNull(schema.keys.deletedAt)));
+
+    const parsedCount = Number.parseInt(count?.[0]?.count);
+    if (Number.isNaN(parsedCount)) {
+      console.error('Failed to parse count result:', count);
+      return;
+    }
+
+    keySpace.sizeApprox = parsedCount;
+    keySpace.sizeLastUpdatedAt = Date.now();
+  } catch (error) {
+    console.error('Failed to update key count:', error);
+  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (keySpace.sizeLastUpdatedAt < Date.now() - 60_000) {
const count = await db
.select({ count: sql<string>`count(*)` })
.from(schema.keys)
.where(and(eq(schema.keys.keySpaceId, keySpace.id), isNull(schema.keys.deletedAt)));
keySpace.sizeApprox = Number.parseInt(count?.at(0)?.count ?? "0");
keySpace.sizeLastUpdatedAt = Date.now();
if (keySpace.sizeLastUpdatedAt < Date.now() - 60_000) {
try {
const count = await db
.select({ count: sql<string>`count(*)` })
.from(schema.keys)
.where(and(eq(schema.keys.keySpaceId, keySpace.id), isNull(schema.keys.deletedAt)));
const parsedCount = Number.parseInt(count?.[0]?.count);
if (Number.isNaN(parsedCount)) {
console.error('Failed to parse count result:', count);
return;
}
keySpace.sizeApprox = parsedCount;
keySpace.sizeLastUpdatedAt = Date.now();
} catch (error) {
console.error('Failed to update key count:', error);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant