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

Add ability to load triemux from Content Store #490

Merged
merged 17 commits into from
Dec 4, 2024

Conversation

theseanything
Copy link
Contributor

@theseanything theseanything commented Oct 28, 2024

This PR adds the ability to build and use a triemux from Content Store's postgres database instead of MongoDB. Moving away from MongoDB, which is running an outdated and unsupported version, resolves multiple challenges, including the inability to shift workloads to ARM and difficulties in maintaining a local development environment. By leveraging PostgreSQL, the Content Store can become the single source of truth for routes, simplifying the system architecture and removing the dependency on MongoDB.

This change also paves the way for retiring the Router API, allowing for a leaner, more efficient architecture. Instead of querying an API endpoint on the Content Store, this approach directly queries the PostgreSQL database. This is beneficial because the large number of routes involved would add significant latency if serialized and deserialized through an API layer. A risk to note is that schema changes in the Content Store could impact route loading in Router, though such changes are infrequent, and ongoing work may eventually make it possible to remove Router altogether.

Traffic control is managed through an environment variable, CSMUX_SAMPLE_RATE, which accepts values between 0.0 and 1.0. This variable sets the sampling rate for serving requests via the triemux built from the Content Store. A value of 0.0 disables triemux loading from the Content Store entirely, while higher values allow gradual adoption, enabling canarying of traffic to minimize rollout risks.

The Content Store PostgreSQL database URL is specified through the CONTENT_STORE_DATABASE_URL environment variable. Route re-loading is triggered automatically by PostgreSQL’s LISTEN/NOTIFY mechanism, which responds to inserts, updates, or deletions in the content_items or publish_intents tables. alphagov/content-store#1340

This resolves: #497

@theseanything theseanything force-pushed the populate-triemux-from-cs branch 3 times, most recently from 4eb7a0b to 5aec294 Compare November 11, 2024 16:11
@theseanything theseanything marked this pull request as ready for review November 11, 2024 16:47
@theseanything theseanything requested a review from a team November 11, 2024 16:47
This struct and type contains the logic for routes contained previously
in the Router API and be used to help choose which handler to use.
Used for interacting with content store's postgres db.
This implements the logic to query the content store and populate the
triemux.
This makes a connection pool to the database url specified with he env
var CONTENT_STORE_DATABASE_URL.
This provides a simple feature flag to turn off routing using content
store and allows us to canary a percentage of requests to use the
content store mux. A value of 0.0 will switch off the feature entirely
and prevent the Router from trying to create a connection pool.
This uses that the CsMuxSampleRate to controller the percentage of
requests served by the mux built using the content store data.
This adds a helper function to update csmux with a new version
containing updated routes.
This fixes a golint error.
This is a fail safe in-case the listen/notify mechanism.
@theseanything theseanything force-pushed the populate-triemux-from-cs branch from eaa62b5 to 0c38590 Compare November 25, 2024 12:13
This ensures we don't reload more than we need to. If a notification
already exists to reload, then we don't need to send another
notification, as all updates will be captured in the next reload.
@theseanything theseanything force-pushed the populate-triemux-from-cs branch from 6de67a8 to e54299c Compare December 2, 2024 13:50
lib/load_routes.go Outdated Show resolved Hide resolved
This are stylistic changes and missing test for invalid json in the
details column.
Copy link
Contributor

@nimalank7 nimalank7 left a comment

Choose a reason for hiding this comment

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

LGTM!

@theseanything theseanything merged commit 90ec177 into main Dec 4, 2024
4 checks passed
@theseanything theseanything deleted the populate-triemux-from-cs branch December 4, 2024 15:45
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.

Use Content Store for populating Triemux
2 participants