AMS.AL

Memcached Configuration for Faster Database Queries

· 6 min read

Memcached configuration that keeps query results in memory and lowers the load on the database for sites with many simultaneous users.

Memcached Configuration for Faster Database Queries

Memcached configuration means setting up a fast memory where the site keeps the results of queries that repeat. Memory caching systems lower the load on the database for portals with many users, and faster database queries are often the difference between a site that holds hundreds of visitors and one that stalls at ten.

The database is the point where most dynamic sites slow down. Every visitor triggers several queries, and many of them ask for the same data: the list of products, the settings, the menu. Why calculate them again every time? Below we explain how Memcached works, where it helps, where it harms and how AMS.AL configures it for portals with heavy traffic.

How Memcached configuration works

Process: Query measurement → What is stored → Load test → Monitoring
Process: Query measurement → What is stored → Load test → Monitoring

Memcached is a service that stores key and value pairs in RAM. The site asks Memcached first: if the answer exists, it takes it at once, without touching the database. If not, it takes it from the database and stores it for next time. A read from memory is far faster than a database query.

The help is greatest when data is read often and changes rarely. The list of categories, the results of a known search, the settings of the site and parts of pages built with many queries are good candidates. Data that changes every second, such as the stock of a product with many orders, needs more care.

Cache expiry is the hard part. Every value is stored for a set time and then rebuilt. If the time is too long, the user sees old data. If it is too short, the cache does not help. When a piece of data changes, the stored value must also be deleted at once, otherwise the site shows a state that no longer exists.

Memory is limited. Memcached has a set amount of RAM and, when it fills, it removes the oldest values. A size that is too small makes the cache empty continuously and lose its effect. A size that is too large takes memory that the rest of the server needs. Finding the right size takes measurement, not guesses.

Cache does not replace query optimization. The IETF HTTP standard describes caching at the network level, and Memcached is another layer, inside the application. A slow query that is cached is still slow the first time and every time it expires. Memcached configuration has more value when the queries have been fixed first.

How AMS.AL configures Memcached

We start with measurement: which queries are the most frequent and the slowest, and how much they load the database during peak hours. Memcached configuration does not begin with installation but with this measurement. We often find that five or ten queries make up most of the load, and only those are worth caching first.

Then we set up the service inside the PABAA platform and connect it to the site, setting the size of the memory according to the server. We decide what is stored, for how long and how it is deleted when the data changes. This last rule is the most important, because a cache with no correct deletion is a constant source of hard-to-find mistakes.

We test with a simulated load before release: how many visitors the site holds without cache and how many with it. We measure the hit rate, meaning how often the cache has the answer ready. If it is low, we adjust what is stored or the size. The numbers show whether the speed-up is real, not just visible in a single test.

In the end we link the work with our security and performance service, so Memcached is monitored and is not left open to outside access. A cache service that listens on the network without protection is a known risk. You receive a report that shows the load before and after and a short guide for your team.

Memcached configuration compared with queries without a cache

Key facts: Focus: Lower load · Stores: Frequent data · Expiry: Planned
Key facts: Focus: Lower load · Stores: Frequent data · Expiry: Planned

The table compares a site with a cache with one that asks the database every time.

AreaNo cacheWith Memcached
Repeated queriesTo the database every timeFrom memory
Database loadHighLower
Response timeLongerShorter
RiskSlow siteOld data if expiry is wrong

Checklist for Memcached configuration

Check these points before you turn it on.

  • Measure which queries are the slowest.
  • Store only data that is read often.
  • Set reasonable expiry times.
  • Delete the value when the data changes.
  • Size the memory by measurement.
  • Close outside access to the service.
Visual overview: Memcached configuration
Visual overview: Memcached configuration

Memcached configuration: frequently asked questions

What does Memcached store on a site?

It stores the results of queries or parts of pages that are built often, such as lists of products, settings and menus, in RAM. This way the site does not ask the database every time a visitor wants the same thing. Only what is read often and changes rarely is stored, while data that changes constantly needs more careful rules to stay correct.

Can Memcached show old data?

Yes, if expiry or deletion is configured wrongly. When a piece of data changes, the stored value must be deleted or refreshed at once, otherwise the visitor sees the earlier state. We plan this rule from the start and test it, because cache mistakes are hard to detect, since the site seems to work normally while it quietly shows outdated information.

How much memory does Memcached need?

It depends on the amount of data stored and on the server. Too little makes the cache empty continuously and lose its effect, while too much takes RAM that the rest of the server needs. We set the size by measuring the hit rate and adjust it according to real use, not with a number taken from a general guide that was written for another kind of site.

Is a cache enough for a slow site?

Not always. A cache hides a slow query but does not fix it, and it is felt again the first time or when the cache expires. We first fix the queries and indexes, and then add the cache for the remaining load. This way the speed-up is lasting and the site does not depend only on a layer that can be emptied at any moment.

Process: Memcached configuration
Process: Memcached configuration

Read also: Git Code Versioning for Safe Releases on the Web · File Integrity Monitoring: Detecting Unauthorized File Changes · WAF Firewall: The Shield Against Web Attacks · Cloudflare CDN and R2 Setup for Maximum Speed and Security · Every guide in this category

  • WordPress, Cloud & Security
  • Memcached configuration
  • WordPress, Cloud & Security