Databases & Storage

Data Locality: Keep Data Close to the Code That Uses It

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

Data locality is a simple idea with a big effect: work runs fastest when the data it needs is physically and logically close. "Close" means different things at different layers. For a CPU it means data already sitting in cache. For an application it means the database is on the same machine or in the same data center rather than across an ocean. For your visitors it means the server is in a region with low latency to them. Most slow systems are not slow because of one heavy operation; they are slow because they keep fetching data from far away, many times over. This guide walks through each layer and gives practical steps for a VPS-based setup.

The cost of distance

Every layer of storage has a different access cost. The exact numbers depend on hardware, but the order of magnitude is what matters:

Where the data isRough access time
CPU cachenanoseconds
RAMaround a hundred nanoseconds
Local NVMe/SSDtens to hundreds of microseconds
Another server, same data centerwell under a millisecond to about a millisecond
Another region or continenttens to hundreds of milliseconds

Each step down is roughly an order of magnitude or more slower. An application that makes one call to a database across the continent might feel fine. One that makes fifty such calls per page will not, because the delays add up.

Layer 1: Locality inside the machine

At the lowest level, CPUs load memory in cache lines (commonly 64 bytes). Code that walks through memory sequentially benefits because neighbouring data comes along for free. Code that jumps around randomly misses the cache constantly.

You rarely tune this directly in web applications, but it explains some practical choices:

  • Databases store rows or columns in pages so related data is read together.
  • Sequential disk reads are much faster than random ones, even on SSDs.
  • Keeping your working set in RAM (enough memory for the database buffer pool and OS page cache) avoids trips to disk.

Check memory pressure with:

free -h
vmstat 5

If the page cache is being squeezed and disk reads climb, more RAM is often the cheapest locality improvement you can make.

Layer 2: Application and database on the same network

A classic mistake is running the web application in one place and the database in another, for example the app on a VPS in Europe and a managed database in the USA. Every query then pays a full round trip.

Practical rules:

  1. Put the app and its database in the same location. On a single VPS, connect over a Unix socket or 127.0.0.1. If you split them across two servers, keep both in the same data center and use a private network where available.
  2. Measure the round trip. From the app server:

``bash ping -c 10 db.internal ``

For PostgreSQL, \timing in psql shows how long a trivial SELECT 1; takes, which approximates per-query overhead.

  1. Reduce chatty access. Fix N+1 query patterns, fetch related records in one query with joins or IN (...), and use batch endpoints instead of loops of single calls.
  2. Use connection pooling. Opening a new connection costs several round trips (TCP, and TLS if used). Poolers such as PgBouncer keep connections warm.

Layer 3: Caches as local copies

A cache is a locality tool: it keeps a copy of data closer to where it is used.

  • In-process cache: fastest, but each worker has its own copy, and it disappears on restart.
  • Redis or Memcached on the same server: shared by all workers, still very close.
  • HTTP caching and a CDN: static files and cacheable pages served near the visitor.

Every cache adds the question of staleness. Decide per piece of data how long it may be out of date, and set a TTL accordingly. Our article on server-side caching covers the common options.

Layer 4: Users and regions

The last layer is the distance between your server and your users. Pick the region closest to most of your audience:

  • Visitors in Europe, the Middle East and North Africa are usually best served from European locations such as Falkenstein, Nuremberg or Helsinki.
  • North American audiences are closer to Ashburn (east) or Hillsboro (west).
  • South and Southeast Asian audiences are closer to Singapore.

Measure rather than guess. From a machine near your users:

mtr -rwc 50 your-server-ip

If your audience is split across continents, serve static assets through a CDN and keep the dynamic application and its database together in one region. Splitting the database across regions brings replication and consistency challenges that are rarely worth it for small and medium projects.

A locality checklist

  • App and database in the same location, ideally the same server or private network.
  • Enough RAM for the database working set and OS cache.
  • No N+1 query patterns on hot pages.
  • Connection pooling for databases with expensive connections.
  • Redis or similar on the same server for shared, frequently read data.
  • Static assets cached by the browser and, if useful, a CDN.
  • Server region chosen by measured latency to real users.
  • Backups stored somewhere else: locality is for performance, not for disaster recovery.

That last point matters. Keeping everything close is great for speed, but your backups should deliberately live far away, so one incident cannot take out both.

When you are ready to deploy, you can choose your server location during deployment.

FAQ

Is it fine to run the database on the same VPS as the app?

For many small and medium projects, yes. It gives the best locality and the simplest setup. Split them when one of them needs dedicated resources, but keep both in the same data center.

Does a CDN solve data locality for dynamic sites?

Only partly. A CDN brings static files and cacheable pages close to visitors, but personalised pages still need the origin server and its database, so the origin's region still matters.

How do I know if distance is my bottleneck?

Time a trivial query and multiply by the number of queries per request. If the total is a large share of response time, reduce round trips or move the app and data closer together.

Related guides