Using Swoole Table for Shared Memory and High‑Performance Data Sharing in PHP
This article explains how Swoole's multi‑process model isolates memory in PHP, recommends external storage such as Redis or shared memory via Swoole/Table, details capacity calculations, handling of oversized strings, and provides a complete PHP example demonstrating table creation, data storage, and server communication.
Because the PHP language does not support multithreading, Swoole operates in a multi‑process mode, which results in memory isolation between processes; modifying global or super‑global variables in one worker process does not affect others.
The recommended solutions are to use external storage services such as databases (e.g., MySQL , MongoDB ), cache servers (e.g., Redis , Memcache ), or disk files with proper locking. However, these approaches involve significant I/O latency, so using an in‑memory solution like Redis or the memory filesystem /dev/shm is preferred for higher performance.
For true shared memory within Swoole, the Swoole\Table structure provides a high‑performance concurrent data container based on shared memory and locks, solving multi‑process data sharing and synchronization issues. Its memory capacity is not limited by PHP's memory_limit .
Advantages of Swoole\Table :
Strong performance – up to 200 万 (2 million) read/write operations per second per thread.
No need for user‑level locking; the table has built‑in row spin locks, making all operations safe for multi‑thread/multi‑process.
Supports multi‑process data sharing.
Uses row‑level locks instead of a global lock, only contending when two processes access the same row simultaneously.
Capacity calculation : the total memory used by a table equals (HashTable struct length + 64‑byte key length + $size) * (1 + $conflict_proportion) * column_size . If the key or hash conflict rate exceeds 20 %, you may encounter “Unable to allocate memory” errors and need to increase $size and restart the service.
Handling strings longer than the defined column size : values exceeding the column's maximum length are automatically truncated by the underlying implementation.
Example code demonstrates creating a Swoole\Table , defining columns, initializing a Swoole\Server , handling the receive event, storing and retrieving data in the table, and sending responses back to the client.
<?php
$table = new Swoole\Table(1024);
$table->column('fd', Swoole\Table::TYPE_INT);
$table->column('reactor_id', Swoole\Table::TYPE_INT);
$table->column('data', Swoole\Table::TYPE_STRING, 64);
$table->create();
$serv = new Swoole\Server('127.0.0.1', 9501);
$serv->set(['dispatch_mode' => 1]);
$serv->table = $table;
$serv->on('receive', function ($serv, $fd, $reactor_id, $data) {
$cmd = explode(' ', trim($data));
if ($cmd[0] == 'get') {
if (count($cmd) < 2) return;
$fdKey = intval($cmd[1]);
$info = $serv->table->get($fdKey);
$serv->send($fd, var_export($info, true) . "\n");
} elseif ($cmd[0] == 'set') {
$ret = $serv->table->set($fd, [
'reactor_id' => $reactor_id,
'fd' => $fd,
'data' => $cmd[1]
]);
if ($ret === false) {
$serv->send($fd, "ERROR\n");
} else {
$serv->send($fd, "OK\n");
}
} else {
$serv->send($fd, "command error.\n");
}
});
$serv->start();
?>Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.