5 Tips for Running Redis over AWS
Join the DZone community and get the full member experience.Join For Free
cloud computing can help host an application in a way that is both scalable and cost effective. the leading vendor in the infrastructure as a service (iaas) arena is amazon web services (aws), which offers scalable, highly available and secure cloud hosting. if your application uses redis for caching or data storage, below are a few tips which will help you save time, money, and achieve better redis performance within aws, in general.
when applicable, hardware virtual machine (hvm) enabled instances (e.g., second generation m3 instances and r3 memory-optimized instances) should be used to reduce latency issues due to fork times and in order to run multiple redis servers on different cores.
non-hvm ec2 instances use the xen hypervisor, which is much slower when it comes to forking large processes. redis' persistence options, rdb and aof, both fork the main thread and launch background rewrite or save processes. when forking involves a large amount of memory, it can increase redis' latency significantly.
multiple cores on the same machine can also be used to deploy a number of redis instances. in this set up, since each redis instance manages a smaller dataset, per-database forking times can be further mitigated.
swapping is an operating system mechanism that attempts to efficiently use ram. however, seeing as redis is an in-memory database, in order to provide top performance, all data must be kept in ram.
with swapping enabled, redis may attempt to access memory pages that are on disk. this will cause redis' process to be blocked by the i/o operation of the disk, which can be a slow process since it involves random i/os. by chaining redis to disk performance, your are likely to experience a latency increase. to mitigate this, you can configure the following options in linux in /etc/sysctl.conf:
this means that when the memory is almost full, the kernel will try to steal pages from cache instead of swapping out of program to memory. use these settings, along with redis' maxmemory and maxmemory-policy directives, to prevent redis from denying writes or the kernel's out of memory manager (oom) from terminating redis.
ec2 instances are equipped with ephemeral storage, with ebs as the de facto standard storage for persistent data in aws. keep in mind that ebs is network-attached storage. therefore, if you are persisting data to the volumes, ebs competes with redis over network bandwidth. there is a chance that extra i/o and limited bandwidth may affect redis’ performance when using ebs . nonetheless, this, too, can be reduced in a number of ways, including:
using large ebs volumes for dedicated-like performance.
using raid over ebs configuration.
using ebs-optimized instances to improve performance as a result of dedicated bandwidth between ec2 instances and ebs.
last but not least - reviewing your application's persistency requirements and tuning redis' persistency to fit them.
if your data's maximum size is less than 4gb per instance, by saving bits, you can also save money. when compiling and running redis on a 32 bit target, it uses less memory per key, since the pointers are smaller.
if you are unsure of the size of your dataset, use the info command to get the relevant numbers (e.g. used_memory). you can also use a benchmarking tool, such as redis-benchmark or memtier-benchmark , to generate random datasets for guesstimations.
remember that the size of your dataset is not the only consideration when planning the overall ram for a redis server. for example, in case you have enabled persistence or replication, a background memory thread will periodically create a copy of the data, which will be in-memory at that point. any data then remains in-memory until the operation is complete and therefore, any update operation on the data/key being copied requires extra memory in the form of memory pages and buffers . a "worst case" scenario, where all keys are updated during this period, may require twice or more memory than what would have been required without persistence and/or replication. to provision against running out of memory in such a scenario, use a factor of 2 at the very least on the actual dataset's size.
the final tip comes full circle, picking up from where tip #1 left off. redis is a (mostly) single-threaded process, which means that each redis process can use a single core, at the very most. to make better use of ec2 instances with multiple cores, you can start one redis process for each core of a server and efficiently utilize the extra cores. these extra servers can act as independent redis servers. for example, one can be utilized to manage user session data while another one can be used to cache user profiles. an alternative approach for harnessing these multiple cores is to shard the data between servers.
sharding is a big topic, in and of itself, and we'll dedicate more time to it in the upcoming weeks. prepare yourself for the drama associated with choosing between the application-side, client-assisted, by proxy or via cluster sharding. learn the horrible truths about sharding for yourself and how to battle them bravely. understand how to take redis to the next step. questions? feedback? email or tweet me - i'm highly available.
Published at DZone with permission of Itamar Haber, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.