FrankenState — Shared state between Go and PHP with Redis protocol support #2324
Replies: 3 comments 8 replies
-
|
Am i correct to view this as a sort of APCu that is available to both php and Go, and apparently also can serve as a sort of in-memory, in-process drop-in replacement for a (basic) Redis Server? If so, it's super cool! I have a few questions:
And if that were to be done, perhaps the caching package could be abstracted to allow for using different Golang caches. Here's a good overview of the evolution of caching libraries. https://maypok86.github.io/otter/blog/cache-evolution/ Or perhaps that would be more hassle than it is worth, as you'd probably have to build a different static php for each.
Thanks for all of your hard work and innovation! |
Beta Was this translation helpful? Give feedback.
-
|
I've got something coming down the pipe that you may be interested in... we're a couple weeks away from open sourcing it! |
Beta Was this translation helpful? Give feedback.
-
|
Another thought: Im sure the read latency table in your post is just a back-of-the-napkin approximation, nor was it meant to be exhaustive. But I think the figures are somewhat off. Eg Redis in localhost (and especially Unix sockets) can be much faster than 0.5–2 ms. And a DB 5-50ms seems slow as well - even mysql locally is faster than this, and sqlite is extremely fast. Still, your point stands that apcu/frankenstate would be vastly faster. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
PHP workers can't share state natively. Every request starts fresh. Need shared config, feature flags, or cached data? You reach for Redis adding a network hop, a server to manage, and serialization overhead for data that lives in the same process.
Note
This is a proof of concept for thread-safe shared state in FrankenPHP, demonstrating the pattern of
Go ↔ PHP data sharing, not optimized for cache-level performance. The focus is on the API and the
architecture: a single store accessible from Go, PHP, and over wire protocol.
FrankenState takes a different approach: a Go
map[string]anyprotected bysync.RWMutex, exposed to PHP asArrayAccess. Same process, same memory. Zero network overhead.Any Go extension can push data that PHP sees immediately:
Same data, accessible via Redis protocol:
Three access paths, one store:
state.Set()$state['key']redis-cli -p 6380The demo speaks Redis wire protocol too. The same shared memory is accessible via
redis-cli,phpredis, or any language with a Redis client. No phpredis extension needed — the demo talks RESP with 40 lines of raw PHP sockets. It's not a full Redis protocol implementation but covers the basics (GET,SET,DEL,KEYS,INCR,MGET,MSET,DBSIZE, etc).Performance: Every write (from Go, PHP, or Redis) increments an atomic version counter on the Go side. Each PHP
SharedArrayobject caches the full snapshot locally and remembers which version it last fetched. On read, it checks the counter, if unchanged, it serves from the local zval cache with zero CGo crossings. If the version bumped (someone wrote), it fetches a fresh snapshot. This means 10 reads in a row cost one version check and zero data fetches if nothing changed.Writes use type-specific setters for scalars, a PHP integer goes straight to Go's
int64without touching JSON. Only complex types (arrays, objects) need JSON encoding.What it replaces: The real win isn't raw speed, it's eliminating infrastructure. No Redis to provision, monitor, and scale. No connection pool to manage. No serialization format to debug. The state is just... there.
This is part of the franken* family of FrankenPHP extensions:
Together, FrankenState + FrankenAsync replace the proposed background workers/sidekicks PR with two composable primitives, no need for
pemalloc, no signaling streams, no new thread types, no Caddyfile directives.Repo: https://github.com/johanjanssens/frankenstate
Design doc: https://github.com/johanjanssens/frankenstate/blob/main/DESIGN.md
Beta Was this translation helpful? Give feedback.
All reactions