Updated June 2026. Tested on Laravel 13, PHP 8.4 and Redis 7. This builds on Introduction to Redis with Laravel.

A Redis string is not just a place to dump a single value. You can treat it like a buffer and work on parts of it, which opens up a neat trick: storing a whole series of fixed width readings inside one key instead of scattering them across many.

The many keys approach

Say you want each product's sales recorded per hour. The obvious way is one key per hour.

Redis::set("product:1:sales:{$currentHour}", $sales);

To plot the day you read them all back, and to reset you delete them all.

Redis::mget('product:1:sales:00', 'product:1:sales:01', /* ... */);
Redis::del('product:1:sales:00', 'product:1:sales:01', /* ... */);

This works, but it litters your keyspace with twenty four keys per product per day, and every one of those keys carries its own bookkeeping overhead.

One string as a buffer

Here is the tidier way. Keep a single key per product and append each hour's reading to it. If you agree on a fixed width, say every sales figure is five digits, the string becomes a predictable sequence you can slice.

Redis::append('product:1:sales', '00030'); // hour 0
Redis::append('product:1:sales', '00200'); // hour 1
Redis::append('product:1:sales', '00001'); // hour 2

Redis::expire('product:1:sales', 86400);   // clear itself after a day

After three hours the value is one string: 000300020000001. One key, set to expire after 24 hours.

Reading parts of the string

Because every reading is exactly five characters, the string tells you a lot. strlen gives the total length, so dividing by five tells you how many hours have been recorded.

$hoursElapsed = Redis::strlen('product:1:sales') / 5;

getrange returns a slice between two offsets, so you can pull just the first two hours and split it back into values.

// first two hours: offsets 0 through 9
$salesUntil2AM = str_split(
    Redis::getrange('product:1:sales', 0, (5 * 2) - 1),
    5
); // ['00030', '00200']

The whole day is just the entire string, split the same way.

$sales = str_split(Redis::get('product:1:sales'), 5);

Overwriting a slice in place

setrange writes into the string starting at an offset, overwriting that region. So to correct the second hour's figure, jump to offset 5 and write the new five characters.

// reset hour 2 (offset 5) to 00300
Redis::setrange('product:1:sales', 5, '00300');

No need to touch the rest of the string, you surgically replace just that window.

When this is worth it

This pattern shines for compact, fixed width, time series style data: hourly counters, daily flags, anything where the position in the string carries meaning. You collapse many keys into one, cut the per key overhead, and get cheap slicing for free. For richer structures where fields have names rather than positions, a hash is the better fit, which is the next stop in the series.

Got a clever use for Redis string slicing? Share it in the comments.