audit: background work under Railway serverless — scheduler for jobs, request-driven telemetry
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 15/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- mongodb, postgresql, python
- Domain
- backend, infrastructure, observability
Research direction
This is a design proposal, not a scoped change: read the open decision list at the end and the linked parent issue #1243 before writing anything. Start from campus/storage/documents/backend/mongodb.py:160 where the default MongoClient is created, to check whether its 10 s topology heartbeat keeps services awake, and read the existing telemetry sampler described in #1243. Done means the maintainers have picked an approach (cron service vs always-on), a schedule, and the retention answer for failed-auth rows, producing smaller follow-up issues.
Written by the indexing model from the issue text.
Description
Part of #1243. Blocks only the work that needs a clock or a timer: the spans retention job (2 years), the telemetry rollups and retention tiers, and the catch-up backfill of span_facts. It also changes the telemetry sampler described in #1243. Everything else there (ingest stamping, metrics endpoints, SDK, CLI, UI) runs on request paths and is not blocked.
Constraint
Railway's serverless deployments sleep on outbound traffic, not CPU. From Deployments → Serverless, read 2026-11-24:
- "Inactivity is based on the detection of any outbound packets, which could include network requests, database connections, or even NTP."
- A service is considered inactive 5 minutes after it stops sending packets. Inactivity is sampled rather than measured continuously, so a service sleeps somewhere between 5 and 10 minutes after its last packet.
- Things that stop a service sleeping include open database connections and "frameworks that report telemetry to their respective services".
- A slept service is woken by traffic from the internet or from another service in the same project over the private network, and "the first request sent to a slept service may return a 502 Bad Gateway response".
So anything that talks on a timer keeps a service awake for good: a heartbeat thread, a connection pool with keep-alives, or a cron that pings a service every minute.
What else the Railway documentation says (checked 2026-11-25)
- Sleep is about outbound packets, not CPU. "Railway automatically detects inactivity based on outbound traffic", and "a sleeping service incurs no compute charges" (Serverless, Free plan). No CPU threshold is involved.
- There is no documented replica autoscaler. "Scale horizontally by manually increasing the number of replicas for a service in the service settings", and removed replicas "are drained gracefully" (Scaling). No scale-down threshold or time window is published anywhere in the docs.
- Railway has native cron jobs. "Railway will look for a defined cron schedule in your service settings, and execute the start command for that service", and "scheduled runs must be at least 5 minutes apart in UTC" (Cron jobs). The docs recommend them "when you want to save resources between task executions, as opposed to having an in-code scheduler run 24/7".
railway cron runtriggers a run off schedule (CLI). - Healthchecks do not keep a service awake. The healthcheck "is only called at the start of the deployment" (Healthchecks).
- An always-on service is cheaper than expected, but not free. RAM is $10 per GB-month, billed on measured use (Pricing), so an idle service holding 100–500 MB costs about $1–5 a month. A cron job costs cents.
Ground rule this implies
No outbound packets except while serving an inbound request. Code may compute, sample and buffer on a timer. It may send only because a request is being handled.
Recommendation for the jobs (revised 2026-11-25 after the docs check below)
Use a Railway cron job. It is the native answer to "a clock without an always-on deployment": a separate scheduled service that starts on a schedule, runs a command, exits, and is billed only while it runs.
- A
campus-audit-jobscron service in each environment, built from this repository with the audit service's database variables. Start commandpython -m campus.audit.jobs(name to be decided), scheduled hourly or more often; Railway's minimum is 5 minutes. It talks to the audit database directly, so it adds no HTTP surface, needs no API key, and never wakes campus.audit. - Catch-up jobs. Every job reads its watermark, processes what is due in chunks, and holds a Postgres advisory lock so runs never overlap. Each run writes a row to a
job_runsledger (started, finished, status, rows processed), which the metrics page shows. - Manual runs for development and tests through the same entry point (
python -m campus.audit.jobs, with--joband--dry-run).
Readers must tolerate a lagging rollup: past the rollup watermark they read raw samples, which are kept for 14 days.
The first version of this issue proposed piggybacking the jobs on request traffic instead. That works and costs nothing, but it puts background threads in request workers and ties freshness to traffic. A cron job is simpler to reason about and to observe, so it replaces the piggyback as the recommendation.
Telemetry under the same rule
Replace the 30-second heartbeat thread described in #1243 with a request-driven outbox:
- Samples are taken at request boundaries, start and end, at most one per 30 s per worker. Each carries the interval it covers, so an idle gap reads as near-zero CPU instead of missing data.
- They are sent from the request's background executor, alongside the span. Nothing is sent on a timer.
- Unsent samples stay in a bounded buffer of about 10 minutes and are retried on the next request. That also covers a 502 from a slept campus.audit.
- A worker's last sample says whether it had requests outstanding. A worker that is silent after that is treated as idle or scaled down.
What this loses is samples during idle periods. Those are near-zero by definition, and sending them is exactly what would stop the service sleeping.
Options considered
- Always-on ("persistent") service with a scheduler thread. About $1–5 a month when idle, and it still needs the advisory lock and catch-up logic because jobs can be killed.
- Railway cron job. Native, billed per run, no inbound traffic to campus.audit. Recommended above.
- Piggybacking on request traffic. Free, but background threads inside request workers and freshness tied to traffic.
- A local machine or external cron. Needs an HTTP trigger and an API key, and every tick wakes campus.audit. At minute cadence that keeps it awake permanently.
- GitHub Actions schedule. Free for this public repository, but the same cadence argument applies, and a production trigger would put a production credential in CI.
- Rolling up on ingest instead of on a schedule. Needs no clock, but couples ingest with aggregation and makes rollups hard to re-derive after a fix.
To verify on development, before relying on sleep
- MongoDB clients. campus-core creates
MongoClient(uri)with default settings (campus/storage/documents/backend/mongodb.py:160). pymongo's default topology monitoring sends a heartbeat every 10 s. If Railway counts those packets, campus.auth and campus.api never sleep whatever telemetry does. - Healthchecks. Whether Railway healthchecks keep a service awake, or are suspended for slept services.
- Ingest after sleep. The first span POST to a slept campus.audit may return 502 and is dropped today (a fail-open warning). The outbox buffer fixes that for telemetry; spans would need the same buffer.
To decide
- Approach for the jobs: a Railway cron job running catch-up jobs (recommended).
- Schedule: hourly, or more often (minimum 5 minutes).
- Whether failed-auth event rows from unauthenticated probes should be kept shorter than the 2-year access log.
- Dominant language
- Python
- Stars
- 0
- Forks
- 2
- Avg merge
- 39m
- Merged PRs (30d)
- 235
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from nyjc-computing/campus
-
enhancement for-collaborators for-contributors
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
nyjc-computing/campus#451 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
nyjc-computing/campus#1320 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 25/100
nyjc-computing/campus#1309 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 18/100
nyjc-computing/campus#1300 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 5/100
nyjc-computing/campus#1295 · 4 comments ·
Maintainers usually reply within 1 day
All issues in nyjc-computing/campus
Similar issues
-
triage
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
kornia/kornia-rs#1416 · 1 comment ·
Maintainers usually reply within 2 days
-
ui
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
PolicyEngine/ai-inequality#64 ·
Maintainers usually reply within 1 day
-
LinearGaussianStateSpace.dist splits states and observations wrongly when the transition varies over timePossibly taken @PabloRoque claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
pymc-devs/pymc-extras#839 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day