How To Improve Performance Of Your Rails App Using DynamoDB And Memcached

December 10, 2021

With the increasing number of apps going live on the web, it’s becoming common to see that they use multiple servers. This is mainly due to scalability and availability purposes. But, these types of applications are also often not as responsive as they should be. Using a simple gem called DynamoDB and Memcached will allow you to achieve higher performance in your Rails app. Let me explain,
What is Memcached

Memcached is a high performance, free and open source distributed memory caching system used to speeding up dynamic web applications by alleviating database load. Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches.
It is used for speeding up dynamic web applications by reducing database load. In other words, every time a database request is made it adds additional load to the server. Memcached reduces that load by storing data objects in dynamic memory (think of it as short-term memory for applications). Memcached stores data based on key-values for small arbitrary strings.
How does Memcached work?

Memcached is comprised of four main components
1. Client software – Which is given a list of available Memcached servers
2. A client-based hashing algorithm – Chooses a server based on the “key”
3. Server software – Stores values and their keys into an internal hash table
4. LRU – Determines when to throw out old data or reuse memory
Each item is comprised of a key, expiration time, and raw data.
At a high-level Memcached works as follows:
1. The client requests a piece of data which Memcached checks to see if it is stored in cache.
2. There are two possible outcomes here:
1. If the data is stored in cache: return the data from Memcached (no need to check the database).
2. If the data isn’t stored in cache: query the database, retrieve the data, and subsequently store it in Memcached.
3. Whenever information is changed or the expiry value of an item has expired, Memcached updates its cache to ensure fresh content is delivered to the client.
A few important points about Memcached architecture include:
• Data is only sent to one server.
• Servers don’t share data.
• Servers keep values in RAM. If RAM runs out the oldest value is discarded.
What is DynamoDB

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. With DynamoDB, you can create database tables that can store and retrieve any amount of data and serve any level of request traffic. You can scale up or scale down your tables’ throughput capacity without downtime or performance degradation.
DynamoDB is a particularly good fit for the following use cases
• Applications with large amounts of data and strict latency requirements
• Server less applications using AWS Lambda
• Data sets with simple, known access patterns
Installing Memcached
There are a few ways you can install Memcached. Depending on which system you’re using, the method will vary. As outlined on the official Memcached Installation Wiki, installation from a package is simple.
Using Memcached with DynamoDB in Rails

Memcached is a quick in-memory protest reserving framework that can make Rails run much quicker with not very many changes. Memcached will work on any database used in Rails application.
When the table is small and request volume is low this isn’t much of an issue, but as your database and user volume grow, these can impact the performance of your application. In such cases Memcached plays an important role to reduce the database load by caching the request object in memory.

Read More On How To Improve Performance Of Your Rails App

Article Categories:

Leave a Comment

Your email address will not be published. Required fields are marked *