Skip to content

Get vs Put

  • by

“Get” and “put” look harmless, yet they steer code in opposite directions. One asks, the other commands. Misusing them quietly bends an entire program out of shape.

Seasoned engineers still pause at the whiteboard to confirm which direction the data flows. A single wrong choice can leak state, duplicate network calls, or deadlock a cache. The next sections strip away every ambiguity so you can reach for the right verb without a second thought.

🤖 This article was created with the assistance of AI and is intended for informational purposes only. While efforts are made to ensure accuracy, some details may be simplified or contain minor errors. Always verify key information from reliable sources.

Core Meaning in Plain English

Get Retrieves, Put Stores

“Get” is a question: hand me what already lives there. “Put” is an order: place this thing exactly here, overwriting whatever sits in that spot.

Memory, disk, cloud bucket, or browser cache—both verbs keep the same polarity everywhere. If you remember only one sentence, remember that polarity.

One-Way Data Flow

With get, data travels from source to caller. With put, data travels from caller to destination. Mixing the two directions in the same line is the fastest route to bugs that compile perfectly and fail silently.

Everyday Code Snippets

Map Access

`value = scores.get(player)` leaves the map untouched. `scores.put(player, newScore)` mutates the map in place. A rookie mistake is expecting get to default a missing key; it returns null instead.

HTTP Semantics

GET /users/7 fetches representation, never changes server state. PUT /users/7 replaces the entire resource at that URI. POST is often mislabeled as “get-like” because it can return data, but POST is intentionally non-idempotent.

Shell Commands

`export TOKEN=$(aws ssm get-parameter)` reads a secret. `aws s3api put-object –body logo.png –bucket assets` writes a file. Notice how CLI verbs echo the same polarity rule.

Hidden Side Effects

Cache Warming on Get

Some caches fetch-through on miss, so the first get triggers a put. Developers who treat get as side-effect-free can be surprised when metrics show writes after a “read-only” endpoint.

Write-Through on Put

Smart stores mirror every put to both fast and slow tiers. A put that looks local might fan out to three remote nodes. Latency budgets should account for that silent replication.

Idempotency Traps

Repeating the same get is harmless; you merely receive the same value again. Repeating the same put is also harmless only when the payload is identical. Change one byte and the second put becomes a destructive overwrite.

Concurrency Nightmares

Lost Update

Thread A gets x=5, thread B gets x=5, both increment, both put x=6. One increment vanishes. The fix is atomic compare-and-swap, not a plain put.

Read-After-Write Inconsistency

A put returns success, yet the next get on a replica still yields the old value. Eventually consistent stores trade immediate visibility for availability. Code must tolerate that lag or explicitly read from the leader.

Naming Conventions That Save Hours

Prefix getters with “find” when null is possible: `findUserOrNull`. Prefix putters with “replace” when old data is destroyed: `replaceUser`. These names broadcast behavior to future reviewers without comments.

Testing Strategies

Mock Get

Stub the supplier to return canned data; assert the consumer reacts correctly. Keep the stub free of side effects so tests stay deterministic.

Spy Put

Capture every put argument in a list; assert the expected mutations arrive in order. A single misplaced put shows up as an extra entry.

Performance Smells

Excessive gets inside loops hint at missing bulk APIs. Excessive puts in tight loops can thrash write buffers. Batch both operations when the API allows it.

Security Mindset

Never put derived secrets back into the same cache tier that serves public gets. Separate namespaces or encryption at rest prevent accidental leakage. A get that unexpectedly returns a private key is a career-limiting event.

API Design Checklist

Expose get endpoints under /resources/{id}. Expose put endpoints under the same path but require ETag or revision number. Reject put if the caller’s view of the resource is stale.

Front-End Angle

React State

`const [name, setName] = useState(“”)` mirrors get-put. Reading name is a get; calling setName is a put. The framework schedules the put, so immediate reads still show old state.

Browser Storage

`localStorage.getItem(“theme”)` reads on every page load. `localStorage.setItem(“theme”,”dark”)` writes once after user toggles. Overwriting large objects on each keystroke serializes JSON repeatedly and blocks the main thread.

Database Perspective

SELECT vs UPDATE

SELECT is SQL’s formal get; it locks nothing under read-committed isolation. UPDATE is the put; it takes write locks and can deadlock if two transactions touch rows in opposite order.

ORM Confusion

`user = User.objects.get(pk=1)` hits the database once. `user.name = “Ada”; user.save()` issues an UPDATE even if the name never changed. Track dirty fields to avoid silent puts.

Microservice Chatter

Service A gets a user from service B, enriches it, then puts the enriched copy into its own cache. Version the payload so B can reject stale puts. Without versioning, a delayed retry can undo months of user edits.

Edge Cases Worth a Bookmark

Some systems return metadata on get: TTL, checksum, lease. Others accept conditional put: “store only if version equals 42”. Ignoring these headers turns subtle optimizations into hard failures under load.

Refactor Recipe

Replace five consecutive gets with a single bulk get; latency drops and cache hit ratio rises. Replace three tiny puts with one transactional put; you shrink network chatter and simplify rollback logic.

Logging Discipline

Log every put at debug level with the key and a hash of the value. Log every get at trace level. When production data drifts, diff the logs to pinpoint the offending put without replaying user traffic.

Documentation Trick

Write the contract in reverse: start with the put, then explain what subsequent gets will observe. This inversion forces authors to think about consistency guarantees first, leaving no room for hand-wavy language.

Leave a Reply

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