Redis is a in-memory key-value storage system. Primary Memory storage size tends to be much smaller than hard disk storage size. So its never a good idea to populate primary memory with binary data/files as you will soon run out of memory and cause redis/other applications unexpected shutdowns.
But still if you want to store binary data in redis then follow these instructions:
- You can store the binary file in filesystem and then store the path to the file in redis. This is the recommended method if you want to manage binary data with redis.
- You can also store binary files/data directly inside redis. Redis keys and values don’t have to be text strings. They can be binary. So a png/exe/pdf etc file can be a key or value in redis. The limitation is that the maximum allowed size of a single key or value is 512MB. You can create binary keys or values the same way you create string keys and values but with binary data. Redis internally represents all keys and values as binary blob(binary sequence of data).When you have large size keys/values your redis instance becomes slower(i.e., comparison during lookup is slower, hashing is slower etc) therefore not recommended to store binary data. You will need to use programming language APIs to store binary data in redis as command line redis client takes only readable characters as input. If you still want to use redis command line client then make sure you first encode binary data using Base64 encoding standard and then give the encoded data to redis command line client for input.
This is an php example to store a image file inside redis value.
$client = new Predis\Client();
$client->set('profile_picture', file_get_contents("profile.png"));//value is binary
$client->set(file_get_contents("profile.png"), 'profile_picture');//key is binary
$client->set('profile_picture', file_get_contents("profile.png"));//value is binary
$client->set(file_get_contents("profile.png"), 'profile_picture');//key is binary