Is it recommended to store PHP Sessions in MemCache?
You are currently weighing two distinct architectural patterns for stateless scaling: sticky sessions versus distributed session storage. Both approaches solve the fundamental problem of maintaining user context across multiple web servers, but they introduce different failure modes and operational complexities. The confusion often stems from a misunderstanding of what "sticky" actually guarantees versus what a shared store provides.
Is it possible to keep PHP sessions in MemCache?
Yes, this is not only possible but is a standard pattern for horizontal scaling. When using memcached, you typically configure your PHP application to serialize the $_SESSION data and write it to the cache backend upon modification. In a shared-hosting or vanilla PHP environment, this is often achieved by setting the session handler in php.ini:
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211?weight=10&persistent=1"
You must ensure that your application does not rely on file-based storage (the default files handler) or database tables for sessions, as this would defeat the purpose of using a high-performance cache like MemCache. The application logic remains identical to standard session usage; the backend infrastructure handles the statelessness.
Sticky Sessions versus Distributed Storage
Sticky sessions, often configured on your Load Balancer (e.g., AWS ELB stickiness or HAProxy cookie-based routing), route a specific user's requests to the same web server instance indefinitely. This approach relies entirely on the capacity of your load balancer and the health of individual application servers.
If you choose sticky sessions, you are effectively creating a dependency chain. If an application server crashes, the session data remains on that disk until the garbage collector runs or the server is rebooted. The user might experience a timeout if they request a resource before the session data is cleaned up, or worse, lose their state if the server fails completely and the load balancer fails to route them back quickly enough. Sticky sessions also limit your ability to scale dynamically; you cannot easily take a node out of rotation for maintenance without risking session loss for active users unless you implement complex draining procedures.
MemCache offers a different trade-off: redundancy at the cost of complexity. By storing sessions in a shared store, every web server can access any user's session data. This eliminates the risk of losing data due to a single server failure. However, it introduces network latency to every session read/write operation. If your application logic involves heavy serialization of large datasets (like entire databases), MemCache might become a bottleneck compared to local disk I/O, though for standard session sizes, the performance difference is negligible.
The Problems with PHP Sessions in MemCache
Since you have confirmed that you can provision sufficient cache capacity via Amazon, storage exhaustion is not a primary concern here. Instead, the critical issues revolve around serialization overhead and consistency.
When using memcache, every time a session variable changes, PHP must serialize the entire session data structure and send it over the network to the cache server. In high-throughput applications with frequent session updates (e.g., real-time dashboards), this can introduce latency. Furthermore, MemCache is an in-memory store, meaning there is no persistence guarantee by default. If you do not configure persistent connections or replication policies correctly, a crash on the cache cluster could result in total session loss for all users.
Another subtle pitfall involves the session.gc_probability and session.gc_divisor settings. With sticky sessions, the garbage collector runs independently on each disk-based file. With MemCache, you must ensure your cleanup logic aligns with how the cache handles expiration to prevent "zombie" sessions from consuming memory indefinitely.
When to Choose Which?
If your primary concern is minimizing network hops and latency for session reads/writes, and you have a stable fleet of servers where sticky sessions can be reliably managed, that path is viable. However, if you value high availability and the ability to swap nodes without data loss, MemCache is generally the robust choice despite the slight serialization overhead.
For modern PHP applications running on frameworks like Laravel, managing this complexity internally often requires a dedicated package or configuration. Resources such as https://laravelcompany.com provide insights into how frameworks abstract these backend choices, allowing developers to focus on application logic rather than infrastructure glue code.