# Atomic SET if not exists

> What does SET key value NX do, and how do you use it correctly to build a distributed lock or an idempotency check?

Canonical: https://www.internhack.in/learn/interview/redis-caching/redis-atomic-set-nx
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

What does \`SET key value NX\` do, and how do you use it correctly to build a distributed lock or an idempotency check?

*The full answer is for InternHack members. Open the page link above to read it.*

## Concepts covered

SET NX, Distributed lock, Lock token, Redlock, Idempotency key

Previous: [Multiple Databases](https://www.internhack.in/learn/interview/redis-caching/redis-databases-count) · Next: [ZSET Range Query](https://www.internhack.in/learn/interview/redis-caching/redis-zset-range-by-score) · [All of Redis & Caching](https://www.internhack.in/learn/interview/redis-caching)

## More Redis & Caching questions

- [Script Caching](https://www.internhack.in/learn/interview/redis-caching/redis-lua-script-cache)
- [Pausing Clients](https://www.internhack.in/learn/interview/redis-caching/redis-client-pause)
- [Memory Fragmentation](https://www.internhack.in/learn/interview/redis-caching/redis-memory-fragmentation)
- [Client-Side Caching (Redis 6+)](https://www.internhack.in/learn/interview/redis-caching/redis-client-caching-tracking)
- [ACLs (Access Control Lists)](https://www.internhack.in/learn/interview/redis-caching/redis-acl-manager)
- [AOF Sync Options](https://www.internhack.in/learn/interview/redis-caching/redis-appendfsync-options)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Redis & Caching](https://www.internhack.in/learn/interview/redis-caching)
