Skip to content

Commit 2f9bf91

Browse files
authored
Cache organizations and events longer in case of error. (#355)
For example, if/when us-east-2 goes down, we can still serve some semblance of data on core website routes. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Optimized HTTP caching policies to improve data retrieval performance and resilience across API endpoints. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 42bc160 commit 2f9bf91

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/api/routes/events.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
ScanCommand,
1010
UpdateItemCommand,
1111
} from "@aws-sdk/client-dynamodb";
12-
import { EVENT_CACHED_DURATION, genericConfig } from "../../common/config.js";
12+
import {
13+
EVENT_CACHED_DURATION,
14+
genericConfig,
15+
STALE_IF_ERROR_CACHED_TIME,
16+
} from "../../common/config.js";
1317
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
1418
import {
1519
BaseError,
@@ -118,7 +122,7 @@ const repeatOptions = ["weekly", "biweekly"] as const;
118122
const zodIncludeMetadata = z.coerce.boolean().default(false).optional().meta({
119123
description: "If true, include metadata for each event entry.",
120124
});
121-
export const CLIENT_HTTP_CACHE_POLICY = `public, max-age=${EVENT_CACHED_DURATION}, stale-while-revalidate=420, stale-if-error=3600`;
125+
export const CLIENT_HTTP_CACHE_POLICY = `public, max-age=${EVENT_CACHED_DURATION}, stale-while-revalidate=${EVENT_CACHED_DURATION * 2}, stale-if-error=${STALE_IF_ERROR_CACHED_TIME}`;
122126
export type EventRepeatOptions = (typeof repeatOptions)[number];
123127

124128
const baseSchema = z.object({

src/api/routes/organizations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
genericConfig,
3434
notificationRecipients,
3535
roleArns,
36+
STALE_IF_ERROR_CACHED_TIME,
3637
} from "common/config.js";
3738
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
3839
import { buildAuditLogTransactPut } from "api/functions/auditLog.js";
@@ -48,7 +49,7 @@ import { retryDynamoTransactionWithBackoff } from "api/utils.js";
4849
import { SKIP_EXTERNAL_ORG_LEAD_UPDATE } from "common/overrides.js";
4950
import { AvailableSQSFunctions, SQSPayload } from "common/types/sqsMessage.js";
5051

51-
export const CLIENT_HTTP_CACHE_POLICY = `public, max-age=${ORG_DATA_CACHED_DURATION}, stale-while-revalidate=${Math.floor(ORG_DATA_CACHED_DURATION * 1.1)}, stale-if-error=3600`;
52+
export const CLIENT_HTTP_CACHE_POLICY = `public, max-age=${ORG_DATA_CACHED_DURATION}, stale-while-revalidate=${ORG_DATA_CACHED_DURATION * 2}, stale-if-error=${STALE_IF_ERROR_CACHED_TIME}`;
5253

5354
const organizationsPlugin: FastifyPluginAsync = async (fastify, _options) => {
5455
fastify.register(rateLimiter, {

src/common/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ const roleArns = {
203203
};
204204

205205
export const EVENT_CACHED_DURATION = 120;
206+
export const STALE_IF_ERROR_CACHED_TIME = 86400; // 1 day
206207

207208
type NotificationRecipientsType = {
208209
[env in RunEnvironment]: {

0 commit comments

Comments
 (0)