Gnoke‑Database: A Self‑Hosted Firebase‑Style Backend on SQLite
#Regulation

Gnoke‑Database: A Self‑Hosted Firebase‑Style Backend on SQLite

Backend Reporter
5 min read

Gnoke‑Database lets PHP‑powered sites run a Firebase‑like backend on a single SQLite file, offering offline sync, role‑based access, and multi‑tenant isolation without per‑operation billing. It trades massive scale for predictable cost and full ownership, making it a pragmatic choice for small‑to‑mid‑size teams.

Problem – Over‑engineered backends for modest apps

Most mobile and web teams reach for managed services such as Firebase, Supabase, or Amplify because they promise real‑time sync, auth, and rule‑based security out of the box. The convenience comes with two hidden costs:

  1. Variable pricing – reads, writes, and storage are metered per operation. A spike in usage can blow the monthly bill, and the cost model is opaque for teams that need a fixed budget.
  2. Vendor lock‑in – the data lives on a provider’s infrastructure, making migrations, custom compliance requirements, or on‑prem deployments painful.
  3. Scale mismatch – many startups, internal tools, or niche B2B apps never approach the traffic levels that justify a globally distributed, multi‑region data plane. Deploying such heavy machinery adds latency and operational overhead for no tangible benefit.

When the backend is essentially a switchboard—receiving occasional writes, broadcasting updates to a handful of collaborators, and persisting a modest amount of data—the heavyweight cloud model becomes a liability.


Solution Approach – Gnoke‑Database as a lightweight Firebase alternative

Gnoke‑Database (MIT‑licensed, see the GitHub repo) implements the core Firebase experience on any PHP host that can run SQLite. Its architecture can be broken down into three layers:

  1. Local‑first client SDK – The mobile/web client writes to a local SQLite (or IndexedDB) store first. Changes are queued for sync, guaranteeing that the UI never stalls waiting for the network.
  2. Sync engine – When connectivity returns, a thin PHP endpoint (/sync) pushes the queued mutations to the server‑side SQLite file and pulls any remote changes. The protocol is essentially optimistic replication with conflict resolution based on timestamps and a deterministic merge rule set.
  3. Identity chain & role enforcement – A single configuration file defines roles (operator, manager, admin). Every request is wrapped by a middleware that extracts the user’s JWT, resolves the identity chain (user → branch → company), and applies the appropriate ACL before touching the database.

Core API patterns

Endpoint Method Body Returns Remarks
/sync POST { queue: [{ collection, id, op, payload }] } { success: true, conflicts: [] } Idempotent – replaying the same queue is safe because each operation includes a monotonically increasing clientVersion.
/auth/login POST { email, password } { token, expiresIn } Issues a short‑lived JWT; OTP recovery flow refreshes the chain without admin intervention.
/role/update PATCH { role, targetUserId } { updated: true } Enforced server‑side; clients cannot elevate privileges.

These patterns keep the public surface small, making it easy to generate SDKs for iOS, Android, or JavaScript.


Trade‑offs – When does the SQLite‑backed model make sense?

Aspect Gnoke‑Database Managed Firebase
Scalability Single‑file SQLite limits concurrent writes. Suitable for < 10 k active users, typical for internal tools or niche SaaS. Horizontally scalable, multi‑region, handles millions of concurrent connections.
Consistency model Eventual consistency with deterministic conflict resolution. Reads are always local; writes are persisted locally first. Strong consistency for single‑document reads/writes; real‑time listeners get near‑instant updates.
Cost predictability Fixed hosting fee (shared PHP + SQLite). No per‑operation charge. Pay‑as‑you‑go on reads/writes/storage; costs can be unpredictable under load.
Operational overhead Deploy a folder to any cheap host, manage backups manually. No server management; Google handles scaling, backups, and security patches.
Data isolation Multi‑tenant mode creates a separate SQLite file per client, guaranteeing zero bleed. Multi‑tenant via security rules; accidental rule misconfiguration can expose data.
Latency Local reads are instant; sync latency depends on network round‑trip to the PHP host. Global edge network reduces latency for geographically dispersed users.

When to choose Gnoke‑Database

  • Your team needs full ownership of the data (e.g., GDPR‑strict environments).
  • Budget constraints demand a predictable monthly cost.
  • The user base is confined to a single region or a limited number of concurrent sessions.
  • You prefer a single‑file backup strategy and can tolerate occasional write contention.

When to stay with Firebase

  • You anticipate rapid growth to millions of users across continents.
  • Real‑time collaboration with sub‑second latency is a core product requirement.
  • You lack the expertise to secure and maintain a PHP/SQLite stack.

Practical considerations for production use

  1. Backups – SQLite is a single file; schedule a cron job to copy it to off‑site storage (e.g., S3) after every successful sync batch.
  2. Write contention – Enable SQLite’s WAL (Write‑Ahead Logging) mode to allow concurrent readers while a writer holds a lock. For heavy write bursts, consider sharding by tenant.
  3. Horizontal scaling – If you outgrow a single host, you can run multiple Gnoke instances behind a load balancer, each serving a distinct tenant pool. The identity chain ensures no cross‑tenant leakage.
  4. Monitoring – Instrument the PHP sync endpoint with Prometheus metrics (gnoke_sync_requests_total, gnoke_sync_errors_total) to detect spikes that could indicate lock contention.
  5. Security – Keep the JWT secret out of the repository, rotate it regularly, and enforce HTTPS on all endpoints.

Conclusion

Gnoke‑Database flips the usual cost model on its head: instead of paying per operation, you pay a fixed hosting fee and own the entire stack. It delivers the offline‑first, real‑time sync experience that developers love about Firebase, but with full control over data, predictable expenses, and a codebase you can audit.

For teams that have outgrown hobby‑level backends but are not ready for Google‑scale infrastructure, Gnoke‑Database offers a pragmatic middle ground. Deploy it in an afternoon, write your role rules once, and let the SQLite file be the single source of truth for your app.

Featured image

Comments

Loading comments...