Abstract-Certain storage media such as flash memories use write-asymmetric, multi-level storage elements. In such media, data is stored in a multi-level memory cell the contents of which can only be increased, or reset. The reset operation is expensive and should be delayed as much as possible. Mathematically, we consider the problem of writing a binary sequence into writeasymmetric q-ary cells, while recording the last r bits written. We want to maximize t, the number of possible writes, before a reset is needed. We introduce the term Buffer Code, to describe the solution to this problem. A buffer code is a code that remembers the r most recent values of a variable. We present the construction of a single-cell (n -1) buffer code that can store a binary (1 = 2) variable with t = PL '? 1 J + r -2 and a universal upper bound to the number of rewrites that a single-cell buffer code can have: t < qL1 j r+ [log,{[(q-1) mod (lr-1)]+1}1. We also show a binary buffer code with arbitrary n, q, r, namely, the code uses n q-ary cells to remember the r most recent values of one binary variable.The code can rewrite the variable t = (q -1) (n -2r + 1) + r -1 times, which is asymptotically optimal in q and n. We then extend the code construction for the case r = 2, and obtain a code that can rewrite the variable t = (q-1) (n-2) + 1 times. When q = 2, the code is strictly optimal.